From ff4813c20e8fc33cd896cb13cd5643c4788ff09f Mon Sep 17 00:00:00 2001 From: Yasuhisa Yoshida Date: Sat, 21 Dec 2024 23:15:17 +0900 Subject: [PATCH] When importing in dbt format, add the dbt `unique` information as a datacontract `unique` field (#558) * When importing in dbt format, add the dbt unique information as a datacontract unique field * Updated CHANGELOG --- CHANGELOG.md | 1 + datacontract/imports/dbt_importer.py | 8 ++++++++ tests/test_import_dbt.py | 11 +++++++++++ 3 files changed, 20 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 29bbe1af..4455a158 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added +- When importing in dbt format, add the dbt unique information as a datacontract unique field (#558) ### Changed diff --git a/datacontract/imports/dbt_importer.py b/datacontract/imports/dbt_importer.py index 96eece78..f9b6e0fd 100644 --- a/datacontract/imports/dbt_importer.py +++ b/datacontract/imports/dbt_importer.py @@ -155,4 +155,12 @@ def create_field(manifest: Manifest, model_unique_id: str, column: ColumnInfo, a if required: field.required = required + unique = False + if any(constraint.type == ConstraintType.unique for constraint in column.constraints): + unique = True + if [test for test in all_tests if test["test_type"] == "unique"]: + unique = True + if unique: + field.unique = unique + return field diff --git a/tests/test_import_dbt.py b/tests/test_import_dbt.py index fa4a3f23..7210b491 100644 --- a/tests/test_import_dbt.py +++ b/tests/test_import_dbt.py @@ -85,6 +85,7 @@ def test_import_dbt_manifest(): type: integer description: This is a unique identifier for an order required: true + unique: true customer_id: type: integer description: Foreign key to the customers table @@ -141,6 +142,7 @@ def test_import_dbt_manifest(): type: integer description: '' required: true + unique: true first_name: type: varchar description: '' @@ -155,6 +157,7 @@ def test_import_dbt_manifest(): type: integer description: '' required: true + unique: true customer_id: type: integer description: '' @@ -172,6 +175,7 @@ def test_import_dbt_manifest(): type: integer description: '' required: true + unique: true order_id: type: integer description: '' @@ -190,6 +194,7 @@ def test_import_dbt_manifest(): type: integer description: This is a unique identifier for a customer required: true + unique: true first_name: type: varchar description: Customer's first name. PII. @@ -238,6 +243,7 @@ def test_import_dbt_manifest_bigquery(): type: bigint description: This is a unique identifier for an order required: true + unique: true customer_id: type: bigint description: Foreign key to the customers table @@ -294,6 +300,7 @@ def test_import_dbt_manifest_bigquery(): type: bigint description: '' required: true + unique: true first_name: type: string description: '' @@ -308,6 +315,7 @@ def test_import_dbt_manifest_bigquery(): type: bigint description: '' required: true + unique: true customer_id: type: bigint description: '' @@ -325,6 +333,7 @@ def test_import_dbt_manifest_bigquery(): type: bigint description: '' required: true + unique: true order_id: type: bigint description: '' @@ -343,6 +352,7 @@ def test_import_dbt_manifest_bigquery(): type: bigint description: This is a unique identifier for a customer required: true + unique: true first_name: type: string description: Customer's first name. PII. @@ -413,6 +423,7 @@ def test_import_dbt_manifest_with_filter(): type: integer description: This is a unique identifier for a customer required: true + unique: true first_name: type: varchar description: Customer's first name. PII.