|
9 | 9 | from sqlmesh.core.audit import StandaloneAudit |
10 | 10 | from sqlmesh.core.dialect import to_schema |
11 | 11 | from sqlmesh.core.engine_adapter import EngineAdapter, create_engine_adapter |
12 | | -from sqlmesh.core.engine_adapter.base import InsertOverwriteStrategy |
| 12 | +from sqlmesh.core.engine_adapter.base import ( |
| 13 | + MERGE_SOURCE_ALIAS, |
| 14 | + MERGE_TARGET_ALIAS, |
| 15 | + InsertOverwriteStrategy, |
| 16 | +) |
13 | 17 | from sqlmesh.core.environment import EnvironmentNamingInfo |
14 | 18 | from sqlmesh.core.macros import RuntimeStage, macro |
15 | 19 | from sqlmesh.core.model import ( |
@@ -1009,6 +1013,64 @@ def test_insert_into_scd_type_2(adapter_mock, make_snapshot): |
1009 | 1013 | ) |
1010 | 1014 |
|
1011 | 1015 |
|
| 1016 | +def test_create_incremental_by_unique_key_updated_at_exp(adapter_mock, make_snapshot): |
| 1017 | + evaluator = SnapshotEvaluator(adapter_mock) |
| 1018 | + model = load_sql_based_model( |
| 1019 | + parse( # type: ignore |
| 1020 | + """ |
| 1021 | + MODEL ( |
| 1022 | + name test_schema.test_model, |
| 1023 | + kind INCREMENTAL_BY_UNIQUE_KEY ( |
| 1024 | + unique_key [id], |
| 1025 | + when_matched WHEN MATCHED THEN UPDATE SET target.name = source.name, target.updated_at = COALESCE(source.updated_at, target.updated_at) |
| 1026 | + ) |
| 1027 | + ); |
| 1028 | +
|
| 1029 | + SELECT id::int, name::string, updated_at::timestamp FROM tbl; |
| 1030 | + """ |
| 1031 | + ) |
| 1032 | + ) |
| 1033 | + |
| 1034 | + snapshot = make_snapshot(model) |
| 1035 | + snapshot.categorize_as(SnapshotChangeCategory.BREAKING) |
| 1036 | + |
| 1037 | + evaluator.evaluate( |
| 1038 | + snapshot, |
| 1039 | + "2020-01-01", |
| 1040 | + "2020-01-02", |
| 1041 | + "2020-01-02", |
| 1042 | + snapshots={}, |
| 1043 | + ) |
| 1044 | + |
| 1045 | + adapter_mock.merge.assert_called_once_with( |
| 1046 | + snapshot.table_name(), |
| 1047 | + model.render_query(), |
| 1048 | + columns_to_types={ |
| 1049 | + "id": exp.DataType.build("INT"), |
| 1050 | + "name": exp.DataType.build("STRING"), |
| 1051 | + "updated_at": exp.DataType.build("TIMESTAMP"), |
| 1052 | + }, |
| 1053 | + unique_key=[exp.to_column("id")], |
| 1054 | + when_matched=exp.When( |
| 1055 | + matched=True, |
| 1056 | + source=False, |
| 1057 | + then=exp.Update( |
| 1058 | + expressions=[ |
| 1059 | + exp.column("name", MERGE_TARGET_ALIAS).eq( |
| 1060 | + exp.column("name", MERGE_SOURCE_ALIAS) |
| 1061 | + ), |
| 1062 | + exp.column("updated_at", MERGE_TARGET_ALIAS).eq( |
| 1063 | + exp.Coalesce( |
| 1064 | + this=exp.column("updated_at", MERGE_SOURCE_ALIAS), |
| 1065 | + expressions=[exp.column("updated_at", MERGE_TARGET_ALIAS)], |
| 1066 | + ) |
| 1067 | + ), |
| 1068 | + ], |
| 1069 | + ), |
| 1070 | + ), |
| 1071 | + ) |
| 1072 | + |
| 1073 | + |
1012 | 1074 | def test_standalone_audit(mocker: MockerFixture, adapter_mock, make_snapshot): |
1013 | 1075 | evaluator = SnapshotEvaluator(adapter_mock) |
1014 | 1076 |
|
|
0 commit comments