Repository: TobikoData/sqlmesh
Pull request #2849 (2024-07-02 01:19:03)

Fix: Drop dev previews of managed tables correctly
In the managed strategy, the dev preview table is always a normal table, so drop it using a normal DROP TABLE command.


Patch:
diff --git a/sqlmesh/core/engine_adapter/bigquery.py b/sqlmesh/core/engine_adapter/bigquery.py
index 7c384d9b..9ef82274 100644
--- a/sqlmesh/core/engine_adapter/bigquery.py
+++ b/sqlmesh/core/engine_adapter/bigquery.py
@@ -65,9 +65,6 @@ class BigQueryEngineAdapter(InsertOverwriteWithMergeMixin):
                 exp.DataType.build("FLOAT64", dialect=DIALECT),
                 exp.DataType.build("BIGNUMERIC", dialect=DIALECT),
             },
-            exp.DataType.build("BIGNUMERIC", dialect=DIALECT): {
-                exp.DataType.build("FLOAT64", dialect=DIALECT),
-            },
             exp.DataType.build("DATE", dialect=DIALECT): {
                 exp.DataType.build("DATETIME", dialect=DIALECT),
             },
diff --git a/sqlmesh/core/snapshot/evaluator.py b/sqlmesh/core/snapshot/evaluator.py
index 17676139..c44349a5 100644
--- a/sqlmesh/core/snapshot/evaluator.py
+++ b/sqlmesh/core/snapshot/evaluator.py
@@ -792,19 +792,19 @@ class SnapshotEvaluator:
     ) -> None:
         snapshot = snapshot.table_info
 
-        table_names = [snapshot.table_name(is_deployable=False)]
+        table_names = [(False, snapshot.table_name(is_deployable=False))]
         if not dev_table_only:
-            table_names.append(snapshot.table_name(is_deployable=True))
+            table_names.append((True, snapshot.table_name(is_deployable=True)))
 
         evaluation_strategy = _evaluation_strategy(snapshot, self.adapter)
 
-        for table_name in table_names:
+        for is_table_deployable, table_name in table_names:
             table = exp.to_table(table_name)
             if table.db != snapshot.physical_schema:
                 raise SQLMeshError(
                     f"Table '{table_name}' is not a part of the physical schema '{snapshot.physical_schema}' and so can't be dropped."
                 )
-            evaluation_strategy.delete(table_name)
+            evaluation_strategy.delete(table_name, is_table_deployable=is_table_deployable)
 
             if on_complete is not None:
                 on_complete(table_name)
@@ -1776,8 +1776,13 @@ class EngineManagedStrategy(MaterializableStrategy):
         raise ConfigError(f"Cannot mutate managed table: {target_table_name}")
 
     def delete(self, name: str, **kwargs: t.Any) -> None:
-        self.adapter.drop_managed_table(name)
-        logger.info("Dropped managed table '%s'", name)
+        # a dev preview table is created as a normal table, so it needs to be dropped as a normal table
+        if kwargs["is_table_deployable"]:
+            self.adapter.drop_managed_table(name)
+            logger.info("Dropped managed table '%s'", name)
+        else:
+            self.adapter.drop_table(name)
+            logger.info("Dropped dev preview for managed table '%s'", name)
 
 
 def _intervals(snapshot: Snapshot, deployability_index: DeployabilityIndex) -> Intervals:


Test patch:
diff --git a/tests/core/test_snapshot_evaluator.py b/tests/core/test_snapshot_evaluator.py
index ea95c45e..3dd4b7f9 100644
--- a/tests/core/test_snapshot_evaluator.py
+++ b/tests/core/test_snapshot_evaluator.py
@@ -2365,3 +2365,41 @@ def test_evaluate_managed(adapter_mock, make_snapshot, mocker: MockerFixture):
         table_description=model.description,
         column_descriptions=model.column_descriptions,
     )
+
+
+def test_cleanup_managed(adapter_mock, make_snapshot, mocker: MockerFixture):
+    evaluator = SnapshotEvaluator(adapter_mock)
+
+    model = load_sql_based_model(
+        parse(  # type: ignore
+            """
+            MODEL (
+                name test_schema.test_model,
+                kind MANAGED,
+                physical_properties (
+                    warehouse = 'small',
+                    target_lag = '10 minutes'
+                ),
+                clustered_by a
+            );
+
+            select a, b from foo;
+            """
+        )
+    )
+
+    snapshot = make_snapshot(model)
+    snapshot.categorize_as(SnapshotChangeCategory.BREAKING)
+
+    adapter_mock.assert_not_called()
+
+    cleanup_task = SnapshotTableCleanupTask(snapshot=snapshot.table_info, dev_table_only=False)
+
+    evaluator.cleanup(target_snapshots=[cleanup_task])
+
+    adapter_mock.drop_table.assert_called_once_with(
+        "sqlmesh__test_schema.test_schema__test_model__1556851963__temp"
+    )
+    adapter_mock.drop_managed_table.assert_called_once_with(
+        "sqlmesh__test_schema.test_schema__test_model__1556851963"
+    )