44import re
55import typing as t
66import zlib
7+ import logging
78
89from pydantic import Field
910from pydantic .functional_validators import BeforeValidator
1920 TableNamingConvention ,
2021 VirtualEnvironmentMode ,
2122)
22- from sqlmesh .core .config .base import BaseConfig , UpdateStrategy
23+ from sqlmesh .core .config .base import BaseConfig , UpdateStrategy , DbtConfigInfo
2324from sqlmesh .core .config .common import variables_validator , compile_regex_mapping
2425from sqlmesh .core .config .connection import (
2526 ConnectionConfig ,
4950from sqlmesh .utils .errors import ConfigError
5051from sqlmesh .utils .pydantic import model_validator
5152
53+ logger = logging .getLogger (__name__ )
54+
5255
5356def validate_no_past_ttl (v : str ) -> str :
5457 current_time = now ()
@@ -96,6 +99,8 @@ class Config(BaseConfig):
9699 default_test_connection: The default connection to use for tests if one is not specified in a gateway.
97100 default_scheduler: The default scheduler configuration to use if one is not specified in a gateway.
98101 default_gateway: The default gateway.
102+ state_schema_naming_pattern: A pattern supporting variable substitutions to determine the state schema name, rather than just using 'sqlmesh'.
103+ Only applies when the state schema is not explicitly set in the gateway config
99104 notification_targets: The notification targets to use.
100105 project: The project name of this config. Used for multi-repo setups.
101106 snapshot_ttl: The period of time that a model snapshot that is not a part of any environment should exist before being deleted.
@@ -128,6 +133,7 @@ class Config(BaseConfig):
128133 before_all: SQL statements or macros to be executed at the start of the `sqlmesh plan` and `sqlmesh run` commands.
129134 after_all: SQL statements or macros to be executed at the end of the `sqlmesh plan` and `sqlmesh run` commands.
130135 cache_dir: The directory to store the SQLMesh cache. Defaults to .cache in the project folder.
136+ dbt_config_info: Dbt-specific properties (such as profile and target) for dbt projects loaded by the dbt loader
131137 """
132138
133139 gateways : GatewayDict = {"" : GatewayConfig ()}
@@ -137,6 +143,7 @@ class Config(BaseConfig):
137143 )
138144 default_scheduler : SchedulerConfig = BuiltInSchedulerConfig ()
139145 default_gateway : str = ""
146+ state_schema_naming_pattern : t .Optional [str ] = None
140147 notification_targets : t .List [NotificationTarget ] = []
141148 project : str = ""
142149 snapshot_ttl : NoPastTTLString = c .DEFAULT_SNAPSHOT_TTL
@@ -173,6 +180,7 @@ class Config(BaseConfig):
173180 linter : LinterConfig = LinterConfig ()
174181 janitor : JanitorConfig = JanitorConfig ()
175182 cache_dir : t .Optional [str ] = None
183+ dbt_config_info : t .Optional [DbtConfigInfo ] = None
176184
177185 _FIELD_UPDATE_STRATEGY : t .ClassVar [t .Dict [str , UpdateStrategy ]] = {
178186 "gateways" : UpdateStrategy .NESTED_UPDATE ,
@@ -344,8 +352,27 @@ def get_test_connection(
344352 def get_scheduler (self , gateway_name : t .Optional [str ] = None ) -> SchedulerConfig :
345353 return self .get_gateway (gateway_name ).scheduler or self .default_scheduler
346354
347- def get_state_schema (self , gateway_name : t .Optional [str ] = None ) -> t .Optional [str ]:
348- return self .get_gateway (gateway_name ).state_schema
355+ def get_state_schema (self , gateway_name : t .Optional [str ] = None ) -> str :
356+ state_schema = self .get_gateway (gateway_name ).state_schema
357+
358+ if state_schema is None and self .state_schema_naming_pattern :
359+ substitutions = {}
360+ if dbt := self .dbt_config_info :
361+ # TODO: keeping this simple for now rather than trying to set up a Jinja or SQLMesh Macro rendering context
362+ substitutions .update (
363+ {
364+ "@{dbt_profile_name}" : dbt .profile_name ,
365+ # TODO @iaroslav: what was the problem with using target name instead of the default schema name again?
366+ "@{dbt_target_name}" : dbt .target_name ,
367+ }
368+ )
369+ state_schema = self .state_schema_naming_pattern
370+ for pattern , value in substitutions .items ():
371+ state_schema = state_schema .replace (pattern , value )
372+
373+ logger .info ("Inferring state schema: %s" , state_schema )
374+
375+ return state_schema or c .SQLMESH
349376
350377 @property
351378 def default_gateway_name (self ) -> str :
0 commit comments