test databases: Add Database.run_db_migrations().

We can reduce some code duplication by having this
on the class.
This commit is contained in:
Steve Howell
2020-04-20 15:21:22 +00:00
committed by Tim Abbott
parent 1795c06a53
commit 5c5d85cf19
2 changed files with 30 additions and 27 deletions

View File

@@ -171,8 +171,12 @@ def main(options: argparse.Namespace) -> int:
import django
django.setup()
from zerver.lib.test_fixtures import template_database_status, run_db_migrations, \
destroy_leaked_test_databases
from zerver.lib.test_fixtures import (
DEV_DATABASE,
TEST_DATABASE,
destroy_leaked_test_databases,
template_database_status,
)
try:
from zerver.lib.queue import SimpleQueueClient
@@ -191,7 +195,7 @@ def main(options: argparse.Namespace) -> int:
run(["tools/setup/postgres-init-dev-db"])
run(["tools/do-destroy-rebuild-database"])
elif dev_template_db_status == 'run_migrations':
run_db_migrations('dev')
DEV_DATABASE.run_db_migrations()
elif dev_template_db_status == 'current':
print("No need to regenerate the dev DB.")
@@ -200,7 +204,7 @@ def main(options: argparse.Namespace) -> int:
run(["tools/setup/postgres-init-test-db"])
run(["tools/do-destroy-rebuild-test-database"])
elif test_template_db_status == 'run_migrations':
run_db_migrations('test')
TEST_DATABASE.run_db_migrations()
elif test_template_db_status == 'current':
print("No need to regenerate the test DB.")

View File

@@ -36,6 +36,27 @@ class Database:
self.migration_status_file
)
def run_db_migrations(self) -> None:
# We shell out to `manage.py` and pass `DJANGO_SETTINGS_MODULE` on
# the command line rather than just calling the migration
# functions, because Django doesn't support changing settings like
# what the database is as runtime.
# Also we export ZULIP_DB_NAME which is ignored by dev platform but
# recognised by test platform and used to migrate correct db.
env_prelude = [
'env',
'DJANGO_SETTINGS_MODULE=' + self.settings,
'ZULIP_DB_NAME=' + self.database_name,
]
run(env_prelude + [
'./manage.py', 'migrate', '--no-input',
])
run(env_prelude + [
'./manage.py', 'get_migration_status', '--output='+self.migration_status_file,
])
DEV_DATABASE = Database(
platform='dev',
database_name='zulip',
@@ -48,28 +69,6 @@ TEST_DATABASE = Database(
settings='zproject.test_settings',
)
def run_db_migrations(platform: str) -> None:
if platform == 'dev':
migration_status_file = 'migration_status_dev'
settings = 'zproject.settings'
db_name = 'ZULIP_DB_NAME=zulip'
elif platform == 'test':
migration_status_file = 'migration_status_test'
settings = 'zproject.test_settings'
db_name = 'ZULIP_DB_NAME=zulip_test_template'
# We shell out to `manage.py` and pass `DJANGO_SETTINGS_MODULE` on
# the command line rather than just calling the migration
# functions, because Django doesn't support changing settings like
# what the database is as runtime.
# Also we export DB_NAME which is ignored by dev platform but
# recognised by test platform and used to migrate correct db.
run(['env', ('DJANGO_SETTINGS_MODULE=%s' % (settings,)), db_name,
'./manage.py', 'migrate', '--no-input'])
run(['env', ('DJANGO_SETTINGS_MODULE=%s' % (settings,)), db_name,
'./manage.py', 'get_migration_status',
'--output=%s' % (migration_status_file,)])
def update_test_databases_if_required(use_force: bool=False,
rebuild_test_database: bool=False) -> None:
"""Checks whether the zulip_test_template database template, is
@@ -95,7 +94,7 @@ def update_test_databases_if_required(use_force: bool=False,
if use_force or test_template_db_status == 'needs_rebuild':
generate_fixtures_command.append('--force')
elif test_template_db_status == 'run_migrations':
run_db_migrations('test')
TEST_DATABASE.run_db_migrations()
elif not rebuild_test_database:
return
subprocess.check_call(generate_fixtures_command)