summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrant Knudson <bknudson@us.ibm.com>2016-04-12 20:29:30 -0500
committerBrant Knudson <bknudson@us.ibm.com>2016-04-12 20:29:30 -0500
commit8eda40da25da2a05c19f9e68a3973986e4a38320 (patch)
tree45f3458046965d068f86824bed1dc8267b3dc817
parent46767e58b909eeb87148e73a2058377568403db3 (diff)
downloadoslo-db-8eda40da25da2a05c19f9e68a3973986e4a38320.tar.gz
Fix db_version checking for sqlalchemy-migrate
When a project has multiple migration repos and oslo_db.sqlalchemy.migration.db_version() is called using the one repo as the abs_path, it will attempt to put the repo under version control. The next time it's called with the other abs_path, it would fail rather than attempting to put the repo under version control when using sqlalchemy-migrate. When using alembic, db_version would attempt to put the new abs_path under version control. This change makes it so that when sqlalchemy-migrate is used db_version() will attempt to version control the new abs_path just like when alembic is used. Change-Id: If5e2ae1a72f9118c7aca34e925fdc5e3e8480275
-rw-r--r--oslo_db/sqlalchemy/migration.py3
-rw-r--r--oslo_db/tests/sqlalchemy/test_migration_common.py36
2 files changed, 38 insertions, 1 deletions
diff --git a/oslo_db/sqlalchemy/migration.py b/oslo_db/sqlalchemy/migration.py
index d07ffc8..b669aa2 100644
--- a/oslo_db/sqlalchemy/migration.py
+++ b/oslo_db/sqlalchemy/migration.py
@@ -128,7 +128,8 @@ def db_version(engine, abs_path, init_version):
meta = sqlalchemy.MetaData()
meta.reflect(bind=engine)
tables = meta.tables
- if len(tables) == 0 or 'alembic_version' in tables:
+ if (len(tables) == 0 or 'alembic_version' in tables or
+ 'migrate_version' in tables):
db_version_control(engine, abs_path, version=init_version)
return versioning_api.db_version(engine, repository)
else:
diff --git a/oslo_db/tests/sqlalchemy/test_migration_common.py b/oslo_db/tests/sqlalchemy/test_migration_common.py
index ebdaaa9..37411c5 100644
--- a/oslo_db/tests/sqlalchemy/test_migration_common.py
+++ b/oslo_db/tests/sqlalchemy/test_migration_common.py
@@ -130,6 +130,42 @@ class TestMigrationCommon(test_base.DbTestCase):
mock_vc.assert_called_once_with(self.engine, self.return_value1,
self.init_version)
+ @mock.patch.object(versioning_api, 'version_control')
+ def test_db_version_raise_not_controlled_alembic_tables(self, mock_vc):
+ # When there are tables but the alembic control table
+ # (alembic_version) is present, attempt to version the db.
+ # This simulates the case where there is are multiple repos (different
+ # abs_paths) and a different path has been versioned already.
+ with mock.patch.object(sqlalchemy, 'MetaData') as mock_meta:
+ self.mock_api_db_version.side_effect = [
+ migrate_exception.DatabaseNotControlledError('oups'), None]
+ my_meta = mock.MagicMock()
+ my_meta.tables = {'alembic_version': 1, 'b': 2}
+ mock_meta.return_value = my_meta
+
+ migration.db_version(self.engine, self.path, self.init_version)
+
+ mock_vc.assert_called_once_with(self.engine, self.return_value1,
+ self.init_version)
+
+ @mock.patch.object(versioning_api, 'version_control')
+ def test_db_version_raise_not_controlled_migrate_tables(self, mock_vc):
+ # When there are tables but the sqlalchemy-migrate control table
+ # (migrate_version) is present, attempt to version the db.
+ # This simulates the case where there is are multiple repos (different
+ # abs_paths) and a different path has been versioned already.
+ with mock.patch.object(sqlalchemy, 'MetaData') as mock_meta:
+ self.mock_api_db_version.side_effect = [
+ migrate_exception.DatabaseNotControlledError('oups'), None]
+ my_meta = mock.MagicMock()
+ my_meta.tables = {'migrate_version': 1, 'b': 2}
+ mock_meta.return_value = my_meta
+
+ migration.db_version(self.engine, self.path, self.init_version)
+
+ mock_vc.assert_called_once_with(self.engine, self.return_value1,
+ self.init_version)
+
def test_db_sync_wrong_version(self):
self.assertRaises(db_exception.DbMigrationError,
migration.db_sync, self.engine, self.path, 'foo')