|
7 | 7 | import time_machine |
8 | 8 | from sqlglot import exp |
9 | 9 | from IPython.utils.capture import capture_output |
| 10 | +from pytest_mock.plugin import MockerFixture |
10 | 11 |
|
11 | 12 | from sqlmesh.core.config import ( |
12 | 13 | Config, |
13 | 14 | ModelDefaultsConfig, |
14 | 15 | ) |
15 | 16 | from sqlmesh.core.context import Context |
| 17 | +from sqlmesh.core.snapshot import SnapshotEvaluator |
16 | 18 | from sqlmesh.utils.errors import ( |
17 | 19 | PlanError, |
18 | 20 | ) |
@@ -346,3 +348,171 @@ def test_default_audits_with_custom_audit_definitions(tmp_path: Path): |
346 | 348 | if audit_name == "positive_amount": |
347 | 349 | assert "column" in audit_args |
348 | 350 | assert audit_args["column"].name == "amount" |
| 351 | + |
| 352 | + |
| 353 | +@pytest.mark.parametrize("forward_only", [False, True]) |
| 354 | +def test_audit_only_change_reads_parent_tables_of_dev_environment( |
| 355 | + tmp_path: Path, mocker: MockerFixture, forward_only: bool |
| 356 | +): |
| 357 | + def write(path: str, contents: str) -> None: |
| 358 | + create_temp_file(tmp_path, Path(path), contents) |
| 359 | + |
| 360 | + def write_parent(tag: str) -> None: |
| 361 | + kind = "INCREMENTAL_BY_UNIQUE_KEY (unique_key id)" |
| 362 | + write("models/p.sql", f"MODEL (name db.p, kind {kind}); SELECT 1 AS id, '{tag}' AS tag") |
| 363 | + |
| 364 | + write_parent("p0") |
| 365 | + write("models/c.sql", "MODEL (name db.c, kind FULL); SELECT id FROM db.p") |
| 366 | + context = Context( |
| 367 | + paths=tmp_path, config=Config(model_defaults=ModelDefaultsConfig(dialect="duckdb")) |
| 368 | + ) |
| 369 | + context.plan("prod", no_prompts=True, auto_apply=True) |
| 370 | + |
| 371 | + write_parent("p1") |
| 372 | + context.load() |
| 373 | + context.plan("dev", no_prompts=True, auto_apply=True, forward_only=forward_only) |
| 374 | + |
| 375 | + # An audit-only change to the child, whose audit reads the parent |
| 376 | + write( |
| 377 | + "audits/reads_parent.sql", "AUDIT (name reads_parent); SELECT * FROM db.p WHERE tag <> 'p1'" |
| 378 | + ) |
| 379 | + write( |
| 380 | + "models/c.sql", "MODEL (name db.c, kind FULL, audits (reads_parent)); SELECT id FROM db.p" |
| 381 | + ) |
| 382 | + context.load() |
| 383 | + plan = context.plan_builder("dev").build() |
| 384 | + assert [s.name for s in plan.new_snapshots] == ['"memory"."db"."c"'] |
| 385 | + assert plan.new_snapshots[0].is_metadata |
| 386 | + |
| 387 | + audit_spy = mocker.spy(SnapshotEvaluator, "audit") |
| 388 | + evaluate_spy = mocker.spy(SnapshotEvaluator, "evaluate") |
| 389 | + context.apply(plan) |
| 390 | + |
| 391 | + # Only the child is audited, and it reads the parent's dev snapshot rather than the prod view |
| 392 | + assert [call.kwargs["snapshot"].name for call in audit_spy.call_args_list] == [ |
| 393 | + '"memory"."db"."c"' |
| 394 | + ] |
| 395 | + assert not evaluate_spy.call_args_list |
| 396 | + assert not any(result.count for result in audit_spy.spy_return) |
| 397 | + dev_p = context.get_snapshot("db.p", raise_if_missing=True) |
| 398 | + audit_kwargs = audit_spy.call_args.kwargs |
| 399 | + assert audit_kwargs["snapshots"]['"memory"."db"."p"'].snapshot_id == dev_p.snapshot_id |
| 400 | + assert audit_kwargs["deployability_index"].is_deployable(dev_p) is not forward_only |
| 401 | + |
| 402 | + |
| 403 | +def test_audit_only_change_with_non_parent_changed_in_same_plan( |
| 404 | + tmp_path: Path, mocker: MockerFixture |
| 405 | +): |
| 406 | + def write(path: str, contents: str) -> None: |
| 407 | + create_temp_file(tmp_path, Path(path), contents) |
| 408 | + |
| 409 | + write("models/p.sql", "MODEL (name db.p, kind FULL); SELECT 1 AS id") |
| 410 | + write("models/s.sql", "MODEL (name db.s, kind FULL); SELECT 1 AS id, 's0' AS tag") |
| 411 | + write("models/c.sql", "MODEL (name db.c, kind FULL); SELECT id FROM db.p") |
| 412 | + context = Context( |
| 413 | + paths=tmp_path, config=Config(model_defaults=ModelDefaultsConfig(dialect="duckdb")) |
| 414 | + ) |
| 415 | + context.plan("prod", no_prompts=True, auto_apply=True) |
| 416 | + |
| 417 | + # The audit reads s, which isn't a parent of c, so s's new table doesn't exist until the backfill |
| 418 | + write("models/s.sql", "MODEL (name db.s, kind FULL); SELECT 1 AS id, 's1' AS tag") |
| 419 | + write("audits/reads_s.sql", "AUDIT (name reads_s); SELECT * FROM db.s WHERE id IS NULL") |
| 420 | + write("models/c.sql", "MODEL (name db.c, kind FULL, audits (reads_s)); SELECT id FROM db.p") |
| 421 | + context.load() |
| 422 | + plan = context.plan_builder("dev").build() |
| 423 | + assert {s.name: s.is_metadata for s in plan.new_snapshots}['"memory"."db"."c"'] |
| 424 | + |
| 425 | + audit_spy = mocker.spy(SnapshotEvaluator, "audit") |
| 426 | + context.apply(plan) |
| 427 | + |
| 428 | + # c is audited once and its audit finds nothing. spy_return would be s's, from the backfill |
| 429 | + c_results = [ |
| 430 | + results |
| 431 | + for call, results in zip(audit_spy.call_args_list, audit_spy.spy_return_list) |
| 432 | + if call.kwargs["snapshot"].name == '"memory"."db"."c"' |
| 433 | + ] |
| 434 | + assert [[result.count for result in results] for results in c_results] == [[0]] |
| 435 | + |
| 436 | + |
| 437 | +@pytest.mark.parametrize("variant", ["new_parent", "changed_parent"]) |
| 438 | +def test_audit_only_change_after_failed_prod_plan(tmp_path: Path, variant: str): |
| 439 | + def write(path: str, contents: str) -> None: |
| 440 | + create_temp_file(tmp_path, Path(path), contents) |
| 441 | + |
| 442 | + def fo(tag: str) -> None: |
| 443 | + write( |
| 444 | + "models/fo.sql", |
| 445 | + "MODEL (name db.fo, kind INCREMENTAL_BY_UNIQUE_KEY (unique_key id, forward_only true));" |
| 446 | + f" SELECT 1 AS id, '{tag}' AS tag", |
| 447 | + ) |
| 448 | + |
| 449 | + config = Config(model_defaults=ModelDefaultsConfig(dialect="duckdb", start="2024-01-01")) |
| 450 | + write("audits/always_fails.sql", "AUDIT (name always_fails); SELECT 1 AS x") |
| 451 | + fo("v0") |
| 452 | + write("models/p.sql", "MODEL (name db.p, kind FULL); SELECT id, 'p0' AS tag FROM db.fo") |
| 453 | + write("models/c.sql", "MODEL (name db.c, kind FULL); SELECT id FROM db.p") |
| 454 | + context = Context(paths=tmp_path, config=config) |
| 455 | + context.plan("prod", no_prompts=True, auto_apply=True) |
| 456 | + |
| 457 | + # The forward-only change makes the backfill run after the environment record is updated, and |
| 458 | + # the failing audit stops the plan before the virtual layer is updated |
| 459 | + fo("v1") |
| 460 | + parent, new_tag = ("q", "q1") if variant == "new_parent" else ("p", "p1") |
| 461 | + write( |
| 462 | + f"models/{parent}.sql", |
| 463 | + f"MODEL (name db.{parent}, kind FULL); SELECT id, '{new_tag}' AS tag FROM db.fo", |
| 464 | + ) |
| 465 | + write( |
| 466 | + "models/c.sql", |
| 467 | + f"MODEL (name db.c, kind FULL, audits (always_fails)); SELECT id FROM db.{parent}", |
| 468 | + ) |
| 469 | + context.load() |
| 470 | + with pytest.raises(PlanError): |
| 471 | + context.plan("prod", no_prompts=True, auto_apply=True) |
| 472 | + |
| 473 | + # The audit must read the parent version named by the environment record |
| 474 | + write( |
| 475 | + "audits/reads_parent.sql", |
| 476 | + f"AUDIT (name reads_parent); SELECT * FROM db.{parent} WHERE tag <> '{new_tag}'", |
| 477 | + ) |
| 478 | + write( |
| 479 | + "models/c.sql", |
| 480 | + f"MODEL (name db.c, kind FULL, audits (reads_parent)); SELECT id FROM db.{parent}", |
| 481 | + ) |
| 482 | + context.load() |
| 483 | + context.plan("prod", no_prompts=True, auto_apply=True) |
| 484 | + |
| 485 | + assert context.engine_adapter.fetchall(f"SELECT tag FROM db.{parent}") == [(new_tag,)] |
| 486 | + |
| 487 | + |
| 488 | +def test_audit_only_change_keeps_intervals_of_snapshots_backfilled_in_same_plan(tmp_path: Path): |
| 489 | + def write_models(start: str, audits: str = "") -> None: |
| 490 | + # Both models replace their whole table when they think it's their first insert |
| 491 | + kind = f"INCREMENTAL_BY_UNIQUE_KEY (unique_key ds), start '{start}', cron '@daily'" |
| 492 | + create_temp_file( |
| 493 | + tmp_path, |
| 494 | + Path("models/p.sql"), |
| 495 | + f"MODEL (name db.p, kind {kind}); SELECT @start_ds AS ds", |
| 496 | + ) |
| 497 | + create_temp_file( |
| 498 | + tmp_path, |
| 499 | + Path("models/c.sql"), |
| 500 | + f"MODEL (name db.c, kind {kind}{audits}); SELECT DISTINCT @start_ds AS ds FROM db.p", |
| 501 | + ) |
| 502 | + |
| 503 | + write_models(start="2023-01-02") |
| 504 | + context = Context( |
| 505 | + paths=tmp_path, config=Config(model_defaults=ModelDefaultsConfig(dialect="duckdb")) |
| 506 | + ) |
| 507 | + with time_machine.travel("2023-01-03 00:00:00 UTC"): |
| 508 | + context.plan("prod", no_prompts=True, auto_apply=True) |
| 509 | + |
| 510 | + # Moving the start back is a metadata change that backfills the earlier day in the same |
| 511 | + # plan that audits the child |
| 512 | + write_models(start="2023-01-01", audits=", audits (not_null(columns := (ds)))") |
| 513 | + context.load() |
| 514 | + context.plan("prod", no_prompts=True, auto_apply=True) |
| 515 | + |
| 516 | + expected = [("2023-01-01",), ("2023-01-02",)] |
| 517 | + assert sorted(context.engine_adapter.fetchall("SELECT ds FROM db.p")) == expected |
| 518 | + assert sorted(context.engine_adapter.fetchall("SELECT ds FROM db.c")) == expected |
0 commit comments