Merge ~ack/maas:1908552-3.0 into maas:3.0

Proposed by Alberto Donato
Status: Merged
Approved by: Alberto Donato
Approved revision: b7c3a8e67a0b0ea59f27ecdadaf0be9443c86ba3
Merge reported by: MAAS Lander
Merged at revision: not available
Proposed branch: ~ack/maas:1908552-3.0
Merge into: maas:3.0
Diff against target: 65 lines (+38/-1)
2 files modified
src/maascli/snap.py (+16/-1)
src/maascli/tests/test_snap.py (+22/-0)
Reviewer Review Type Date Requested Status
Alberto Donato (community) Approve
Review via email: mp+403338@code.launchpad.net

Commit message

LP: #1908552 - only run DB migrations on init if DB is empty

To post a comment you must log in.
Revision history for this message
Alberto Donato (ack) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
diff --git a/src/maascli/snap.py b/src/maascli/snap.py
index bef3af1..5a16465 100644
--- a/src/maascli/snap.py
+++ b/src/maascli/snap.py
@@ -315,6 +315,21 @@ def change_user(username, effective=False):
315 os.setuid(running_uid)315 os.setuid(running_uid)
316316
317317
318def db_need_init(connection=None) -> bool:
319 """Whether the database needs initializing.
320
321 It assumes the database is set up if there's any table in it.
322 """
323 if connection is None:
324 # local import since the CLI shouldn't unconditionally depend on Django
325 from django.db import connection
326
327 try:
328 return not connection.introspection.table_names()
329 except Exception:
330 return True
331
332
318def migrate_db(capture=False):333def migrate_db(capture=False):
319 """Migrate the database."""334 """Migrate the database."""
320 if capture:335 if capture:
@@ -645,7 +660,7 @@ class cmd_init(SnapCommand):
645 start_services,660 start_services,
646 )661 )
647662
648 if mode in ("region", "region+rack"):663 if mode in ("region", "region+rack") and db_need_init():
649 # When in 'region' or 'region+rack' the migrations for the database664 # When in 'region' or 'region+rack' the migrations for the database
650 # must be at the same level as this controller.665 # must be at the same level as this controller.
651 perform_work(666 perform_work(
diff --git a/src/maascli/tests/test_snap.py b/src/maascli/tests/test_snap.py
index a0cf740..aee41db 100644
--- a/src/maascli/tests/test_snap.py
+++ b/src/maascli/tests/test_snap.py
@@ -598,3 +598,25 @@ class TestCmdConfig(MAASTestCase):
598 config_manager.update.assert_not_called()598 config_manager.update.assert_not_called()
599 self.assertEqual(stdout.getvalue(), "")599 self.assertEqual(stdout.getvalue(), "")
600 mock_sighup_supervisord.assert_not_called()600 mock_sighup_supervisord.assert_not_called()
601
602
603class TestDBNeedInit(MAASTestCase):
604 def test_has_tables(self):
605 connection = MagicMock()
606 connection.introspection.table_names.return_value = [
607 "table1",
608 "table2",
609 ]
610 self.assertFalse(snap.db_need_init(connection))
611
612 def test_no_tables(self):
613 connection = MagicMock()
614 connection.introspection.table_names.return_value = []
615 self.assertTrue(snap.db_need_init(connection))
616
617 def test_fail(self):
618 connection = MagicMock()
619 connection.introspection.table_names.side_effect = Exception(
620 "connection failed"
621 )
622 self.assertTrue(snap.db_need_init(connection))

Subscribers

People subscribed via source and target branches