Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for changing databases #124

Closed
wants to merge 2 commits into from
Closed

Support for changing databases #124

wants to merge 2 commits into from

Conversation

semcha
Copy link
Contributor

@semcha semcha commented Mar 30, 2021

Fixes #110.

@semcha
Copy link
Contributor Author

semcha commented Mar 30, 2021

@swanderz @mikaelene Hi! How I can test this PR in azuresql?

@semcha semcha closed this Mar 30, 2021
@dataders
Copy link
Collaborator

@semcha I'm not sure! CircleCI should be testing this automatically...

@dataders
Copy link
Collaborator

@semcha you should re-open this! This link will show you how the integration tests are failing on Azure SQL, you just have to sign into Circle Ci with your GItHub account.

It's also quite easy to make a basic Azure SQL database on a free Azure subscription and use that to run the integration tests against it...

issue

error message

('42000', "[42000] [Microsoft][ODBC Driver 17 for SQL Server][SQL Server]
    Incorrect syntax near '-'. (102)
(SQLExecDirectW)")

SQL query responsible for message

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 [type]
    from **********************.information_schema.tables
    where table_schema = 'dbt_test_azure_sql_210330220928759444776398'

root cause

looks like the bug right now might have to do with our current quoting policy because our database (starrred out) right now for security reasons, has a few -'s in it. The from line should probably be double-quoted like this:

from "**********************"."information_schema"."tables"

Copy link
Collaborator

@dataders dataders left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@semcha you're closer than anyone's ever gotten before! if you accept my changes about properly quoting, the new error message is

Reference to database and/or server name in [****************].sys.schemas is not supported in this version of SQL Server.

So here's two potential approaches:

  1. try a USE [{{relation.database}}] at the top of these macros?
  2. implement some tricky conditional logic based on the result of the SERVERPROPERTY() function. there's a previous discussion of this option in Installation of dbt-synapse in same env as dbt-sqlserver conflicts namespace and starts running SQL server models using synapse Adapter microsoft/dbt-synapse#18 (relevant comment).

@mikaelene what do you think? we're close I feel!

Comment on lines +46 to +56
{% call statement('create_schema') -%}
use [{{ relation.database }}];
if not exists (
select *
from sys.schemas
where name = '{{ relation.without_identifier().schema }}'
)
begin
execute('create schema {{ relation.without_identifier().schema }}')
end
{% endcall %}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the formating you've done here, but we should probably have formatting be a separate PR, so we can more easily see the diffs required to enable using multiple databases

Comment on lines +158 to +162
if exists (
select *
from sys.indexes
where name = '{{ cci_name }}'
and [object_id] = object_id('{{ relation.include(database=False) }}')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same thing about formatting from other note

Comment on lines +164 to +168
begin
drop index {{cci_name}} on {{ relation.include(database=False) }}
end
create clustered columnstore index {{ cci_name }}
on {{ relation.include(database=False) }}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are you adding a begin and end here? can you help me understand why?

when table_type = 'VIEW' then 'view'
else table_type
end as [type]
from {{ schema_relation.database }}.information_schema.tables
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotta put db name into square brackets to properly escape weird chars like - in the db name.

Suggested change
from {{ schema_relation.database }}.information_schema.tables
from [{{ schema_relation.database }}].information_schema.tables

{{ return(load_result('list_schemas').table) }}
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
select name as [schema]
from {{ database }}.sys.schemas
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotta put db name into square brackets to properly escape weird chars like - in the db name.

Suggested change
from {{ database }}.sys.schemas
from [{{ database }}].sys.schemas

@@ -192,7 +220,7 @@
character_maximum_length,
numeric_precision,
numeric_scale
from INFORMATION_SCHEMA.COLUMNS
from {{ relation.database }}.INFORMATION_SCHEMA.COLUMNS
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

gotta put db name into square brackets to properly escape weird chars like - in the db name.

Suggested change
from {{ relation.database }}.INFORMATION_SCHEMA.COLUMNS
from [{{ relation.database }}].INFORMATION_SCHEMA.COLUMNS

@@ -4,7 +4,7 @@

#### fixes
- solved a bug in snapshots introduced in v0.19.0. Fixes: [#108](https://github.com/dbt-msft/dbt-sqlserver/issues/108), [#117](https://github.com/dbt-msft/dbt-sqlserver/issues/117).

- changing databases is now supported. [#110](https://github.com/dbt-msft/dbt-sqlserver/issues/110)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- changing databases is now supported. [#110](https://github.com/dbt-msft/dbt-sqlserver/issues/110)
- changing databases is now supported. [#110](https://github.com/dbt-msft/dbt-sqlserver/issues/110) thanks [@semcha ](https://github.com/semcha)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Changing the database via {{ config(database='OTHER_DB') }} doesn't work
2 participants