diff --git a/CHANGELOG.md b/CHANGELOG.md index 4a29139a..8be3519f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,7 +19,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - SQL Server: cannot escape reserved word on model (#557) - +- Replaced deprecated `primary` field with `primaryKey` in exporters, importers, examples, and Jinja templates for backward compatibility. Fixes [#518](https://github.com/your-repo/your-project/issues/518). ## [0.10.16] - 2024-12-19 diff --git a/datacontract/export/dbml_converter.py b/datacontract/export/dbml_converter.py index 18a8adcd..5c13ea31 100644 --- a/datacontract/export/dbml_converter.py +++ b/datacontract/export/dbml_converter.py @@ -90,7 +90,7 @@ def generate_table(model_name: str, model: spec.Model, server: spec.Server) -> s def generate_field(field_name: str, field: spec.Field, model_name: str, server: spec.Server) -> Tuple[str, str]: - if field.primary: + if field.primaryKey or field.primary: if field.required is not None: if not field.required: raise DataContractException( @@ -115,7 +115,7 @@ def generate_field(field_name: str, field: spec.Field, model_name: str, server: field.unique = True field_attrs = [] - if field.primary: + if field.primaryKey or field.primary: field_attrs.append("pk") if field.unique: diff --git a/datacontract/export/sql_converter.py b/datacontract/export/sql_converter.py index fc066c49..2aabe111 100644 --- a/datacontract/export/sql_converter.py +++ b/datacontract/export/sql_converter.py @@ -113,7 +113,7 @@ def _to_sql_table(model_name, model, server_type="snowflake"): result += f" {field_name} {type}" if field.required: result += " not null" - if field.primary: + if field.primaryKey or field.primary: result += " primary key" if server_type == "databricks" and field.description is not None: result += f' COMMENT "{_escape(field.description)}"' diff --git a/datacontract/export/sqlalchemy_converter.py b/datacontract/export/sqlalchemy_converter.py index 9dff4fee..f5d6d38f 100644 --- a/datacontract/export/sqlalchemy_converter.py +++ b/datacontract/export/sqlalchemy_converter.py @@ -114,7 +114,7 @@ def constant_field_value(field_name: str, field: spec.Field) -> tuple[ast.Call, if new_type is None: raise RuntimeError(f"Unsupported field type {field.type}.") - return Column(new_type, nullable=not field.required, comment=field.description, primary_key=field.primary), None + return Column(new_type, nullable=not field.required, comment=field.description, primary_key=field.primaryKey or field.primary), None def column_assignment(field_name: str, field: spec.Field) -> tuple[ast.Call, typing.Optional[ast.ClassDef]]: diff --git a/datacontract/imports/dbml_importer.py b/datacontract/imports/dbml_importer.py index 54a49c39..c04715ec 100644 --- a/datacontract/imports/dbml_importer.py +++ b/datacontract/imports/dbml_importer.py @@ -84,6 +84,7 @@ def import_table_fields(table, references) -> dict[str, Field]: imported_fields[field_name] = Field() imported_fields[field_name].required = field.not_null imported_fields[field_name].description = field.note.text + imported_fields[field_name].primaryKey = field.pk imported_fields[field_name].primary = field.pk imported_fields[field_name].unique = field.unique # This is an assumption, that these might be valid SQL Types, since diff --git a/datacontract/imports/sql_importer.py b/datacontract/imports/sql_importer.py index 127eb771..38078d1f 100644 --- a/datacontract/imports/sql_importer.py +++ b/datacontract/imports/sql_importer.py @@ -38,6 +38,7 @@ def import_sql(data_contract_specification: DataContractSpecification, format: s if primary_key in fields: fields[primary_key].unique = True fields[primary_key].required = True + fields[primary_key].primaryKey = True fields[primary_key].primary = True data_contract_specification.models[table_name] = Model( diff --git a/datacontract/model/data_contract_specification.py b/datacontract/model/data_contract_specification.py index 29aa5de1..c5ca3bb2 100644 --- a/datacontract/model/data_contract_specification.py +++ b/datacontract/model/data_contract_specification.py @@ -180,6 +180,13 @@ class Field(pyd.BaseModel): quality: List[Quality] | None = [] config: Dict[str, Any] | None = None + def __init__(self, **data): + super().__init__(**data) + if self.primary is not None and self.primaryKey is None: + self.primaryKey = self.primary + elif self.primaryKey is not None and self.primary is None: + self.primary = self.primaryKey + model_config = pyd.ConfigDict( extra="allow", ) diff --git a/datacontract/templates/partials/model_field.html b/datacontract/templates/partials/model_field.html index 1a9f5eba..30273dcc 100644 --- a/datacontract/templates/partials/model_field.html +++ b/datacontract/templates/partials/model_field.html @@ -40,7 +40,7 @@ {% endif %}
dataContractSpecification: 1.1.0
-id: orders-unit-test
-info:
- title: Orders Unit Test
- version: 1.0.0
- description: The orders data contract
- owner: checkout
- contact:
- url: https://wiki.example.com/teams/checkout
- email: team-orders@example.com
-servers:
- production:
- type: snowflake
- account: my-account
- database: my-database
- schema_: my-schema
-terms:
- usage: This data contract serves to demo datacontract CLI export.
- limitations: Not intended to use in production
- billing: free
- noticePeriod: P3M
-models:
- orders:
- description: The orders model
- type: table
- fields:
- order_id:
- type: varchar
- required: true
- primary: false
- unique: true
- pii: true
- classification: sensitive
- pattern: ^B[0-9]+$
- minLength: 8
- maxLength: 10
- tags:
- - order_id
- order_total:
- type: bigint
- required: true
- primary: false
- unique: false
- description: The order_total field
- minimum: 0
- maximum: 1000000
- order_status:
- type: text
- required: true
- primary: false
- unique: false
- enum:
- - pending
- - shipped
- - delivered
-
+
+
+ dataContractSpecification: 1.1.0
+ id: orders-unit-test
+ info:
+ title: Orders Unit Test
+ version: 1.0.0
+ description: The orders data contract
+ owner: checkout
+ contact:
+ url: https://wiki.example.com/teams/checkout
+ email: team-orders@example.com
+ servers:
+ production:
+ type: snowflake
+ account: my-account
+ database: my-database
+ schema_: my-schema
+ terms:
+ usage: This data contract serves to demo datacontract CLI export.
+ limitations: Not intended to use in production
+ billing: free
+ noticePeriod: P3M
+ models:
+ orders:
+ description: The orders model
+ type: table
+ fields:
+ order_id:
+ type: varchar
+ required: true
+ primaryKey: false
+ unique: true
+ pii: true
+ classification: sensitive
+ pattern: ^B[0-9]+$
+ minLength: 8
+ maxLength: 10
+ tags:
+ - order_id
+ order_total:
+ type: bigint
+ required: true
+ primaryKey: false
+ unique: false
+ description: The order_total field
+ minimum: 0
+ maximum: 1000000
+ order_status:
+ type: text
+ required: true
+ primaryKey: false
+ unique: false
+ enum:
+ - pending
+ - shipped
+ - delivered
+