Skip to content

Commit 0addcac

Browse files
committed
Refactor: move pre_check into the migration script
1 parent 6c06470 commit 0addcac

File tree

3 files changed

+16
-23
lines changed

3 files changed

+16
-23
lines changed

sqlmesh/core/state_sync/base.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from sqlglot import __version__ as SQLGLOT_VERSION
1010

11-
from sqlmesh import migrations, pre_checks
11+
from sqlmesh import migrations
1212
from sqlmesh.core.environment import (
1313
Environment,
1414
EnvironmentNamingInfo,
@@ -64,11 +64,10 @@ def _schema_version_validator(cls, v: t.Any) -> int:
6464
importlib.import_module(f"sqlmesh.migrations.{migration}")
6565
for migration in sorted(info.name for info in pkgutil.iter_modules(migrations.__path__))
6666
]
67-
PRE_CHECKS = {
68-
pre_check: importlib.import_module(f"sqlmesh.pre_checks.{pre_check}")
69-
for pre_check in sorted(info.name for info in pkgutil.iter_modules(pre_checks.__path__))
70-
}
7167
SCHEMA_VERSION: int = len(MIGRATIONS)
68+
PRE_CHECK_VERSION: int = (
69+
max(idx for idx, migration in enumerate(MIGRATIONS) if hasattr(migration, "pre_check")) + 1
70+
)
7271

7372

7473
class PromotionResult(PydanticModel):

sqlmesh/core/state_sync/db/migrator.py

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,7 @@
2525
from sqlmesh.core.snapshot.definition import (
2626
_parents_from_node,
2727
)
28-
from sqlmesh.core.state_sync.base import (
29-
MIGRATIONS,
30-
PRE_CHECKS,
31-
)
28+
from sqlmesh.core.state_sync.base import MIGRATIONS
3229
from sqlmesh.core.state_sync.base import StateSync
3330
from sqlmesh.core.state_sync.db.environment import EnvironmentState
3431
from sqlmesh.core.state_sync.db.interval import IntervalState
@@ -102,7 +99,7 @@ def migrate(
10299
promoted_snapshots_only: Whether to migrate only promoted snapshots.
103100
pre_check_only: If True, only run pre-checks without performing migration.
104101
"""
105-
pre_check_warnings = self.run_pre_checks(state_sync)
102+
pre_check_warnings = self._run_pre_checks(state_sync)
106103
should_migrate = self.console.log_pre_check_warnings(pre_check_warnings, pre_check_only)
107104
if not should_migrate:
108105
return
@@ -168,30 +165,27 @@ def rollback(self) -> None:
168165

169166
logger.info("Migration rollback successful.")
170167

171-
def run_pre_checks(self, state_sync: StateSync) -> t.List[t.Tuple[str, t.List[str]]]:
168+
def _run_pre_checks(self, state_sync: StateSync) -> t.List[t.Tuple[str, t.List[str]]]:
172169
"""Run pre-checks for migrations between specified versions.
173170
174171
Args:
175172
state_sync: The state sync instance.
176173
177174
Returns:
178-
A list of pairs comprising the executed pre-checks and the corresponding warnings.
175+
A list of pairs comprising the migration name containing the executed pre-checks
176+
and the corresponding warnings.
179177
"""
180-
# Get the range of the migrations that would be applied
181-
from_version = self.version_state.get_versions().schema_version
182-
to_version = len(MIGRATIONS)
178+
versions = self.version_state.get_versions()
179+
migrations = MIGRATIONS[versions.schema_version :]
183180

184181
pre_check_warnings = []
185-
for i in range(from_version, to_version):
186-
# Assumption: pre-check and migration names match
187-
pre_check_name = MIGRATIONS[i].__name__.split(".")[-1]
188-
pre_check_module = PRE_CHECKS.get(pre_check_name)
189-
190-
if callable(pre_check := getattr(pre_check_module, "pre_check", None)):
191-
logger.info(f"Running pre-check for {pre_check_name}")
182+
for migration in migrations:
183+
if callable(pre_check := getattr(migration, "pre_check", None)):
184+
migration_name = migration.__name__.split(".")[-1]
185+
logger.info(f"Running pre-check for {migration_name}")
192186
warnings = pre_check(state_sync)
193187
if warnings:
194-
pre_check_warnings.append((pre_check_name, warnings))
188+
pre_check_warnings.append((migration_name, warnings))
195189

196190
return pre_check_warnings
197191

sqlmesh/pre_checks/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)