Skip to content

Commit

Permalink
Merge pull request #1398 from fishtown-analytics/feature/archive-no-c…
Browse files Browse the repository at this point in the history
…reate-existing-schema

archives: do not create existing schema (#758)
  • Loading branch information
beckjake authored Apr 26, 2019
2 parents 96cb056 + cc4f285 commit 3f18b93
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,14 @@
{%- set target_table = model.get('alias', model.get('name')) -%}
{%- set strategy = config.get('strategy') -%}

{{ create_schema(target_database, target_schema) }}
{% set information_schema = api.Relation.create(
database=target_database,
schema=target_schema,
identifier=target_table).information_schema() %}

{% if not check_schema_exists(information_schema, target_schema) %}
{{ create_schema(target_database, target_schema) }}
{% endif %}

{%- set target_relation = adapter.get_relation(
database=target_database,
Expand Down
6 changes: 6 additions & 0 deletions plugins/bigquery/dbt/adapters/bigquery/impl.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ def rename_relation(self, from_relation, to_relation):
'`rename_relation` is not implemented for this adapter!'
)

@available
def list_schemas(self, database):
conn = self.connections.get_thread_connection()
client = conn.handle
Expand All @@ -84,6 +85,11 @@ def list_schemas(self, database):
include_all=True)
return [ds.dataset_id for ds in all_datasets]

@available
def check_schema_exists(self, database, schema):
superself = super(BigQueryAdapter, self)
return superself.check_schema_exists(database, schema)

def get_columns_in_relation(self, relation):
try:
table = self.connections.get_bq_table(
Expand Down
10 changes: 10 additions & 0 deletions plugins/bigquery/dbt/include/bigquery/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,13 @@
{% macro bigquery__current_timestamp() -%}
CURRENT_TIMESTAMP()
{%- endmacro %}


{% macro bigquery__list_schemas(database) %}
{{ return(adapter.list_schemas()) }}
{% endmacro %}


{% macro bigquery__check_schema_exists(information_schema, schema) %}
{{ return(adapter.check_schema_exists(information_schema.database, schema)) }}
{% endmacro %}

0 comments on commit 3f18b93

Please sign in to comment.