pass custom connection at backend construction

We have a database that requires an SSL connection, but the URI constructor does not appear to allow passing the necessary options to use SSL. We use PyMySQL to make the connection, so it would work for us to optionally pass a connection object in to "backend(..)".

If that sounds okay to you, I'd be happy to cook up a pull request.

bitbucket:Andrew Schamp
Assigned to
7 years ago
4 years ago
No labels applied.

bitbucket:Andrew Schamp 7 years ago · edit

I found I was able to workaround the connection thing by implementing my own backend subclass, like so:


class PresetConnectionBackend(yoyo.backends.MySQLBackend):
    def __init__(self, connection):
        self.preset_connection = connection
        super().__init__(dburi=None, migration_table=yoyo.default_migration_table)

    def connect(self, dburi):
        return self.preset_connection

bitbucket:ollyc 7 years ago · edit

Sounds like a useful feature to have generally available. I've just looked through the PyMySQL docs but can't see any examples of how to connect with SSL. What options would you need pass to make an SSL connection?

bitbucket:Andrew Schamp 7 years ago · edit

Looking at: https://github.com/PyMySQL/PyMySQL/blob/master/pymysql/connections.py and searching for the string 'ssl', I found this argument for the connection constructor:

            A dict of arguments similar to mysql_ssl_set()'s parameters.
            For now the capath and cipher arguments are not supported.

If you dig around further, you'll see that 'ssl' can be an SSL context or a dict. I use the 'ca' parameter, but it also accepts 'cert' and 'key'. The docs might be out of date, it does look like it uses 'capath' and 'cipher' to set the context parameters.

But yeah, the docs are not really clear on it.

bitbucket:ollyc 7 years ago · edit

Thanks for the reference. I think the best way is to add a new option to allow arbitrary data to be passed to the connect method, eg:

yoyo apply --database=mysql://blah/foo --connect-args='{"ssl": {"ca": …}}'

If that sounds like a sane idea to you and you still have the motivation to put together a pull request, then go for it :) Otherwise I'll take a look whenever I next have some time free.

bitbucket:Andrew Schamp 7 years ago · edit

I use yoyo programatically, but that idea sounds sane to me.

Like I mentioned, I have a workaround and this is for work, which normally means I'd contribute but at the moment, I'm swamped. I'll post here though if I think I can carve out time to get to it.

Register here or Log in to comment, or comment via email.