views:

222

answers:

2

I am trying to switch between 2 mysql servers at runtime. I do not need to maintain both connections alive all the time.

This is what I am doing

from django.conf import settings
from django.db import connection
from django.contrib.auth.models import User

connection.close()
setattr(settings, 'DATABASE_HOST', 'mysql1.com')
list1 = User.objects.all()

connection.close()
setattr(settings, 'DATABASE_HOST', 'mysql2.com')
list2 = User.objects.all()

I have the following settings.py:

DATABASE_HOST = '' # localhost
DATABASE_NAME = test
...

The database name is the same on all servers and only the content of each tables differ.

I should get list1 != list2 as the users are different on both servers.

The issue is that I always get the list of users from the default database defined in settings.py (which is running on localhost) instead of the one from mysql 1 server and then from mysql 2 server.

Any idea what I am doing wrong here?

Laurent

+1  A: 

My guess, from the information, would be a potential error in your set DATABASE_HOST lines (in yor pseudo code above). read: "setattr(settings..."

Other than that, I'm not sure how you've configured your database to switch based on your criteria, as you've not explained this. If you are doing it by model, it may be worth considering how Django knows this, or even using external connections (manually loading the database driver and running commands by hand prior to the render stage), and using the main.

I'd query the whole approach, but mostly because I'm not sure how you're actually differentiating the two databases, or why. Could you provide a bit more information on how you're doing this? I assume the variables you're pulling in dot-points 2 and 5 above are different. I don't need the values, I'm just making sure you've not used the old code duplication and forgotten to edit it (we've all been there).

Note: I'd post this as a comment if I could, but I think the solution may be in how you're pulling the variables. Finally, you could try adding the database name (just the server IP or whatever) to the output, if you're in 'dev'/debug (offline/non-production) mode, to check if it's actually making it to the second server.

Swixel
I updated my post to include a more detailled version of the code.I have the same database running on mysql1 and mysql2 with different content. I should get a different list of users but I get the same and the one from localhost which is my default database defined in settings.py
Laurent Luce
So my question becomes: "How is the content differentiated"?If the system knows where to update to DB1 or DB2 (also not sure how you're swinging that one!), then the manner in which it pulls the information should be done the same way.Are you basically using two authentication servers (MySQL servers)? If so, unless you've got a lot of users I'd beg the question as to why, and even then if that's all it is you could simply override the auth model. If it's for something else it's equally important to know why. Hypothetically if it's for a blog, it'd be by section; so switch in the view?
Swixel
I am trying to migrate data from one database to another one (2 different servers).
Laurent Luce
+1  A: 

For reference, the Django documentation explicitly states you shouldn't do this -- Altering settings at runetime.

There is a lot of talk within the Django community about the ORM supporting multiple connections/databases at once. There's a lot of good reference info out there on it. Check out this blog post: Easy Multi-Database Support for Django and this Django wiki page Multiple Database Support.

In the blog post, Eric Florenzano does something like this in his settings.py file:

DATABASES = dict(
    primary = dict(
        DATABASE_NAME=DATABASE_NAME,
        # ...
    ),
    secondary = dict(
        DATABASE_NAME='secondary.db',
        # ...
    ),
)
T. Stone
I know those links but they are quite complicated when you just need a temporary solution. I would prefer a quick hack using settings.py to move what I need to move and then be done with it.
Laurent Luce
What Eric is doing is different than what I am trying to do. He is associating one model to database A and a second model to database B.I want to use the same model but on 2 different databases.
Laurent Luce
If you look at Eric's post. He is modifying settings at runtime too:setattr(settings, key, value)
Laurent Luce