Skip to content

Commit 87e81a4

Browse files
feat(bigquery): Add enable_change_history option (#1225)
Co-authored-by: Shuting Zhang <[email protected]>
1 parent 0e5f13e commit 87e81a4

File tree

3 files changed

+102
-0
lines changed

3 files changed

+102
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
kind: Features
2+
body: Add `enable_change_history` option for models on BigQuery.
3+
time: 2025-08-11T19:44:47.222876+02:00
4+
custom:
5+
Author: Robin_CHESNE
6+
Issue: "1206"

dbt-bigquery/src/dbt/adapters/bigquery/impl.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ class BigqueryConfig(AdapterConfig):
121121
intermediate_format: Optional[str] = None
122122
submission_method: Optional[str] = None
123123
notebook_template_id: Optional[str] = None
124+
enable_change_history: Optional[bool] = None
124125

125126

126127
class BigQueryAdapter(BaseAdapter):
@@ -811,6 +812,9 @@ def get_table_options(
811812
if config.get("partition_expiration_days") is not None:
812813
opts["partition_expiration_days"] = config.get("partition_expiration_days")
813814

815+
if config.get("enable_change_history") is not None:
816+
opts["enable_change_history"] = config.get("enable_change_history")
817+
814818
relation_config = getattr(config, "model", None)
815819
if not temporary and (
816820
catalog_relation := self.build_catalog_relation(relation_config)
@@ -827,6 +831,10 @@ def get_table_options(
827831
@available.parse(lambda *a, **k: {})
828832
def get_view_options(self, config: Dict[str, Any], node: Dict[str, Any]) -> Dict[str, Any]:
829833
opts = self.get_common_options(config, node)
834+
if config.get("enable_change_history"):
835+
raise dbt_common.exceptions.DbtRuntimeError(
836+
"`enable_change_history` is not supported for views on BigQuery."
837+
)
830838
return opts
831839

832840
@available.parse(lambda *a, **k: True)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
import pytest
2+
from dbt.tests.util import run_dbt, run_dbt_and_capture
3+
import dbt.exceptions
4+
5+
incremental_change_history_enabled_model = """
6+
{{
7+
config(
8+
materialized='incremental',
9+
enable_change_history=True
10+
)
11+
}}
12+
select 1 as id
13+
"""
14+
15+
incremental_change_history_disabled_model = """
16+
{{
17+
config(
18+
materialized='incremental',
19+
enable_change_history=False
20+
)
21+
}}
22+
select 1 as id
23+
"""
24+
25+
table_change_history_enabled_model = """
26+
{{
27+
config(
28+
materialized='table',
29+
enable_change_history=True
30+
)
31+
}}
32+
select 1 as id
33+
"""
34+
35+
view_change_history_enabled_model = """
36+
{{
37+
config(
38+
materialized='view',
39+
enable_change_history=True
40+
)
41+
}}
42+
select 1 as id
43+
"""
44+
45+
46+
class TestChangeHistorySuccessful:
47+
@pytest.fixture(scope="class")
48+
def models(self):
49+
return {
50+
"incremental_change_history_enabled.sql": incremental_change_history_enabled_model,
51+
"incremental_change_history_disabled.sql": incremental_change_history_disabled_model,
52+
"table_change_history_enabled.sql": table_change_history_enabled_model,
53+
}
54+
55+
def get_is_change_history_enabled(self, project, table_name):
56+
sql = f"""
57+
select is_change_history_enabled
58+
from `{project.database}.{project.test_schema}.INFORMATION_SCHEMA.TABLES`
59+
where table_name = '{table_name}'
60+
"""
61+
results = project.run_sql(sql, fetch="one")
62+
return results[0] if results else None
63+
64+
def test_change_history_options(self, project):
65+
results = run_dbt(["run"])
66+
assert len(results) == 3
67+
68+
assert (
69+
self.get_is_change_history_enabled(project, "incremental_change_history_enabled")
70+
== "YES"
71+
)
72+
assert (
73+
self.get_is_change_history_enabled(project, "incremental_change_history_disabled")
74+
== "NO"
75+
)
76+
assert self.get_is_change_history_enabled(project, "table_change_history_enabled") == "YES"
77+
78+
79+
class TestChangeHistoryFail:
80+
@pytest.fixture(scope="class")
81+
def models(self):
82+
return {
83+
"view_with_flag.sql": view_change_history_enabled_model,
84+
}
85+
86+
def test_view_with_flag_fails(self, project):
87+
_, stdout = run_dbt_and_capture(["run", "--select", "view_with_flag"], expect_pass=False)
88+
assert "`enable_change_history` is not supported for views" in stdout

0 commit comments

Comments
 (0)