diff --git a/test-requirements.txt b/test-requirements.txt index 169caf4..ef7c9af 100644 --- a/test-requirements.txt +++ b/test-requirements.txt @@ -8,8 +8,9 @@ coverage>=3.6 discover python-subunit>=0.0.18 sphinx!=1.2.0,!=1.3b1,<1.3,>=1.1.2 +oslo.db[fixtures] oslosphinx>=2.5.0 # Apache-2.0 oslotest>=1.10.0 # Apache-2.0 testrepository>=0.0.18 testscenarios>=0.4 -testtools>=1.4.0 \ No newline at end of file +testtools>=1.4.0 diff --git a/tuning_box/migrations/env.py b/tuning_box/migrations/env.py index 4704170..01cdee6 100644 --- a/tuning_box/migrations/env.py +++ b/tuning_box/migrations/env.py @@ -20,7 +20,8 @@ from tuning_box import db config = context.config if config.get_main_option('table_prefix') is None: config.set_main_option('table_prefix', '') -logging.config.fileConfig(config.config_file_name) +if config.config_file_name: + logging.config.fileConfig(config.config_file_name) target_metadata = db.db.metadata diff --git a/tuning_box/tests/test_db.py b/tuning_box/tests/test_db.py index 69f1e7f..033998f 100644 --- a/tuning_box/tests/test_db.py +++ b/tuning_box/tests/test_db.py @@ -10,7 +10,22 @@ # License for the specific language governing permissions and limitations # under the License. +import os +import tempfile + +_fd, _sqlite_db_file_name = tempfile.mkstemp() +os.close(_fd) +del _fd + +# oslo_db internals refuse to work properly if this is not set +os.environ["OS_TEST_DBAPI_ADMIN_CONNECTION"] = \ + "sqlite:///" + _sqlite_db_file_name + +from alembic import command as alembic_command +from alembic import config as alembic_config import flask +from oslo_db.sqlalchemy import test_base +from oslo_db.sqlalchemy import test_migrations from tuning_box import db from tuning_box.tests import base @@ -93,3 +108,28 @@ class TestEnvironmentHierarchyLevel(_DBTestCase): class TestEnvironmentHierarchyLevelPrefixed(base.PrefixedTestCaseMixin, TestEnvironmentHierarchyLevel): pass + + +class TestMigrationsSync(test_migrations.ModelsMigrationsSync, + base.TestCase, + test_base.DbTestCase): + def setUp(self): + super(TestMigrationsSync, self).setUp() + self.addCleanup(os.remove, _sqlite_db_file_name) + + def get_metadata(self): + return db.db.metadata + + def get_engine(self): + return self.engine + + def get_alembic_config(self, engine): + config = alembic_config.Config() + config.set_main_option('sqlalchemy.url', str(engine.url)) + config.set_main_option('script_location', 'tuning_box/migrations') + config.set_main_option('version_table', 'alembic_version') + return config + + def db_sync(self, engine): + config = self.get_alembic_config(engine) + alembic_command.upgrade(config, 'head')