Skip to content

Commit

Permalink
Add macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Mayra Peña authored and Mayra Peña committed Nov 6, 2024
1 parent 12c495d commit 1d22c09
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
25 changes: 25 additions & 0 deletions transform/macros/create_database.sql
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 -%}
33 changes: 33 additions & 0 deletions transform/macros/drop_recreate_db.sql
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 -%}
33 changes: 33 additions & 0 deletions transform/macros/ref.sql
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 %}

0 comments on commit 1d22c09

Please sign in to comment.