mirror of
https://github.com/zulip/zulip.git
synced 2025-11-05 06:23:38 +00:00
test databases: Add Database.run_db_migrations().
We can reduce some code duplication by having this on the class.
This commit is contained in:
@@ -171,8 +171,12 @@ def main(options: argparse.Namespace) -> int:
|
|||||||
import django
|
import django
|
||||||
django.setup()
|
django.setup()
|
||||||
|
|
||||||
from zerver.lib.test_fixtures import template_database_status, run_db_migrations, \
|
from zerver.lib.test_fixtures import (
|
||||||
destroy_leaked_test_databases
|
DEV_DATABASE,
|
||||||
|
TEST_DATABASE,
|
||||||
|
destroy_leaked_test_databases,
|
||||||
|
template_database_status,
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from zerver.lib.queue import SimpleQueueClient
|
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/setup/postgres-init-dev-db"])
|
||||||
run(["tools/do-destroy-rebuild-database"])
|
run(["tools/do-destroy-rebuild-database"])
|
||||||
elif dev_template_db_status == 'run_migrations':
|
elif dev_template_db_status == 'run_migrations':
|
||||||
run_db_migrations('dev')
|
DEV_DATABASE.run_db_migrations()
|
||||||
elif dev_template_db_status == 'current':
|
elif dev_template_db_status == 'current':
|
||||||
print("No need to regenerate the dev DB.")
|
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/setup/postgres-init-test-db"])
|
||||||
run(["tools/do-destroy-rebuild-test-database"])
|
run(["tools/do-destroy-rebuild-test-database"])
|
||||||
elif test_template_db_status == 'run_migrations':
|
elif test_template_db_status == 'run_migrations':
|
||||||
run_db_migrations('test')
|
TEST_DATABASE.run_db_migrations()
|
||||||
elif test_template_db_status == 'current':
|
elif test_template_db_status == 'current':
|
||||||
print("No need to regenerate the test DB.")
|
print("No need to regenerate the test DB.")
|
||||||
|
|
||||||
|
|||||||
@@ -36,6 +36,27 @@ class Database:
|
|||||||
self.migration_status_file
|
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(
|
DEV_DATABASE = Database(
|
||||||
platform='dev',
|
platform='dev',
|
||||||
database_name='zulip',
|
database_name='zulip',
|
||||||
@@ -48,28 +69,6 @@ TEST_DATABASE = Database(
|
|||||||
settings='zproject.test_settings',
|
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,
|
def update_test_databases_if_required(use_force: bool=False,
|
||||||
rebuild_test_database: bool=False) -> None:
|
rebuild_test_database: bool=False) -> None:
|
||||||
"""Checks whether the zulip_test_template database template, is
|
"""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':
|
if use_force or test_template_db_status == 'needs_rebuild':
|
||||||
generate_fixtures_command.append('--force')
|
generate_fixtures_command.append('--force')
|
||||||
elif test_template_db_status == 'run_migrations':
|
elif test_template_db_status == 'run_migrations':
|
||||||
run_db_migrations('test')
|
TEST_DATABASE.run_db_migrations()
|
||||||
elif not rebuild_test_database:
|
elif not rebuild_test_database:
|
||||||
return
|
return
|
||||||
subprocess.check_call(generate_fixtures_command)
|
subprocess.check_call(generate_fixtures_command)
|
||||||
|
|||||||
Reference in New Issue
Block a user