|  | 
|  | 1 | +# Copyright (c) 2023, Crate.io Inc. | 
|  | 2 | +# Distributed under the terms of the AGPLv3 license, see LICENSE. | 
|  | 3 | +import dataclasses | 
|  | 4 | +import os | 
|  | 5 | +import typing as t | 
|  | 6 | + | 
|  | 7 | +from sqlalchemy_cratedb.support import quote_relation_name | 
|  | 8 | + | 
|  | 9 | +from cratedb_toolkit.model import DatabaseAddress, TableAddress | 
|  | 10 | + | 
|  | 11 | + | 
|  | 12 | +@dataclasses.dataclass | 
|  | 13 | +class MaterializedView: | 
|  | 14 | +    """ | 
|  | 15 | +    Manage the database representation of a "materialized view" entity. | 
|  | 16 | +
 | 
|  | 17 | +    This layout has to be synchronized with the corresponding table definition | 
|  | 18 | +    per SQL DDL statement within `schema.sql`. | 
|  | 19 | +    """ | 
|  | 20 | + | 
|  | 21 | +    table_schema: t.Optional[str] = dataclasses.field( | 
|  | 22 | +        default=None, | 
|  | 23 | +        metadata={"help": "The target table schema"}, | 
|  | 24 | +    ) | 
|  | 25 | +    table_name: t.Optional[str] = dataclasses.field( | 
|  | 26 | +        default=None, | 
|  | 27 | +        metadata={"help": "The target table name"}, | 
|  | 28 | +    ) | 
|  | 29 | +    sql: t.Optional[str] = dataclasses.field( | 
|  | 30 | +        default=None, | 
|  | 31 | +        metadata={"help": "The SQL statement defining the emulated materialized view"}, | 
|  | 32 | +    ) | 
|  | 33 | + | 
|  | 34 | +    id: t.Optional[str] = dataclasses.field(  # noqa: A003 | 
|  | 35 | +        default=None, | 
|  | 36 | +        metadata={"help": "The materialized view identifier"}, | 
|  | 37 | +    ) | 
|  | 38 | + | 
|  | 39 | +    @property | 
|  | 40 | +    def table_fullname(self) -> str: | 
|  | 41 | +        return quote_relation_name(f"{self.table_schema}.{self.table_name}") | 
|  | 42 | + | 
|  | 43 | +    @property | 
|  | 44 | +    def staging_table_fullname(self) -> str: | 
|  | 45 | +        return quote_relation_name(f"{self.table_schema}-staging.{self.table_name}") | 
|  | 46 | + | 
|  | 47 | +    @classmethod | 
|  | 48 | +    def from_record(cls, record) -> "MaterializedView": | 
|  | 49 | +        return cls(**record) | 
|  | 50 | + | 
|  | 51 | +    def to_storage_dict(self, identifier: t.Optional[str] = None) -> t.Dict[str, str]: | 
|  | 52 | +        """ | 
|  | 53 | +        Return representation suitable for storing into a database table using SQLAlchemy. | 
|  | 54 | +
 | 
|  | 55 | +        Args: | 
|  | 56 | +            identifier: If provided, this will override any existing id in the instance. | 
|  | 57 | +        """ | 
|  | 58 | + | 
|  | 59 | +        # Serialize to dictionary. | 
|  | 60 | +        data = dataclasses.asdict(self) | 
|  | 61 | + | 
|  | 62 | +        # Optionally add identifier. | 
|  | 63 | +        if identifier is not None: | 
|  | 64 | +            # Explicitly override any existing id. | 
|  | 65 | +            data["id"] = identifier | 
|  | 66 | + | 
|  | 67 | +        return data | 
|  | 68 | + | 
|  | 69 | + | 
|  | 70 | +def default_table_address(): | 
|  | 71 | +    """ | 
|  | 72 | +    The default address of the materialized view management table. | 
|  | 73 | +    """ | 
|  | 74 | +    schema = os.environ.get("CRATEDB_EXT_SCHEMA", "ext") | 
|  | 75 | +    return TableAddress(schema=schema, table="materialized_view") | 
|  | 76 | + | 
|  | 77 | + | 
|  | 78 | +@dataclasses.dataclass | 
|  | 79 | +class MaterializedViewSettings: | 
|  | 80 | +    """ | 
|  | 81 | +    Bundle all configuration and runtime settings. | 
|  | 82 | +    """ | 
|  | 83 | + | 
|  | 84 | +    # Database connection URI. | 
|  | 85 | +    database: DatabaseAddress = dataclasses.field( | 
|  | 86 | +        default_factory=lambda: DatabaseAddress.from_string("crate://localhost/") | 
|  | 87 | +    ) | 
|  | 88 | + | 
|  | 89 | +    # The address of the materialized view table. | 
|  | 90 | +    materialized_table: TableAddress = dataclasses.field(default_factory=default_table_address) | 
|  | 91 | + | 
|  | 92 | +    # Only pretend to invoke statements. | 
|  | 93 | +    dry_run: t.Optional[bool] = False | 
|  | 94 | + | 
|  | 95 | +    def to_dict(self): | 
|  | 96 | +        data = dataclasses.asdict(self) | 
|  | 97 | +        data["materialized_table"] = self.materialized_table | 
|  | 98 | +        return data | 
0 commit comments