Skip to content

Commit 95dfc02

Browse files
authored
Fix: Make sure that we don't attempt to create two seed records with the same version in state (#2501)
1 parent 4ebe44d commit 95dfc02

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

sqlmesh/core/state_sync/engine_adapter.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
Intervals,
4040
Node,
4141
Snapshot,
42+
SnapshotChangeCategory,
4243
SnapshotFingerprint,
4344
SnapshotId,
4445
SnapshotIdLike,
@@ -215,7 +216,10 @@ def _push_snapshots(self, snapshots: t.Iterable[Snapshot], overwrite: bool = Fal
215216
snapshots_to_store = []
216217

217218
for snapshot in snapshots:
218-
if isinstance(snapshot.node, SeedModel):
219+
if isinstance(snapshot.node, SeedModel) and snapshot.change_category in (
220+
SnapshotChangeCategory.BREAKING,
221+
SnapshotChangeCategory.NON_BREAKING,
222+
):
219223
seed_model = t.cast(SeedModel, snapshot.node)
220224
seed_contents.append(
221225
{

tests/core/test_state_sync.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2225,3 +2225,40 @@ def test_snapshot_batching(state_sync, mocker, make_snapshot):
22252225
assert len(snapshots) == 3
22262226
calls = mock.fetchall.call_args_list
22272227
assert len(calls) == 2
2228+
2229+
2230+
def test_seed_model_metadata_update(
2231+
state_sync: EngineAdapterStateSync,
2232+
make_snapshot: t.Callable,
2233+
):
2234+
model = SeedModel(
2235+
name="a",
2236+
kind=SeedKind(path="./path/to/seed"),
2237+
seed=Seed(content="header\n1\n2"),
2238+
column_hashes={"header": "hash"},
2239+
depends_on=set(),
2240+
)
2241+
snapshot = make_snapshot(model)
2242+
snapshot.categorize_as(SnapshotChangeCategory.BREAKING)
2243+
2244+
state_sync.push_snapshots([snapshot])
2245+
2246+
seed_query = (
2247+
exp.select("COUNT(*)")
2248+
.from_("sqlmesh._seeds")
2249+
.where(exp.column("version").eq(snapshot.version))
2250+
)
2251+
2252+
assert state_sync.engine_adapter.fetchone(seed_query)[0] == 1
2253+
2254+
model = model.copy(update={"owner": "jen"})
2255+
new_snapshot = make_snapshot(model)
2256+
new_snapshot.previous_versions = snapshot.all_versions
2257+
new_snapshot.categorize_as(SnapshotChangeCategory.FORWARD_ONLY)
2258+
2259+
assert snapshot.fingerprint != new_snapshot.fingerprint
2260+
assert snapshot.version == new_snapshot.version
2261+
2262+
state_sync.push_snapshots([new_snapshot])
2263+
assert state_sync.engine_adapter.fetchone(seed_query)[0] == 1
2264+
assert len(state_sync.get_snapshots([new_snapshot, snapshot])) == 2

0 commit comments

Comments
 (0)