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

Ensure that same_contract is called for state:modified #7283

Merged
merged 1 commit into from
Apr 6, 2023
Merged
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
6 changes: 6 additions & 0 deletions .changes/unreleased/Fixes-20230405-182927.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Ensure same_contract is called for state:modified
time: 2023-04-05T18:29:27.369084-04:00
custom:
Author: gshank
Issue: "7282"
5 changes: 4 additions & 1 deletion core/dbt/contracts/graph/nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -430,13 +430,16 @@ def same_contents(self, old) -> bool:
if old is None:
return False

# Need to ensure that same_contract is called because it
# could throw an error
same_contract = self.same_contract(old)
return (
self.same_body(old)
and self.same_config(old)
and self.same_persisted_description(old)
and self.same_fqn(old)
and self.same_database_representation(old)
and self.same_contract(old)
and same_contract
and True
)

Expand Down
59 changes: 59 additions & 0 deletions tests/functional/defer_state/test_modified_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,62 @@ def test_changed_contract(self, project):
write_file(schema_yml, "models", "schema.yml")
with pytest.raises(ModelContractError):
results = run_dbt(["run", "--models", "state:modified.contract", "--state", "./state"])


my_model_sql = """
select 1 as id
"""

modified_my_model_sql = """
-- a comment
select 1 as id
"""

my_model_yml = """
models:
- name: my_model
config:
contract:
enforced: true
columns:
- name: id
data_type: int
"""

modified_my_model_yml = """
models:
- name: my_model
config:
contract:
enforced: true
columns:
- name: id
data_type: string
"""


class TestModifiedBodyAndContract:
@pytest.fixture(scope="class")
def models(self):
return {
"my_model.sql": my_model_sql,
"my_model.yml": my_model_yml,
}

def copy_state(self):
if not os.path.exists("state"):
os.makedirs("state")
shutil.copyfile("target/manifest.json", "state/manifest.json")

def test_modified_body_and_contract(self, project):
results = run_dbt(["run"])
assert len(results) == 1
self.copy_state()

# Change both body and contract
write_file(modified_my_model_yml, "models", "my_model.yml")
write_file(modified_my_model_sql, "models", "my_model.sql")

# should raise even without specifying state:modified.contract
with pytest.raises(ModelContractError):
results = run_dbt(["run", "--models", "state:modified", "--state", "./state"])