-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from devoteamgcloud/dv
New assertion type
- Loading branch information
Showing
8 changed files
with
97 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
config { | ||
type: "table" | ||
} | ||
|
||
SELECT | ||
1 AS parent_id, | ||
CURRENT_DATE() AS updated_date | ||
UNION ALL | ||
SELECT | ||
2 AS parent_id, | ||
CURRENT_DATE() AS updated_date |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
/** | ||
* referential_integrity_assertions.js | ||
* | ||
* This file contains a function to create referential integrity assertions for specific tables in a database. | ||
* The assertions are used to check if the foreign key relationships are maintained between tables. | ||
* The conditions for referential integrity checks are defined in an object format: | ||
* { parentTable: [{ parentKey, childTable, childKey }, ...], ... } | ||
* | ||
* The function `createReferentialIntegrityAssertions` takes in global parameters and the referential integrity conditions. | ||
*/ | ||
|
||
/** | ||
* @param {Object} globalParams - See index.js for details. | ||
* @param {Object} parentTable - The name of the parent table in the foreign key relationship. | ||
* @param {Object} parentKey - The name of the column in the parent table that is the primary key. | ||
* @param {Object} childTable - The name of the child table in the foreign key relationship. | ||
* @param {Object} childKey - The name of the column in the child table that is the foreign key. | ||
*/ | ||
|
||
const assertions = []; | ||
|
||
const createReferentialIntegrityAssertion = (globalParams, parentTable, parentKey, childTable, childKey) => { | ||
|
||
const assertion = assert(`assert_referential_integrity_${parentTable}_${childTable}`) | ||
.database(globalParams.database) | ||
.schema(globalParams.schema) | ||
.description(`Check referential integrity for ${childTable}.${childKey} referencing ${parentTable}.${parentKey}`) | ||
.tags("assert-referential-integrity") | ||
.query(ctx => ` | ||
SELECT pt.${parentKey} | ||
FROM ${ctx.ref(parentTable)} AS pt | ||
LEFT JOIN ${ctx.ref(childTable)} AS t ON t.${childKey} = pt.${parentKey} | ||
WHERE t.${childKey} IS NULL | ||
`); | ||
|
||
(globalParams.tags && globalParams.tags.forEach((tag) => assertion.tags(tag))); | ||
|
||
(globalParams.disabledInEnvs && globalParams.disabledInEnvs.includes(dataform.projectConfig.vars.env)) && assertion.disabled(); | ||
|
||
assertions.push(assertion); | ||
}; | ||
|
||
module.exports = (globalParams, referentialIntegrityConditions) => { | ||
for (let parentTable in referentialIntegrityConditions) { | ||
const relationships = referentialIntegrityConditions[parentTable]; | ||
|
||
relationships.forEach(({ | ||
parentKey, | ||
childTable, | ||
childKey | ||
}) => { | ||
createReferentialIntegrityAssertion(globalParams, parentTable, parentKey, childTable, childKey); | ||
}) | ||
} | ||
return assertions; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters