Merge lp:~canonical-isd-hackers/canonical-identity-provider/716494-readonly-set-db into lp:canonical-identity-provider/release

Proposed by Ricardo Kirkner
Status: Merged
Approved by: David Owen
Approved revision: no longer in the source branch.
Merged at revision: 129
Proposed branch: lp:~canonical-isd-hackers/canonical-identity-provider/716494-readonly-set-db
Merge into: lp:canonical-identity-provider/release
Diff against target: 86 lines (+36/-6)
2 files modified
identityprovider/readonly.py (+6/-2)
identityprovider/tests/test_readonly.py (+30/-4)
To merge this branch: bzr merge lp:~canonical-isd-hackers/canonical-identity-provider/716494-readonly-set-db
Reviewer Review Type Date Requested Status
David Owen (community) Approve
Review via email: mp+49283@code.launchpad.net

Commit message

fixed bug in selecting the proper database for failover

Description of the change

This branch fixes a bug introduced by moving from django 1.0.2 to django 1.1, because of the way the connection settings are retrieved in the latter.

Now the DatabaseWrapper.settings_dict also gets updated when the new database is selected.

To post a comment you must log in.
Revision history for this message
David Owen (dsowen) wrote :

Nice fix for a subtle bug!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'identityprovider/readonly.py'
2--- identityprovider/readonly.py 2010-10-14 17:30:00 +0000
3+++ identityprovider/readonly.py 2011-02-10 20:09:58 +0000
4@@ -85,8 +85,12 @@
5 to set the database connection
6 """
7 for attname in ['HOST', 'PORT', 'NAME', 'USER', 'PASSWORD', 'ID']:
8- attvalue = db.get('DATABASE_' + attname, '')
9- setattr(settings, 'DATABASE_' + attname, attvalue)
10+ setting_name = "DATABASE_%s" % attname
11+ attvalue = db.get(setting_name, '')
12+ setattr(settings, setting_name, attvalue)
13+ # need to update the DatabaseWrapper cached settings_dict
14+ # to keep up to date with the new db setting
15+ connection.settings_dict[setting_name] = attvalue
16
17 def mark_current_failed(self, automatic=False):
18 """Mark the current DB connection as failed.
19
20=== modified file 'identityprovider/tests/test_readonly.py'
21--- identityprovider/tests/test_readonly.py 2010-10-14 17:30:00 +0000
22+++ identityprovider/tests/test_readonly.py 2011-02-10 20:09:58 +0000
23@@ -73,6 +73,7 @@
24
25 settings.DATABASE_ID = old_DATABASE_ID
26
27+
28 class RemoteRequestTest(SQLCachedTestCase):
29 msg = 'hello'
30 host = 'myhost'
31@@ -141,12 +142,18 @@
32 settings.READONLY_SECRET = 'testsecret'
33 self.orig_db_connections = settings.DB_CONNECTIONS
34 settings.DB_CONNECTIONS = [{'DATABASE_ID': 'master',
35- 'DATABASE_HOST': 'dbhost1',
36- 'DATABASE_NAME': 'masterdb',
37+ 'DATABASE_HOST': settings.DATABASE_HOST,
38+ 'DATABASE_NAME': settings.DATABASE_NAME,
39+ 'DATABASE_USER': settings.DATABASE_USER,
40+ 'DATABASE_PORT': settings.DATABASE_PORT,
41+ 'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
42 },
43 {'DATABASE_ID': 'slave',
44- 'DATABASE_HOST': 'dbhost2',
45- 'DATABASE_NAME': 'slavedb',
46+ 'DATABASE_HOST': settings.DATABASE_HOST,
47+ 'DATABASE_NAME': settings.DATABASE_NAME,
48+ 'DATABASE_USER': settings.DATABASE_USER,
49+ 'DATABASE_PORT': settings.DATABASE_PORT,
50+ 'DATABASE_PASSWORD': settings.DATABASE_PASSWORD,
51 }]
52 self._DBFAILOVER_FLAG_DIR = getattr(settings, 'DBFAILOVER_FLAG_DIR',
53 None)
54@@ -162,6 +169,24 @@
55 self.rm.set_db(settings.DB_CONNECTIONS[0])
56
57
58+class ReadOnlyManagerTestCase(ReadOnlyBackUp):
59+ def test_set_db(self):
60+ db = {
61+ 'DATABASE_ID': 'foo',
62+ 'DATABASE_HOST': 'foo_host',
63+ 'DATABASE_PORT': 'foo_port',
64+ 'DATABASE_NAME': 'foo_name',
65+ 'DATABASE_USER': 'foo_user',
66+ 'DATABASE_PASSWORD': 'foo_password',
67+ }
68+ self.rm.set_db(db)
69+
70+ from django.db import connection
71+ for setting in db:
72+ self.assertEqual(getattr(settings, setting), db[setting])
73+ self.assertEqual(connection.settings_dict[setting], db[setting])
74+
75+
76 class ReadOnlyRecoveryTestCase(ReadOnlyBackUp):
77 def setUp(self):
78 super(ReadOnlyRecoveryTestCase, self).setUp()
79@@ -233,6 +258,7 @@
80 def mock_ping_current_connection(self):
81 return False
82
83+
84 class ReadOnlyFlagFilesTest(ReadOnlyBackUp):
85 def test_flag_files_in_right_directory(self):
86 self.rm.set_readonly()