Extract CREATE statements for selected catalog items. Processes only items that match the ILIKE pattern defined by the parameter triple (DATABASE, SCHEMA, NAME).
(
target: Path,
database: str,
schema: str,
name: str,
db_port: int,
db_host: str,
db_user: str,
db_pass: str | None,
db_require_ssl: bool,
mzfmt: bool,
)
| 37 | |
| 38 | |
| 39 | def defs( |
| 40 | target: Path, |
| 41 | database: str, |
| 42 | schema: str, |
| 43 | name: str, |
| 44 | db_port: int, |
| 45 | db_host: str, |
| 46 | db_user: str, |
| 47 | db_pass: str | None, |
| 48 | db_require_ssl: bool, |
| 49 | mzfmt: bool, |
| 50 | ) -> None: |
| 51 | """ |
| 52 | Extract CREATE statements for selected catalog items. |
| 53 | |
| 54 | Processes only items that match the ILIKE pattern defined by the parameter |
| 55 | triple (DATABASE, SCHEMA, NAME). |
| 56 | """ |
| 57 | |
| 58 | # Ensure that the target dir exists. |
| 59 | target.mkdir(parents=True, exist_ok=True) |
| 60 | |
| 61 | with closing( |
| 62 | sql.Database( |
| 63 | port=db_port, |
| 64 | host=db_host, |
| 65 | user=db_user, |
| 66 | database=None, |
| 67 | password=db_pass, |
| 68 | require_ssl=db_require_ssl, |
| 69 | ) |
| 70 | ) as db: |
| 71 | output_template = string.Template(textwrap.dedent(""" |
| 72 | -- id: $id |
| 73 | -- oid: $oid |
| 74 | $create_sql |
| 75 | """).lstrip()) |
| 76 | |
| 77 | # Extract materialized view definitions |
| 78 | # ------------------------------------- |
| 79 | |
| 80 | for item in db.catalog_items(database, schema, name, system=False): |
| 81 | item_database = sql.identifier(item["database"]) |
| 82 | item_schema = sql.identifier(item["schema"]) |
| 83 | item_name = sql.identifier(item["name"]) |
| 84 | fqname = f"{item_database}.{item_schema}.{item_name}" |
| 85 | |
| 86 | try: |
| 87 | item_type = ItemType(item["type"]) |
| 88 | except ValueError: |
| 89 | warn(f"Unsupported item type `{item['type']}` for {fqname}") |
| 90 | continue |
| 91 | |
| 92 | create_file = CreateFile( |
| 93 | database=item["database"], |
| 94 | schema=item["schema"], |
| 95 | name=item["name"], |
| 96 | item_type=item_type, |
nothing calls this directly
no test coverage detected