Skip to content

Commit de806a1

Browse files
committed
fix impl
1 parent 75493af commit de806a1

File tree

5 files changed

+51
-19
lines changed

5 files changed

+51
-19
lines changed

dbt/adapters/sqlserver/sql_server_adapter.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
import agate
44
from dbt.adapters.base.relation import BaseRelation
5+
from dbt.adapters.cache import _make_ref_key_msg
56
from dbt.adapters.sql import SQLAdapter
7+
from dbt.adapters.sql.impl import CREATE_SCHEMA_MACRO_NAME
8+
from dbt.events.functions import fire_event
9+
from dbt.events.types import SchemaCreation
610

711
from dbt.adapters.sqlserver.sql_server_column import SQLServerColumn
812
from dbt.adapters.sqlserver.sql_server_configs import SQLServerConfigs
@@ -14,6 +18,21 @@ class SQLServerAdapter(SQLAdapter):
1418
Column = SQLServerColumn
1519
AdapterSpecificConfigs = SQLServerConfigs
1620

21+
def create_schema(self, relation: BaseRelation) -> None:
22+
relation = relation.without_identifier()
23+
fire_event(SchemaCreation(relation=_make_ref_key_msg(relation)))
24+
macro_name = CREATE_SCHEMA_MACRO_NAME
25+
kwargs = {
26+
"relation": relation,
27+
}
28+
29+
if self.config.credentials.schema_authorization:
30+
kwargs["schema_authorization"] = self.config.credentials.schema_authorization
31+
macro_name = "sqlserver__create_schema_with_authorization"
32+
33+
self.execute_macro(macro_name, kwargs=kwargs)
34+
self.commit_if_has_connection()
35+
1736
@classmethod
1837
def date_function(cls):
1938
return "getdate()"

dbt/adapters/sqlserver/sql_server_credentials.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ class SQLServerCredentials(Credentials):
2121
encrypt: Optional[bool] = True # default value in MS ODBC Driver 18 as well
2222
trust_cert: Optional[bool] = False # default value in MS ODBC Driver 18 as well
2323
retries: int = 1
24+
schema_authorization: Optional[str] = None
2425

2526
_ALIASES = {
2627
"user": "UID",
@@ -33,6 +34,7 @@ class SQLServerCredentials(Credentials):
3334
"app_id": "client_id",
3435
"app_secret": "client_secret",
3536
"TrustServerCertificate": "trust_cert",
37+
"schema_auth": "schema_authorization",
3638
}
3739

3840
@property

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

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,19 @@
11
{% macro sqlserver__create_schema(relation) -%}
2-
{% set schema_authorization = config.get("schema_authorization") %}
32
{% call statement('create_schema') -%}
43
USE [{{ relation.database }}];
54
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
65
BEGIN
7-
EXEC('CREATE SCHEMA [{{ relation.schema }}] {% if schema_authorization %} AUTHORIZATION: [{{ schema_authorization }}] {% endif %}')
6+
EXEC('CREATE SCHEMA [{{ relation.schema }}]')
7+
END
8+
{% endcall %}
9+
{% endmacro %}
10+
11+
{% macro sqlserver__create_schema_with_authorization(relation, schema_authorization) -%}
12+
{% call statement('create_schema') -%}
13+
USE [{{ relation.database }}];
14+
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
15+
BEGIN
16+
EXEC('CREATE SCHEMA [{{ relation.schema }}] AUTHORIZATION [{{ schema_authorization }}]')
817
END
918
{% endcall %}
1019
{% endmacro %}

tests/conftest.py

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -13,27 +13,32 @@ def pytest_addoption(parser):
1313

1414

1515
@pytest.fixture(scope="class")
16-
def dbt_profile_target(request: FixtureRequest):
16+
def dbt_profile_target(request: FixtureRequest, profile_extra_options):
1717
profile = request.config.getoption("--profile")
1818

1919
if profile == "ci_sql_server":
20-
return _profile_ci_sql_server()
20+
return _profile_ci_sql_server() | profile_extra_options
2121
if profile == "ci_azure_cli":
22-
return _profile_ci_azure_cli()
22+
return _profile_ci_azure_cli() | profile_extra_options
2323
if profile == "ci_azure_auto":
24-
return _profile_ci_azure_auto()
24+
return _profile_ci_azure_auto() | profile_extra_options
2525
if profile == "ci_azure_environment":
26-
return _profile_ci_azure_environment()
26+
return _profile_ci_azure_environment() | profile_extra_options
2727
if profile == "ci_azure_basic":
28-
return _profile_ci_azure_basic()
28+
return _profile_ci_azure_basic() | profile_extra_options
2929
if profile == "user":
30-
return _profile_user()
30+
return _profile_user() | profile_extra_options
3131
if profile == "user_azure":
32-
return _profile_user_azure()
32+
return _profile_user_azure() | profile_extra_options
3333

3434
raise ValueError(f"Unknown profile: {profile}")
3535

3636

37+
@pytest.fixture(scope="class")
38+
def profile_extra_options():
39+
return {}
40+
41+
3742
@pytest.fixture(scope="class")
3843
def is_azure(request: FixtureRequest) -> bool:
3944
profile = request.config.getoption("--profile")

tests/functional/adapter/test_schema.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,16 @@ class TestSchemaCreation:
77
def models(self):
88
return {
99
"dummy.sql": """
10-
{{ config(schema='custom_test_schema') }}
11-
select 1 as id
12-
""",
13-
"dummy_with_auth.sql": """
14-
{{ config(schema='custom_test_schema', schema_authorization=env_var('DBT_TEST_USER_1')) }}
10+
{{ config(schema='with_custom_auth') }}
1511
select 1 as id
1612
""",
1713
}
1814

15+
@staticmethod
16+
@pytest.fixture(scope="class")
17+
def profile_extra_options():
18+
return {"schema_authorization": "{{ env_var('DBT_TEST_USER_1') }}"}
19+
1920
def test_schema_creation(self, project):
2021
res = run_dbt(["run", "-s", "dummy"])
2122
assert len(res) == 1
22-
23-
def test_schema_creation_with_auth(self, project):
24-
res = run_dbt(["run", "-s", "dummy_with_auth"])
25-
assert len(res) == 1

0 commit comments

Comments
 (0)