|
| 1 | +{# |
| 2 | + Integration-test helper for drop_stale_ci_schemas. |
| 3 | + |
| 4 | + Creates two CI-style schemas (one with an old timestamp, one recent), |
| 5 | + runs the cleanup macro, checks which schemas survived, cleans up, |
| 6 | + and returns a JSON result dict. |
| 7 | +#} |
| 8 | + |
| 9 | +{% macro test_drop_stale_ci_schemas() %} |
| 10 | + {% set database = elementary.target_database() %} |
| 11 | + {% set now = modules.datetime.datetime.utcnow() %} |
| 12 | + |
| 13 | + {# Old schema: timestamp in the past (2020-01-01 00:00:00) #} |
| 14 | + {% set old_schema = 'dbt_200101_000000_citest_00000000' %} |
| 15 | + {# Recent schema: timestamp = now #} |
| 16 | + {% set recent_ts = now.strftime('%y%m%d_%H%M%S') %} |
| 17 | + {% set recent_schema = 'dbt_' ~ recent_ts ~ '_citest_11111111' %} |
| 18 | + |
| 19 | + {{ log("TEST: creating old schema: " ~ old_schema, info=true) }} |
| 20 | + {{ log("TEST: creating recent schema: " ~ recent_schema, info=true) }} |
| 21 | + |
| 22 | + {# ── Create both schemas ───────────────────────────────────────────── #} |
| 23 | + {% do edr_create_schema(database, old_schema) %} |
| 24 | + {% do edr_create_schema(database, recent_schema) %} |
| 25 | + |
| 26 | + {# ── Verify both exist before running cleanup ──────────────────────── #} |
| 27 | + {% set old_exists_before = edr_schema_exists(database, old_schema) %} |
| 28 | + {% set recent_exists_before = edr_schema_exists(database, recent_schema) %} |
| 29 | + {{ log("TEST: old_exists_before=" ~ old_exists_before ~ ", recent_exists_before=" ~ recent_exists_before, info=true) }} |
| 30 | + |
| 31 | + {# ── Run cleanup with a large threshold so only the artificially old |
| 32 | + schema (year 2020) is caught, and real CI schemas from parallel |
| 33 | + workers are safely below the threshold. ──────────────────────────── #} |
| 34 | + {% do drop_stale_ci_schemas(prefixes=['dbt_'], max_age_hours=8760) %} |
| 35 | + |
| 36 | + {# ── Check which schemas survived ─────────────────────────────────── #} |
| 37 | + {% set old_exists_after = edr_schema_exists(database, old_schema) %} |
| 38 | + {% set recent_exists_after = edr_schema_exists(database, recent_schema) %} |
| 39 | + {{ log("TEST: old_exists_after=" ~ old_exists_after ~ ", recent_exists_after=" ~ recent_exists_after, info=true) }} |
| 40 | + |
| 41 | + {# ── Cleanup: drop any remaining test schemas ─────────────────────── #} |
| 42 | + {% if old_exists_after is true %} |
| 43 | + {% do edr_drop_schema(database, old_schema) %} |
| 44 | + {% endif %} |
| 45 | + {% if recent_exists_after %} |
| 46 | + {% do edr_drop_schema(database, recent_schema) %} |
| 47 | + {% endif %} |
| 48 | + |
| 49 | + {# ── Return results ────────────────────────────────────────────────── #} |
| 50 | + {% set results = { |
| 51 | + "old_exists_before": old_exists_before, |
| 52 | + "recent_exists_before": recent_exists_before, |
| 53 | + "old_dropped": not old_exists_after, |
| 54 | + "recent_kept": recent_exists_after |
| 55 | + } %} |
| 56 | + {% do return(results) %} |
| 57 | +{% endmacro %} |
| 58 | + |
| 59 | + |
| 60 | +{# ── Per-adapter schema creation ─────────────────────────────────────── #} |
| 61 | + |
| 62 | +{% macro edr_create_schema(database, schema_name) %} |
| 63 | + {% do return(adapter.dispatch('edr_create_schema', 'elementary_tests')(database, schema_name)) %} |
| 64 | +{% endmacro %} |
| 65 | + |
| 66 | +{% macro default__edr_create_schema(database, schema_name) %} |
| 67 | + {% set schema_relation = api.Relation.create(database=database, schema=schema_name) %} |
| 68 | + {% do dbt.create_schema(schema_relation) %} |
| 69 | + {% do adapter.commit() %} |
| 70 | +{% endmacro %} |
| 71 | + |
| 72 | +{% macro clickhouse__edr_create_schema(database, schema_name) %} |
| 73 | + {% do run_query("CREATE DATABASE IF NOT EXISTS `" ~ schema_name ~ "`") %} |
| 74 | + {% do adapter.commit() %} |
| 75 | +{% endmacro %} |
0 commit comments