|
27 | 27 | ) |
28 | 28 | from sqlmesh.core.state_sync.base import ( |
29 | 29 | MIGRATIONS, |
| 30 | + PRE_CHECKS, |
30 | 31 | ) |
31 | 32 | from sqlmesh.core.state_sync.base import StateSync |
32 | 33 | from sqlmesh.core.state_sync.db.environment import EnvironmentState |
@@ -90,8 +91,22 @@ def migrate( |
90 | 91 | default_catalog: t.Optional[str], |
91 | 92 | skip_backup: bool = False, |
92 | 93 | promoted_snapshots_only: bool = True, |
| 94 | + pre_check_only: bool = False, |
93 | 95 | ) -> None: |
94 | | - """Migrate the state sync to the latest SQLMesh / SQLGlot version.""" |
| 96 | + """Migrate the state sync to the latest SQLMesh / SQLGlot version. |
| 97 | +
|
| 98 | + Args: |
| 99 | + state_sync: The state sync instance. |
| 100 | + default_catalog: The default catalog. |
| 101 | + skip_backup: Whether to skip backing up state tables. |
| 102 | + promoted_snapshots_only: Whether to migrate only promoted snapshots. |
| 103 | + pre_check_only: If True, only run pre-checks without performing migration. |
| 104 | + """ |
| 105 | + pre_check_warnings = self.run_pre_checks(state_sync) |
| 106 | + should_migrate = self.console.log_pre_check_warnings(pre_check_warnings, pre_check_only) |
| 107 | + if not should_migrate: |
| 108 | + return |
| 109 | + |
95 | 110 | versions = self.version_state.get_versions() |
96 | 111 | migration_start_ts = time.perf_counter() |
97 | 112 |
|
@@ -153,6 +168,33 @@ def rollback(self) -> None: |
153 | 168 |
|
154 | 169 | logger.info("Migration rollback successful.") |
155 | 170 |
|
| 171 | + def run_pre_checks(self, state_sync: StateSync) -> t.List[t.Tuple[str, t.List[str]]]: |
| 172 | + """Run pre-checks for migrations between specified versions. |
| 173 | +
|
| 174 | + Args: |
| 175 | + state_sync: The state sync instance. |
| 176 | +
|
| 177 | + Returns: |
| 178 | + A list of pairs comprising the executed pre-checks and the corresponding warnings. |
| 179 | + """ |
| 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) |
| 183 | + |
| 184 | + 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}") |
| 192 | + warnings = pre_check(state_sync) |
| 193 | + if warnings: |
| 194 | + pre_check_warnings.append((pre_check_name, warnings)) |
| 195 | + |
| 196 | + return pre_check_warnings |
| 197 | + |
156 | 198 | def _apply_migrations( |
157 | 199 | self, |
158 | 200 | state_sync: StateSync, |
|
0 commit comments