|
6 | 6 | import zlib |
7 | 7 |
|
8 | 8 | from pydantic import Field |
| 9 | +from pydantic.functional_validators import BeforeValidator |
9 | 10 | from sqlglot import exp |
10 | 11 | from sqlglot.helper import first |
11 | 12 | from sqlglot.optimizer.normalize_identifiers import normalize_identifiers |
|
45 | 46 | from sqlmesh.utils.errors import ConfigError |
46 | 47 | from sqlmesh.utils.pydantic import model_validator |
47 | 48 |
|
| 49 | + |
| 50 | +def validate_no_past_ttl(v: str) -> str: |
| 51 | + current_time = now() |
| 52 | + if to_timestamp(v, relative_base=current_time) < to_timestamp(current_time): |
| 53 | + raise ValueError( |
| 54 | + f"TTL '{v}' is in the past. Please specify a relative time in the future. Ex: `in 1 week` instead of `1 week`." |
| 55 | + ) |
| 56 | + return v |
| 57 | + |
| 58 | + |
| 59 | +def gateways_ensure_dict(value: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: |
| 60 | + try: |
| 61 | + if not isinstance(value, GatewayConfig): |
| 62 | + GatewayConfig.parse_obj(value) |
| 63 | + return {"": value} |
| 64 | + except Exception: |
| 65 | + return value |
| 66 | + |
| 67 | + |
| 68 | +def validate_regex_key_dict(value: t.Dict[str | re.Pattern, t.Any]) -> t.Dict[re.Pattern, t.Any]: |
| 69 | + return compile_regex_mapping(value) |
| 70 | + |
| 71 | + |
48 | 72 | if t.TYPE_CHECKING: |
49 | 73 | from sqlmesh.core._typing import Self |
50 | 74 |
|
| 75 | + NoPastTTLString = str |
| 76 | + GatewayDict = t.Dict[str, GatewayConfig] |
| 77 | + RegexKeyDict = t.Dict[re.Pattern, str] |
| 78 | +else: |
| 79 | + NoPastTTLString = t.Annotated[str, BeforeValidator(validate_no_past_ttl)] |
| 80 | + GatewayDict = t.Annotated[t.Dict[str, GatewayConfig], BeforeValidator(gateways_ensure_dict)] |
| 81 | + RegexKeyDict = t.Annotated[t.Dict[re.Pattern, str], BeforeValidator(validate_regex_key_dict)] |
| 82 | + |
51 | 83 |
|
52 | 84 | class Config(BaseConfig): |
53 | 85 | """An object used by a Context to configure your SQLMesh project. |
@@ -286,37 +318,3 @@ def dialect(self) -> t.Optional[str]: |
286 | 318 | @property |
287 | 319 | def fingerprint(self) -> str: |
288 | 320 | return str(zlib.crc32(pickle.dumps(self.dict(exclude={"loader", "notification_targets"})))) |
289 | | - |
290 | | - |
291 | | -def validate_no_past_ttl(v: str) -> str: |
292 | | - current_time = now() |
293 | | - if to_timestamp(v, relative_base=current_time) < to_timestamp(current_time): |
294 | | - raise ValueError( |
295 | | - f"TTL '{v}' is in the past. Please specify a relative time in the future. Ex: `in 1 week` instead of `1 week`." |
296 | | - ) |
297 | | - return v |
298 | | - |
299 | | - |
300 | | -def gateways_ensure_dict(value: t.Dict[str, t.Any]) -> t.Dict[str, t.Any]: |
301 | | - try: |
302 | | - if not isinstance(value, GatewayConfig): |
303 | | - GatewayConfig.parse_obj(value) |
304 | | - return {"": value} |
305 | | - except Exception: |
306 | | - return value |
307 | | - |
308 | | - |
309 | | -def validate_regex_key_dict(value: t.Dict[str | re.Pattern, t.Any]) -> t.Dict[re.Pattern, t.Any]: |
310 | | - return compile_regex_mapping(value) |
311 | | - |
312 | | - |
313 | | -if t.TYPE_CHECKING: |
314 | | - NoPastTTLString = str |
315 | | - GatewayDict = t.Dict[str, GatewayConfig] |
316 | | - RegexKeyDict = t.Dict[re.Pattern, str] |
317 | | -else: |
318 | | - from pydantic.functional_validators import BeforeValidator |
319 | | - |
320 | | - NoPastTTLString = t.Annotated[str, BeforeValidator(validate_no_past_ttl)] |
321 | | - GatewayDict = t.Annotated[t.Dict[str, GatewayConfig], BeforeValidator(gateways_ensure_dict)] |
322 | | - RegexKeyDict = t.Annotated[t.Dict[re.Pattern, str], BeforeValidator(validate_regex_key_dict)] |
0 commit comments