Skip to content

Commit 808222a

Browse files
author
Noel Gomez
committed
create missing database before swapping QA database
1 parent 32720db commit 808222a

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

.github/workflows/20_release_dbt_checks.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ jobs:
112112

113113
- name: Swap QA with QA_TEMP database and drop old QA db
114114
run: |
115-
dbt --no-write-json run-operation swap_database --args '{db1: ${{ env.DATACOVES__MAIN__DATABASE }}, db2: ${{ env.DATACOVES__MAIN__DATABASE_QA }}}'
115+
dbt --no-write-json run-operation swap_database --args '{db1: ${{ env.DATACOVES__MAIN__DATABASE }}, db2: ${{ env.DATACOVES__MAIN__DATABASE_QA }}}, create_missing_db: true'
116116
dbt --no-write-json run-operation drop_recreate_db --args '{db_name: ${{ env.DATACOVES__MAIN__DATABASE }}, recreate: False}'
117117
118118
# # We drop the database when there is a failure to grant access to the db because

transform/macros/tooling/blue-green/swap_database.sql

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,38 @@
11
{# This macro swaps two databases, for use in blue/green runs. #}
22
{#
3-
To run:
3+
To run:
44
dbt run-operation swap_database --args "{db1: dev_commercial_dw2, db2: dev_commercial_dw}"
5+
6+
To create missing db2 if it doesn't exist:
7+
dbt run-operation swap_database --args "{db1: dev_commercial_dw2, db2: dev_commercial_dw, create_missing_db: true}"
58
#}
69
7-
{%- macro swap_database(db1, db2) -%}
10+
{%- macro swap_database(db1, db2, create_missing_db=false) -%}
11+
{# Check if db2 exists #}
12+
{% set check_db2_sql %}
13+
select count(*) as db_count
14+
from information_schema.databases
15+
where database_name = upper('{{ db2 }}')
16+
{% endset %}
17+
18+
{% set db2_exists_result = run_query(check_db2_sql) %}
19+
{% set db2_exists = db2_exists_result.columns[0].values()[0] > 0 %}
20+
21+
{# Create db2 if it doesn't exist and create_missing_db is true #}
22+
{% if not db2_exists %}
23+
{% if create_missing_db %}
24+
{% set create_db2_sql %}
25+
create database {{ db2 }};
26+
{% endset %}
27+
28+
{% do run_query(create_db2_sql) %}
29+
{{ print("Created database " ~ db2 ~ " as it did not exist") }}
30+
{% else %}
31+
{{ exceptions.raise_compiler_error("Database " ~ db2 ~ " does not exist. Set create_missing_db=true to create it automatically.") }}
32+
{% endif %}
33+
{% endif %}
34+
35+
{# Perform the swap #}
836
{% set swap_db_sql %}
937
alter database {{ db1 }} swap with {{ db2 }};
1038
{% endset %}

0 commit comments

Comments
 (0)