Skip to content

Commit d88dc70

Browse files
fix: add Athena-specific edr_drop_schema and always-run schema cleanup step (#960)
1 parent dd42ece commit d88dc70

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed

.github/workflows/test-warehouse.yml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,14 @@ jobs:
202202
with:
203203
name: detailed_report_${{ inputs.warehouse-type }}_dbt_${{ inputs.dbt-version }}
204204
path: ${{ env.TESTS_DIR }}/tests/detailed_report_${{ inputs.warehouse-type }}_dbt_${{ inputs.dbt-version }}.html
205+
206+
- name: Drop test schemas
207+
if: >-
208+
always() &&
209+
contains(fromJSON('["snowflake","bigquery","redshift","databricks_catalog","athena"]'), inputs.warehouse-type)
210+
working-directory: ${{ env.TESTS_DIR }}
211+
continue-on-error: true
212+
run: |
213+
${{ (inputs.dbt-version == 'fusion' && '~/.local/bin/dbt') || 'dbt' }} run-operation elementary_tests.drop_test_schemas \
214+
--project-dir dbt_project \
215+
-t "${{ inputs.warehouse-type }}"

integration_tests/dbt_project/macros/clear_env.sql

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,32 @@
88
) %}
99
{% endmacro %}
1010

11+
{% macro drop_test_schemas(num_workers=8) %}
12+
{#
13+
Drop every schema that a CI test run may have created.
14+
This covers the base schema (no xdist suffix) as well as
15+
each pytest-xdist worker schema (_gw0 … _gw<N-1>).
16+
Called from the workflow with `if: always()` so that schemas
17+
are cleaned up even when the pytest process is cancelled or
18+
crashes before its own teardown runs.
19+
#}
20+
{% set database = elementary.target_database() %}
21+
{% set base_schema = target.schema %}
22+
{% set suffixes = [""] %}
23+
{% for i in range(num_workers) %} {% do suffixes.append("_gw" ~ i) %} {% endfor %}
24+
25+
{% for suffix in suffixes %}
26+
{% set test_schema = base_schema ~ suffix %}
27+
{% set elementary_schema = base_schema ~ "_elementary" ~ suffix %}
28+
{% do log(
29+
"Dropping schemas: " ~ test_schema ~ ", " ~ elementary_schema,
30+
info=true,
31+
) %}
32+
{% do elementary_tests.edr_drop_schema(database, elementary_schema) %}
33+
{% do elementary_tests.edr_drop_schema(database, test_schema) %}
34+
{% endfor %}
35+
{% endmacro %}
36+
1137
{% macro edr_drop_schema(database_name, schema_name) %}
1238
{% do return(
1339
adapter.dispatch("edr_drop_schema", "elementary_tests")(
@@ -34,6 +60,24 @@
3460
{% do run_query("DROP DATABASE IF EXISTS `" ~ safe_schema ~ "` CASCADE") %}
3561
{% endmacro %}
3662

63+
{% macro athena__edr_drop_schema(database_name, schema_name) %}
64+
{#
65+
Athena's SQL `DROP SCHEMA … CASCADE` can fail when the schema
66+
contains Iceberg tables. Work around this by first dropping every
67+
relation individually (the adapter handles Iceberg vs Hive
68+
differences in its drop_relation implementation) and then removing
69+
the now-empty schema.
70+
#}
71+
{% set schema_relation = api.Relation.create(
72+
database=database_name, schema=schema_name
73+
) %}
74+
{% set relations = adapter.list_relations_without_caching(schema_relation) %}
75+
{% for relation in relations %}
76+
{% do adapter.drop_relation(relation) %}
77+
{% endfor %}
78+
{% do dbt.drop_schema(schema_relation) %}
79+
{% endmacro %}
80+
3781
{% macro duckdb__edr_drop_schema(database_name, schema_name) %}
3882
{% do run_query("DROP SCHEMA IF EXISTS " ~ schema_name ~ " CASCADE") %}
3983
{% do adapter.commit() %}

0 commit comments

Comments
 (0)