Skip to content

Commit

Permalink
Make auto-assertions disabled if their associated table is disabled. (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
BenBirt authored Oct 22, 2020
1 parent 7a497c5 commit fa35462
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
21 changes: 17 additions & 4 deletions core/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,9 @@ export class Table {
private contextablePreOps: Array<Contextable<ITableContext, string | string[]>> = [];
private contextablePostOps: Array<Contextable<ITableContext, string | string[]>> = [];

private uniqueKeyAssertion: Assertion;
private rowConditionsAssertion: Assertion;

public config(config: ITableConfig) {
checkExcessProperties(
(e: Error) => this.session.compileError(e),
Expand Down Expand Up @@ -443,6 +446,8 @@ export class Table {

public disabled() {
this.proto.disabled = true;
this.uniqueKeyAssertion?.disabled();
this.rowConditionsAssertion?.disabled();
return this;
}

Expand Down Expand Up @@ -590,10 +595,14 @@ export class Table {
if (!!assertions.uniqueKey) {
const indexCols =
typeof assertions.uniqueKey === "string" ? [assertions.uniqueKey] : assertions.uniqueKey;
this.session.assert(
this.uniqueKeyAssertion = this.session.assert(
`${this.proto.target.schema}_${this.proto.target.name}_assertions_uniqueKey`,
ctx => this.session.adapter().indexAssertion(ctx.ref(this.proto.target), indexCols)
).proto.parentAction = this.proto.target;
);
this.uniqueKeyAssertion.proto.parentAction = this.proto.target;
if (this.proto.disabled) {
this.uniqueKeyAssertion.disabled();
}
}
const mergedRowConditions = assertions.rowConditions || [];
if (!!assertions.nonNull) {
Expand All @@ -602,13 +611,17 @@ export class Table {
nonNullCols.forEach(nonNullCol => mergedRowConditions.push(`${nonNullCol} IS NOT NULL`));
}
if (!!mergedRowConditions && mergedRowConditions.length > 0) {
this.session.assert(
this.rowConditionsAssertion = this.session.assert(
`${this.proto.target.schema}_${this.proto.target.name}_assertions_rowConditions`,
ctx =>
this.session
.adapter()
.rowConditionsAssertion(ctx.ref(this.proto.target), mergedRowConditions)
).proto.parentAction = this.proto.target;
);
this.rowConditionsAssertion.proto.parentAction = this.proto.target;
if (this.proto.disabled) {
this.rowConditionsAssertion.disabled();
}
}
return this;
}
Expand Down
3 changes: 2 additions & 1 deletion examples/common_v2/definitions/example_table_with_tags.sqlx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@ config {
assertions: {
uniqueKey: ["sample"],
nonNull: ["sample"]
}
},
disabled: true
}

select * from ${ref("sample_data")}
Expand Down

This file was deleted.

3 changes: 3 additions & 0 deletions tests/api/examples.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ suite("examples", () => {
t.name ===
"tada-analytics." + schemaWithSuffix("df_integration_test") + ".example_table_with_tags"
);
expect(exampleTableWithTags.disabled).eql(true);
expect(exampleTableWithTags.tags).to.eql(["tag1", "tag2", "tag3"]);

// Check table-with-tags's unique key assertion
Expand All @@ -307,6 +308,7 @@ suite("examples", () => {
schemaWithSuffix("df_integration_test_assertions") +
".df_integration_test_example_table_with_tags_assertions_uniqueKey"
)[0];
expect(exampleTableWithTagsUniqueKeyAssertion.disabled).eql(true);
expect(cleanSql(exampleTableWithTagsUniqueKeyAssertion.query)).equals(
"select * from (select sample, count(1) as index_row_count from `tada-analytics." +
schemaWithSuffix("df_integration_test") +
Expand All @@ -331,6 +333,7 @@ suite("examples", () => {
schemaWithSuffix("df_integration_test_assertions") +
".df_integration_test_example_table_with_tags_assertions_rowConditions"
)[0];
expect(exampleTableWithTagsRowConditionsAssertion.disabled).eql(true);
expect(cleanSql(exampleTableWithTagsRowConditionsAssertion.query)).equals(
"select 'sample is not null' as failing_row_condition, * from `tada-analytics." +
schemaWithSuffix("df_integration_test") +
Expand Down
2 changes: 1 addition & 1 deletion version.bzl
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
# NOTE: If you change the format of this line, you must change the bash command
# in /scripts/publish to extract the version string correctly.
DF_VERSION = "1.14.2"
DF_VERSION = "1.14.3"

0 comments on commit fa35462

Please sign in to comment.