Generate a DDL script that clones object_ids and dependencies that live in the same cluster into a different cluster.
(
ddl_out: TextIO,
cmp_out: TextIO,
database: str,
schema: str,
cluster: str,
object_ids: list[str],
db_port: int,
db_host: str,
db_user: str,
db_pass: str | None,
db_require_ssl: bool,
mzfmt: bool,
)
| 25 | |
| 26 | |
| 27 | def defs( |
| 28 | ddl_out: TextIO, |
| 29 | cmp_out: TextIO, |
| 30 | database: str, |
| 31 | schema: str, |
| 32 | cluster: str, |
| 33 | object_ids: list[str], |
| 34 | db_port: int, |
| 35 | db_host: str, |
| 36 | db_user: str, |
| 37 | db_pass: str | None, |
| 38 | db_require_ssl: bool, |
| 39 | mzfmt: bool, |
| 40 | ) -> None: |
| 41 | """ |
| 42 | Generate a DDL script that clones object_ids and dependencies that live in |
| 43 | the same cluster into a different cluster. |
| 44 | """ |
| 45 | |
| 46 | database_new = sql.identifier(database, True) |
| 47 | schema_new = sql.identifier(schema, True) |
| 48 | cluster_new = sql.identifier(cluster, True) |
| 49 | |
| 50 | with closing( |
| 51 | sql.Database( |
| 52 | port=db_port, |
| 53 | host=db_host, |
| 54 | user=db_user, |
| 55 | database=None, |
| 56 | password=db_pass, |
| 57 | require_ssl=db_require_ssl, |
| 58 | ) |
| 59 | ) as db: |
| 60 | output_template = string.Template(textwrap.dedent(""" |
| 61 | -- original id: $id |
| 62 | $create_sql |
| 63 | """).lstrip()) |
| 64 | |
| 65 | # Extract dependencies |
| 66 | # -------------------- |
| 67 | |
| 68 | if len(object_ids) == 0: |
| 69 | msg = "At least one object_id necessary" |
| 70 | raise ValueError(msg) |
| 71 | |
| 72 | old_clusters = list(db.object_clusters(object_ids)) |
| 73 | if len(old_clusters) != 1: |
| 74 | msg = f"Cannot find unique old cluster for object ids: {object_ids}" |
| 75 | raise ValueError(msg) |
| 76 | |
| 77 | [cluster_old] = old_clusters |
| 78 | |
| 79 | if cluster_old["name"] == cluster: |
| 80 | msg = f"Old and new clusters can't have the same name: {cluster}" |
| 81 | raise ValueError(msg) |
| 82 | |
| 83 | # Replacement pattern for clusters. |
| 84 | cluster_str_old = f"IN CLUSTER {sql.identifier(cluster_old['name'], True)}" |
nothing calls this directly
no test coverage detected