Skip to content

Adding USE [{{relation.database}}] where appropriate in adapters.sql #111

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

### v0.19.0.2

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

### v0.19.0.1

#### fixes
Expand Down
2 changes: 1 addition & 1 deletion dbt/adapters/sqlserver/__version__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
version = '0.19.0.1'
version = '0.19.0.2'
54 changes: 25 additions & 29 deletions dbt/include/sqlserver/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -72,31 +72,16 @@
{% endmacro %}

{% macro sqlserver__drop_relation(relation) -%}
{% if relation.type == 'view' -%}
{% set object_id_type = 'V' %}
{% elif relation.type == 'table'%}
{% set object_id_type = 'U' %}
{%- else -%} invalid target name
{% endif %}
{% call statement('drop_relation', auto_begin=False) -%}
if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null
begin
drop {{ relation.type }} {{ relation.include(database=False) }}
end
{% call statement('drop_relation', auto_begin=False) %}
USE [{{ relation.database }}]
DROP VIEW IF EXISTS {{ relation.include(database=False) }}
DROP TABLE IF EXISTS {{ relation.include(database=False) }}
Comment on lines +76 to +78
Copy link
Collaborator

Choose a reason for hiding this comment

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

the reason we're using the object_id() calls is because IF EXISTS wasn't added to SQL Server until 2016, which @mikaelene already mentioned. Can you see a way of getting the USE statement to play nicely with the IF object_id() piece?

{%- endcall %}
{% endmacro %}

{% macro sqlserver__drop_relation_script(relation) -%}
{% if relation.type == 'view' -%}
{% set object_id_type = 'V' %}
{% elif relation.type == 'table'%}
{% set object_id_type = 'U' %}
{%- else -%} invalid target name
{% endif %}
if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null
begin
drop {{ relation.type }} {{ relation.include(database=False) }}
end
DROP VIEW IF EXISTS {{ relation.include(database=False) }}
DROP TABLE IF EXISTS {{ relation.include(database=False) }}
{% endmacro %}

{% macro sqlserver__check_schema_exists(information_schema, schema) -%}
Expand All @@ -107,14 +92,26 @@
{{ return(load_result('check_schema_exists').table) }}
{% endmacro %}

{% macro sqlserver__create_view_as(relation, sql) -%}
create view {{ relation.schema }}.{{ relation.identifier }} as
{{ sql }}
{% macro sqlserver__create_view_exec(relation, sql) -%}
{%- set temp_view_sql = sql.replace("'", "''") -%}

EXEC('create view {{ relation.schema }}.{{ relation.identifier }} as
{{ temp_view_sql }}
');
{% endmacro %}

{% macro sqlserver__create_view_as(relation, sql) -%}
USE [{{ relation.database }}]
{{ sqlserver__create_view_exec(relation, sql) }}
{% endmacro %}

{% macro sqlserver__rename_relation(from_relation, to_relation) -%}
{% call statement('rename_relation') -%}
{% if from_relation.database != to_relation.database %}
{{ exceptions.raise_compiler_error("Can't do cross-database rename {} -> {}".format(from_relation, to_relation)) }}
{% endif %}
USE [{{ from_relation.database }}]
{{ sqlserver__drop_relation_script(to_relation) }}
EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}'
IF EXISTS(
SELECT *
Expand Down Expand Up @@ -143,15 +140,14 @@
{% set tmp_relation = relation.incorporate(
path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'},
type='view')-%}
{%- set temp_view_sql = sql.replace("'", "''") -%}

USE [{{ relation.database }}]

{{ sqlserver__drop_relation_script(tmp_relation) }}

{{ sqlserver__drop_relation_script(relation) }}

EXEC('create view {{ tmp_relation.schema }}.{{ tmp_relation.identifier }} as
{{ temp_view_sql }}
');

{{ sqlserver__create_view_exec(tmp_relation, sql) }}

SELECT * INTO {{ relation.schema }}.{{ relation.identifier }} FROM
{{ tmp_relation.schema }}.{{ tmp_relation.identifier }}
Expand Down