Thursday, February 23, 2017

DatabaseRouter, add on runtime add new items to db.connections

Hello,

We are using in our system for more than 2 years now, a specific DatabaseRouter allowing us to use multiple databases depending on which clients is connecting.
It appears now, that the databases lists start to be really big, and that it becomes bad to be forced to re-deploy our backend to add a new database in the settings.

So we add the configurations in a specific database, and add to our middleware that was previously just setting the database to use in function of the request, the ability to load dynamically new database configurations.
With a function like this :

from django.db import connections
def add_db_connection(system_id, name, user, password, host, protocol):
    connections.databases[system_id] = {
        'ENGINE': 'django.db.backends.' + protocol,
        'NAME': name,
        'USER': user,
        'PASSWORD': password,
        'HOST': host,
        'CONN_MAX_AGE': 0
    }

It appears we don't seams to be firsts to edit dynamically this, as it is also the way works this extension : 
https://github.com/ambitioninc/django-dynamic-db-router

But before we put our new code in production, as we doesn't have found some clear indication about if we can or if we cannot edit django.db.connections in the documentation. We will be more confident, if someone involved in django development can give us it's feedback about this way of making things and if it's the recommended way ? or if their is a better way ?

Thanks a lot, for all you feedback,

Matthieu

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscribe@googlegroups.com.
To post to this group, send email to django-users@googlegroups.com.
Visit this group at https://groups.google.com/group/django-users.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/747bc43a-bb42-4aac-a399-cbfb0d1470cb%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

No comments:

Post a Comment