Skip to content

Commit

Permalink
Merge branch 'main' into incremental_contract_varchar
Browse files Browse the repository at this point in the history
  • Loading branch information
gshank committed Jul 29, 2024
2 parents 738e210 + 4b2755d commit b1ae023
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 12 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20240723-104221.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: render 'to' and 'to_columns' fields on foreign key constraints, and bump dbt-common lower bound to 1.6
time: 2024-07-23T10:42:21.222203-04:00
custom:
Author: michelleark
Issue: "271"
12 changes: 12 additions & 0 deletions .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,25 @@ updates:
schedule:
interval: "daily"
rebase-strategy: "disabled"
ignore:
- dependency-name: "*"
update-types:
- version-update:semver-patch
- package-ecosystem: "pip"
directory: "/dbt-tests-adapter"
schedule:
interval: "daily"
rebase-strategy: "disabled"
ignore:
- dependency-name: "*"
update-types:
- version-update:semver-patch
- package-ecosystem: "github-actions"
directory: "/"
schedule:
interval: "weekly"
rebase-strategy: "disabled"
ignore:
- dependency-name: "*"
update-types:
- version-update:semver-patch
3 changes: 2 additions & 1 deletion dbt-tests-adapter/dbt/tests/adapter/constraints/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,8 @@
- type: check
expression: id >= 1
- type: foreign_key
expression: {schema}.foreign_key_model (id)
to: ref('foreign_key_model')
to_columns: ["id"]
- type: unique
data_tests:
- unique
Expand Down
34 changes: 24 additions & 10 deletions dbt/adapters/base/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -1602,8 +1602,13 @@ def render_column_constraint(cls, constraint: ColumnLevelConstraint) -> Optional
rendered_column_constraint = f"unique {constraint_expression}"
elif constraint.type == ConstraintType.primary_key:
rendered_column_constraint = f"primary key {constraint_expression}"
elif constraint.type == ConstraintType.foreign_key and constraint_expression:
rendered_column_constraint = f"references {constraint_expression}"
elif constraint.type == ConstraintType.foreign_key:
if constraint.to and constraint.to_columns:
rendered_column_constraint = (
f"references {constraint.to} ({', '.join(constraint.to_columns)})"
)
elif constraint_expression:
rendered_column_constraint = f"references {constraint_expression}"
elif constraint.type == ConstraintType.custom and constraint_expression:
rendered_column_constraint = constraint_expression

Expand Down Expand Up @@ -1682,20 +1687,29 @@ def render_model_constraint(cls, constraint: ModelLevelConstraint) -> Optional[s
rendering."""
constraint_prefix = f"constraint {constraint.name} " if constraint.name else ""
column_list = ", ".join(constraint.columns)
rendered_model_constraint = None

if constraint.type == ConstraintType.check and constraint.expression:
return f"{constraint_prefix}check ({constraint.expression})"
rendered_model_constraint = f"{constraint_prefix}check ({constraint.expression})"
elif constraint.type == ConstraintType.unique:
constraint_expression = f" {constraint.expression}" if constraint.expression else ""
return f"{constraint_prefix}unique{constraint_expression} ({column_list})"
rendered_model_constraint = (
f"{constraint_prefix}unique{constraint_expression} ({column_list})"
)
elif constraint.type == ConstraintType.primary_key:
constraint_expression = f" {constraint.expression}" if constraint.expression else ""
return f"{constraint_prefix}primary key{constraint_expression} ({column_list})"
elif constraint.type == ConstraintType.foreign_key and constraint.expression:
return f"{constraint_prefix}foreign key ({column_list}) references {constraint.expression}"
rendered_model_constraint = (
f"{constraint_prefix}primary key{constraint_expression} ({column_list})"
)
elif constraint.type == ConstraintType.foreign_key:
if constraint.to and constraint.to_columns:
rendered_model_constraint = f"{constraint_prefix}foreign key ({column_list}) references {constraint.to} ({', '.join(constraint.to_columns)})"
elif constraint.expression:
rendered_model_constraint = f"{constraint_prefix}foreign key ({column_list}) references {constraint.expression}"
elif constraint.type == ConstraintType.custom and constraint.expression:
return f"{constraint_prefix}{constraint.expression}"
else:
return None
rendered_model_constraint = f"{constraint_prefix}{constraint.expression}"

return rendered_model_constraint

@classmethod
def capabilities(cls) -> CapabilityDict:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
{%- do default_row.update({column_name: (safe_cast("null", column_type) | trim )}) -%}
{%- endfor -%}

{{ validate_fixture_rows(rows, row_number) }}

{%- for row in rows -%}
{%- set formatted_row = format_row(row, column_name_to_data_types) -%}
Expand Down Expand Up @@ -93,3 +94,11 @@ union all
{%- endfor -%}
{{ return(formatted_row) }}
{%- endmacro -%}

{%- macro validate_fixture_rows(rows, row_number) -%}
{{ return(adapter.dispatch('validate_fixture_rows', 'dbt')(rows, row_number)) }}
{%- endmacro -%}

{%- macro default__validate_fixture_rows(rows, row_number) -%}
{# This is an abstract method for adapter overrides as needed #}
{%- endmacro -%}
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ classifiers = [
"Programming Language :: Python :: 3.12",
]
dependencies = [
"dbt-common>=1.3,<2.0",
"dbt-common>=1.6,<2.0",
"pytz>=2015.7",
# installed via dbt-common but used directly
"agate>=1.0,<2.0",
Expand Down
32 changes: 32 additions & 0 deletions tests/unit/test_base_adapter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,14 @@ def connection_manager(self):
[{"type": "foreign_key", "expression": "other_table (c1)"}],
["column_name integer references other_table (c1)"],
),
(
[{"type": "foreign_key", "to": "other_table", "to_columns": ["c1"]}],
["column_name integer references other_table (c1)"],
),
(
[{"type": "foreign_key", "to": "other_table", "to_columns": ["c1", "c2"]}],
["column_name integer references other_table (c1, c2)"],
),
([{"type": "check"}, {"type": "unique"}], ["column_name integer unique"]),
([{"type": "custom", "expression": "-- noop"}], ["column_name integer -- noop"]),
]
Expand Down Expand Up @@ -176,6 +184,30 @@ def test_render_raw_columns_constraints_unsupported(
],
["constraint test_name foreign key (c1, c2) references other_table (c1)"],
),
(
[
{
"type": "foreign_key",
"columns": ["c1", "c2"],
"to": "other_table",
"to_columns": ["c1"],
"name": "test_name",
}
],
["constraint test_name foreign key (c1, c2) references other_table (c1)"],
),
(
[
{
"type": "foreign_key",
"columns": ["c1", "c2"],
"to": "other_table",
"to_columns": ["c1", "c2"],
"name": "test_name",
}
],
["constraint test_name foreign key (c1, c2) references other_table (c1, c2)"],
),
]

@pytest.mark.parametrize("constraints,expected_rendered_constraints", model_constraints)
Expand Down

0 comments on commit b1ae023

Please sign in to comment.