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