test_fixtures: Refactor to have template_database_status API.

In this commit we are essentially just refactoring the function
is_template_database_current to be called template_database_status
and adjusting the return values accordingly.
This is essentially a preparatory commit for the upcoming commits
which will essentially enable us to not throw away entire DB and
rebuild from scratch if only running migrations could do the job.
This commit is contained in:
Aditya Bansal
2018-06-06 03:46:27 +05:30
committed by Tim Abbott
parent 53237d39aa
commit f7c11d1747
4 changed files with 25 additions and 19 deletions

View File

@@ -316,12 +316,12 @@ def main(options):
# of the development environment (it just uses the development # of the development environment (it just uses the development
# environment to build a release tarball). # environment to build a release tarball).
# Need to set up Django before using is_template_database_current # Need to set up Django before using template_database_status
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings") os.environ.setdefault("DJANGO_SETTINGS_MODULE", "zproject.settings")
import django import django
django.setup() django.setup()
from zerver.lib.test_fixtures import is_template_database_current from zerver.lib.test_fixtures import template_database_status
try: try:
from zerver.lib.queue import SimpleQueueClient from zerver.lib.queue import SimpleQueueClient
@@ -336,20 +336,22 @@ def main(options):
print("RabbitMQ is already configured.") print("RabbitMQ is already configured.")
migration_status_path = os.path.join(UUID_VAR_PATH, "migration_status_dev") migration_status_path = os.path.join(UUID_VAR_PATH, "migration_status_dev")
if options.is_force or not is_template_database_current( dev_template_db_status = template_database_status(
migration_status=migration_status_path, migration_status=migration_status_path,
settings="zproject.settings", settings="zproject.settings",
database_name="zulip", database_name="zulip",
): )
if options.is_force or dev_template_db_status == 'needs_rebuild':
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"])
else: elif dev_template_db_status == 'current':
print("No need to regenerate the dev DB.") print("No need to regenerate the dev DB.")
if options.is_force or not is_template_database_current(): test_template_db_status = template_database_status()
if options.is_force or test_template_db_status == 'needs_rebuild':
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"])
else: elif test_template_db_status == 'current':
print("No need to regenerate the test DB.") print("No need to regenerate the test DB.")
# Consider updating generated translations data: both `.mo` # Consider updating generated translations data: both `.mo`

View File

@@ -19,7 +19,7 @@ TOOLS_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
if TOOLS_DIR not in sys.path: if TOOLS_DIR not in sys.path:
sys.path.insert(0, os.path.dirname(TOOLS_DIR)) sys.path.insert(0, os.path.dirname(TOOLS_DIR))
from zerver.lib.test_fixtures import is_template_database_current from zerver.lib.test_fixtures import template_database_status
def set_up_django(external_host): def set_up_django(external_host):
# type: (str) -> None # type: (str) -> None
@@ -63,7 +63,8 @@ def test_server_running(force: bool=False, external_host: str='testserver',
if use_db: if use_db:
generate_fixtures_command = ['tools/setup/generate-fixtures'] generate_fixtures_command = ['tools/setup/generate-fixtures']
if not is_template_database_current(): test_template_db_status = template_database_status()
if test_template_db_status == 'needs_rebuild':
generate_fixtures_command.append('--force') generate_fixtures_command.append('--force')
subprocess.check_call(generate_fixtures_command) subprocess.check_call(generate_fixtures_command)

View File

@@ -171,7 +171,7 @@ if __name__ == "__main__":
os.environ["http_proxy"] = "" os.environ["http_proxy"] = ""
os.environ["https_proxy"] = "" os.environ["https_proxy"] = ""
from zerver.lib.test_fixtures import is_template_database_current from zerver.lib.test_fixtures import template_database_status
from tools.lib.test_script import ( from tools.lib.test_script import (
get_provisioning_status, get_provisioning_status,
@@ -344,7 +344,8 @@ if __name__ == "__main__":
# files, since part of setup is importing the models for all applications in INSTALLED_APPS. # files, since part of setup is importing the models for all applications in INSTALLED_APPS.
django.setup() django.setup()
if options.generate_fixtures or not is_template_database_current(): test_template_db_status = template_database_status()
if options.generate_fixtures or test_template_db_status == 'needs_rebuild':
generate_fixtures_command = [os.path.join(TOOLS_DIR, 'setup', 'generate-fixtures')] generate_fixtures_command = [os.path.join(TOOLS_DIR, 'setup', 'generate-fixtures')]
generate_fixtures_command.append('--force') generate_fixtures_command.append('--force')
subprocess.call(generate_fixtures_command) subprocess.call(generate_fixtures_command)

View File

@@ -107,14 +107,15 @@ def check_setting_hash(setting_name: str, status_dir: str) -> bool:
return _check_hash(source_hash_file, target_content) return _check_hash(source_hash_file, target_content)
def is_template_database_current( def template_database_status(
database_name: str='zulip_test_template', database_name: str='zulip_test_template',
migration_status: Optional[str]=None, migration_status: Optional[str]=None,
settings: str='zproject.test_settings', settings: str='zproject.test_settings',
status_dir: Optional[str]=None, status_dir: Optional[str]=None,
check_files: Optional[List[str]]=None, check_files: Optional[List[str]]=None,
check_settings: Optional[List[str]]=None) -> bool: check_settings: Optional[List[str]]=None) -> str:
# Using str type for check_files because re.split doesn't accept unicode # This function returns a status string specifying the type of
# state the template db is in and thus the kind of action required.
if check_files is None: if check_files is None:
check_files = [ check_files = [
'zilencer/management/commands/populate_db.py', 'zilencer/management/commands/populate_db.py',
@@ -144,6 +145,7 @@ def is_template_database_current(
for setting_name in check_settings]) for setting_name in check_settings])
hash_status = files_hash_status and settings_hash_status hash_status = files_hash_status and settings_hash_status
return are_migrations_the_same(migration_status, settings=settings) and hash_status if are_migrations_the_same(migration_status, settings=settings) and hash_status:
return 'current'
return False return 'needs_rebuild'