Skip to content

Commit 6619db2

Browse files
authored
Merge pull request #550 from dbt-msft/549-error-on-table-materialization-when-using-non-standard-database-names
updated create table logic for using proper dbt render
2 parents d3af1e6 + 3c71a75 commit 6619db2

File tree

2 files changed

+118
-1
lines changed

2 files changed

+118
-1
lines changed

dbt/include/sqlserver/macros/relations/table/create.sql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
{{ get_create_view_as_sql(tmp_relation, sql) }}
88

99
{%- set table_name -%}
10-
{{ relation.database}}.{{ relation.schema }}.{{ relation.identifier }}
10+
{{ relation }}
1111
{%- endset -%}
1212

13+
1314
{%- set contract_config = config.get('contract') -%}
1415
{%- set query -%}
1516
{% if contract_config.enforced and (not temporary) %}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import pytest
2+
from dbt.tests.util import get_connection, run_dbt
3+
4+
database_name = "my-data-base"
5+
schema_name = "mysource"
6+
source_table_name = "my_table"
7+
8+
sources_yml = f"""
9+
version: 2
10+
11+
sources:
12+
- name: mysource
13+
database: {database_name}
14+
tables:
15+
- name: my_table
16+
17+
"""
18+
19+
model_sql = """
20+
{{ config(database="my-data-base", schema="mysource", materialized="table") }}
21+
SELECT
22+
*
23+
FROM
24+
{{ source('mysource', 'my_table') }}
25+
"""
26+
27+
28+
class TestNonStandardDB:
29+
def create_db(self, project):
30+
create_sql = """
31+
DECLARE @col NVARCHAR(256)
32+
SET @col = (SELECT CONVERT (varchar(256), SERVERPROPERTY('collation')));
33+
34+
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name='{database}')
35+
BEGIN
36+
EXEC ('CREATE DATABASE [{database}] COLLATE ' + @col)
37+
END
38+
"""
39+
40+
with get_connection(project.adapter):
41+
project.adapter.execute(
42+
create_sql.format(database=database_name),
43+
fetch=True,
44+
)
45+
46+
def create_source_schema(self, project):
47+
create_sql = """
48+
USE [{database}];
49+
50+
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{schema}')
51+
BEGIN
52+
EXEC('CREATE SCHEMA {schema}')
53+
END
54+
"""
55+
with get_connection(project.adapter):
56+
project.adapter.execute(
57+
create_sql.format(database=database_name, schema=schema_name),
58+
fetch=True,
59+
)
60+
61+
def create_primary_table(self, project):
62+
src_query = """
63+
SELECT *
64+
INTO
65+
[{database}].{schema}.{table}
66+
FROM
67+
(
68+
SELECT
69+
1 as id,
70+
CAST('2024-01-01' as DATETIME2(6)) updated_at
71+
72+
UNION ALL
73+
74+
SELECT
75+
2 as id,
76+
CAST('2024-01-01' as DATETIME2(6)) updated_at
77+
78+
UNION ALL
79+
80+
SELECT
81+
3 as id,
82+
CAST('2024-01-01' as DATETIME2(6)) updated_at
83+
) as src_data
84+
"""
85+
with get_connection(project.adapter):
86+
project.adapter.execute(
87+
src_query.format(
88+
database=database_name, schema=schema_name, table=source_table_name
89+
),
90+
fetch=True,
91+
)
92+
93+
def cleanup_primary_table(self, project):
94+
drop_sql = "DROP TABLE IF EXISTS [{database}].{schema}.{table}"
95+
with get_connection(project.adapter):
96+
project.adapter.execute(
97+
drop_sql.format(
98+
database=database_name, schema=schema_name, table=source_table_name
99+
),
100+
fetch=True,
101+
)
102+
103+
@pytest.fixture(scope="class")
104+
def models(self):
105+
return {"model.sql": model_sql, "sources.yml": sources_yml}
106+
107+
def test_non_standard_database(self, project):
108+
self.create_db(project)
109+
110+
self.cleanup_primary_table(project)
111+
self.create_source_schema(project)
112+
self.create_primary_table(project)
113+
114+
run_dbt()
115+
116+
self.cleanup_primary_table(project)

0 commit comments

Comments
 (0)