Skip to content

Commit 6ed0da9

Browse files
semchadataders
authored andcommitted
Support for changing database in model configuration (dbt-msft#126)
* Support for changing database in model configuration * Bumbed version * Update dbt/include/sqlserver/macros/adapters.sql * Removed unnecessary check * drop redundancy * 3-part versions from now on * capitals as style guide * document * typo * style: capitalized function words Co-authored-by: Anders <[email protected]>
1 parent ae1a11d commit 6ed0da9

File tree

4 files changed

+33
-27
lines changed

4 files changed

+33
-27
lines changed

CHANGELOG.md

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
# Changelog
2+
### v0.19.1
23

3-
### v0.19.0.3
4+
#### features:
5+
6+
- users can now delcare a model's database to be other than the one specified in the profile. This will only work for on-premise SQL Server and Azure SQL Managed Instance. [#126](https://github.com/dbt-msft/dbt-sqlserver/issues/126) thanks [@semcha](https://github.com/semcha)!
47

58
#### under the hood
9+
10+
- abandon four-part version names (`v0.19.0.2`) in favor of three-part version names because it isn't [SemVer](https://semver.org/) and it causes problems with the `~=` pip operator used dbt-synapse, a pacakge that depends on dbt-sqlserver
611
- allow CI to work with the lower-cost serverless Azure SQL [#132](https://github.com/dbt-msft/dbt-sqlserver/pull/132)
12+
713
### v0.19.0.2
814

915
#### fixes
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
version = '0.19.0.2'
1+
version = '0.19.1'

dbt/include/sqlserver/macros/adapters.sql

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,15 @@
2626
else table_type
2727
end as table_type
2828

29-
from information_schema.tables
29+
from [{{ schema_relation.database }}].information_schema.tables
3030
where table_schema like '{{ schema_relation.schema }}'
31-
and table_catalog like '{{ schema_relation.database }}'
3231
{% endcall %}
3332
{{ return(load_result('list_relations_without_caching').table) }}
3433
{% endmacro %}
3534

3635
{% macro sqlserver__list_schemas(database) %}
3736
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
37+
USE {{ database }};
3838
select name as [schema]
3939
from sys.schemas
4040
{% endcall %}
@@ -43,7 +43,7 @@
4343

4444
{% macro sqlserver__create_schema(relation) -%}
4545
{% call statement('create_schema') -%}
46-
USE [{{ relation.database }}]
46+
USE [{{ relation.database }}];
4747
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.without_identifier().schema }}')
4848
BEGIN
4949
EXEC('CREATE SCHEMA {{ relation.without_identifier().schema }}')
@@ -72,17 +72,8 @@
7272
{% endmacro %}
7373

7474
{% macro sqlserver__drop_relation(relation) -%}
75-
{% if relation.type == 'view' -%}
76-
{% set object_id_type = 'V' %}
77-
{% elif relation.type == 'table'%}
78-
{% set object_id_type = 'U' %}
79-
{%- else -%} invalid target name
80-
{% endif %}
8175
{% call statement('drop_relation', auto_begin=False) -%}
82-
if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null
83-
begin
84-
drop {{ relation.type }} {{ relation.include(database=False) }}
85-
end
76+
{{ sqlserver__drop_relation_script(relation) }}
8677
{%- endcall %}
8778
{% endmacro %}
8879

@@ -93,6 +84,7 @@
9384
{% set object_id_type = 'U' %}
9485
{%- else -%} invalid target name
9586
{% endif %}
87+
USE [{{ relation.database }}];
9688
if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null
9789
begin
9890
drop {{ relation.type }} {{ relation.include(database=False) }}
@@ -107,14 +99,24 @@
10799
{{ return(load_result('check_schema_exists').table) }}
108100
{% endmacro %}
109101

102+
103+
{% macro sqlserver__create_view_exec(relation, sql) -%}
104+
{%- set temp_view_sql = sql.replace("'", "''") -%}
105+
execute('create view {{ relation.include(database=False) }} as
106+
{{ temp_view_sql }}
107+
');
108+
{% endmacro %}
109+
110+
110111
{% macro sqlserver__create_view_as(relation, sql) -%}
111-
create view {{ relation.schema }}.{{ relation.identifier }} as
112-
{{ sql }}
112+
USE [{{ relation.database }}];
113+
{{ sqlserver__create_view_exec(relation, sql) }}
113114
{% endmacro %}
114115

115116

116117
{% macro sqlserver__rename_relation(from_relation, to_relation) -%}
117118
{% call statement('rename_relation') -%}
119+
USE [{{ to_relation.database }}];
118120
EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}'
119121
IF EXISTS(
120122
SELECT *
@@ -128,6 +130,7 @@
128130
{%- set cci_name = relation.schema ~ '_' ~ relation.identifier ~ '_cci' -%}
129131
{%- set relation_name = relation.schema ~ '_' ~ relation.identifier -%}
130132
{%- set full_relation = relation.schema ~ '.' ~ relation.identifier -%}
133+
use [{{ relation.database }}];
131134
if EXISTS (
132135
SELECT * FROM
133136
sys.indexes WHERE name = '{{cci_name}}'
@@ -149,12 +152,13 @@
149152

150153
{{ sqlserver__drop_relation_script(relation) }}
151154

152-
EXEC('create view {{ tmp_relation.schema }}.{{ tmp_relation.identifier }} as
155+
USE [{{ relation.database }}];
156+
EXEC('create view {{ tmp_relation.include(database=False) }} as
153157
{{ temp_view_sql }}
154158
');
155159

156-
SELECT * INTO {{ relation.schema }}.{{ relation.identifier }} FROM
157-
{{ tmp_relation.schema }}.{{ tmp_relation.identifier }}
160+
SELECT * INTO {{ relation }} FROM
161+
{{ tmp_relation }}
158162

159163
{{ sqlserver__drop_relation_script(tmp_relation) }}
160164

@@ -165,11 +169,7 @@
165169
{% endmacro %}_
166170

167171
{% macro sqlserver__insert_into_from(to_relation, from_relation) -%}
168-
{%- set full_to_relation = to_relation.schema ~ '.' ~ to_relation.identifier -%}
169-
{%- set full_from_relation = from_relation.schema ~ '.' ~ from_relation.identifier -%}
170-
171-
SELECT * INTO {{full_to_relation}} FROM {{full_from_relation}}
172-
172+
SELECT * INTO {{ to_relation }} FROM {{ from_relation }}
173173
{% endmacro %}
174174

175175
{% macro sqlserver__current_timestamp() -%}
@@ -192,7 +192,7 @@
192192
character_maximum_length,
193193
numeric_precision,
194194
numeric_scale
195-
from INFORMATION_SCHEMA.COLUMNS
195+
from [{{ relation.database }}].INFORMATION_SCHEMA.COLUMNS
196196
where table_name = '{{ relation.identifier }}'
197197
and table_schema = '{{ relation.schema }}'
198198
UNION ALL

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def _dbt_sqlserver_version():
2828
package_version = _dbt_sqlserver_version()
2929
description = """A sqlserver adapter plugin for dbt (data build tool)"""
3030

31-
dbt_version = '0.19.0'
31+
dbt_version = '0.19'
3232
# the package version should be the dbt version, with maybe some things on the
3333
# ends of it. (0.18.1 vs 0.18.1a1, 0.18.1.1, ...)
3434
if not package_version.startswith(dbt_version):

0 commit comments

Comments
 (0)