Skip to content

Commit

Permalink
CT-2215: Modify adapter to support unified constraint fields (#681)
Browse files Browse the repository at this point in the history
* CT-2215: Modify adapter to support unified constraint fields

* CT-2215: Fix up not null constraint issue

* CT-2215: Revert requirements in preparation for merge
  • Loading branch information
peterallenwebb authored Mar 22, 2023
1 parent 7e1fd69 commit 28e4493
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 10 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/Features-20230314-223919.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Features
body: Modify adapter to support unified constraint fields
time: 2023-03-14T22:39:19.183649-04:00
custom:
Author: peterallenwebb
Issue: "655"
22 changes: 12 additions & 10 deletions dbt/include/spark/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -193,13 +193,15 @@
{% macro spark__alter_table_add_constraints(relation, column_dict) %}

{% for column_name in column_dict %}
{% set constraints_check = column_dict[column_name]['constraints_check'] %}
{% if constraints_check and not is_incremental() %}
{%- set constraint_hash = local_md5(column_name ~ ";" ~ constraint_check) -%}
{% call statement() %}
alter table {{ relation }} add constraint {{ constraint_hash }} check {{ constraints_check }};
{% endcall %}
{% endif %}
{% set constraints = column_dict[column_name]['constraints'] %}
{% for constraint in constraints %}
{% if constraint.type == 'check' and not is_incremental() %}
{%- set constraint_hash = local_md5(column_name ~ ";" ~ constraint.expression ~ ";" ~ loop.index) -%}
{% call statement() %}
alter table {{ relation }} add constraint {{ constraint_hash }} check {{ constraint.expression }};
{% endcall %}
{% endif %}
{% endfor %}
{% endfor %}
{% endmacro %}

Expand All @@ -211,12 +213,12 @@
{% for column_name in column_dict %}
{% set constraints = column_dict[column_name]['constraints'] %}
{% for constraint in constraints %}
{% if constraint != 'not null' %}
{{ exceptions.warn('Invalid constraint for column ' ~ column_name ~ '. Only `not null` is supported.') }}
{% if constraint.type != 'not_null' %}
{{ exceptions.warn('Invalid constraint for column ' ~ column_name ~ '. Only `not_null` is supported.') }}
{% else %}
{% set quoted_name = adapter.quote(column_name) if column_dict[column_name]['quote'] else column_name %}
{% call statement() %}
alter table {{ relation }} change column {{ quoted_name }} set {{ constraint }};
alter table {{ relation }} change column {{ quoted_name }} set not null {{ constraint.expression or "" }};
{% endcall %}
{% endif %}
{% endfor %}
Expand Down

0 comments on commit 28e4493

Please sign in to comment.