-
Notifications
You must be signed in to change notification settings - Fork 60
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
[Bug] Fix scenario where dbt attempts to add existing columns to relations when using the SDK for column metadata #919
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
8106bb1
add skeleton test case
mikealfare cb62bbb
changie
mikealfare 31a3c2d
Merge branch 'main' into pg-catalog-migration/fix-adding-existing-col…
mikealfare b0c1a05
alias dtypes back to legacy method names
mikealfare ed27568
Merge remote-tracking branch 'origin/pg-catalog-migration/fix-adding-…
mikealfare d92c9ad
add a test case for both settings of the flag
mikealfare bef7bcc
instead of testing schema updates, test data updates
mikealfare b946cdd
instead of testing schema updates, test data updates
mikealfare 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: Fix scenario where dbt attempts to add existing columns to relations when using the SDK for column metadata | ||
time: 2024-09-27T17:17:25.584838-04:00 | ||
custom: | ||
Author: mikealfare | ||
Issue: "914" |
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
Empty file.
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
92 changes: 92 additions & 0 deletions
92
tests/functional/columns_in_relation_tests/test_incremental_updates.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,92 @@ | ||
from dbt.tests.util import run_dbt | ||
import pytest | ||
|
||
from tests.functional.utils import update_model | ||
|
||
|
||
SEED = """ | ||
id,col7,col6,occurred_at | ||
1,a,green,'2024-01-01' | ||
2,b,green,'2024-01-01' | ||
3,c,green,'2024-01-01' | ||
""".strip() | ||
|
||
|
||
SEED_UPDATES = """ | ||
id,col7,col6,occurred_at | ||
4,b,red,'2024-02-01' | ||
5,c,red,'2024-02-01' | ||
6,c,blue,'2024-03-01' | ||
""".strip() | ||
|
||
|
||
MODEL = """ | ||
{{ config(materialized='incremental') }} | ||
select * from {{ ref('my_seed') }} | ||
where occurred_at::timestamptz >= '2024-01-01'::timestamptz | ||
and occurred_at::timestamptz < '2024-02-01'::timestamptz | ||
""" | ||
|
||
|
||
MODEL_UPDATES = """ | ||
{{ config(materialized='incremental') }} | ||
select * from {{ ref('my_seed') }} | ||
where occurred_at::timestamptz >= '2024-02-01'::timestamptz | ||
and occurred_at::timestamptz < '2024-03-01'::timestamptz | ||
""" | ||
|
||
|
||
class TestIncrementalUpdates: | ||
""" | ||
This addresses: https://github.com/dbt-labs/dbt-redshift/issues/914 | ||
|
||
We test it with the `restrict_direct_pg_catalog_access` flag both off and on since the bug | ||
only emerges when the flag is on (the former is a control). | ||
""" | ||
|
||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return {"flags": {"restrict_direct_pg_catalog_access": False}} | ||
|
||
@pytest.fixture(scope="class") | ||
def seeds(self): | ||
return {"my_seed.csv": SEED, "my_seed_updates.csv": SEED_UPDATES} | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"my_model.sql": MODEL} | ||
|
||
def test_columns_in_relation(self, project): | ||
# create the initial table | ||
run_dbt(["seed"]) | ||
run_dbt(["run"]) | ||
|
||
# verify the table starts with the initial records | ||
sql = ( | ||
f"select count(*) as row_count from {project.database}.{project.test_schema}.my_model" | ||
) | ||
assert project.run_sql(sql, fetch="one")[0] == 3 | ||
|
||
# move forward in time and pick up records in the source that should generate an incremental | ||
sql = f""" | ||
insert into {project.database}.{project.test_schema}.my_seed | ||
select * from {project.database}.{project.test_schema}.my_seed_updates | ||
""" | ||
project.run_sql(sql) | ||
update_model(project, "my_model", MODEL_UPDATES) | ||
|
||
# apply the incremental | ||
run_dbt(["run"]) | ||
|
||
# verify the new records made it into the table | ||
sql = ( | ||
f"select count(*) as row_count from {project.database}.{project.test_schema}.my_model" | ||
) | ||
assert project.run_sql(sql, fetch="one")[0] == 5 | ||
|
||
|
||
class TestIncrementalUpdatesFlagOn(TestIncrementalUpdates): | ||
|
||
@pytest.fixture(scope="class") | ||
def project_config_update(self): | ||
return {"flags": {"restrict_direct_pg_catalog_access": True}} |
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,8 @@ | ||
from dbt.tests.util import get_model_file, relation_from_name, set_model_file | ||
|
||
|
||
def update_model(project, name: str, model: str) -> str: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was copy/pasted from other adapters. We should move it to |
||
relation = relation_from_name(project.adapter, name) | ||
original_model = get_model_file(project, relation) | ||
set_model_file(project, relation, model) | ||
return original_model |
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.
Since we are aliasing dtypes back to the original names, we don't need to specify the expected column dtypes based on whether the flag is enabled.