Skip to content

Commit 8f28d01

Browse files
committed
Fix: Support a proper migration of seed models
1 parent 50aee2c commit 8f28d01

File tree

3 files changed

+64
-3
lines changed

3 files changed

+64
-3
lines changed

sqlmesh/core/snapshot/definition.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1480,7 +1480,7 @@ def expiration_ts(self) -> int:
14801480
@property
14811481
def supports_schema_migration_in_prod(self) -> bool:
14821482
"""Returns whether or not this snapshot supports schema migration when deployed to production."""
1483-
return self.is_paused and self.is_model and not self.is_symbolic and not self.is_seed
1483+
return self.is_paused and self.is_model and not self.is_symbolic
14841484

14851485
@property
14861486
def requires_schema_migration_in_prod(self) -> bool:

sqlmesh/core/snapshot/evaluator.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1092,7 +1092,7 @@ def _migrate_snapshot(
10921092
**render_kwargs
10931093
)
10941094

1095-
if table_exists:
1095+
if table_exists and not snapshot.is_seed:
10961096
self._migrate_target_table(
10971097
target_table_name=target_table_name,
10981098
snapshot=snapshot,
@@ -1105,14 +1105,19 @@ def _migrate_snapshot(
11051105
run_pre_post_statements=True,
11061106
)
11071107
else:
1108+
if table_exists:
1109+
assert snapshot.is_seed
1110+
logger.info("Deleting the existing seed table '%s'", target_table_name)
1111+
adapter.drop_table(target_table_name)
1112+
11081113
self._execute_create(
11091114
snapshot=snapshot,
11101115
table_name=snapshot.table_name(is_deployable=True),
11111116
is_table_deployable=True,
11121117
deployability_index=deployability_index,
11131118
create_render_kwargs=render_kwargs,
11141119
rendered_physical_properties=rendered_physical_properties,
1115-
dry_run=True,
1120+
dry_run=not snapshot.is_seed,
11161121
)
11171122

11181123
# Retry in case when the table is migrated concurrently from another plan application

tests/core/test_integration.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3232,6 +3232,62 @@ def test_virtual_environment_mode_dev_only_model_change_standalone_audit(
32323232
context.apply(plan)
32333233

32343234

3235+
@time_machine.travel("2023-01-08 15:00:00 UTC")
3236+
def test_virtual_environment_mode_dev_only_seed_model_change_schema(
3237+
init_and_plan_context: t.Callable,
3238+
):
3239+
context, plan = init_and_plan_context(
3240+
"examples/sushi", config="test_config_virtual_environment_mode_dev_only"
3241+
)
3242+
context.apply(plan)
3243+
3244+
new_csv = []
3245+
with open(context.path / "seeds" / "waiter_names.csv", "r") as fd:
3246+
is_header = True
3247+
for idx, line in enumerate(fd):
3248+
line = line.strip()
3249+
if not line:
3250+
continue
3251+
if is_header:
3252+
new_csv.append(line + ",new_column")
3253+
is_header = False
3254+
else:
3255+
new_csv.append(line + f",v{idx}")
3256+
3257+
with open(context.path / "seeds" / "waiter_names.csv", "w") as fd:
3258+
fd.write("\n".join(new_csv))
3259+
3260+
context.load()
3261+
3262+
downstream_model = context.get_model("sushi.waiter_as_customer_by_day")
3263+
downstream_model_kind = downstream_model.kind.dict()
3264+
downstream_model_kwargs = {
3265+
**downstream_model.dict(),
3266+
"kind": {
3267+
**downstream_model_kind,
3268+
"on_destructive_change": "allow",
3269+
},
3270+
"audits": [],
3271+
# Use the new column
3272+
"query": "SELECT '2023-01-07' AS event_date, new_column AS new_column FROM sushi.waiter_names",
3273+
}
3274+
context.upsert_model(SqlModel.parse_obj(downstream_model_kwargs))
3275+
3276+
context.plan("dev", auto_apply=True, no_prompts=True, skip_tests=True, enable_preview=True)
3277+
3278+
assert (
3279+
context.engine_adapter.fetchone(
3280+
"SELECT COUNT(*) FROM sushi__dev.waiter_as_customer_by_day"
3281+
)[0]
3282+
== len(new_csv) - 1
3283+
)
3284+
3285+
# Deploy to prod
3286+
context.clear_caches()
3287+
context.plan("prod", auto_apply=True, no_prompts=True, skip_tests=True)
3288+
assert "new_column" in context.engine_adapter.columns("sushi.waiter_as_customer_by_day")
3289+
3290+
32353291
@time_machine.travel("2023-01-08 15:00:00 UTC")
32363292
def test_restatement_plan_ignores_changes(init_and_plan_context: t.Callable):
32373293
context, plan = init_and_plan_context("examples/sushi")

0 commit comments

Comments
 (0)