-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
Incremental models with a contract don't need their columns modified #10371
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
dd40127
Reference dbt-adapters branch
gshank b6ad35e
Add test case
gshank 3bba738
Merge branch 'main' into incremental_contract_varchar
gshank f49d790
Changie
gshank b7fb80e
Remove branch from dev-requirements.txt
gshank abd2274
Merge branch 'main' into incremental_contract_varchar
gshank File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
kind: Fixes | ||
body: Do not update varchar column definitions if a contract exists | ||
time: 2024-07-28T22:14:21.67712-04:00 | ||
custom: | ||
Author: gshank | ||
Issue: "10362" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
131 changes: 131 additions & 0 deletions
131
tests/functional/materializations/test_incremental_with_contract.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,131 @@ | ||
import pytest | ||
|
||
from dbt.tests.util import ( | ||
check_relations_equal, | ||
get_relation_columns, | ||
relation_from_name, | ||
run_dbt, | ||
) | ||
|
||
seeds_base_csv = """ | ||
id,name_xxx,some_date | ||
1,Easton,1981-05-20T06:46:51 | ||
2,Lillian,1978-09-03T18:10:33 | ||
3,Jeremiah,1982-03-11T03:59:51 | ||
4,Nolan,1976-05-06T20:21:35 | ||
5,Hannah,1982-06-23T05:41:26 | ||
6,Eleanor,1991-08-10T23:12:21 | ||
7,Lily,1971-03-29T14:58:02 | ||
8,Jonathan,1988-02-26T02:55:24 | ||
9,Adrian,1994-02-09T13:14:23 | ||
10,Nora,1976-03-01T16:51:39 | ||
""".lstrip() | ||
|
||
|
||
seeds_added_csv = ( | ||
seeds_base_csv | ||
+ """ | ||
11,Mateo,2014-09-07T17:04:27 | ||
12,Julian,2000-02-04T11:48:30 | ||
13,Gabriel,2001-07-10T07:32:52 | ||
14,Isaac,2002-11-24T03:22:28 | ||
15,Levi,2009-11-15T11:57:15 | ||
16,Elizabeth,2005-04-09T03:50:11 | ||
17,Grayson,2019-08-06T19:28:17 | ||
18,Dylan,2014-03-01T11:50:41 | ||
19,Jayden,2009-06-06T07:12:49 | ||
20,Luke,2003-12-05T21:42:18 | ||
""".lstrip() | ||
) | ||
|
||
incremental_not_schema_change_sql = """ | ||
{{ config(materialized="incremental", unique_key="user_id_current_time",on_schema_change="sync_all_columns") }} | ||
select | ||
1 || '-' || current_timestamp as user_id_current_time, | ||
{% if is_incremental() %} | ||
'thisis18characters' as platform | ||
{% else %} | ||
'okthisis20characters' as platform | ||
{% endif %} | ||
""" | ||
|
||
incremental_sql = """ | ||
{{ config(materialized="incremental") }} | ||
select * from {{ source('raw', 'seed') }} | ||
{% if is_incremental() %} | ||
where id > (select max(id) from {{ this }}) | ||
{% endif %} | ||
""" | ||
|
||
schema_base_yml = """ | ||
sources: | ||
- name: raw | ||
schema: "{{ target.schema }}" | ||
tables: | ||
- name: seed | ||
identifier: "{{ var('seed_name', 'base') }}" | ||
|
||
models: | ||
- name: incremental | ||
config: | ||
contract: | ||
enforced: true | ||
on_schema_change: append_new_columns | ||
columns: | ||
- name: id | ||
data_type: int | ||
- name: name_xxx | ||
data_type: character varying(10) | ||
- name: some_date | ||
data_type: timestamp | ||
""" | ||
|
||
|
||
class TestIncremental: | ||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return {"name": "incremental"} | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"incremental.sql": incremental_sql, "schema.yml": schema_base_yml} | ||
|
||
@pytest.fixture(scope="class") | ||
def seeds(self): | ||
return {"base.csv": seeds_base_csv, "added.csv": seeds_added_csv} | ||
|
||
def test_incremental(self, project): | ||
# seed command | ||
results = run_dbt(["seed"]) | ||
assert len(results) == 2 | ||
|
||
# base table rowcount | ||
relation = relation_from_name(project.adapter, "base") | ||
result = project.run_sql(f"select count(*) as num_rows from {relation}", fetch="one") | ||
assert result[0] == 10 | ||
|
||
# added table rowcount | ||
relation = relation_from_name(project.adapter, "added") | ||
result = project.run_sql(f"select count(*) as num_rows from {relation}", fetch="one") | ||
assert result[0] == 20 | ||
|
||
# run command | ||
# the "seed_name" var changes the seed identifier in the schema file | ||
results = run_dbt(["run", "--vars", "seed_name: base"]) | ||
assert len(results) == 1 | ||
|
||
# check relations equal | ||
check_relations_equal(project.adapter, ["base", "incremental"]) | ||
|
||
# change seed_name var | ||
# the "seed_name" var changes the seed identifier in the schema file | ||
results = run_dbt(["run", "--debug", "--vars", "seed_name: added"]) | ||
assert len(results) == 1 | ||
|
||
# Error before fix: Changing col type from character varying(10) to character varying(256) in table: | ||
# "dbt"."test<...>_test_incremental_with_contract"."incremental" | ||
columns = get_relation_columns(project.adapter, "incremental") | ||
# [('id', 'integer', None), ('name_xxx', 'character varying', 10), ('some_date', 'timestamp without time zone', None)] | ||
for column in columns: | ||
if column[0] == "name_xxx": | ||
assert column[2] == 10 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reminder to revert this prior to merging.