]> review.fuel-infra Code Review - openstack-build/neutron-build.git/commitdiff
[neutron-db-manage] remove old HEAD file when updating for branches
authorIhar Hrachyshka <ihrachys@redhat.com>
Wed, 29 Jul 2015 07:38:48 +0000 (09:38 +0200)
committerIhar Hrachyshka <ihrachys@redhat.com>
Thu, 13 Aug 2015 12:08:31 +0000 (12:08 +0000)
Partially-Implements: blueprint online-schema-migrations
Change-Id: I259d4f9090df821ade9d58f440c809e79458211d

neutron/db/migration/cli.py
neutron/tests/unit/db/test_migration.py

index 53c5393bd891160a07ff864f5659af0341edcd4f..4c3472e0c8bc5b2fcb11ca6f19dd1b6301d1e066 100644 (file)
@@ -235,6 +235,10 @@ def update_heads_file(config):
     heads_path = _get_active_head_file_path(config)
     with open(heads_path, 'w+') as f:
         f.write('\n'.join(heads))
+    if _use_separate_migration_branches(config):
+        old_head_file = _get_head_file_path(config)
+        if os.path.exists(old_head_file):
+            os.remove(old_head_file)
 
 
 def add_command_parsers(subparsers):
index 87f57f7e16c0690f18bd3929a7ecbc53e90a90d5..aba0616d844cf2fa85fd8f070a18503f0bef5d1a 100644 (file)
@@ -259,24 +259,23 @@ class TestCli(base.BaseTestCase):
                 mock_open.return_value.read.return_value = (
                     '\n'.join(file_heads))
 
-                with mock.patch('os.path.isfile') as is_file:
-                    is_file.return_value = bool(file_heads)
-
-                    if all(head in file_heads for head in heads):
-                        cli.validate_heads_file(fake_config)
-                    else:
-                        self.assertRaises(
-                            SystemExit,
-                            cli.validate_heads_file,
-                            fake_config
-                        )
-                        self.mock_alembic_err.assert_called_once_with(mock.ANY)
+                if all(head in file_heads for head in heads):
+                    cli.validate_heads_file(fake_config)
+                else:
+                    self.assertRaises(
+                        SystemExit,
+                        cli.validate_heads_file,
+                        fake_config
+                    )
+                    self.assertTrue(self.mock_alembic_err.called)
+
                 if branchless:
                     mock_open.assert_called_with(
                         cli._get_head_file_path(fake_config))
                 else:
                     mock_open.assert_called_with(
                         cli._get_heads_file_path(fake_config))
+
             fc.assert_called_once_with(fake_config)
 
     def test_validate_heads_file_multiple_heads(self):
@@ -324,7 +323,9 @@ class TestCli(base.BaseTestCase):
             )
             self.mock_alembic_err.assert_called_once_with(mock.ANY)
 
-    def test_update_heads_file_success(self):
+    @mock.patch('os.path.exists')
+    @mock.patch('os.remove')
+    def test_update_heads_file_success(self, *os_mocks):
         with mock.patch('alembic.script.ScriptDirectory.from_config') as fc:
             heads = ('a', 'b')
             fc.return_value.get_heads.return_value = heads
@@ -336,6 +337,10 @@ class TestCli(base.BaseTestCase):
                 mock_open.return_value.write.assert_called_once_with(
                     '\n'.join(heads))
 
+                old_head_file = cli._get_head_file_path(self.configs[0])
+                for mock_ in os_mocks:
+                    mock_.assert_called_with(old_head_file)
+
     def test_get_project_base(self):
         config = alembic_config.Config()
         config.set_main_option('script_location', 'a.b.c:d')