-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #550 from dbt-msft/549-error-on-table-materializat…
…ion-when-using-non-standard-database-names updated create table logic for using proper dbt render
- Loading branch information
Showing
2 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
import pytest | ||
from dbt.tests.util import get_connection, run_dbt | ||
|
||
database_name = "my-data-base" | ||
schema_name = "mysource" | ||
source_table_name = "my_table" | ||
|
||
sources_yml = f""" | ||
version: 2 | ||
sources: | ||
- name: mysource | ||
database: {database_name} | ||
tables: | ||
- name: my_table | ||
""" | ||
|
||
model_sql = """ | ||
{{ config(database="my-data-base", schema="mysource", materialized="table") }} | ||
SELECT | ||
* | ||
FROM | ||
{{ source('mysource', 'my_table') }} | ||
""" | ||
|
||
|
||
class TestNonStandardDB: | ||
def create_db(self, project): | ||
create_sql = """ | ||
DECLARE @col NVARCHAR(256) | ||
SET @col = (SELECT CONVERT (varchar(256), SERVERPROPERTY('collation'))); | ||
IF NOT EXISTS (SELECT * FROM sys.databases WHERE name='{database}') | ||
BEGIN | ||
EXEC ('CREATE DATABASE [{database}] COLLATE ' + @col) | ||
END | ||
""" | ||
|
||
with get_connection(project.adapter): | ||
project.adapter.execute( | ||
create_sql.format(database=database_name), | ||
fetch=True, | ||
) | ||
|
||
def create_source_schema(self, project): | ||
create_sql = """ | ||
USE [{database}]; | ||
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{schema}') | ||
BEGIN | ||
EXEC('CREATE SCHEMA {schema}') | ||
END | ||
""" | ||
with get_connection(project.adapter): | ||
project.adapter.execute( | ||
create_sql.format(database=database_name, schema=schema_name), | ||
fetch=True, | ||
) | ||
|
||
def create_primary_table(self, project): | ||
src_query = """ | ||
SELECT * | ||
INTO | ||
[{database}].{schema}.{table} | ||
FROM | ||
( | ||
SELECT | ||
1 as id, | ||
CAST('2024-01-01' as DATETIME2(6)) updated_at | ||
UNION ALL | ||
SELECT | ||
2 as id, | ||
CAST('2024-01-01' as DATETIME2(6)) updated_at | ||
UNION ALL | ||
SELECT | ||
3 as id, | ||
CAST('2024-01-01' as DATETIME2(6)) updated_at | ||
) as src_data | ||
""" | ||
with get_connection(project.adapter): | ||
project.adapter.execute( | ||
src_query.format( | ||
database=database_name, schema=schema_name, table=source_table_name | ||
), | ||
fetch=True, | ||
) | ||
|
||
def cleanup_primary_table(self, project): | ||
drop_sql = "DROP TABLE IF EXISTS [{database}].{schema}.{table}" | ||
with get_connection(project.adapter): | ||
project.adapter.execute( | ||
drop_sql.format( | ||
database=database_name, schema=schema_name, table=source_table_name | ||
), | ||
fetch=True, | ||
) | ||
|
||
@pytest.fixture(scope="class") | ||
def models(self): | ||
return {"model.sql": model_sql, "sources.yml": sources_yml} | ||
|
||
def test_non_standard_database(self, project): | ||
self.create_db(project) | ||
|
||
self.cleanup_primary_table(project) | ||
self.create_source_schema(project) | ||
self.create_primary_table(project) | ||
|
||
run_dbt() | ||
|
||
self.cleanup_primary_table(project) |