|
| 1 | +from snowddl.blueprint import SnapshotPolicyBlueprint |
| 2 | +from snowddl.resolver.abc_schema_object_resolver import AbstractSchemaObjectResolver, ResolveResult, ObjectType |
| 3 | + |
| 4 | + |
| 5 | +class SnapshotPolicyResolver(AbstractSchemaObjectResolver): |
| 6 | + skip_on_empty_blueprints = True |
| 7 | + |
| 8 | + def get_object_type(self) -> ObjectType: |
| 9 | + return ObjectType.SNAPSHOT_POLICY |
| 10 | + |
| 11 | + def get_existing_objects_in_schema(self, schema: dict): |
| 12 | + existing_objects = {} |
| 13 | + |
| 14 | + cur = self.engine.execute_meta( |
| 15 | + "SHOW SNAPSHOT POLICIES IN SCHEMA {database:i}.{schema:i}", |
| 16 | + { |
| 17 | + "database": schema["database"], |
| 18 | + "schema": schema["schema"], |
| 19 | + }, |
| 20 | + ) |
| 21 | + |
| 22 | + for r in cur: |
| 23 | + existing_objects[f"{r['database_name']}.{r['schema_name']}.{r['name']}"] = { |
| 24 | + "database": r["database_name"], |
| 25 | + "schema": r["schema_name"], |
| 26 | + "name": r["name"], |
| 27 | + "owner": r["owner"], |
| 28 | + "schedule": r["schedule"], |
| 29 | + "expire_after_days": r["expire_after_days"], |
| 30 | + "comment": r["comment"] if r["comment"] else None, |
| 31 | + } |
| 32 | + |
| 33 | + return existing_objects |
| 34 | + |
| 35 | + def get_blueprints(self): |
| 36 | + return self.config.get_blueprints_by_type(SnapshotPolicyBlueprint) |
| 37 | + |
| 38 | + def create_object(self, bp: SnapshotPolicyBlueprint): |
| 39 | + query = self.engine.query_builder() |
| 40 | + |
| 41 | + query.append( |
| 42 | + "CREATE SNAPSHOT POLICY {full_name:i}", |
| 43 | + { |
| 44 | + "full_name": bp.full_name, |
| 45 | + }, |
| 46 | + ) |
| 47 | + |
| 48 | + if bp.schedule: |
| 49 | + query.append_nl( |
| 50 | + "SCHEDULE = {schedule}", |
| 51 | + { |
| 52 | + "schedule": bp.schedule, |
| 53 | + }, |
| 54 | + ) |
| 55 | + |
| 56 | + if bp.expire_after_days: |
| 57 | + query.append_nl( |
| 58 | + "EXPIRE_AFTER_DAYS = {expire_after_days:d}", |
| 59 | + { |
| 60 | + "expire_after_days": bp.expire_after_days, |
| 61 | + }, |
| 62 | + ) |
| 63 | + |
| 64 | + if bp.comment: |
| 65 | + query.append_nl( |
| 66 | + "COMMENT = {comment}", |
| 67 | + { |
| 68 | + "comment": bp.comment, |
| 69 | + }, |
| 70 | + ) |
| 71 | + |
| 72 | + self.engine.execute_unsafe_ddl(query) |
| 73 | + |
| 74 | + return ResolveResult.CREATE |
| 75 | + |
| 76 | + def compare_object(self, bp: SnapshotPolicyBlueprint, row: dict): |
| 77 | + result = ResolveResult.NOCHANGE |
| 78 | + |
| 79 | + if bp.schedule != row["schedule"]: |
| 80 | + if bp.schedule: |
| 81 | + self.engine.execute_unsafe_ddl( |
| 82 | + "ALTER SNAPSHOT POLICY {full_name:i} SET SCHEDULE = {schedule}", |
| 83 | + { |
| 84 | + "full_name": bp.full_name, |
| 85 | + "schedule": bp.schedule, |
| 86 | + }, |
| 87 | + ) |
| 88 | + else: |
| 89 | + self.engine.execute_unsafe_ddl( |
| 90 | + "ALTER SNAPSHOT POLICY {full_name:i} UNSET SCHEDULE", |
| 91 | + { |
| 92 | + "full_name": bp.full_name, |
| 93 | + }, |
| 94 | + ) |
| 95 | + |
| 96 | + result = ResolveResult.ALTER |
| 97 | + |
| 98 | + if bp.expire_after_days != row["expire_after_days"]: |
| 99 | + if bp.expire_after_days: |
| 100 | + self.engine.execute_unsafe_ddl( |
| 101 | + "ALTER SNAPSHOT POLICY {full_name:i} SET EXPIRE_AFTER_DAYS = {expire_after_days:d}", |
| 102 | + { |
| 103 | + "full_name": bp.full_name, |
| 104 | + "expire_after_days": bp.expire_after_days, |
| 105 | + }, |
| 106 | + ) |
| 107 | + else: |
| 108 | + self.engine.execute_unsafe_ddl( |
| 109 | + "ALTER SNAPSHOT POLICY {full_name:i} UNSET EXPIRE_AFTER_DAYS", |
| 110 | + { |
| 111 | + "full_name": bp.full_name, |
| 112 | + }, |
| 113 | + ) |
| 114 | + |
| 115 | + result = ResolveResult.ALTER |
| 116 | + |
| 117 | + if bp.comment != row["comment"]: |
| 118 | + self.engine.execute_unsafe_ddl( |
| 119 | + "ALTER SNAPSHOT POLICY {full_name:i} SET COMMENT = {comment}", |
| 120 | + { |
| 121 | + "full_name": bp.full_name, |
| 122 | + "comment": bp.comment, |
| 123 | + }, |
| 124 | + ) |
| 125 | + |
| 126 | + result = ResolveResult.ALTER |
| 127 | + |
| 128 | + return result |
| 129 | + |
| 130 | + def drop_object(self, row: dict): |
| 131 | + self.engine.execute_unsafe_ddl( |
| 132 | + "DROP SNAPSHOT POLICY {database:i}.{schema:i}.{name:i}", |
| 133 | + { |
| 134 | + "database": row["database"], |
| 135 | + "schema": row["schema"], |
| 136 | + "name": row["name"], |
| 137 | + }, |
| 138 | + ) |
| 139 | + |
| 140 | + return ResolveResult.DROP |
0 commit comments