From 323ab1e02d05983050a05adbea0516e79dc78c69 Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Mon, 18 Dec 2017 11:14:18 -0500 Subject: [PATCH] Fix/snowflake custom schema (#626) * Fixes already opened transaction issue For https://github.com/fishtown-analytics/dbt/issues/602 * Fixes https://github.com/fishtown-analytics/dbt/issues/621 --- dbt/adapters/snowflake.py | 6 +++++- dbt/node_runners.py | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/dbt/adapters/snowflake.py b/dbt/adapters/snowflake.py index 801628e2dbc..431f1b1c33f 100644 --- a/dbt/adapters/snowflake.py +++ b/dbt/adapters/snowflake.py @@ -147,7 +147,11 @@ def add_begin_query(cls, profile, name): def create_schema(cls, profile, schema, model_name=None): logger.debug('Creating schema "%s".', schema) sql = cls.get_create_schema_sql(schema) - return cls.add_query(profile, sql, model_name, select_schema=False) + res = cls.add_query(profile, sql, model_name, select_schema=False) + + cls.commit_if_has_connection(profile, model_name) + + return res @classmethod def get_existing_schemas(cls, profile, model_name=None): diff --git a/dbt/node_runners.py b/dbt/node_runners.py index 74c07434bcf..af62b461874 100644 --- a/dbt/node_runners.py +++ b/dbt/node_runners.py @@ -318,6 +318,13 @@ def safe_run_hooks(cls, project, adapter, flat_graph, hook_type): def create_schemas(cls, project, adapter, flat_graph): profile = project.run_environment() required_schemas = cls.get_model_schemas(flat_graph) + + # Snowflake needs to issue a "use {schema}" query, where schema + # is the one defined in the profile. Create this schema if it + # does not exist, otherwise subsequent queries will fail. Generally, + # dbt expects that this schema will exist anyway. + required_schemas.add(adapter.get_default_schema(profile)) + existing_schemas = set(adapter.get_existing_schemas(profile)) for schema in (required_schemas - existing_schemas):