|
87 | 87 | NotificationTarget, |
88 | 88 | NotificationTargetManager, |
89 | 89 | ) |
90 | | -from sqlmesh.core.plan import Plan, PlanBuilder |
| 90 | +from sqlmesh.core.plan import Plan, PlanBuilder, SnapshotIntervals |
91 | 91 | from sqlmesh.core.reference import ReferenceGraph |
92 | 92 | from sqlmesh.core.scheduler import Scheduler, CompletionStatus |
93 | 93 | from sqlmesh.core.schema_loader import create_external_models_file |
|
97 | 97 | Snapshot, |
98 | 98 | SnapshotEvaluator, |
99 | 99 | SnapshotFingerprint, |
| 100 | + missing_intervals, |
100 | 101 | to_table_mapping, |
101 | 102 | ) |
102 | 103 | from sqlmesh.core.snapshot.definition import get_next_model_interval_start |
@@ -469,11 +470,12 @@ def execution_context( |
469 | 470 | self, |
470 | 471 | deployability_index: t.Optional[DeployabilityIndex] = None, |
471 | 472 | engine_adapter: t.Optional[EngineAdapter] = None, |
| 473 | + snapshots: t.Optional[t.Dict[str, Snapshot]] = None, |
472 | 474 | ) -> ExecutionContext: |
473 | 475 | """Returns an execution context.""" |
474 | 476 | return ExecutionContext( |
475 | 477 | engine_adapter=engine_adapter or self.engine_adapter, |
476 | | - snapshots=self.snapshots, |
| 478 | + snapshots=snapshots or self.snapshots, |
477 | 479 | deployability_index=deployability_index, |
478 | 480 | default_dialect=self.default_dialect, |
479 | 481 | default_catalog=self.default_catalog, |
@@ -1893,6 +1895,61 @@ def rewrite(self, sql: str, dialect: str = "") -> exp.Expression: |
1893 | 1895 | dialect=dialect or self.default_dialect, |
1894 | 1896 | ) |
1895 | 1897 |
|
| 1898 | + @python_api_analytics |
| 1899 | + def check_intervals( |
| 1900 | + self, |
| 1901 | + environment: t.Optional[str], |
| 1902 | + no_signals: bool, |
| 1903 | + select_models: t.Collection[str], |
| 1904 | + start: t.Optional[TimeLike] = None, |
| 1905 | + end: t.Optional[TimeLike] = None, |
| 1906 | + ) -> t.Dict[Snapshot, SnapshotIntervals]: |
| 1907 | + """Check intervals for a given environment. |
| 1908 | +
|
| 1909 | + Args: |
| 1910 | + environment: The environment or prod if None. |
| 1911 | + select_models: A list of model selection strings to show intervals for. |
| 1912 | + start: The start of the intervals to check. |
| 1913 | + end: The end of the intervals to check. |
| 1914 | + """ |
| 1915 | + |
| 1916 | + environment = environment or c.PROD |
| 1917 | + env = self.state_reader.get_environment(environment) |
| 1918 | + if not env: |
| 1919 | + raise SQLMeshError(f"Environment '{environment}' was not found.") |
| 1920 | + |
| 1921 | + snapshots = {k.name: v for k, v in self.state_sync.get_snapshots(env.snapshots).items()} |
| 1922 | + |
| 1923 | + missing = { |
| 1924 | + k.name: v |
| 1925 | + for k, v in missing_intervals( |
| 1926 | + snapshots.values(), start=start, end=end, execution_time=end |
| 1927 | + ).items() |
| 1928 | + } |
| 1929 | + |
| 1930 | + if select_models: |
| 1931 | + selected: t.Collection[str] = self._select_models_for_run( |
| 1932 | + select_models, True, snapshots.values() |
| 1933 | + ) |
| 1934 | + else: |
| 1935 | + selected = snapshots.keys() |
| 1936 | + |
| 1937 | + results = {} |
| 1938 | + execution_context = self.execution_context(snapshots=snapshots) |
| 1939 | + |
| 1940 | + for fqn in selected: |
| 1941 | + snapshot = snapshots[fqn] |
| 1942 | + intervals = missing.get(fqn) or [] |
| 1943 | + |
| 1944 | + results[snapshot] = SnapshotIntervals( |
| 1945 | + snapshot.snapshot_id, |
| 1946 | + intervals |
| 1947 | + if no_signals |
| 1948 | + else snapshot.check_ready_intervals(intervals, execution_context), |
| 1949 | + ) |
| 1950 | + |
| 1951 | + return results |
| 1952 | + |
1896 | 1953 | @python_api_analytics |
1897 | 1954 | def migrate(self) -> None: |
1898 | 1955 | """Migrates SQLMesh to the current running version. |
|
0 commit comments