-
Notifications
You must be signed in to change notification settings - Fork 111
Implements dbt_sqlserver_use_dbt_transactions behavior flag
#710
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
Open
axellpadilla
wants to merge
1
commit into
master
Choose a base branch
from
fix/708-dbt-transactions
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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
This file contains hidden or 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,176 @@ | ||
| import pytest | ||
|
|
||
| from dbt.tests.util import run_dbt | ||
|
|
||
|
|
||
| class BaseTransactionsEnabled: | ||
| @pytest.fixture(scope="class") | ||
| def project_config_update(self): | ||
| return {"flags": {"dbt_sqlserver_use_dbt_transactions": True}} | ||
|
|
||
|
|
||
| class TestTableMaterializationTransactionsOn(BaseTransactionsEnabled): | ||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return { | ||
| "table_model.sql": """ | ||
| {{ config(materialized='table') }} | ||
| select 1 as id, 'hello' as name | ||
| """, | ||
| } | ||
|
|
||
| def test_table_materialization(self, project): | ||
| results = run_dbt(["run"]) | ||
| assert len(results) == 1 | ||
|
|
||
| rows = project.run_sql("select id, name from {schema}.table_model", fetch="all") | ||
| assert len(rows) == 1 | ||
| assert rows[0][0] == 1 | ||
| assert rows[0][1] == "hello" | ||
|
|
||
|
|
||
| class TestViewMaterializationTransactionsOn(BaseTransactionsEnabled): | ||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return { | ||
| "view_model.sql": """ | ||
| {{ config(materialized='view') }} | ||
| select 42 as answer | ||
| """, | ||
| } | ||
|
|
||
| def test_view_materialization(self, project): | ||
| results = run_dbt(["run"]) | ||
| assert len(results) == 1 | ||
|
|
||
| rows = project.run_sql("select answer from {schema}.view_model", fetch="all") | ||
| assert len(rows) == 1 | ||
| assert rows[0][0] == 42 | ||
|
|
||
|
|
||
| class TestIncrementalMaterializationTransactionsOn(BaseTransactionsEnabled): | ||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return { | ||
| "incremental_model.sql": """ | ||
| {{ config(materialized='incremental', unique_key='id') }} | ||
| select 1 as id, 'first' as value | ||
| {% if is_incremental() %} | ||
| union all | ||
| select 2 as id, 'second' as value | ||
| {% endif %} | ||
| """, | ||
| } | ||
|
|
||
| def test_incremental_materialization(self, project): | ||
| results = run_dbt(["run"]) | ||
| assert len(results) == 1 | ||
|
|
||
| rows = project.run_sql( | ||
| "select count(*) as cnt from {schema}.incremental_model", fetch="one" | ||
| ) | ||
| assert rows[0] == 1 | ||
|
|
||
| results = run_dbt(["run"]) | ||
| assert len(results) == 1 | ||
|
|
||
| rows = project.run_sql( | ||
| "select count(*) as cnt from {schema}.incremental_model", fetch="one" | ||
| ) | ||
| assert rows[0] == 2 | ||
|
|
||
|
|
||
| class BaseFailingModelWithSideEffect: | ||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return { | ||
| "failing_model.sql": """ | ||
| {{ config( | ||
| materialized='table', | ||
| pre_hook=[ | ||
| "INSERT INTO {{ this.schema }}.audit_log " | ||
| "(msg, created_at) VALUES ('from_model', getdate())" | ||
| ] | ||
| ) }} | ||
| select 1/0 as boom | ||
| """, | ||
| } | ||
|
|
||
|
|
||
| class TestRollbackWithoutFlag(BaseFailingModelWithSideEffect): | ||
| @pytest.fixture(scope="class") | ||
| def project_config_update(self): | ||
| return {"flags": {"dbt_sqlserver_use_dbt_transactions": False}} | ||
|
|
||
| @pytest.mark.xfail( | ||
| strict=True, | ||
| reason="Without transactions flag, DML in pre-hooks is auto-committed and not rolled back," | ||
| " remove after migration to always use transactions.", | ||
| ) | ||
| def test_side_effect_rolled_back(self, project): | ||
| project.run_sql("CREATE TABLE {schema}.audit_log (msg varchar(100), created_at datetime)") | ||
| run_dbt(["run", "-m", "failing_model"], expect_pass=False) | ||
| rows = project.run_sql("SELECT COUNT(*) FROM {schema}.audit_log", fetch="one") | ||
| assert rows[0] == 0 | ||
|
|
||
|
|
||
| class TestRollbackWithFlag(BaseFailingModelWithSideEffect): | ||
| @pytest.fixture(scope="class") | ||
| def project_config_update(self): | ||
| return {"flags": {"dbt_sqlserver_use_dbt_transactions": True}} | ||
|
|
||
| def test_side_effect_rolled_back(self, project): | ||
| project.run_sql("CREATE TABLE {schema}.audit_log (msg varchar(100), created_at datetime)") | ||
| run_dbt(["run", "-m", "failing_model"], expect_pass=False) | ||
| rows = project.run_sql("SELECT COUNT(*) FROM {schema}.audit_log", fetch="one") | ||
| assert rows[0] == 0 | ||
|
|
||
|
|
||
| class TestAfterCommitModelHookTransactionsOn(BaseTransactionsEnabled): | ||
| @pytest.fixture(scope="class") | ||
| def project_config_update(self): | ||
| return { | ||
| "flags": {"dbt_sqlserver_use_dbt_transactions": True}, | ||
| "models": { | ||
| "test": { | ||
| "post-hook": [ | ||
| {"sql": "select 1", "transaction": False}, | ||
| ], | ||
| } | ||
| }, | ||
| } | ||
|
|
||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return {"after_commit_hook_model.sql": "select 1 as id"} | ||
|
|
||
| def test_after_commit_post_hook_does_not_double_commit(self, project): | ||
| run_dbt() | ||
|
|
||
|
|
||
| class TestFailedModelThenSuccessTransactionsOn(BaseTransactionsEnabled): | ||
| @pytest.fixture(scope="class") | ||
| def models(self): | ||
| return { | ||
| "good_model.sql": """ | ||
| {{ config(materialized='table') }} | ||
| select 1 as id | ||
| """, | ||
| "bad_model.sql": """ | ||
| {{ config(materialized='table') }} | ||
| select 1/0 as boom | ||
| """, | ||
| } | ||
|
|
||
| def test_failed_then_successful_run(self, project): | ||
| results = run_dbt(["run", "-m", "bad_model"], expect_pass=False) | ||
| assert len(results) == 1 | ||
| assert results[0].status == "error" | ||
|
|
||
| results = run_dbt(["run", "-m", "good_model"]) | ||
| assert len(results) == 1 | ||
| assert results[0].status == "success" | ||
|
|
||
| rows = project.run_sql("select id from {schema}.good_model", fetch="all") | ||
| assert len(rows) == 1 | ||
| assert rows[0][0] == 1 |
This file contains hidden or 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
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.
hooks.sql reads the flag from a different source (flags) than the connection manager (adapter.behavior), and the two agree only while the default is False