From 3eaf7f7d30e5b571ede7bb4ffced32501c86bc3e Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Fri, 5 Jul 2024 12:28:44 -0400 Subject: [PATCH 1/4] add to and to_column(s) to ColumnLevelConstraint and ModelLevelConstraint contracts --- dbt_common/contracts/constraints.py | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) diff --git a/dbt_common/contracts/constraints.py b/dbt_common/contracts/constraints.py index c01ee6f8..7cb11afc 100644 --- a/dbt_common/contracts/constraints.py +++ b/dbt_common/contracts/constraints.py @@ -2,7 +2,7 @@ from enum import Enum from typing import Optional, List -from dbt_common.dataclass_schema import dbtClassMixin +from dbt_common.dataclass_schema import dbtClassMixin, ValidationError class ConstraintType(str, Enum): @@ -36,8 +36,30 @@ class ColumnLevelConstraint(dbtClassMixin): warn_unsupported: bool = ( True # Warn if constraint is not supported by the platform and won't be in DDL ) + to: Optional[str] = None + to_column: Optional[str] = None + + @classmethod + def validate(cls, data): + super().validate(data) + if data.get("type") is not ConstraintType.foreign_key: + if data.get("to") is not None: + raise ValidationError(f"Only column-level constraint of type {ConstraintType.foreign_key} can specify a 'to' field.") + if data.get("to_column") is not None: + raise ValidationError(f"Only column-level constraint of type {ConstraintType.foreign_key} can specify a 'to_column' field.") @dataclass class ModelLevelConstraint(ColumnLevelConstraint): columns: List[str] = field(default_factory=list) + to: Optional[str] = None + to_columns: List[str] = field(default_factory=list) + + @classmethod + def validate(cls, data): + super().validate(data) + if data.get("type") is not ConstraintType.foreign_key: + if data.get("to") is not None: + raise ValidationError(f"Only model-level constraint of type {ConstraintType.foreign_key} can specify a 'to' field.") + if data.get("to_columns") is not None: + raise ValidationError(f"Only model-level constraint of type {ConstraintType.foreign_key} can specify a 'to_columns' field.") From c74d7866bda7206e0b986cafdc15c026dd5d4ccc Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Fri, 5 Jul 2024 14:29:40 -0400 Subject: [PATCH 2/4] fix schema --- dbt_common/contracts/constraints.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/dbt_common/contracts/constraints.py b/dbt_common/contracts/constraints.py index b2b90ae0..b08373b4 100644 --- a/dbt_common/contracts/constraints.py +++ b/dbt_common/contracts/constraints.py @@ -37,11 +37,9 @@ class ColumnLevelConstraint(dbtClassMixin): True # Warn if constraint is not supported by the platform and won't be in DDL ) to: Optional[str] = None - to_column: Optional[str] = None + to_columns: List[str] = field(default_factory=list) @dataclass class ModelLevelConstraint(ColumnLevelConstraint): columns: List[str] = field(default_factory=list) - to: Optional[str] = None - to_columns: List[str] = field(default_factory=list) From 35c6bfc26f60af2d4ca8e55f8bc820ae19d42a9a Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Tue, 16 Jul 2024 10:20:30 -0400 Subject: [PATCH 3/4] linting --- dbt_common/contracts/constraints.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dbt_common/contracts/constraints.py b/dbt_common/contracts/constraints.py index b08373b4..4e2d9c7a 100644 --- a/dbt_common/contracts/constraints.py +++ b/dbt_common/contracts/constraints.py @@ -2,7 +2,7 @@ from enum import Enum from typing import Optional, List -from dbt_common.dataclass_schema import dbtClassMixin, ValidationError +from dbt_common.dataclass_schema import dbtClassMixin class ConstraintType(str, Enum): From d516a4ec2ddf68ca7ac0caa4cd349bda246ef24a Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Tue, 16 Jul 2024 10:25:02 -0400 Subject: [PATCH 4/4] changelog entry --- .changes/unreleased/Features-20240716-102457.yaml | 6 ++++++ 1 file changed, 6 insertions(+) create mode 100644 .changes/unreleased/Features-20240716-102457.yaml diff --git a/.changes/unreleased/Features-20240716-102457.yaml b/.changes/unreleased/Features-20240716-102457.yaml new file mode 100644 index 00000000..096b4259 --- /dev/null +++ b/.changes/unreleased/Features-20240716-102457.yaml @@ -0,0 +1,6 @@ +kind: Features +body: Add to and to_columns to ColumnLevelConstraint and ModelLevelConstraint contracts +time: 2024-07-16T10:24:57.11251-04:00 +custom: + Author: michelleark + Issue: "168"