Repository: dbt-labs/dbt-core

Issue:
Snowflake database quoting on 0.13
## Issue

### Issue description
We're attempting to update to 0.13 and are unable to run dbt when testing.

The command that fails is:

`dbt -d seed --profiles-dir profile --target prod`

The key parts of the logs seem to be:

```bash
$ dbt -d seed --profiles-dir profile --target prod
Running with dbt=0.13.0
2019-04-12 14:27:33,923 (MainThread): Tracking: do not track
2019-04-12 14:27:33,980 (MainThread): Parsing macros/monthly_change.sql
...
2019-04-12 14:27:36,663 (MainThread): Parsing model.snowplow.snowplow_web_events_time
2019-04-12 14:27:36,678 (MainThread): * Deprecation Warning: The adapter function `adapter.already_exists` is deprecated and will be removed in
 a future release of dbt. Please use `adapter.get_relation` instead.
 Documentation for get_relation can be found here:
 https://docs.getdbt.com/reference#adapter

2019-04-12 14:27:36,678 (MainThread): On "snowplow_web_events_time": cache miss for schema "1149-UPGRADE-DBT-TO-0-13_ANALYTICS.ANALYTICS", this is inefficient
2019-04-12 14:27:36,692 (MainThread): Parsing macros/core.sql
...
2019-04-12 14:27:36,911 (MainThread): Parsing macros/materializations/merge.sql
2019-04-12 14:27:36,960 (MainThread): Acquiring new snowflake connection "snowplow_web_events_time".
2019-04-12 14:27:36,961 (MainThread): Opening a new connection, currently in state init
2019-04-12 14:27:37,664 (MainThread): Using snowflake connection "snowplow_web_events_time".
2019-04-12 14:27:37,664 (MainThread): On snowplow_web_events_time: BEGIN
2019-04-12 14:27:37,787 (MainThread): SQL status: SUCCESS 1 in 0.12 seconds
2019-04-12 14:27:37,788 (MainThread): Using snowflake connection "snowplow_web_events_time".
2019-04-12 14:27:37,788 (MainThread): On snowplow_web_events_time: select
      table_catalog as database,
      table_name as name,
      table_schema as schema,
      case when table_type = 'BASE TABLE' then 'table'
           when table_type = 'VIEW' then 'view'
           else table_type
      end as table_type
    from 1149-UPGRADE-DBT-TO-0-13_ANALYTICS.information_schema.tables
    where table_schema ilike 'ANALYTICS'
      and table_catalog ilike '1149-upgrade-dbt-to-0-13_analytics'
2019-04-12 14:27:38,327 (MainThread): Snowflake error: 001003 (42000): 018b7a23-01d4-643a-0000-289d04fea226: SQL compilation error:
syntax error line 9 at position 9 unexpected '1149'.
2019-04-12 14:27:38,327 (MainThread): On snowplow_web_events_time: ROLLBACK
2019-04-12 14:27:38,489 (MainThread): Flushing usage events
2019-04-12 14:27:38,489 (MainThread): Encountered an error:
2019-04-12 14:27:38,489 (MainThread): Database Error
  001003 (42000): 018b7a23-01d4-643a-0000-289d04fea226: SQL compilation error:
  syntax error line 9 at position 9 unexpected '1149'.
2019-04-12 14:27:38,497 (MainThread): Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/snowflake/connections.py", line 77, in exception_handler
    yield
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/sql/connections.py", line 61, in add_query
    cursor.execute(sql, bindings)
  File "/usr/local/lib/python3.7/site-packages/snowflake/connector/cursor.py", line 550, in execute
    errvalue)
  File "/usr/local/lib/python3.7/site-packages/snowflake/connector/errors.py", line 97, in errorhandler_wrapper
    cursor.errorhandler(connection, cursor, errorclass, errorvalue)
  File "/usr/local/lib/python3.7/site-packages/snowflake/connector/errors.py", line 73, in default_errorhandler
    done_format_msg=errorvalue.get(u'done_format_msg'))
snowflake.connector.errors.ProgrammingError: 001003 (42000): 018b7a23-01d4-643a-0000-289d04fea226: SQL compilation error:
syntax error line 9 at position 9 unexpected '1149'.

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/dbt/main.py", line 79, in main
    results, succeeded = handle_and_check(args)
  File "/usr/local/lib/python3.7/site-packages/dbt/main.py", line 153, in handle_and_check
    task, res = run_from_args(parsed)
  File "/usr/local/lib/python3.7/site-packages/dbt/main.py", line 209, in run_from_args
    results = run_from_task(task, cfg, parsed)
  File "/usr/local/lib/python3.7/site-packages/dbt/main.py", line 217, in run_from_task
    result = task.run()
  File "/usr/local/lib/python3.7/site-packages/dbt/task/runnable.py", line 242, in run
    self._runtime_initialize()
  File "/usr/local/lib/python3.7/site-packages/dbt/task/runnable.py", line 50, in _runtime_initialize
    self.manifest = load_manifest(self.config)
  File "/usr/local/lib/python3.7/site-packages/dbt/task/runnable.py", line 29, in load_manifest
    internal_manifest=internal_manifest)
  File "/usr/local/lib/python3.7/site-packages/dbt/loader.py", line 173, in load_all
    internal_manifest)
  File "/usr/local/lib/python3.7/site-packages/dbt/loader.py", line 166, in _load_from_projects
    loader.load(internal_manifest=internal_manifest)
  File "/usr/local/lib/python3.7/site-packages/dbt/loader.py", line 139, in load
    self._load_nodes()
  File "/usr/local/lib/python3.7/site-packages/dbt/loader.py", line 78, in _load_nodes
    self._load_sql_nodes(ModelParser, NodeType.Model, 'source_paths')
  File "/usr/local/lib/python3.7/site-packages/dbt/loader.py", line 43, in _load_sql_nodes
    **kwargs
  File "/usr/local/lib/python3.7/site-packages/dbt/parser/base_sql.py", line 63, in load_and_parse
    return self.parse_sql_nodes(result, tags)
  File "/usr/local/lib/python3.7/site-packages/dbt/parser/base_sql.py", line 82, in parse_sql_nodes
    node_parsed = self.parse_node(node, node_path, project, tags=tags)
  File "/usr/local/lib/python3.7/site-packages/dbt/parser/base.py", line 224, in parse_node
    self._render_with_context(parsed_node, config)
  File "/usr/local/lib/python3.7/site-packages/dbt/parser/base.py", line 160, in _render_with_context
    capture_macros=True)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 279, in get_rendered
    return render_template(template, ctx, node)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 266, in render_template
    return template.render(ctx)
  File "/usr/local/lib/python3.7/site-packages/jinja2/asyncsupport.py", line 76, in render
    return original_render(self, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 1008, in render
    return self.environment.handle_exception(exc_info, True)
  File "/usr/local/lib/python3.7/site-packages/jinja2/environment.py", line 780, in handle_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python3.7/site-packages/jinja2/_compat.py", line 37, in reraise
    raise value.with_traceback(tb)
  File "dbt-e4fc89a406e64fd8502658b5", line 1, in top-level template code
    from __future__ import division, generator_stop
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 438, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 104, in call
    return macro(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "dbt-2b309f9e7ea9717e61106a77", line 3, in template
    name = None
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 438, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 104, in call
    return macro(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "dbt-1326913a3a7b62469d9260c4", line 27, in template
    t_3.append(
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 438, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 104, in call
    return macro(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "dbt-2b309f9e7ea9717e61106a77", line 21, in template
    context.exported_vars.add('dbt_macro__snowplow_web_events_time')
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 438, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/context/common.py", line 67, in wrapped
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/base/meta.py", line 45, in inner
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/base/impl.py", line 632, in already_exists
    model_name=model_name)
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/base/impl.py", line 607, in get_relation
    relations_list = self.list_relations(database, schema, model_name)
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/base/impl.py", line 569, in list_relations
    information_schema, schema, model_name=model_name
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/sql/impl.py", line 201, in list_relations_without_caching
    release=True
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/base/impl.py", line 856, in execute_macro
    result = macro_function(**kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 104, in call
    return macro(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "dbt-1326913a3a7b62469d9260c4", line 218, in template
    l_1_sql = undefined("parameter 'sql' was not provided", name='sql')
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 438, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 104, in call
    return macro(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "dbt-1326913a3a7b62469d9260c4", line 25, in template
    pass
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 438, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 104, in call
    return macro(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "dbt-ca70e94c428665d55249974a", line 54, in template
    context.vars['dbt_macro__snowflake__create_table_as'] = l_0_dbt_macro__snowflake__create_table_as = Macro(environment, macro, 'dbt_macro__snowflake__create_table_as', ('temporary', 'relation', 'sql'), False, False, False, context.eval_ctx.autoescape)
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 438, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/clients/jinja.py", line 104, in call
    return macro(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/jinja2/runtime.py", line 579, in _invoke
    rv = self._func(*arguments)
  File "dbt-6d2e15fbbd8937005642a0f8", line 10, in template
    t_1 = environment.tests['none']
  File "/usr/local/lib/python3.7/site-packages/jinja2/sandbox.py", line 438, in call
    return __context.call(__obj, *args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/context/common.py", line 67, in wrapped
    return func(*args, **kwargs)
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/base/impl.py", line 229, in execute
    fetch=fetch
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/sql/connections.py", line 95, in execute
    _, cursor = self.add_query(sql, name, auto_begin)
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/snowflake/connections.py", line 224, in add_query
    abridge_sql_log=abridge_sql_log
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/sql/connections.py", line 66, in add_query
    return connection, cursor
  File "/usr/local/lib/python3.7/contextlib.py", line 130, in __exit__
    self.gen.throw(type, value, traceback)
  File "/usr/local/lib/python3.7/site-packages/dbt/adapters/snowflake/connections.py", line 95, in exception_handler
    raise dbt.exceptions.DatabaseException(msg)
dbt.exceptions.DatabaseException: Database Error
  001003 (42000): 018b7a23-01d4-643a-0000-289d04fea226: SQL compilation error:
  syntax error line 9 at position 9 unexpected '1149'.
```

### Results

```sql
select
      table_catalog as database,
      table_name as name,
      table_schema as schema,
      case when table_type = 'BASE TABLE' then 'table'
           when table_type = 'VIEW' then 'view'
           else table_type
      end as table_type
    from 1149-UPGRADE-DBT-TO-0-13_ANALYTICS.information_schema.tables
    where table_schema ilike 'ANALYTICS'
      and table_catalog ilike '1149-upgrade-dbt-to-0-13_analytics'
```

is failing b/c the DB name is not being quoted at all. We're testing this in this MR https://gitlab.com/gitlab-data/analytics/merge_requests/881/diffs where you can see we've set 

```yaml
quoting:
  database: true
  identifier: false
  schema: false
```

### System information
The output of `dbt --version`:
```
± dbt --version
installed version: 0.13.0
   latest version: 0.13.0

Up to date!
```

The operating system you're running on: Mac OSX 10.14

The python version you're using: I see this on 3.5.2 and 3.7

### Steps to reproduce
In as much detail as possible, please provide steps to reproduce the issue. Sample data that triggers the issue, example models, etc are all very helpful here.



I'm able to reproduce this locally by setting my profile to:

```
gitlab-snowflake:
  target: dev
  outputs:
    dev:
      type: snowflake
      threads: 16
      account: gitlab
      user: tmurphy
      password: 
      role: TRANSFORMER
      database: 1149-UPGRADE-DBT-TO-0-13_ANALYTICS
      warehouse: ENGINEER_XS
      schema: tmurphy_scratch
```

The DB name is set based on the branch name. We know it's not valid for Snowflake but when it's quoted it's fine and this wasn't a problem in 0.12

Convo from Slack:

Is it possible that the information schema isn’t getting quoted as configured? The error I found seems linked to https://github.com/fishtown-analytics/dbt/blob/027a0d2ee6d138b5c7ae05a0b20e511e6bfdf516/core/dbt/adapters/sql/impl.py#L177 which only has a quote policy for schema and identifier.

my understanding of the code flow is that information_schema comes with the quoting policy but it appears to be overwritten? https://github.com/fishtown-analytics/dbt/blob/027a0d2ee6d138b5c7ae05a0b20e511e6bfdf516/core/dbt/adapters/base/relation.py#L139 I fully acknowledge I could be talking through my hat here lol


drew.banin 
Haha, yeah, this code is super complicated :confused:

The big challenge is that this logic needs to account for:
- warehouses where cross-database information schema queries are supported (like Snowflake)
- warehouses where they are _not_ supported (like Redshift)
- snowflake’s uppercase-by-default identifiers

In `list_relations_without_caching`, the query that’s failing for you is actually executed via the call to `execute_macro` on L179. We then apply a quote policy to the resulting relations that are returned by the query, but I think that would happen _after_ the `information_schema` query succeeds.

In the `information_schema` method, I think the relevant line of code is probably L147. `self.quote_policy['database']` should be sourced from the adapter default (unquoted) and then overwritten with a user-supplied quote policy if provided. If there’s a bug in this code, it would probably be there!

(all line numbers based on commit `027a0d2ee6d138b5c7ae05a0b20e511e6bfdf516`) (edited)

So, the very quick fix is to make your database name a valid snowflake identifier (ie. use underscores instead of dashes, prefix it with a letter instead of a number). 


Discussion:
Thanks for the detailed report @tayloramurphy! I will check it out
Hey @tayloramurphy - I was able to spend some more time trying to reproduce this. It was a tricky one to diagnose!

This is specifically an issue with the call to `adapter.already_exists()` in the `snowplow_web_events_time` model. I think the quick fix is to replace calls to `already_exists()` with `is_incremental()`. We made that change in the [0.7.x releases](https://github.com/fishtown-analytics/snowplow/releases) of the Snowplow package, though there may be other places in your codebase where this change is required.

Interestingly, `is_incremental()` calls `get_relation()`, which is also called `already_exists()`. I think the operative difference is that `is_incremental` no-ops during parsing, whereas `already_exists()` will actually execute the query. My assumption is that dbt doesn't configure database quoting appropriately during parsing, which would probably be in the class of "bug".

Still going to need to spend some more time digging in here to come up with a resolution, but wanted to hit you back with a workaround in the meantime!

Resolving patch:
diff --git a/core/dbt/adapters/base/impl.py b/core/dbt/adapters/base/impl.py
--- a/core/dbt/adapters/base/impl.py
+++ b/core/dbt/adapters/base/impl.py
@@ -561,7 +561,9 @@ def list_relations(self, database, schema, model_name=None):
         information_schema = self.Relation.create(
             database=database,
             schema=schema,
-            model_name='').information_schema()
+            model_name='',
+            quote_policy=self.config.quoting
+        ).information_schema()
 
         # we can't build the relations cache because we don't have a
         # manifest so we can't run any operations.
@@ -581,7 +583,7 @@ def _make_match_kwargs(self, database, schema, identifier):
         if schema is not None and quoting['schema'] is False:
             schema = schema.lower()
 
-        if database is not None and quoting['schema'] is False:
+        if database is not None and quoting['database'] is False:
             database = database.lower()
 
         return filter_null_values({
diff --git a/core/dbt/adapters/sql/impl.py b/core/dbt/adapters/sql/impl.py
--- a/core/dbt/adapters/sql/impl.py
+++ b/core/dbt/adapters/sql/impl.py
@@ -203,6 +203,7 @@ def list_relations_without_caching(self, information_schema, schema,
 
         relations = []
         quote_policy = {
+            'database': True,
             'schema': True,
             'identifier': True
         }
@@ -233,7 +234,8 @@ def list_schemas(self, database, model_name=None):
 
     def check_schema_exists(self, database, schema, model_name=None):
         information_schema = self.Relation.create(
-            database=database, schema=schema
+            database=database, schema=schema,
+            quote_policy=self.config.quoting
         ).information_schema()
 
         kwargs = {'information_schema': information_schema, 'schema': schema}
diff --git a/plugins/postgres/dbt/adapters/postgres/impl.py b/plugins/postgres/dbt/adapters/postgres/impl.py
--- a/plugins/postgres/dbt/adapters/postgres/impl.py
+++ b/plugins/postgres/dbt/adapters/postgres/impl.py
@@ -25,7 +25,10 @@ def date_function(cls):
 
     @available_raw
     def verify_database(self, database):
-        database = database.strip('"')
+        if database.startswith('"'):
+            database = database.strip('"')
+        else:
+            database = database.lower()
         expected = self.config.credentials.database
         if database != expected:
             raise dbt.exceptions.NotImplementedException(


Test patch:
diff --git a/test.env.sample b/test.env.sample
--- a/test.env.sample
+++ b/test.env.sample
@@ -3,6 +3,8 @@ SNOWFLAKE_TEST_ACCOUNT=
 SNOWFLAKE_TEST_USER=
 SNOWFLAKE_TEST_PASSWORD=
 SNOWFLAKE_TEST_DATABASE=
+SNOWFLAKE_TEST_ALT_DATABASE=
+SNOWFLAKE_TEST_QUOTED_DATABASE=
 SNOWFLAKE_TEST_WAREHOUSE=
 
 BIGQUERY_TYPE=
diff --git a/test/integration/001_simple_copy_test/test_simple_copy.py b/test/integration/001_simple_copy_test/test_simple_copy.py
--- a/test/integration/001_simple_copy_test/test_simple_copy.py
+++ b/test/integration/001_simple_copy_test/test_simple_copy.py
@@ -17,6 +17,7 @@ def models(self):
 
 
 class TestSimpleCopy(BaseTestSimpleCopy):
+
     @use_profile("postgres")
     def test__postgres__simple_copy(self):
         self.use_default_project({"data-paths": [self.dir("seed-initial")]})
@@ -83,6 +84,12 @@ def test__snowflake__simple_copy(self):
 
         self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
 
+        self.use_default_project({
+            "test-paths": [self.dir("tests")],
+            "data-paths": [self.dir("seed-update")],
+        })
+        self.run_dbt(['test'])
+
     @use_profile("snowflake")
     def test__snowflake__simple_copy__quoting_off(self):
         self.use_default_project({
@@ -108,6 +115,13 @@ def test__snowflake__simple_copy__quoting_off(self):
 
         self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
 
+        self.use_default_project({
+            "test-paths": [self.dir("tests")],
+            "data-paths": [self.dir("seed-update")],
+            "quoting": {"identifier": False},
+        })
+        self.run_dbt(['test'])
+
     @use_profile("snowflake")
     def test__snowflake__seed__quoting_switch(self):
         self.use_default_project({
@@ -124,6 +138,12 @@ def test__snowflake__seed__quoting_switch(self):
         })
         results = self.run_dbt(["seed"], expect_pass=False)
 
+        self.use_default_project({
+            "test-paths": [self.dir("tests")],
+            "data-paths": [self.dir("seed-initial")],
+        })
+        self.run_dbt(['test'])
+
     @use_profile("bigquery")
     def test__bigquery__simple_copy(self):
         self.use_default_project({"data-paths": [self.dir("seed-initial")]})
@@ -181,6 +201,8 @@ def test__snowflake__simple_copy__quoting_on(self):
 
         self.assertManyTablesEqual(["seed", "view_model", "incremental", "materialized"])
 
+        # can't run the test as this one's identifiers will be the wrong case
+
 
 class BaseLowercasedSchemaTest(BaseTestSimpleCopy):
     def unique_schema(self):
@@ -210,6 +232,12 @@ def test__snowflake__simple_copy(self):
 
         self.assertManyTablesEqual(["SEED", "VIEW_MODEL", "INCREMENTAL", "MATERIALIZED"])
 
+        self.use_default_project({
+            "test-paths": [self.dir("tests")],
+            "data-paths": [self.dir("seed-update")],
+        })
+        self.run_dbt(['test'])
+
 
 class TestSnowflakeSimpleLowercasedSchemaQuoted(BaseLowercasedSchemaTest):
     @property
diff --git a/test/integration/001_simple_copy_test/tests/get_relation_quoting.sql b/test/integration/001_simple_copy_test/tests/get_relation_quoting.sql
new file mode 100644
--- /dev/null
+++ b/test/integration/001_simple_copy_test/tests/get_relation_quoting.sql
@@ -0,0 +1,9 @@
+{%- set tgt = ref('seed') -%}
+{%- set got = adapter.get_relation(database=tgt.database, schema=tgt.schema, identifier=tgt.identifier) | string -%}
+{% set replaced = got.replace('"', '-') %}
+{% set expected = "-" + tgt.database.upper() + '-.-' + tgt.schema.upper() + '-.-' + tgt.identifier.upper() + '-' %}
+
+with cte as (
+    select '{{ replaced }}' as name
+)
+select * from cte where name not like '{{ expected }}'
diff --git a/test/integration/040_override_database_test/models/view_1.sql b/test/integration/040_override_database_test/models/view_1.sql
--- a/test/integration/040_override_database_test/models/view_1.sql
+++ b/test/integration/040_override_database_test/models/view_1.sql
@@ -1 +1,7 @@
+{#
+	We are running against a database that must be quoted.
+	These calls ensure that we trigger an error if we're failing to quote at parse-time
+#}
+{% do adapter.already_exists(this.schema, this.table) %}
+{% do adapter.get_relation(this.database, this.schema, this.table) %}
 select * from {{ ref('seed') }}
diff --git a/test/integration/040_override_database_test/models/view_2.sql b/test/integration/040_override_database_test/models/view_2.sql
--- a/test/integration/040_override_database_test/models/view_2.sql
+++ b/test/integration/040_override_database_test/models/view_2.sql
@@ -1,5 +1,4 @@
 {{
     config(database=var('alternate_db'))
 }}
-
 select * from {{ ref('seed') }}
diff --git a/test/integration/040_override_database_test/test_override_database.py b/test/integration/040_override_database_test/test_override_database.py
--- a/test/integration/040_override_database_test/test_override_database.py
+++ b/test/integration/040_override_database_test/test_override_database.py
@@ -1,6 +1,8 @@
 from nose.plugins.attrib import attr
 from test.integration.base import DBTIntegrationTest
 
+import os
+
 
 class BaseOverrideDatabase(DBTIntegrationTest):
     setup_alternate_db = True
@@ -12,6 +14,45 @@ def schema(self):
     def models(self):
         return "test/integration/040_override_database_test/models"
 
+    @property
+    def alternative_database(self):
+        if self.adapter_type == 'snowflake':
+            return os.getenv('SNOWFLAKE_TEST_DATABASE')
+        else:
+            return super(BaseOverrideDatabase, self).alternative_database
+
+    def snowflake_profile(self):
+        return {
+            'config': {
+                'send_anonymous_usage_stats': False
+            },
+            'test': {
+                'outputs': {
+                    'default2': {
+                        'type': 'snowflake',
+                        'threads': 4,
+                        'account': os.getenv('SNOWFLAKE_TEST_ACCOUNT'),
+                        'user': os.getenv('SNOWFLAKE_TEST_USER'),
+                        'password': os.getenv('SNOWFLAKE_TEST_PASSWORD'),
+                        'database': os.getenv('SNOWFLAKE_TEST_QUOTED_DATABASE'),
+                        'schema': self.unique_schema(),
+                        'warehouse': os.getenv('SNOWFLAKE_TEST_WAREHOUSE'),
+                    },
+                    'noaccess': {
+                        'type': 'snowflake',
+                        'threads': 4,
+                        'account': os.getenv('SNOWFLAKE_TEST_ACCOUNT'),
+                        'user': 'noaccess',
+                        'password': 'password',
+                        'database': os.getenv('SNOWFLAKE_TEST_DATABASE'),
+                        'schema': self.unique_schema(),
+                        'warehouse': os.getenv('SNOWFLAKE_TEST_WAREHOUSE'),
+                    }
+                },
+                'target': 'default2'
+            }
+        }
+
     @property
     def project_config(self):
         return {
@@ -20,9 +61,15 @@ def project_config(self):
                 'vars': {
                     'alternate_db': self.alternative_database,
                 },
+            },
+            'quoting': {
+                'database': True,
             }
         }
 
+    def run_dbt_notstrict(self, args):
+        return self.run_dbt(args, strict=False)
+
 
 class TestModelOverride(BaseOverrideDatabase):
     def run_database_override(self):
@@ -31,9 +78,9 @@ def run_database_override(self):
         else:
             func = lambda x: x
 
-        self.run_dbt(['seed'])
+        self.run_dbt_notstrict(['seed'])
 
-        self.assertEqual(len(self.run_dbt(['run'])), 4)
+        self.assertEqual(len(self.run_dbt_notstrict(['run'])), 4)
         self.assertManyRelationsEqual([
             (func('seed'), self.unique_schema(), self.default_database),
             (func('view_2'), self.unique_schema(), self.alternative_database),
@@ -71,9 +118,9 @@ def run_database_override(self):
                 },
             }
         })
-        self.run_dbt(['seed'])
+        self.run_dbt_notstrict(['seed'])
 
-        self.assertEqual(len(self.run_dbt(['run'])), 4)
+        self.assertEqual(len(self.run_dbt_notstrict(['run'])), 4)
         self.assertManyRelationsEqual([
             (func('seed'), self.unique_schema(), self.default_database),
             (func('view_2'), self.unique_schema(), self.alternative_database),
@@ -101,9 +148,9 @@ def run_database_override(self):
         self.use_default_project({
             'seeds': {'database': self.alternative_database}
         })
-        self.run_dbt(['seed'])
+        self.run_dbt_notstrict(['seed'])
 
-        self.assertEqual(len(self.run_dbt(['run'])), 4)
+        self.assertEqual(len(self.run_dbt_notstrict(['run'])), 4)
         self.assertManyRelationsEqual([
             (func('seed'), self.unique_schema(), self.alternative_database),
             (func('view_2'), self.unique_schema(), self.alternative_database),
diff --git a/test/integration/base.py b/test/integration/base.py
--- a/test/integration/base.py
+++ b/test/integration/base.py
@@ -582,7 +582,9 @@ def get_many_table_columns(self, tables, schema, database=None):
                   and ({table_filter})
                 order by column_name asc"""
 
-        db_string = '' if database is None else database + '.'
+        db_string = ''
+        if database:
+            db_string = self.quote_as_configured(database, 'database') + '.'
 
         table_filters_s = " OR ".join(
             self._ilike('table_name', table.replace('"', ''))