Skip to content

Commit

Permalink
Allow generic snapshot definitions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacob Beck committed Jul 11, 2019
1 parent ce6c9da commit 32c94f9
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions core/dbt/contracts/graph/parsed.py
Original file line number Diff line number Diff line change
Expand Up @@ -222,13 +222,21 @@ def __init__(
target_schema: str,
**kwargs
) -> None:

self.target_database = target_database
self.target_schema = target_schema
self.unique_key = unique_key
super().__init__(**kwargs)


@dataclass(init=False)
class GenericSnapshotConfig(_SnapshotConfig):
strategy: str

def __init__(self, strategy: str, **kwargs) -> None:
self.strategy = strategy
super().__init__(**kwargs)


@dataclass(init=False)
class TimestampSnapshotConfig(_SnapshotConfig):
strategy: TimestampStrategy
Expand Down Expand Up @@ -275,7 +283,9 @@ class IntermediateSnapshotNode(ParsedNode):


def _create_if_else_chain(
key: str, criteria: List[Tuple[str, Type[JsonSchemaMixin]]]
key: str,
criteria: List[Tuple[str, Type[JsonSchemaMixin]]],
default: Type[JsonSchemaMixin]
) -> dict:
"""Mutate a given schema key that contains a 'oneOf' to instead be an
'if-then-else' chain. This results is much better/more consistent errors
Expand All @@ -291,15 +301,18 @@ def _create_if_else_chain(
schema['then'] = then_clause.json_schema()
schema['else'] = {}
schema = schema['else']
schema['additionalProperties'] = False
schema['required'] = ['invalid']
schema['properties'] = default.json_schema()
return result


@dataclass
class ParsedSnapshotNode(ParsedNode):
resource_type: SnapshotType
config: Union[CheckSnapshotConfig, TimestampSnapshotConfig]
config: Union[
CheckSnapshotConfig,
TimestampSnapshotConfig,
GenericSnapshotConfig,
]

@classmethod
def json_schema(cls):
Expand All @@ -312,7 +325,7 @@ def json_schema(cls):
]

schema['properties']['config'] = _create_if_else_chain(
'strategy', configs
'strategy', configs, GenericSnapshotConfig
)
return schema

Expand Down

0 comments on commit 32c94f9

Please sign in to comment.