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
This commit is contained in:
Jason Kölker
2012-02-24 10:10:21 -06:00
parent 7a5c0c0e15
commit a7df900895
2 changed files with 38 additions and 16 deletions

View File

@@ -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)

View File

@@ -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,