Skip to content

Commit

Permalink
Create schema for external tables when it is not there. (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
guillesd authored Mar 24, 2023
1 parent f62a9c4 commit 34c1cc4
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 2 deletions.
11 changes: 11 additions & 0 deletions macros/common/create_external_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% macro create_external_schema(source_node) %}
{{ adapter.dispatch('create_external_schema', 'dbt_external_tables')(source_node) }}
{% endmacro %}

{% macro default__create_external_schema(source_node) %}
{% set ddl %}
create schema if not exists {{ source_node.schema }}
{% endset %}

{{return(ddl)}}
{% endmacro %}
5 changes: 4 additions & 1 deletion macros/plugins/bigquery/get_external_build_plan.sql
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
{% set create_or_replace = (old_relation is none or var('ext_full_refresh', false)) %}

{% if create_or_replace %}
{% set build_plan = build_plan + [dbt_external_tables.create_external_table(source_node)] %}
{% set build_plan = build_plan + [
dbt_external_tables.create_external_schema(source_node),
dbt_external_tables.create_external_table(source_node)
] %}
{% else %}
{% set build_plan = build_plan + dbt_external_tables.refresh_external_table(source_node) %}
{% endif %}
Expand Down
6 changes: 5 additions & 1 deletion macros/plugins/snowflake/get_external_build_plan.sql
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

{% if create_or_replace %}
{% set build_plan = build_plan + [
dbt_external_tables.create_external_schema(source_node),
dbt_external_tables.snowflake_create_empty_table(source_node),
dbt_external_tables.snowflake_get_copy_sql(source_node, explicit_transaction=true),
dbt_external_tables.snowflake_create_snowpipe(source_node)
Expand All @@ -25,7 +26,10 @@
{% else %}

{% if create_or_replace %}
{% set build_plan = build_plan + [dbt_external_tables.create_external_table(source_node)] %}
{% set build_plan = build_plan + [
dbt_external_tables.create_external_schema(source_node),
dbt_external_tables.create_external_table(source_node)
] %}
{% else %}
{% set build_plan = build_plan + dbt_external_tables.refresh_external_table(source_node) %}
{% endif %}
Expand Down
1 change: 1 addition & 0 deletions macros/plugins/spark/get_external_build_plan.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

{% if create_or_replace %}
{% set build_plan = build_plan + [
dbt_external_tables.create_external_schema(source_node),
dbt_external_tables.dropif(source_node),
dbt_external_tables.create_external_table(source_node)
] %}
Expand Down
13 changes: 13 additions & 0 deletions macros/plugins/sqlserver/create_external_schema.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{% macro sqlserver__create_external_schema(source_node) %}
{# https://learn.microsoft.com/en-us/sql/t-sql/statements/create-schema-transact-sql?view=sql-server-ver16 #}

{% set ddl %}
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ source_node.schema }}')
BEGIN
EXEC('CREATE SCHEMA [{{ source_node.schema }}]')
END
{% endset %}

{{return(ddl)}}

{% endmacro %}
1 change: 1 addition & 0 deletions macros/plugins/sqlserver/get_external_build_plan.sql
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

{% if create_or_replace %}
{% set build_plan = build_plan + [
dbt_external_tables.create_external_schema(source_node),
dbt_external_tables.dropif(source_node),
dbt_external_tables.create_external_table(source_node)
] %}
Expand Down

0 comments on commit 34c1cc4

Please sign in to comment.