From 452f95bc634a5259fcf5097a45eaacb2e96988fb Mon Sep 17 00:00:00 2001 From: Ihar Hrachyshka Date: Wed, 29 Jul 2015 09:38:48 +0200 Subject: [PATCH] [neutron-db-manage] remove old HEAD file when updating for branches Partially-Implements: blueprint online-schema-migrations Change-Id: I259d4f9090df821ade9d58f440c809e79458211d --- neutron/db/migration/cli.py | 4 ++++ neutron/tests/unit/db/test_migration.py | 31 ++++++++++++++----------- 2 files changed, 22 insertions(+), 13 deletions(-) diff --git a/neutron/db/migration/cli.py b/neutron/db/migration/cli.py index 53c5393bd..4c3472e0c 100644 --- a/neutron/db/migration/cli.py +++ b/neutron/db/migration/cli.py @@ -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): diff --git a/neutron/tests/unit/db/test_migration.py b/neutron/tests/unit/db/test_migration.py index 87f57f7e1..aba0616d8 100644 --- a/neutron/tests/unit/db/test_migration.py +++ b/neutron/tests/unit/db/test_migration.py @@ -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') -- 2.45.2