Skip to content

Commit 8f4382f

Browse files
committed
PR feedback
1 parent 8919c48 commit 8f4382f

File tree

4 files changed

+20
-26
lines changed

4 files changed

+20
-26
lines changed

sqlmesh/core/model/definition.py

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -594,8 +594,7 @@ def _statement_renderer(self, expression: exp.Expression) -> ExpressionRenderer:
594594
python_env=self.python_env,
595595
only_execution_time=False,
596596
default_catalog=self.default_catalog,
597-
model_fqn=self.fqn,
598-
raw_code=self._raw_code,
597+
model=self,
599598
)
600599
return self._statement_renderer_cache[expression_key]
601600

@@ -1306,10 +1305,6 @@ def _is_time_column_in_partitioned_by(self) -> bool:
13061305
def violated_rules_for_query(self) -> t.Dict[type[Rule], t.Any]:
13071306
return {}
13081307

1309-
@property
1310-
def _raw_code(self) -> t.Optional[str]:
1311-
return None
1312-
13131308

13141309
class SqlModel(_Model):
13151310
"""The model definition which relies on a SQL query to fetch the data.
@@ -1578,15 +1573,14 @@ def _query_renderer(self) -> QueryRenderer:
15781573
self.dialect,
15791574
self.macro_definitions,
15801575
schema=self.mapping_schema,
1581-
model_fqn=self.fqn,
15821576
path=self._path,
15831577
jinja_macro_registry=self.jinja_macros,
15841578
python_env=self.python_env,
15851579
only_execution_time=self.kind.only_execution_time,
15861580
default_catalog=self.default_catalog,
15871581
quote_identifiers=not no_quote_identifiers,
15881582
optimize_query=self.optimize_query,
1589-
raw_code=self._raw_code,
1583+
model=self,
15901584
)
15911585

15921586
@property
@@ -1612,11 +1606,6 @@ def violated_rules_for_query(self) -> t.Dict[type[Rule], t.Any]:
16121606
self.render_query()
16131607
return self._query_renderer._violated_rules
16141608

1615-
@property
1616-
def _raw_code(self) -> t.Optional[str]:
1617-
query = self.query
1618-
return query.name if isinstance(query, d.JinjaQuery) else None
1619-
16201609

16211610
class SeedModel(_Model):
16221611
"""The model definition which uses a pre-built static dataset to source the data from.

sqlmesh/core/renderer.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
from sqlglot.dialects.dialect import DialectType
3232

3333
from sqlmesh.core.linter.rule import Rule
34+
from sqlmesh.core.model.definition import _Model
3435
from sqlmesh.core.snapshot import DeployabilityIndex, Snapshot
3536

3637

@@ -50,10 +51,9 @@ def __init__(
5051
schema: t.Optional[t.Dict[str, t.Any]] = None,
5152
default_catalog: t.Optional[str] = None,
5253
quote_identifiers: bool = True,
53-
model_fqn: t.Optional[str] = None,
5454
normalize_identifiers: bool = True,
5555
optimize_query: t.Optional[bool] = True,
56-
raw_code: t.Optional[str] = None,
56+
model: t.Optional[_Model] = None,
5757
):
5858
self._expression = expression
5959
self._dialect = dialect
@@ -67,9 +67,9 @@ def __init__(
6767
self._quote_identifiers = quote_identifiers
6868
self.update_schema({} if schema is None else schema)
6969
self._cache: t.List[t.Optional[exp.Expression]] = []
70-
self._model_fqn = model_fqn
70+
self._model_fqn = model.fqn if model else None
7171
self._optimize_query_flag = optimize_query is not False
72-
self._raw_code = raw_code
72+
self._model = model
7373

7474
def update_schema(self, schema: t.Dict[str, t.Any]) -> None:
7575
self.schema = d.normalize_mapping_schema(schema, dialect=self._dialect)
@@ -206,7 +206,7 @@ def _resolve_table(table: str | exp.Table) -> str:
206206
"default_catalog": self._default_catalog,
207207
"runtime_stage": runtime_stage.value,
208208
"resolve_table": _resolve_table,
209-
"raw_code": self._raw_code,
209+
"model_instance": self._model,
210210
}
211211

212212
if this_model:

sqlmesh/dbt/builtin.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
from sqlmesh.core.console import get_console
1818
from sqlmesh.core.engine_adapter import EngineAdapter
19+
from sqlmesh.core.model.definition import SqlModel
1920
from sqlmesh.core.snapshot.definition import DeployabilityIndex
2021
from sqlmesh.dbt.adapter import BaseAdapter, ParsetimeAdapter, RuntimeAdapter
2122
from sqlmesh.dbt.common import RAW_CODE_KEY
@@ -485,8 +486,12 @@ def create_builtin_globals(
485486
)
486487

487488
if (model := jinja_globals.pop("model", None)) is not None:
488-
raw_code = jinja_globals.pop("raw_code", "")
489-
builtin_globals["model"] = AttributeDict({**model, RAW_CODE_KEY: raw_code})
489+
if isinstance(model_instance := jinja_globals.pop("model_instance", None), SqlModel):
490+
builtin_globals["model"] = AttributeDict(
491+
{**model, RAW_CODE_KEY: model_instance.query.name}
492+
)
493+
else:
494+
builtin_globals["model"] = AttributeDict(model.copy())
490495

491496
if engine_adapter is not None:
492497
builtin_globals["flags"] = Flags(which="run")

sqlmesh/migrations/v0095_warn_about_dbt_raw_sql_diff.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ def migrate(state_sync, **kwargs): # type: ignore
2525
snapshots_table = f"{schema}.{snapshots_table}"
2626

2727
warning = (
28-
"SQLMesh now includes dbt's {{ config(...) }} blocks in the model's raw SQL when "
29-
"processing dbt models. This change ensures that all model attributes referenced "
30-
"in macros are properly tracked for fingerprinting. As a result, you may see diffs "
31-
"for existing dbt models even though the actual SQL logic hasn't changed. This is "
32-
"a one-time diff that will be resolved after applying a plan. Run 'sqlmesh diff prod' "
33-
"to review any changes, then apply a plan if the diffs look expected."
28+
"SQLMesh detected that it may not be able to fully migrate the state database. This should not impact "
29+
"the migration process, but may result in unexpected changes being reported by the next `sqlmesh plan` "
30+
"command. Please run `sqlmesh diff prod` after the migration has completed, before making any new "
31+
"changes. If any unexpected changes are reported, consider running a forward-only plan to apply these "
32+
"changes and avoid unnecessary backfills: sqlmesh plan prod --forward-only. "
33+
"See https://sqlmesh.readthedocs.io/en/stable/concepts/plans/#forward-only-plans for more details.\n"
3434
)
3535

3636
for (snapshot,) in engine_adapter.fetchall(

0 commit comments

Comments
 (0)