Skip to content

Commit 3a78eb7

Browse files
feat: add drop_test_schemas macro and always-run cleanup step in CI
Add a drop_test_schemas macro that drops all schemas created by a CI test run (base + xdist worker schemas _gw0 through _gw7). Add an 'if: always()' step to test-warehouse.yml that calls this macro, ensuring schemas are cleaned up even when pytest is cancelled or crashes before its own teardown runs. Co-Authored-By: Itamar Hartstein <haritamar@gmail.com>
1 parent 2e61b62 commit 3a78eb7

2 files changed

Lines changed: 35 additions & 0 deletions

File tree

.github/workflows/test-warehouse.yml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,3 +202,12 @@ 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: always()
208+
working-directory: ${{ env.TESTS_DIR }}
209+
continue-on-error: true
210+
run: |
211+
dbt run-operation elementary_tests.drop_test_schemas \
212+
--project-dir dbt_project \
213+
-t "${{ inputs.warehouse-type }}"

integration_tests/dbt_project/macros/clear_env.sql

Lines changed: 26 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")(

0 commit comments

Comments
 (0)