Skip to content

Commit 0cf3d09

Browse files
authored
Merge pull request #382 from dbt-msft/patch-1
Add schema authorization config
2 parents 5f389b1 + 7fdceed commit 0cf3d09

File tree

5 files changed

+94
-18
lines changed

5 files changed

+94
-18
lines changed

dbt/adapters/sqlserver/sql_server_adapter.py

+19
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

+2
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

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,19 @@
11
{% macro sqlserver__create_schema(relation) -%}
22
{% call statement('create_schema') -%}
33
USE [{{ relation.database }}];
4-
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.without_identifier().schema }}')
4+
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
55
BEGIN
6-
EXEC('CREATE SCHEMA [{{ relation.without_identifier().schema }}]')
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 }}]')
717
END
818
{% endcall %}
919
{% endmacro %}

tests/conftest.py

+24-16
Original file line numberDiff line numberDiff line change
@@ -13,25 +13,33 @@ 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, dbt_profile_target_update):
1717
profile = request.config.getoption("--profile")
1818

1919
if profile == "ci_sql_server":
20-
return _profile_ci_sql_server()
21-
if profile == "ci_azure_cli":
22-
return _profile_ci_azure_cli()
23-
if profile == "ci_azure_auto":
24-
return _profile_ci_azure_auto()
25-
if profile == "ci_azure_environment":
26-
return _profile_ci_azure_environment()
27-
if profile == "ci_azure_basic":
28-
return _profile_ci_azure_basic()
29-
if profile == "user":
30-
return _profile_user()
31-
if profile == "user_azure":
32-
return _profile_user_azure()
33-
34-
raise ValueError(f"Unknown profile: {profile}")
20+
target = _profile_ci_sql_server()
21+
elif profile == "ci_azure_cli":
22+
target = _profile_ci_azure_cli()
23+
elif profile == "ci_azure_auto":
24+
target = _profile_ci_azure_auto()
25+
elif profile == "ci_azure_environment":
26+
target = _profile_ci_azure_environment()
27+
elif profile == "ci_azure_basic":
28+
target = _profile_ci_azure_basic()
29+
elif profile == "user":
30+
target = _profile_user()
31+
elif profile == "user_azure":
32+
target = _profile_user_azure()
33+
else:
34+
raise ValueError(f"Unknown profile: {profile}")
35+
36+
target.update(dbt_profile_target_update)
37+
return target
38+
39+
40+
@pytest.fixture(scope="class")
41+
def dbt_profile_target_update():
42+
return {}
3543

3644

3745
@pytest.fixture(scope="class")
+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
3+
import pytest
4+
from dbt.tests.util import run_dbt
5+
6+
7+
class TestSchemaCreation:
8+
@pytest.fixture(scope="class")
9+
def models(self):
10+
return {
11+
"dummy.sql": """
12+
{{ config(schema='with_custom_auth') }}
13+
select 1 as id
14+
""",
15+
}
16+
17+
@staticmethod
18+
@pytest.fixture(scope="class")
19+
def dbt_profile_target_update():
20+
return {"schema_authorization": "{{ env_var('DBT_TEST_USER_1') }}"}
21+
22+
@staticmethod
23+
def _verify_schema_owner(schema_name, owner, project):
24+
get_schema_owner = f"""
25+
select schema_owner from information_schema.schemata where schema_name = '{schema_name}'
26+
"""
27+
result = project.run_sql(get_schema_owner, fetch="one")[0]
28+
assert result == owner
29+
30+
def test_schema_creation(self, project, unique_schema):
31+
res = run_dbt(["run"])
32+
assert len(res) == 1
33+
34+
self._verify_schema_owner(unique_schema, os.getenv("DBT_TEST_USER_1"), project)
35+
self._verify_schema_owner(
36+
unique_schema + "_with_custom_auth", os.getenv("DBT_TEST_USER_1"), project
37+
)

0 commit comments

Comments
 (0)