Skip to content

Commit 11acff0

Browse files
committed
Added database hint macro, changes in profiles, removed sql/basic authentication profiles
1 parent ea06d2d commit 11acff0

File tree

10 files changed

+78
-95
lines changed

10 files changed

+78
-95
lines changed

.vscode/launch.json

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"version": "0.2.0",
3+
"configurations": [
4+
5+
{
6+
"name": "Python: Current File",
7+
"type": "python",
8+
"request": "launch",
9+
"program": "${file}",
10+
"console": "integratedTerminal",
11+
"justMyCode": false
12+
}
13+
]
14+
}

23.1.2

Whitespace-only changes.

dbt/adapters/fabric/fabric_adapter.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ def create_schema(self, relation: BaseRelation) -> None:
2828

2929
if self.config.credentials.schema_authorization:
3030
kwargs["schema_authorization"] = self.config.credentials.schema_authorization
31-
macro_name = "sqlserver__create_schema_with_authorization"
31+
macro_name = "fabric__create_schema_with_authorization"
3232

3333
self.execute_macro(macro_name, kwargs=kwargs)
3434
self.commit_if_has_connection()

dbt/include/fabric/macros/adapters/metadata.sql

+11-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,12 @@
1+
{% macro use_database_hint() %}
2+
{{ return(adapter.dispatch('use_database_hint')()) }}
3+
{% endmacro %}
4+
5+
{% macro default__use_database_hint() %}{% endmacro %}
6+
{% macro fabric__use_database_hint() %}
7+
{# USE [{{ relation.database }}]; #}
8+
{% endmacro %}
9+
110
{% macro information_schema_hints() %}
211
{{ return(adapter.dispatch('information_schema_hints')()) }}
312
{% endmacro %}
@@ -129,7 +138,7 @@
129138

130139
{% macro fabric__list_schemas(database) %}
131140
{% call statement('list_schemas', fetch_result=True, auto_begin=False) -%}
132-
USE {{ database }};
141+
{{ use_database_hint() }}
133142
select name as [schema]
134143
from sys.schemas
135144
{% endcall %}
@@ -138,7 +147,7 @@
138147

139148
{% macro fabric__check_schema_exists(information_schema, schema) -%}
140149
{% call statement('check_schema_exists', fetch_result=True, auto_begin=False) -%}
141-
--USE {{ database_name }}
150+
142151
SELECT count(*) as schema_exist FROM sys.schemas WHERE name = '{{ schema }}'
143152
{%- endcall %}
144153
{{ return(load_result('check_schema_exists').table) }}

dbt/include/fabric/macros/adapters/relation.sql

+25-10
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
{% macro fabric__drop_relation_script(relation) -%}
1616
{% call statement('find_references', fetch_result=true) %}
17-
USE [{{ relation.database }}];
17+
{{ use_database_hint() }}
1818
select
1919
sch.name as schema_name,
2020
obj.name as view_name
@@ -43,11 +43,10 @@
4343
{%- else -%}
4444
{{ exceptions.raise_not_implemented('Invalid relation being dropped: ' ~ relation) }}
4545
{% endif %}
46-
USE [{{ relation.database }}];
47-
if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null
48-
begin
49-
drop {{ relation.type }} {{ relation.include(database=False) }}
50-
end
46+
47+
{{ use_database_hint() }}
48+
DROP {{ relation.type }} IF EXISTS {{ relation.include(database=False) }}
49+
5150
{% endmacro %}
5251

5352
{% macro fabric__rename_relation(from_relation, to_relation) -%}
@@ -60,24 +59,40 @@
6059
and TABLE_NAME = '{{ from_relation.identifier }}'
6160
{% endcall %}
6261
{% set view_def_full = load_result('get_view_definition')['data'][0][0] %}
63-
{{ log("Found view definition " ~ view_def_full) }}
62+
6463
{% set view_def_sql_matches = modules.re.match('^create\\s+view\\s+[0-9a-z.\\"\\[\\]_]+\\s+as\\s+\\(?(.*)\\)?\\s+;?\\s+$', view_def_full, modules.re.I) %}
6564
{% if not view_def_sql_matches %}
6665
{{ exceptions.raise_compiler_error("Could not extract view definition to rename") }}
6766
{% endif %}
6867
{% set view_def_sql = view_def_sql_matches.group(1) %}
69-
{{ log("Found view SQL " ~ view_def_sql) }}
68+
7069
{% call statement('create_new_view') %}
7170
{{ create_view_as(to_relation, view_def_sql) }}
7271
{% endcall %}
7372
{% call statement('drop_old_view') %}
74-
drop view {{ from_relation.include(database=False) }};
73+
DROP VIEW IF EXISTS {{ from_relation.include(database=False) }};
7574
{% endcall %}
7675
{% endif %}
7776
{% if from_relation.type == 'table' %}
7877
{% call statement('rename_relation') %}
7978
create table {{ to_relation.include(database=False) }} as select * from {{ from_relation.include(database=False) }}
8079
{%- endcall %}
81-
{{ sqlserver__drop_relation(from_relation) }}
80+
{{ fabric__drop_relation(from_relation) }}
8281
{% endif %}
8382
{% endmacro %}
83+
84+
-- DROP synapsevnext__truncate_relation when TRUNCATE TABLE is supported
85+
{% macro fabric__truncate_relation(relation) -%}
86+
87+
{% set tempTableName %}
88+
{{ relation.identifier.replace("#", "") }}_{{ range(21000, 109000) | random }}
89+
{% endset %}
90+
91+
{% call statement('truncate_relation') -%}
92+
CREATE TABLE {{ tempTableName }} AS SELECT * FROM {{ relation }} WHERE 1=2
93+
DROP TABLE IF EXISTS {{ relation }}
94+
CREATE TABLE {{ relation }} AS SELECT * FROM {{ tempTableName }}
95+
DROP TABLE IF EXISTS {{ tempTableName }}
96+
{%- endcall %}
97+
98+
{% endmacro %}

dbt/include/fabric/macros/adapters/schema.sql

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{% macro fabric__create_schema(relation) -%}
22
{% call statement('create_schema') -%}
3-
USE [{{ relation.database }}];
3+
{{ use_database_hint() }}
44
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
55
BEGIN
66
EXEC('CREATE SCHEMA [{{ relation.schema }}]')
@@ -10,7 +10,7 @@
1010

1111
{% macro fabric__create_schema_with_authorization(relation, schema_authorization) -%}
1212
{% call statement('create_schema') -%}
13-
USE [{{ relation.database }}];
13+
{{ use_database_hint() }}
1414
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
1515
BEGIN
1616
EXEC('CREATE SCHEMA [{{ relation.schema }}] AUTHORIZATION [{{ schema_authorization }}]')
@@ -31,8 +31,6 @@
3131
{%- endfor %}
3232

3333
{% call statement('drop_schema') -%}
34-
IF EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
35-
BEGIN
36-
EXEC('DROP SCHEMA {{ relation.schema }}')
37-
END {% endcall %}
34+
EXEC('DROP SCHEMA IF EXISTS {{ relation.schema }}')
35+
{% endcall %}
3836
{% endmacro %}

dbt/include/fabric/macros/materializations/models/table/create_table_as.sql

+5-6
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@
99
{% do run_query(fabric__drop_relation_script(tmp_relation)) %}
1010
{% do run_query(fabric__drop_relation_script(relation)) %}
1111

12-
USE [{{ relation.database }}];
13-
EXEC('create view {{ tmp_relation.include(database=False) }} as {{ temp_view_sql }}');
12+
{{ use_database_hint() }}
13+
{{ fabric__create_view_as(tmp_relation, temp_view_sql) }}
14+
{# EXEC('create view {{ tmp_relation.include(database=False) }} as {{ temp_view_sql }}'); #}
1415

15-
{{ log(relation.include(database=False), info=True) }}
16-
17-
CREATE TABLE {{ relation.include(database=(not temporary), schema=(not temporary)) }}
18-
AS (SELECT * FROM {{ tmp_relation.include(database=False) }})
16+
CREATE TABLE {{ relation }}
17+
AS (SELECT * FROM {{ tmp_relation }})
1918

2019
{{ fabric__drop_relation_script(tmp_relation) }}
2120

dbt/include/fabric/macros/materializations/models/view/create_view_as.sql

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
{% macro fabric__create_view_exec(relation, sql) -%}
77
{#- TODO: add contracts here when in dbt 1.5 -#}
88
{%- set temp_view_sql = sql.replace("'", "''") -%}
9+
10+
{{ use_database_hint() }}
911
execute('create view {{ relation.include(database=False) }} as
1012
{{ temp_view_sql }}
1113
');

test.env.sample

+7-10
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
SQLSERVER_TEST_DRIVER=ODBC Driver 18 for SQL Server
2-
SQLSERVER_TEST_HOST=127.0.0.1
3-
SQLSERVER_TEST_USER=SA
4-
SQLSERVER_TEST_PASS=L0calTesting!
5-
SQLSERVER_TEST_PORT=1433
6-
SQLSERVER_TEST_DBNAME=TestDB
7-
SQLSERVER_TEST_ENCRYPT=True
1+
FABRIC_TEST_DRIVER=ODBC Driver 18 for SQL Server
2+
FABRIC_TEST_HOST=127.0.0.1
3+
FABRIC_TEST_USER=SA
4+
FABRIC_TEST_PASS=L0calTesting!
5+
FABRIC_TEST_PORT=1433
6+
FABRIC_TEST_DBNAME=TestDB
7+
FABRIC_TEST_ENCRYPT=True
88
SQLSERVER_TEST_TRUST_CERT=True
9-
DBT_TEST_USER_1=DBT_TEST_USER_1
10-
DBT_TEST_USER_2=DBT_TEST_USER_2
11-
DBT_TEST_USER_3=DBT_TEST_USER_3

tests/conftest.py

+9-60
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,12 @@ def pytest_addoption(parser):
1919
def dbt_profile_target(request: FixtureRequest, dbt_profile_target_update):
2020
profile = request.config.getoption("--profile")
2121

22-
if profile == "ci_sql_server":
23-
target = _profile_ci_sql_server()
24-
elif profile == "ci_azure_cli":
22+
if profile == "ci_azure_cli":
2523
target = _profile_ci_azure_cli()
2624
elif profile == "ci_azure_auto":
2725
target = _profile_ci_azure_auto()
2826
elif profile == "ci_azure_environment":
2927
target = _profile_ci_azure_environment()
30-
elif profile == "ci_azure_basic":
31-
target = _profile_ci_azure_basic()
32-
elif profile == "user":
33-
target = _profile_user()
3428
elif profile == "user_azure":
3529
target = _profile_user_azure()
3630
else:
@@ -45,17 +39,11 @@ def dbt_profile_target_update():
4539
return {}
4640

4741

48-
@pytest.fixture(scope="class")
49-
def is_azure(request: FixtureRequest) -> bool:
50-
profile = request.config.getoption("--profile")
51-
return "azure" in profile
52-
53-
5442
def _all_profiles_base():
5543
return {
5644
"type": "fabric",
57-
"driver": os.getenv("SQLSERVER_TEST_DRIVER", "ODBC Driver 18 for SQL Server"),
58-
"port": int(os.getenv("SQLSERVER_TEST_PORT", "1433")),
45+
"driver": os.getenv("FABRIC_TEST_DRIVER", "ODBC Driver 18 for SQL Server"),
46+
"port": int(os.getenv("FABRIC_TEST_PORT", "1433")),
5947
"retries": 2,
6048
}
6149

@@ -72,16 +60,6 @@ def _profile_ci_azure_base():
7260
}
7361

7462

75-
def _profile_ci_azure_basic():
76-
return {
77-
**_profile_ci_azure_base(),
78-
**{
79-
"user": os.getenv("DBT_AZURESQL_UID"),
80-
"pass": os.getenv("DBT_AZURESQL_PWD"),
81-
},
82-
}
83-
84-
8563
def _profile_ci_azure_cli():
8664
return {
8765
**_profile_ci_azure_base(),
@@ -109,47 +87,18 @@ def _profile_ci_azure_environment():
10987
}
11088

11189

112-
def _profile_ci_sql_server():
113-
return {
114-
**_all_profiles_base(),
115-
**{
116-
"host": "fabric",
117-
"user": "SA",
118-
"pass": "5atyaNadella",
119-
"database": "TestDB",
120-
"encrypt": True,
121-
"trust_cert": True,
122-
},
123-
}
124-
125-
126-
def _profile_user():
127-
profile = {
128-
**_all_profiles_base(),
129-
**{
130-
"host": os.getenv("SQLSERVER_TEST_HOST"),
131-
"user": os.getenv("SQLSERVER_TEST_USER"),
132-
"pass": os.getenv("SQLSERVER_TEST_PASS"),
133-
"database": os.getenv("SQLSERVER_TEST_DBNAME"),
134-
"encrypt": bool(os.getenv("SQLSERVER_TEST_ENCRYPT", "False")),
135-
"trust_cert": bool(os.getenv("SQLSERVER_TEST_TRUST_CERT", "False")),
136-
},
137-
}
138-
return profile
139-
140-
14190
def _profile_user_azure():
14291
profile = {
14392
**_all_profiles_base(),
14493
**{
145-
"host": os.getenv("SQLSERVER_TEST_HOST"),
146-
"authentication": os.getenv("SQLSERVER_TEST_AUTH", "auto"),
94+
"host": os.getenv("FABRIC_TEST_HOST"),
95+
"authentication": os.getenv("FABRIC_TEST_AUTH", "auto"),
14796
"encrypt": True,
14897
"trust_cert": True,
149-
"database": os.getenv("SQLSERVER_TEST_DBNAME"),
150-
"client_id": os.getenv("SQLSERVER_TEST_CLIENT_ID"),
151-
"client_secret": os.getenv("SQLSERVER_TEST_CLIENT_SECRET"),
152-
"tenant_id": os.getenv("SQLSERVER_TEST_TENANT_ID"),
98+
"database": os.getenv("FABRIC_TEST_DBNAME"),
99+
"client_id": os.getenv("FABRIC_TEST_CLIENT_ID"),
100+
"client_secret": os.getenv("FABRIC_TEST_CLIENT_SECRET"),
101+
"tenant_id": os.getenv("FABRIC_TEST_TENANT_ID"),
153102
},
154103
}
155104
return profile

0 commit comments

Comments
 (0)