Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CT-2215: Modify adapter to support unified constraint fields #681

Merged
merged 4 commits into from
Mar 22, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -192,13 +192,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 @@ -210,12 +212,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
4 changes: 2 additions & 2 deletions dev-requirements.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# install latest changes in dbt-core
# TODO: how to automate switching from develop to version branches?
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git#egg=dbt-tests-adapter&subdirectory=tests/adapter
git+https://github.com/dbt-labs/dbt-core.git@paw/ct-2198-unify-constraints#egg=dbt-core&subdirectory=core
git+https://github.com/dbt-labs/dbt-core.git@paw/ct-2198-unify-constraints#egg=dbt-tests-adapter&subdirectory=tests/adapter

# if version 1.x or greater -> pin to major version
# if version 0.x -> pin to minor
Expand Down