-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Mayra Peña
authored and
Mayra Peña
committed
Nov 6, 2024
1 parent
12c495d
commit 1d22c09
Showing
8 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
|
||
{# This macro creates a database for use in pull request CI scripts. #} | ||
{# | ||
To run: | ||
dbt run-operation create_database | ||
#} | ||
|
||
{%- macro create_database() -%} | ||
{%- set database_exists = adapter.get_relation( | ||
database=target.database, | ||
schema="information_schema", | ||
identifier="tables") -%} | ||
{% if not database_exists %} | ||
{% set create_db_sql %} | ||
use role transformer_dbt; | ||
create database {{ target.database }}; | ||
grant ownership on database {{ target.database }} to role {{ target.role }}; | ||
use role {{ target.role }}; | ||
{% endset %} | ||
{% do run_query(create_db_sql) %} | ||
{{ log("Created Database: " ~ target.database, info=true) }} | ||
{% else %} | ||
{{ log("Database already exists: " ~ target.database, info=true) }} | ||
{% endif %} | ||
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
|
||
{# This macro drops and recreates in a given database #} | ||
{# | ||
To run: | ||
dbt run-operation drop_recreate_db --args '{db_name: dev_commercial_dw2, recreate: False}' | ||
#} | ||
|
||
{%- macro drop_recreate_db(db_name, recreate = True) -%} | ||
{% set db_name = db_name | upper %} | ||
|
||
{% set drop_recreate_sql %} | ||
drop database if exists {{ db_name }}; | ||
{% if recreate %} | ||
create database if not exists {{ db_name }}; | ||
{{ print("Recreating Database: " ~ db_name) }} | ||
{% else %} | ||
{{ print("Dropped Database: " ~ db_name) }} | ||
{% endif %} | ||
{% endset %} | ||
|
||
{% do run_query(drop_recreate_sql) %} | ||
|
||
{% if recreate %} | ||
{% set apply_grants_sql %} | ||
grant usage, create schema, monitor on database {{ db_name }} to analyst; | ||
grant usage on database {{ db_name }} to securityadmin; | ||
{% endset %} | ||
|
||
{{ log("Applying grants on Database: " ~ db_name, info=true) }} | ||
{% do run_query(apply_grants_sql) %} | ||
{% endif %} | ||
|
||
{%- endmacro -%} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
{# | ||
Overrides default ref macro for staging databases, to not include database name. | ||
This creates relative links to allows swapping of those databases without breaking view references | ||
#} | ||
|
||
{% macro ref() %} | ||
|
||
{% set version = kwargs.get('version') or kwargs.get('v') %} | ||
{% set packagename = none %} | ||
{%- if (varargs | length) == 1 -%} | ||
{% set modelname = varargs[0] %} | ||
{%- else -%} | ||
{% set packagename = varargs[0] %} | ||
{% set modelname = varargs[1] %} | ||
{% endif %} | ||
|
||
{% set rel = None %} | ||
{% if packagename is not none %} | ||
{% set rel = builtins.ref(packagename, modelname, version=version) %} | ||
{% else %} | ||
{% set rel = builtins.ref(modelname, version=version) %} | ||
{% endif %} | ||
|
||
|
||
{% set db_name = rel.database | lower %} | ||
|
||
{% if db_name.startswith('staging') or db_name.endswith('staging') %} | ||
{% do return(rel.include(database=false)) %} | ||
{% else %} | ||
{% do return(rel) %} | ||
{% endif %} | ||
|
||
{% endmacro %} |