Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix protected default, add a unit test for SQLX config parameters #1718

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 8 additions & 4 deletions core/actions/table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -453,8 +453,8 @@ export class Table extends ActionBuilder<dataform.Table> {
if (config.disabled) {
this.disabled();
}
if (config.protected) {
this.protected();
if (config.type === "incremental") {
this.protected(config.protected);
}
if (config.bigquery && Object.keys(config.bigquery).length > 0) {
this.bigquery(config.bigquery);
Expand Down Expand Up @@ -520,8 +520,12 @@ export class Table extends ActionBuilder<dataform.Table> {
return this;
}

public protected() {
this.proto.protected = true;
public protected(defaultsToTrueProtected: boolean) {
// To prevent accidental data deletion, protected defaults to true if unspecified.
if (defaultsToTrueProtected === undefined || defaultsToTrueProtected === null) {
defaultsToTrueProtected = true;
}
this.proto.protected = defaultsToTrueProtected;
return this;
}

Expand Down
189 changes: 189 additions & 0 deletions core/main_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,6 +729,195 @@ select 1 AS \${dataform.projectConfig.vars.columnVar}`
});
});

suite("incremental tables", () => {
test("sqlx config parameters are applied", () => {
const projectDir = tmpDirFixture.createNewTmpDir();
fs.writeFileSync(
path.join(projectDir, "workflow_settings.yaml"),
VALID_WORKFLOW_SETTINGS_YAML
);
fs.mkdirSync(path.join(projectDir, "definitions"));
fs.writeFileSync(path.join(projectDir, "definitions/operation.sqlx"), "SELECT 1");
fs.writeFileSync(
path.join(projectDir, "definitions/incremental_table.sqlx"),
`
config {
type: "incremental",
disabled: true,
protected: false,
name: "name",
bigquery: {
partitionBy: "partitionBy",
clusterBy: ["clusterBy"],
updatePartitionFilter: "updatePartitionFilter",
labels: {"key": "val"},
partitionExpirationDays: 1,
requirePartitionFilter: true,
additionalOptions: {
option1Key: "option1",
option2Key: "option2",
}
},
tags: ["tag1", "tag2"],
uniqueKey: ["key1", "key2"],
dependencies: ["operation"],
hermetic: true,
schema: "schema",
assertions: {
uniqueKeys: [["uniqueKey1", "uniqueKey2"]],
nonNull: "nonNull",
rowConditions: ["rowConditions1", "rowConditions2"],
},
database: "database",
columns: {
column1Key: "column1Val",
column2Key: {
description: "description",
columns: {
nestedColumnKey: "nestedColumnVal"
},
displayName: "displayName",
tags: ["tag3", "tag4"],
bigqueryPolicyTags: ["bigqueryPolicyTag1", "bigqueryPolicyTag2"],
}
},
description: "description",
materialized: false
}
SELECT 1`
);

const result = runMainInVm(coreExecutionRequestFromPath(projectDir));

expect(asPlainObject(result.compile.compiledGraph.graphErrors.compilationErrors)).deep.equals(
[]
);
expect(asPlainObject(result.compile.compiledGraph.tables)).deep.equals([
{
type: "incremental",
disabled: true,
protected: false,
hermeticity: "HERMETIC",
target: {
database: "database",
name: "name",
schema: "schema"
},
canonicalTarget: {
database: "database",
name: "name",
schema: "schema"
},
bigquery: {
additionalOptions: {
option1Key: "option1",
option2Key: "option2"
},
clusterBy: ["clusterBy"],
labels: {
key: "val"
},
partitionBy: "partitionBy",
partitionExpirationDays: 1,
requirePartitionFilter: true,
updatePartitionFilter: "updatePartitionFilter"
},
tags: ["tag1", "tag2"],
uniqueKey: ["key1", "key2"],
dependencyTargets: [
{
database: "dataform",
name: "operation"
}
],
actionDescriptor: {
bigqueryLabels: {
key: "val"
},
columns: [
{
description: "column1Val",
path: ["column1Key"]
},
{
bigqueryPolicyTags: ["bigqueryPolicyTag1", "bigqueryPolicyTag2"],
description: "description",
displayName: "displayName",
path: ["column2Key"],
tags: ["tag3", "tag4"]
},
{
description: "nestedColumnVal",
path: ["column2Key", "nestedColumnKey"]
}
],
description: "description"
},
enumType: "INCREMENTAL",
fileName: "definitions/incremental_table.sqlx",
query: "\n\nSELECT 1",
incrementalQuery: "\n\nSELECT 1"
}
]);
expect(asPlainObject(result.compile.compiledGraph.assertions)).deep.equals([
{
target: {
database: "dataform",
name: "schema_name_assertions_uniqueKey_0"
},
canonicalTarget: {
database: "dataform",
name: "schema_name_assertions_uniqueKey_0"
},
dependencyTargets: [
{
database: "database",
name: "name",
schema: "schema"
}
],
disabled: true,
fileName: "definitions/incremental_table.sqlx",
parentAction: {
database: "database",
name: "name",
schema: "schema"
},
query:
"\nSELECT\n *\nFROM (\n SELECT\n uniqueKey1, uniqueKey2,\n COUNT(1) AS index_row_count\n FROM `database.schema.name`\n GROUP BY uniqueKey1, uniqueKey2\n ) AS data\nWHERE index_row_count > 1\n",
tags: ["tag1", "tag2"]
},
{
target: {
database: "dataform",
name: "schema_name_assertions_rowConditions"
},
canonicalTarget: {
database: "dataform",
name: "schema_name_assertions_rowConditions"
},
dependencyTargets: [
{
database: "database",
name: "name",
schema: "schema"
}
],
disabled: true,
fileName: "definitions/incremental_table.sqlx",
parentAction: {
database: "database",
name: "name",
schema: "schema"
},
query:
"\nSELECT\n 'rowConditions1' AS failing_row_condition,\n *\nFROM `database.schema.name`\nWHERE NOT (rowConditions1)\nUNION ALL\nSELECT\n 'rowConditions2' AS failing_row_condition,\n *\nFROM `database.schema.name`\nWHERE NOT (rowConditions2)\nUNION ALL\nSELECT\n 'nonNull IS NOT NULL' AS failing_row_condition,\n *\nFROM `database.schema.name`\nWHERE NOT (nonNull IS NOT NULL)\n",
tags: ["tag1", "tag2"]
}
]);
});
});

suite("notebooks", () => {
const createSimpleNotebookProject = (
workflowSettingsYaml = VALID_WORKFLOW_SETTINGS_YAML
Expand Down
2 changes: 1 addition & 1 deletion scripts/run_tests
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ bazel run @nodejs//:yarn
bazel build @npm//tslint/bin:tslint && bazel-bin/external/npm/tslint/bin/tslint.sh --project .

# Run all the tests
bazel test --config=remote-cache ... --build_tests_only --test_env=USE_CLOUD_BUILD_NETWORK=true
bazel test ... --build_tests_only
3 changes: 2 additions & 1 deletion tests/core/core.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,8 @@ suite("@dataform/core", () => {
disabled: false,
fileName: path.basename(__filename),
type: "incremental",
enumType: "INCREMENTAL"
enumType: "INCREMENTAL",
protected: true
}
]);
});
Expand Down