-
Notifications
You must be signed in to change notification settings - Fork 29
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 #39 from Snowflake-Labs/37-feature-request-add-con…
…straints-to-seeds Added support for seeds
- Loading branch information
Showing
13 changed files
with
239 additions
and
23 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
version: 2 | ||
|
||
seeds: | ||
|
||
- name: part | ||
columns: | ||
- name: p_partkey | ||
description: "The primary key for this table" | ||
tests: | ||
- unique | ||
- not_null | ||
|
||
- name: partsupp | ||
columns: | ||
- name: ps_partkey | ||
description: "Part of compound primary key for this table" | ||
tests: | ||
- not_null | ||
- relationships: | ||
to: ref('part') | ||
field: p_partkey | ||
- name: ps_suppkey | ||
description: "Part of compound primary key for this table" | ||
tests: | ||
- not_null | ||
- dbt_constraints.foreign_key: | ||
pk_table_name: ref('supplier') | ||
pk_column_name: s_suppkey | ||
tests: | ||
# This is a higher performance way to test compound PK/UK | ||
- dbt_constraints.unique_key: | ||
column_names: | ||
- ps_partkey | ||
- ps_suppkey | ||
# How to validate a compound primary key natively | ||
- unique: | ||
column_name: "coalesce(cast(ps_partkey as varchar(100)), '') || '~' || coalesce(cast(ps_suppkey as varchar(100)), '')" | ||
|
||
- name: supplier | ||
columns: | ||
- name: s_suppkey | ||
description: "The primary key for this table" | ||
tests: | ||
- unique | ||
- not_null | ||
- name: s_nationkey | ||
tests: | ||
- not_null | ||
|
||
- name: orders | ||
columns: | ||
- name: o_orderkey | ||
description: "The primary key for this table" | ||
tests: | ||
- unique | ||
- not_null | ||
- name: o_custkey | ||
tests: | ||
- not_null | ||
- relationships: | ||
to: ref('customer') | ||
field: c_custkey | ||
|
||
- name: customer | ||
columns: | ||
- name: c_custkey | ||
description: "The primary key for dim_customers" | ||
tests: | ||
- dbt_constraints.primary_key | ||
- name: c_name | ||
description: "Customer Name" | ||
tests: | ||
- not_null | ||
- name: c_nationkey | ||
tests: | ||
- not_null | ||
|
||
- name: lineitem | ||
columns: | ||
- name: l_orderkey | ||
tests: | ||
- not_null | ||
- relationships: | ||
to: ref('orders') | ||
field: o_orderkey | ||
- name: l_linenumber | ||
tests: | ||
- not_null | ||
tests: | ||
# This is a higher performance way to test compound PK/UK | ||
- dbt_constraints.unique_key: | ||
column_names: | ||
- l_orderkey | ||
- l_linenumber | ||
# How to validate a compound primary key natively | ||
- unique: | ||
column_name: "coalesce(cast(l_orderkey as varchar(100)), '') || '~' || coalesce(cast(l_linenumber as varchar(100)), '')" | ||
# How to validate a compound foreign key | ||
- relationships: | ||
column_name: "coalesce(cast(l_partkey as varchar(100)), '') || '~' || coalesce(cast(l_suppkey as varchar(100)), '')" | ||
to: ref('partsupp') | ||
field: "coalesce(cast(ps_partkey as varchar(100)), '') || '~' || coalesce(cast(ps_suppkey as varchar(100)), '')" |
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,75 @@ | ||
{%- macro clone_table(new_prefix) -%} | ||
{{ return(adapter.dispatch('clone_table')(new_prefix)) }} | ||
{%- endmacro -%} | ||
|
||
|
||
{%- macro snowflake__clone_table(new_prefix) -%} | ||
{%- set table_clone = api.Relation.create( | ||
database = this.database, | ||
schema = this.schema, | ||
identifier = new_prefix ~ this.identifier ) -%} | ||
|
||
{%- set clone_statement -%} | ||
create or replace table {{table_clone}} clone {{this}} | ||
{%- endset -%} | ||
{%- do log("Creating table clone: " ~ table_clone, info=false) -%} | ||
{%- do run_query(clone_statement) -%} | ||
|
||
{%- endmacro -%} | ||
|
||
|
||
{%- macro postgres__clone_table(new_prefix) -%} | ||
{%- set table_clone = api.Relation.create( | ||
database = this.database, | ||
schema = this.schema, | ||
identifier = new_prefix ~ this.identifier ) -%} | ||
|
||
{%- set clone_statement -%} | ||
drop table if exists {{table_clone}} | ||
{%- endset -%} | ||
{%- do log("Drop table if exists: " ~ table_clone, info=false) -%} | ||
|
||
{%- set clone_statement -%} | ||
create table {{table_clone}} as select * from {{this}} | ||
{%- endset -%} | ||
{%- do log("Creating table clone: " ~ table_clone, info=false) -%} | ||
{%- do run_query(clone_statement) -%} | ||
|
||
{%- endmacro -%} | ||
|
||
|
||
{%- macro oracle__clone_table(new_prefix) -%} | ||
{%- set table_clone = api.Relation.create( | ||
database = this.database, | ||
schema = this.schema, | ||
identifier = new_prefix ~ this.identifier ) -%} | ||
|
||
{%- set clone_statement -%} | ||
DECLARE | ||
tbl_count number; | ||
sql_stmt long; | ||
|
||
BEGIN | ||
SELECT COUNT(*) INTO tbl_count | ||
FROM dba_tables | ||
WHERE owner = '{{table_clone.schema}}' | ||
AND table_name = '{{table_clone.identifier}}'; | ||
|
||
IF(tbl_count <> 0) | ||
THEN | ||
sql_stmt:='DROP TABLE {{table_clone}}'; | ||
EXECUTE IMMEDIATE sql_stmt; | ||
END IF; | ||
END; | ||
{%- endset -%} | ||
{%- do log("Drop table if exists: " ~ table_clone, info=false) -%} | ||
|
||
{%- set clone_statement -%} | ||
create table {{table_clone}} as select * from {{this}} | ||
{%- endset -%} | ||
{%- do log("Creating table clone: " ~ table_clone, info=false) -%} | ||
{%- do run_query(clone_statement) -%} | ||
|
||
{%- endmacro -%} | ||
|
||
|
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
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
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