From a7df90089557516a82c0ba8f7368cdd541099059 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jason=20K=C3=B6lker?= Date: Fri, 24 Feb 2012 10:10:21 -0600 Subject: [PATCH] Monkey patch migrate < 0.7.3 * Works around migrate issue 72 [1] by monkey patching migrate prior to import. * Removes previous workaround * Refactor test config to work under nosetests directly * Fixes LP940407 1: https://code.google.com/p/sqlalchemy-migrate/issues/detail?id=72 Change-Id: I219e4cecf8bb2e34ae238ac270428f496378ee61 --- nova/tests/test_migrations.py | 44 ++++++++++++++++++++++++++++++----- run_tests.sh | 10 -------- 2 files changed, 38 insertions(+), 16 deletions(-) diff --git a/nova/tests/test_migrations.py b/nova/tests/test_migrations.py index b0caea22081e..4ec3dbc76fac 100644 --- a/nova/tests/test_migrations.py +++ b/nova/tests/test_migrations.py @@ -26,31 +26,63 @@ if possible. import ConfigParser import commands +import distutils.version as dist_version import os import unittest import urlparse -import migrate.versioning.api as migration_api -from migrate.versioning.repository import Repository +import migrate +from migrate.versioning import util as migrate_util from sqlalchemy import create_engine +import nova.db.sqlalchemy.migrate_repo from nova import log as logging from nova import test + LOG = logging.getLogger('nova.tests.test_migrations') +MIGRATE_PKG_VER = dist_version.StrictVersion(migrate.__version__) +USE_MIGRATE_PATCH = MIGRATE_PKG_VER < dist_version.StrictVersion('0.7.3') + + +@migrate_util.decorator +def patched_with_engine(f, *a, **kw): + url = a[0] + engine = migrate_util.construct_engine(url, **kw) + + try: + kw['engine'] = engine + return f(*a, **kw) + finally: + if isinstance(engine, migrate_util.Engine) and engine is not url: + migrate_util.log.debug('Disposing SQLAlchemy engine %s', engine) + engine.dispose() + + +# TODO(jkoelker) When migrate 0.7.3 is released and nova depends +# on that version or higher, this can be removed +if USE_MIGRATE_PATCH: + migrate_util.with_engine = patched_with_engine + + +# NOTE(jkoelker) Delay importing migrate until we are patched +from migrate.versioning import api as migration_api +from migrate.versioning.repository import Repository + class TestMigrations(unittest.TestCase): """Test sqlalchemy-migrate migrations""" TEST_DATABASES = {} + DEFAULT_CONFIG_FILE = os.path.join(os.path.dirname(__file__), + 'test_migrations.conf') # Test machines can set the NOVA_TEST_MIGRATIONS_CONF variable # to override the location of the config file for migration testing CONFIG_FILE_PATH = os.environ.get('NOVA_TEST_MIGRATIONS_CONF', - os.path.join('test_migrations.conf')) - REPOSITORY_PATH = os.path.abspath(os.path.join('..', 'db', 'sqlalchemy', - 'migrate_repo')) - REPOSITORY = Repository(REPOSITORY_PATH) + DEFAULT_CONFIG_FILE) + MIGRATE_FILE = nova.db.sqlalchemy.migrate_repo.__file__ + REPOSITORY = Repository(os.path.abspath(os.path.dirname(MIGRATE_FILE))) def __init__(self, *args, **kwargs): super(TestMigrations, self).__init__(*args, **kwargs) diff --git a/run_tests.sh b/run_tests.sh index 9ebb0bc42a73..6d8cb7cae651 100755 --- a/run_tests.sh +++ b/run_tests.sh @@ -185,16 +185,6 @@ if [ $recreate_db -eq 1 ]; then rm -f tests.sqlite fi -# Workaround for sqlalchemy-migrate issue 72 -# see: http://code.google.com/p/sqlalchemy-migrate/issues/detail?id=72 -if [ $patch_migrate -eq 1 ]; then - pyver=python`python -c 'import sys; print(".".join(map(str, sys.version_info[:2])))'` - target=${venv}/lib/${pyver}/site-packages/migrate/versioning/util/__init__.py - if [ -f $target ]; then - sed -i -e '/^\s\+finally:$/ {N; /^\(\s\+finally:\n\s\+if isinstance(engine, Engine)\):$/ {s//\1 and engine is not url:/}}' $target - fi -fi - run_tests # NOTE(sirp): we only want to run pep8 when we're running the full-test suite,