Add support for populating test data during migration testing

This change ensures that the tables in the database during migration have at least one row of "real" data, which should help catch issues in the future where we forget to set column defaults and other such schema oversights that can only be truly tested with non-empty tables

Fixes https://jira.coreos.com/browse/QUAY-913
This commit is contained in:
Joseph Schorr 2018-05-07 16:45:57 +03:00
parent c92c0ca5e1
commit f6ff0d6ca0
41 changed files with 653 additions and 86 deletions

View file

@ -12,6 +12,8 @@ from urllib import unquote, quote
from peewee import SqliteDatabase
from data.database import all_models, db
from data.migrations.tester import NoopTester, PopulateTestDataTester
from app import app
from data.model.sqlalchemybridge import gen_sqlalchemy_metadata
from release import GIT_HEAD, REGION, SERVICE
@ -39,6 +41,18 @@ tables = AttrDict(target_metadata.tables)
# my_important_option = config.get_main_option("my_important_option")
# ... etc.
def get_tester():
""" Returns the tester to use. We only return the tester that populates data
if the TEST_MIGRATE env var is set to `true` AND we make sure we're not
connecting to a production database.
"""
if os.environ.get('TEST_MIGRATE', '') == 'true':
url = unquote(app.config['DB_URI'])
if url.find('.quay.io') < 0:
return PopulateTestDataTester()
return NoopTester()
def run_migrations_offline():
"""Run migrations in 'offline' mode.
@ -55,7 +69,8 @@ def run_migrations_offline():
context.configure(url=url, target_metadata=target_metadata, transactional_ddl=True)
with context.begin_transaction():
context.run_migrations(tables=tables)
context.run_migrations(tables=tables, tester=get_tester())
def run_migrations_online():
"""Run migrations in 'online' mode.
@ -84,7 +99,7 @@ def run_migrations_online():
try:
with context.begin_transaction():
try:
context.run_migrations(tables=tables)
context.run_migrations(tables=tables, tester=get_tester())
except (CommandError, ResolutionError) as ex:
if 'No such revision' not in str(ex):
raise