Merge lp:~elachuni/django-pgtools/fix-long-seqs-and-dbcheck into lp:django-pgtools

Proposed by Anthony Lenton
Status: Merged
Approved by: Danny Tamez
Approved revision: 7
Merged at revision: 5
Proposed branch: lp:~elachuni/django-pgtools/fix-long-seqs-and-dbcheck
Merge into: lp:django-pgtools
Diff against target: 121 lines (+64/-6)
4 files modified
debian/changelog (+7/-1)
pgtools/management/commands/grantuser.py (+3/-1)
pgtools/tests.py (+44/-3)
pgtools/utils.py (+10/-1)
To merge this branch: bzr merge lp:~elachuni/django-pgtools/fix-long-seqs-and-dbcheck
Reviewer Review Type Date Requested Status
Danny Tamez (community) Approve
Review via email: mp+101954@code.launchpad.net

Commit message

Support 1.3 style db config; long sequences are handled correctly

Description of the change

This branch includes two fixes:
 - utils.check_database_engine now supports 1.3-style DATABASE settings
 - long sequences (with names over 64 chars long) are handled correctly, via pg_get_serial_sequence

To post a comment you must log in.
Revision history for this message
Danny Tamez (zematynnad) wrote :

OK, I gave up on the tests but the 2 fixes look fine.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'debian/changelog'
2--- debian/changelog 2011-06-02 14:54:18 +0000
3+++ debian/changelog 2012-04-13 18:16:24 +0000
4@@ -1,8 +1,14 @@
5+django-pgtools (0.1-2) lucid; urgency=low
6+
7+ * Fixed long sequences and DB engine check.
8+
9+ -- Anthony Lenton <anthony.lenton@canonical.com> Fri, 13 Apr 2012 15:12:15 -0300
10+
11 django-pgtools (0.1-1) lucid; urgency=low
12
13 * Fixed packaging issue (no commands were included in the package)
14
15- -- Łukasz Czyżykowski <lukasz.czyzykowski@canonical.com> jue, 02 jun 2011 15:54:06 +0100
16+ -- Łukasz Czyżykowski <lukasz.czyzykowski@canonical.com> Thu, 02 jun 2011 15:54:06 +0100
17
18 django-pgtools (0.1) lucid; urgency=low
19
20
21=== modified file 'pgtools/management/commands/grantuser.py'
22--- pgtools/management/commands/grantuser.py 2011-05-30 14:30:10 +0000
23+++ pgtools/management/commands/grantuser.py 2012-04-13 18:16:24 +0000
24@@ -53,7 +53,9 @@
25
26 def _grant_many_to_many(self, field):
27 table = field.m2m_db_table()
28- sequence = "%s_id_seq" % table
29+ self.cursor.execute("select pg_get_serial_sequence('%s', 'id')" %
30+ table)
31+ sequence = self.cursor.fetchone()[0]
32 self._grant_one(table)
33 self._grant_one(sequence)
34
35
36=== modified file 'pgtools/tests.py'
37--- pgtools/tests.py 2011-05-30 14:30:10 +0000
38+++ pgtools/tests.py 2012-04-13 18:16:24 +0000
39@@ -441,13 +441,37 @@
40 self.assertEqual(str(err),
41 "User cannot be deleted: %s" % TEST_USER_NAME1)
42
43- def test_utils_check_database_engine(self):
44+ def test_utils_check_database_engine_11_style(self):
45 # Test default: 'postgresql_psycopg2' (hopefully)
46- check_database_engine()
47+ settings_to_patch = {}
48+ if hasattr(settings, 'DATABASES'):
49+ settings_to_patch['DATABASES'] = None
50+ for engine in [
51+ 'postgresql_psycopg2',
52+ 'django.db.backends.postgresql_psycopg2']:
53+ settings_to_patch['DATABASE_ENGINE'] = engine
54+ with patch_settings(**settings_to_patch):
55+ check_database_engine()
56+
57 # Test an unsupported engine
58- with patch_settings(DATABASE_ENGINE='sqlite3'):
59+ settings_to_patch['DATABASE_ENGINE'] = 'sqlite3'
60+ with patch_settings(**settings_to_patch):
61 self.assertRaises(CommandError, check_database_engine)
62
63+ @patch('pgtools.utils.settings')
64+ def test_utils_check_database_engine_13_style(self, mock_settings):
65+ dbconfig = {'default': {'NAME': 'mydatabase'}}
66+ mock_settings.DATABASES = dbconfig
67+ for engine in ['postgresql_psycopg2',
68+ 'django.db.backends.postgresql_psycopg2']:
69+ dbconfig['default']['ENGINE'] = engine
70+ mock_settings.DATABASE_ENGINE = 'sqlite3'
71+ check_database_engine()
72+ # Test an unsupported engine
73+ dbconfig['default']['ENGINE'] = 'sqlite3'
74+ mock_settings.DATABASE_ENGINE = 'postgresql_psycopg2'
75+ self.assertRaises(CommandError, check_database_engine)
76+
77 def test_utils_get_rolename_from_settings(self):
78 # Test with default (nothing set)
79 self.assertRaises(CommandError, get_rolename_from_settings)
80@@ -517,3 +541,20 @@
81 # sequences
82 self.assertEqual(mock_grant_one.call_args_list[1],
83 (('django_admin_log_id_seq',), {}))
84+
85+ @patch('pgtools.management.commands.grantuser.get_models')
86+ @patch('pgtools.management.commands.grantuser.connection')
87+ def test__grant_many_to_many(self, mock_connection, mock_get_models):
88+ mock_connection.cursor.return_value.fetchone.return_value = ['foo']
89+ from django.contrib.auth import models as auth_models
90+ mock_get_models.return_value = get_models(auth_models)
91+
92+ call_command('grantuser', 'payments')
93+
94+ args_list = mock_connection.cursor.return_value.execute.call_args_list
95+ calls = [x[0][0] for x in args_list]
96+ for expected in [
97+ "select pg_get_serial_sequence('auth_user_groups', 'id')",
98+ 'GRANT ALL ON foo TO payments;'
99+ ]:
100+ self.assertTrue(expected in calls)
101
102=== modified file 'pgtools/utils.py'
103--- pgtools/utils.py 2011-05-30 14:30:10 +0000
104+++ pgtools/utils.py 2012-04-13 18:16:24 +0000
105@@ -7,7 +7,16 @@
106
107
108 def check_database_engine():
109- if settings.DATABASE_ENGINE != 'postgresql_psycopg2':
110+ all_psql = True
111+ if getattr(settings, 'DATABASES', None):
112+ # Django 1.2+ style
113+ for db in settings.DATABASES.values():
114+ if not db['ENGINE'].endswith('postgresql_psycopg2'):
115+ all_psql = False
116+ else:
117+ # Django -1.1 style
118+ all_psql = settings.DATABASE_ENGINE.endswith('postgresql_psycopg2')
119+ if not all_psql:
120 raise CommandError(
121 'Only the postgresql_psycopg2 database engine is supported.')
122

Subscribers

People subscribed via source and target branches