|
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, field_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. |
|
0 commit comments