mirror of
https://github.com/zulip/zulip.git
synced 2025-10-23 04:52:12 +00:00
migration_status: Move get_migration_status
to a new file.
This moves `get_migration_status` to its own file in zerver/lib/migration_status.py. This is a prep work to refactor the check migration function of import/export later on. Some of the imports are moved into `get_migration_status` because we're planning to share this file with `check-database-compatibility` which is also called when one does `production-upgrade`, so we'd want to avoid doing file-wide import on certain types of modules because it will fail under that scenario. In `test_fixtures.py`, `get_migration_status` is imported within `Database.what_to_do_with_migrations` so that it is called after `cov.start()` in `test-backend`. This is to avoid wierd interaction with coverage, see more details in #33063. Fixes #33063.
This commit is contained in:
41
zerver/lib/migration_status.py
Normal file
41
zerver/lib/migration_status.py
Normal file
@@ -0,0 +1,41 @@
|
||||
import os
|
||||
import re
|
||||
from importlib import import_module
|
||||
from io import StringIO
|
||||
from typing import Any
|
||||
|
||||
|
||||
def get_migration_status(**options: Any) -> str:
|
||||
from django.apps import apps
|
||||
from django.core.management import call_command
|
||||
from django.db import DEFAULT_DB_ALIAS, connections
|
||||
from django.utils.module_loading import module_has_submodule
|
||||
|
||||
verbosity = options.get("verbosity", 1)
|
||||
|
||||
for app_config in apps.get_app_configs():
|
||||
if module_has_submodule(app_config.module, "management"):
|
||||
import_module(".management", app_config.name)
|
||||
|
||||
app_label = options["app_label"] if options.get("app_label") else None
|
||||
db = options.get("database", DEFAULT_DB_ALIAS)
|
||||
out = StringIO()
|
||||
command_args = ["--list"]
|
||||
if app_label:
|
||||
command_args.append(app_label)
|
||||
|
||||
call_command(
|
||||
"showmigrations",
|
||||
*command_args,
|
||||
database=db,
|
||||
no_color=options.get("no_color", False),
|
||||
settings=options.get("settings", os.environ["DJANGO_SETTINGS_MODULE"]),
|
||||
stdout=out,
|
||||
skip_checks=options.get("skip_checks", True),
|
||||
traceback=options.get("traceback", True),
|
||||
verbosity=verbosity,
|
||||
)
|
||||
connections.close_all()
|
||||
out.seek(0)
|
||||
output = out.read()
|
||||
return re.sub(r"\x1b\[(1|0)m", "", output)
|
@@ -6,16 +6,10 @@ import shutil
|
||||
import subprocess
|
||||
import sys
|
||||
import time
|
||||
from importlib import import_module
|
||||
from io import StringIO
|
||||
from typing import Any
|
||||
|
||||
from django.apps import apps
|
||||
from django.conf import settings
|
||||
from django.core.management import call_command
|
||||
from django.db import DEFAULT_DB_ALIAS, ProgrammingError, connection, connections
|
||||
from django.db.utils import OperationalError
|
||||
from django.utils.module_loading import module_has_submodule
|
||||
|
||||
sys.path.append(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
|
||||
from scripts.lib.zulip_tools import (
|
||||
@@ -117,6 +111,8 @@ class Database:
|
||||
)
|
||||
|
||||
def what_to_do_with_migrations(self) -> str:
|
||||
from zerver.lib.migration_status import get_migration_status
|
||||
|
||||
status_fn = self.migration_status_path
|
||||
settings = self.settings
|
||||
|
||||
@@ -295,37 +291,6 @@ def update_test_databases_if_required(rebuild_test_database: bool = False) -> No
|
||||
run(["tools/setup/generate-fixtures"])
|
||||
|
||||
|
||||
def get_migration_status(**options: Any) -> str:
|
||||
verbosity = options.get("verbosity", 1)
|
||||
|
||||
for app_config in apps.get_app_configs():
|
||||
if module_has_submodule(app_config.module, "management"):
|
||||
import_module(".management", app_config.name)
|
||||
|
||||
app_label = options["app_label"] if options.get("app_label") else None
|
||||
db = options.get("database", DEFAULT_DB_ALIAS)
|
||||
out = StringIO()
|
||||
command_args = ["--list"]
|
||||
if app_label:
|
||||
command_args.append(app_label)
|
||||
|
||||
call_command(
|
||||
"showmigrations",
|
||||
*command_args,
|
||||
database=db,
|
||||
no_color=options.get("no_color", False),
|
||||
settings=options.get("settings", os.environ["DJANGO_SETTINGS_MODULE"]),
|
||||
stdout=out,
|
||||
skip_checks=options.get("skip_checks", True),
|
||||
traceback=options.get("traceback", True),
|
||||
verbosity=verbosity,
|
||||
)
|
||||
connections.close_all()
|
||||
out.seek(0)
|
||||
output = out.read()
|
||||
return re.sub(r"\x1b\[(1|0)m", "", output)
|
||||
|
||||
|
||||
def extract_migrations_as_list(migration_status: str) -> list[str]:
|
||||
MIGRATIONS_RE = re.compile(r"\[[X| ]\] (\d+_.+)\n")
|
||||
return MIGRATIONS_RE.findall(migration_status)
|
||||
|
@@ -7,7 +7,7 @@ from typing_extensions import override
|
||||
|
||||
from scripts.lib.zulip_tools import get_dev_uuid_var_path
|
||||
from zerver.lib.management import ZulipBaseCommand
|
||||
from zerver.lib.test_fixtures import get_migration_status
|
||||
from zerver.lib.migration_status import get_migration_status
|
||||
|
||||
|
||||
class Command(ZulipBaseCommand):
|
||||
|
Reference in New Issue
Block a user