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

support composite UKs for archival #324

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
### Bugfixes

- Fix ephemeral load order bug ([#292](https://github.com/fishtown-analytics/dbt/pull/292), [#285](https://github.com/fishtown-analytics/dbt/pull/285))
- Support composite unique key in archivals ([#324](https://github.com/fishtown-analytics/dbt/pull/324))

### Changes

Expand Down
6 changes: 4 additions & 2 deletions dbt/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,8 @@ def execute_archive(profile, node, context):
profile, node_cfg.get('source_schema'), node_cfg.get('source_table'))

if len(source_columns) == 0:
source_schema = node_cfg.get('source_schema')
source_table = node_cfg.get('source_table')
raise RuntimeError(
'Source table "{}"."{}" does not '
'exist'.format(source_schema, source_table))
Expand All @@ -304,8 +306,8 @@ def execute_archive(profile, node, context):
schema=node_cfg.get('target_schema'),
table=node_cfg.get('target_table'),
columns=dest_columns,
sort=node_cfg.get('updated_at'),
dist=node_cfg.get('unique_key'))
sort='dbt_updated_at',
dist='scd_id')

# TODO move this to inject_runtime_config, generate archive SQL
# in wrap step. can't do this right now because we actually need
Expand Down
10 changes: 5 additions & 5 deletions dbt/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,9 @@ def wrap(self, opts):
{% for col in get_columns_in_table(source_schema, source_table) %}
"{{ col.name }}" {% if not loop.last %},{% endif %}
{% endfor %},
"{{ updated_at }}" as "dbt_updated_at",
"{{ unique_key }}" as "dbt_pk",
"{{ updated_at }}" as "valid_from",
{{ updated_at }} as "dbt_updated_at",
{{ unique_key }} as "dbt_pk",
{{ updated_at }} as "valid_from",
null::timestamp as "tmp_valid_to"
from "{{ source_schema }}"."{{ source_table }}"

Expand All @@ -134,8 +134,8 @@ def wrap(self, opts):
{% for col in get_columns_in_table(source_schema, source_table) %}
"{{ col.name }}" {% if not loop.last %},{% endif %}
{% endfor %},
"{{ updated_at }}" as "dbt_updated_at",
"{{ unique_key }}" as "dbt_pk",
{{ updated_at }} as "dbt_updated_at",
{{ unique_key }} as "dbt_pk",
"valid_from",
"valid_to" as "tmp_valid_to"
from "{{ target_schema }}"."{{ target_table }}"
Expand Down
2 changes: 1 addition & 1 deletion test/integration/004_simple_archive_test/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,5 @@ select
"updated_at" as valid_from,
null::timestamp as valid_to,
"updated_at" as dbt_updated_at,
md5("id" || '|' || "updated_at"::text) as scd_id
md5("id" || '-' || "first_name" || '|' || "updated_at"::text) as scd_id
from "simple_archive_004"."seed";
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def project_config(self):
"source_table": "seed",
"target_table": "archive_actual",
"updated_at": "updated_at",
"unique_key": "id"
"unique_key": "id || '-' || first_name"
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions test/integration/004_simple_archive_test/update.sql
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ select
"updated_at" as "valid_from",
null::timestamp as "valid_to",
"updated_at" as "dbt_updated_at",
md5("id" || '|' || "updated_at"::text) as "scd_id"
md5("id" || '-' || "first_name" || '|' || "updated_at"::text) as "scd_id"
from "simple_archive_004"."seed"
where "id" >= 10 and "id" <= 20;

Expand Down Expand Up @@ -72,6 +72,6 @@ select
"updated_at" as "valid_from",
null::timestamp as "valid_to",
"updated_at" as "dbt_updated_at",
md5("id" || '|' || "updated_at"::text) as "scd_id"
md5("id" || '-' || "first_name" || '|' || "updated_at"::text) as "scd_id"
from "simple_archive_004"."seed"
where "id" > 20;