Skip to content

Commit 967461c

Browse files
committed
Fix: Table creation for forward-only snapshots of newly added models
1 parent ff753e5 commit 967461c

File tree

2 files changed

+7
-12
lines changed

2 files changed

+7
-12
lines changed

sqlmesh/core/snapshot/evaluator.py

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -407,22 +407,17 @@ def _create_snapshot(
407407
evaluation_strategy = _evaluation_strategy(snapshot, self.adapter)
408408

409409
with self.adapter.transaction(TransactionType.DDL), self.adapter.session():
410+
self.adapter.execute(snapshot.model.render_pre_statements(**render_kwargs))
411+
410412
if is_dev and not snapshot.previous_versions:
411413
# This is a FORWARD_ONLY snapshot which represents an added model. This means
412414
# that the physical table associated with it doesn't exist yet.
413415
logger.info(
414416
f"Detected a forward-only snapshot without previous versions: {snapshot.snapshot_id}"
415417
)
416-
non_dev_render_kwargs: t.Dict[str, t.Any] = {**render_kwargs, "is_dev": False}
417-
self.adapter.execute(snapshot.model.render_pre_statements(**non_dev_render_kwargs))
418-
evaluation_strategy.create(
419-
snapshot.model, snapshot.table_name(), **non_dev_render_kwargs
420-
)
421-
self.adapter.execute(snapshot.model.render_post_statements(**non_dev_render_kwargs))
422-
423-
self.adapter.execute(snapshot.model.render_pre_statements(**render_kwargs))
424-
425-
if is_dev and snapshot.is_materialized and self.adapter.SUPPORTS_CLONING:
418+
evaluation_strategy.create(snapshot.model, target_table_name, **render_kwargs)
419+
evaluation_strategy.create(snapshot.model, snapshot.table_name(), **render_kwargs)
420+
elif is_dev and snapshot.is_materialized and self.adapter.SUPPORTS_CLONING:
426421
tmp_table_name = f"{target_table_name}__schema_migration_source"
427422
source_table_name = snapshot.table_name()
428423

tests/core/test_snapshot_evaluator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -790,11 +790,11 @@ def test_forward_only_snapshot_for_added_model(mocker: MockerFixture, adapter_mo
790790
adapter_mock.create_table.assert_has_calls(
791791
[
792792
call(
793-
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}",
793+
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}__temp",
794794
**common_create_args,
795795
),
796796
call(
797-
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}__temp",
797+
f"sqlmesh__test_schema.test_schema__test_model__{snapshot.version}",
798798
**common_create_args,
799799
),
800800
]

0 commit comments

Comments
 (0)