Merge lp:~blake-rouse/maas/fix-1524152 into lp:~maas-committers/maas/trunk

Proposed by Blake Rouse
Status: Merged
Approved by: Andres Rodriguez
Approved revision: no longer in the source branch.
Merged at revision: 4545
Proposed branch: lp:~blake-rouse/maas/fix-1524152
Merge into: lp:~maas-committers/maas/trunk
Diff against target: 138 lines (+81/-19)
3 files modified
src/maasserver/management/commands/dbupgrade.py (+29/-11)
src/maasserver/migrations/builtin/piston3/0002_auto_20151209_1652.py (+22/-0)
src/maasserver/tests/test_commands_dbupgrade.py (+30/-8)
To merge this branch: bzr merge lp:~blake-rouse/maas/fix-1524152
Reviewer Review Type Date Requested Status
Andres Rodriguez (community) Approve
Review via email: mp+280057@code.launchpad.net

Commit message

Fix upgrade path for piston. Fix missing migrations in the in-tree MAAS 1.9 tarball. Fix missing piston3 migration.

To post a comment you must log in.
Revision history for this message
Andres Rodriguez (andreserl) wrote :

lgtm!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/maasserver/management/commands/dbupgrade.py'
2--- src/maasserver/management/commands/dbupgrade.py 2015-12-08 18:48:31 +0000
3+++ src/maasserver/management/commands/dbupgrade.py 2015-12-09 17:02:06 +0000
4@@ -190,13 +190,25 @@
5 not cls._south_migrations_are_complete(database))
6
7 @classmethod
8- def _rename_piston_to_piston3(cls, database):
9+ def _find_tables(cls, database, startwith):
10+ """Return list of tables that start with `startwith`."""
11+ cursor = connections[database].cursor()
12+ cursor.execute(
13+ "SELECT table_name FROM information_schema.tables "
14+ "WHERE table_schema = 'public' AND table_name LIKE %s",
15+ [startwith + "%"])
16+ return [
17+ row[0]
18+ for row in cursor.fetchall()
19+ ]
20+
21+ @classmethod
22+ def _rename_piston_to_piston3(cls, database, tables):
23 """Rename piston to piston3."""
24 cursor = connections[database].cursor()
25- cursor.execute(
26- "ALTER TABLE piston_consumer RENAME TO piston3_consumer")
27- cursor.execute("ALTER TABLE piston_nonce RENAME TO piston3_nonce")
28- cursor.execute("ALTER TABLE piston_token RENAME TO piston3_token")
29+ for table in tables:
30+ cursor.execute(
31+ "ALTER TABLE piston_%s RENAME TO piston3_%s" % (table, table))
32
33 @classmethod
34 def _perform_south_migrations(cls, script_path, database):
35@@ -259,13 +271,19 @@
36 if rc != 0:
37 sys.exit(rc)
38 else:
39- # Piston has been renamed from piston to piston3. If south was ever
40- # performed then the tables need to be renamed.
41- south_was_performed = self._south_was_performed(database)
42- if south_was_performed:
43- self._rename_piston_to_piston3(database)
44+ # Piston has been renamed from piston to piston3.
45+ piston_tables = self._find_tables(database, "piston")
46+ piston_tables = [
47+ table[7:]
48+ for table in piston_tables
49+ if not table.startswith("piston3")
50+ ]
51+ if len(piston_tables):
52+ self._rename_piston_to_piston3(database, piston_tables)
53
54 # Perform the builtin migration faking the initial migrations
55 # if south was ever performed.
56 call_command(
57- "migrate", interactive=False, fake_initial=south_was_performed)
58+ "migrate",
59+ interactive=False,
60+ fake_initial=self._south_was_performed(database))
61
62=== added file 'src/maasserver/migrations/builtin/piston3/0002_auto_20151209_1652.py'
63--- src/maasserver/migrations/builtin/piston3/0002_auto_20151209_1652.py 1970-01-01 00:00:00 +0000
64+++ src/maasserver/migrations/builtin/piston3/0002_auto_20151209_1652.py 2015-12-09 17:02:06 +0000
65@@ -0,0 +1,22 @@
66+# -*- coding: utf-8 -*-
67+from __future__ import unicode_literals
68+
69+from django.db import (
70+ migrations,
71+ models,
72+)
73+
74+
75+class Migration(migrations.Migration):
76+
77+ dependencies = [
78+ ('piston3', '0001_initial'),
79+ ]
80+
81+ operations = [
82+ migrations.AlterField(
83+ model_name='consumer',
84+ name='status',
85+ field=models.CharField(default='pending', choices=[('pending', 'Pending'), ('accepted', 'Accepted'), ('canceled', 'Canceled'), ('rejected', 'Rejected')], max_length=16),
86+ ),
87+ ]
88
89=== modified file 'src/maasserver/migrations/south/django16_south_maas19.tar.gz'
90Binary files src/maasserver/migrations/south/django16_south_maas19.tar.gz 2015-12-02 20:43:30 +0000 and src/maasserver/migrations/south/django16_south_maas19.tar.gz 2015-12-09 17:02:06 +0000 differ
91=== modified file 'src/maasserver/tests/test_commands_dbupgrade.py'
92--- src/maasserver/tests/test_commands_dbupgrade.py 2015-12-08 18:48:31 +0000
93+++ src/maasserver/tests/test_commands_dbupgrade.py 2015-12-09 17:02:06 +0000
94@@ -137,14 +137,36 @@
95 self.assertCalledSouth(popen)
96 self.assertCalledDjango(popen)
97
98- def test_django_run_renames_piston_tables_if_south_ran_before(self):
99- self.patch(
100- dbupgrade_command, "_south_was_performed").return_value = True
101- mock_rename = self.patch(
102- dbupgrade_command, "_rename_piston_to_piston3")
103- mock_call = self.patch(dbupgrade_module, "call_command")
104- call_command('dbupgrade', django=True)
105- self.assertThat(mock_rename, MockCalledOnceWith("default"))
106+ def test_django_run_renames_piston_tables_if_piston_tables_exists(self):
107+ self.patch(
108+ dbupgrade_command, "_south_was_performed").return_value = True
109+ self.patch(dbupgrade_command, "_find_tables").return_value = [
110+ "piston_consumer",
111+ "piston_token",
112+ ]
113+ mock_rename = self.patch(
114+ dbupgrade_command, "_rename_piston_to_piston3")
115+ mock_call = self.patch(dbupgrade_module, "call_command")
116+ call_command('dbupgrade', django=True)
117+ self.assertThat(
118+ mock_rename, MockCalledOnceWith("default", ["consumer", "token"]))
119+ self.assertThat(
120+ mock_call, MockCalledOnceWith(
121+ "migrate", interactive=False, fake_initial=True))
122+
123+ def test_django_run_doesnt_renames_piston_tables_if_piston3(self):
124+ self.patch(
125+ dbupgrade_command, "_south_was_performed").return_value = True
126+ self.patch(dbupgrade_command, "_find_tables").return_value = [
127+ "piston3_consumer",
128+ "piston3_token",
129+ ]
130+ mock_rename = self.patch(
131+ dbupgrade_command, "_rename_piston_to_piston3")
132+ mock_call = self.patch(dbupgrade_module, "call_command")
133+ call_command('dbupgrade', django=True)
134+ self.assertThat(
135+ mock_rename, MockNotCalled())
136 self.assertThat(
137 mock_call, MockCalledOnceWith(
138 "migrate", interactive=False, fake_initial=True))