Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Support for MS SQL Server #424

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ The package currently supports
- Snowflake :white_check_mark:
- Google BigQuery :white_check_mark:
- Postgres :white_check_mark:
- SQL Server :white_check_mark:

Models included:

Expand Down
3 changes: 2 additions & 1 deletion dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ models:
+full_refresh: false
+persist_docs:
# Databricks doesn't offer column-level support for persisting docs
columns: '{{ target.name != "databricks" }}'
columns: '{{ target.name != "databricks" and target.name != "sqlserver" }}'
+as_columnstore: False
4 changes: 2 additions & 2 deletions integration_test_project/dbt_project.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ vars:

models:
+persist_docs:
relation: true
columns: true
relation: '{{ target.name != "sqlserver" }}' # sqlserver-adapter doesn't support this
columns: '{{ target.name != "sqlserver" }}' # sqlserver-adapter doesn't support this
seeds:
+quote_columns: false

Expand Down
11 changes: 11 additions & 0 deletions integration_test_project/profiles.yml
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,14 @@ dbt_artifacts:
dbname: postgres
schema: public
threads: 8
sqlserver:
type: sqlserver
driver: 'ODBC Driver 18 for SQL Server'
server: localhost
port: 1433
database: dbt_artifact_integrationtests
schema: dbo
windows_login: False
trust_cert: True
user: dbt
password: "123"
2 changes: 1 addition & 1 deletion integration_test_project/tests/singular_test.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
select 1 as failures from (select 2) as foo where 1 = 2
select 1 as failures from (select 2 as two) as foo where 1 = 2
39 changes: 39 additions & 0 deletions macros/upload_individual_datasets/upload_exposures.sql
Original file line number Diff line number Diff line change
Expand Up @@ -118,3 +118,42 @@
{{ return("") }}
{% endif %}
{%- endmacro %}

{% macro sqlserver__get_exposures_dml_sql(exposures) -%}

{% if exposures != [] %}
{% set exposure_values %}
select "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14"
from ( values
{% for exposure in exposures -%}
(
'{{ invocation_id }}', {# command_invocation_id #}
'{{ exposure.unique_id | replace("'","''") }}', {# node_id #}
'{{ run_started_at }}', {# run_started_at #}
'{{ exposure.name | replace("'","''") }}', {# name #}
'{{ exposure.type }}', {# type #}
'{{ tojson(exposure.owner) }}', {# owner #}
'{{ exposure.maturity }}', {# maturity #}
'{{ exposure.original_file_path }}', {# path #}
'{{ exposure.description | replace("'","''") }}', {# description #}
'{{ exposure.url }}', {# url #}
'{{ exposure.package_name }}', {# package_name #}
'{{ tojson(exposure.depends_on.nodes) }}', {# depends_on_nodes #}
'{{ tojson(exposure.tags) }}', {# tags #}
{% if var('dbt_artifacts_exclude_all_results', false) %}
null
{% else %}
'{{ tojson(exposure) | replace("'", "''") }}' {# all_results #}
{% endif %}
)
{%- if not loop.last %},{%- endif %}
{%- endfor %}

) v ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14")

{% endset %}
{{ exposure_values }}
{% else %}
{{ return("") }}
{% endif %}
{% endmacro -%}
80 changes: 80 additions & 0 deletions macros/upload_individual_datasets/upload_invocations.sql
Original file line number Diff line number Diff line change
Expand Up @@ -221,3 +221,83 @@
{{ invocation_values }}

{% endmacro -%}


{% macro sqlserver__get_invocations_dml_sql() -%}
{% set invocation_values %}
select
"1",
"2",
"3",
"4",
"5",
"6",
"7",
"8",
"9",
"10",
nullif("11", ''),
nullif("12", ''),
nullif("13", ''),
nullif("14", ''),
nullif("15", ''),
"16",
"17",
"18",
"19"
from (values
(
'{{ invocation_id }}', {# command_invocation_id #}
'{{ dbt_version }}', {# dbt_version #}
'{{ project_name }}', {# project_name #}
'{{ run_started_at }}', {# run_started_at #}
'{{ flags.WHICH }}', {# dbt_command #}
'{{ flags.FULL_REFRESH }}', {# full_refresh_flag #}
'{{ target.profile_name }}', {# target_profile_name #}
'{{ target.name }}', {# target_name #}
'{{ target.schema }}', {# target_schema #}
{{ target.threads }}, {# target_threads #}

'{{ env_var('DBT_CLOUD_PROJECT_ID', '') }}', {# dbt_cloud_project_id #}
'{{ env_var('DBT_CLOUD_JOB_ID', '') }}', {# dbt_cloud_job_id #}
'{{ env_var('DBT_CLOUD_RUN_ID', '') }}', {# dbt_cloud_run_id #}
'{{ env_var('DBT_CLOUD_RUN_REASON_CATEGORY', '') }}', {# dbt_cloud_run_reason_category #}
'{{ env_var('DBT_CLOUD_RUN_REASON', '') | replace("'","''") }}', {# dbt_cloud_run_reason #}

{% if var('env_vars', none) %}
{% set env_vars_dict = {} %}
{% for env_variable in var('env_vars') %}
{% do env_vars_dict.update({env_variable: (env_var(env_variable, '') | replace("'", "''"))}) %}
{% endfor %}
'{{ tojson(env_vars_dict) }}', {# env_vars #}
{% else %}
null, {# env_vars #}
{% endif %}

{% if var('dbt_vars', none) %}
{% set dbt_vars_dict = {} %}
{% for dbt_var in var('dbt_vars') %}
{% do dbt_vars_dict.update({dbt_var: (var(dbt_var, '') | replace("'", "''"))}) %}
{% endfor %}
'{{ tojson(dbt_vars_dict) }}', {# dbt_vars #}
{% else %}
null, {# dbt_vars #}
{% endif %}

'{{ tojson(invocation_args_dict) | replace("'", "''") }}', {# invocation_args #}

{% set metadata_env = {} %}
{% for key, value in dbt_metadata_envs.items() %}
{% do metadata_env.update({key: (value | replace("'", "''"))}) %}
{% endfor %}
'{{ tojson(metadata_env) }}' {# dbt_custom_envs #}

)

) v ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19")


{% endset %}
{{ invocation_values }}

{% endmacro -%}
46 changes: 46 additions & 0 deletions macros/upload_individual_datasets/upload_model_executions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -203,3 +203,49 @@
{{ return("") }}
{% endif %}
{%- endmacro %}

{% macro sqlserver__get_model_executions_dml_sql(models) -%}
{% if models != [] %}
{% set model_execution_values %}
select
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"
from ( values
{% for model in models -%}
(
'{{ invocation_id }}', {# command_invocation_id #}
'{{ model.node.unique_id }}', {# node_id #}
'{{ run_started_at }}', {# run_started_at #}

{% set config_full_refresh = model.node.config.full_refresh %}
{% if config_full_refresh is none %}
{% set config_full_refresh = flags.FULL_REFRESH %}
{% endif %}
'{{ config_full_refresh }}', {# was_full_refresh #}

'{{ model.thread_id }}', {# thread_id #}
'{{ model.status }}', {# status #}

{% set compile_started_at = (model.timing | selectattr("name", "eq", "compile") | first | default({}))["started_at"] %}
{% if compile_started_at %}'{{ compile_started_at }}'{% else %}null{% endif %}, {# compile_started_at #}
{% set query_completed_at = (model.timing | selectattr("name", "eq", "execute") | first | default({}))["completed_at"] %}
{% if query_completed_at %}'{{ query_completed_at }}'{% else %}null{% endif %}, {# query_completed_at #}

{{ model.execution_time }}, {# total_node_runtime #}
null, -- rows_affected not available {# Only available in Snowflake & BigQuery #}
'{{ model.node.config.materialized }}', {# materialization #}
'{{ model.node.schema }}', {# schema #}
'{{ model.node.name }}', {# name #}
'{{ model.node.alias }}', {# alias #}
'{{ model.message | replace("'", "''") }}', {# message #}
'{{ tojson(model.adapter_response) | replace("'", "''") }}' {# adapter_response #}
)
{%- if not loop.last %},{%- endif %}
{%- endfor %}
) v ( "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16" )

{% endset %}
{{ model_execution_values }}
{% else %}
{{ return("") }}
{% endif %}
{% endmacro -%}
43 changes: 43 additions & 0 deletions macros/upload_individual_datasets/upload_models.sql
Original file line number Diff line number Diff line change
Expand Up @@ -127,3 +127,46 @@
{{ return("") }}
{% endif %}
{%- endmacro %}

{% macro sqlserver__get_models_dml_sql(models) -%}

{% if models != [] %}
{% set model_values %}
select
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15"
from ( values
{% for model in models -%}
{% set model_copy = model.copy() -%}
{% do model_copy.pop('raw_code', None) %}
(
'{{ invocation_id }}', {# command_invocation_id #}
'{{ model_copy.unique_id }}', {# node_id #}
'{{ run_started_at }}', {# run_started_at #}
'{{ model_copy.database }}', {# database #}
'{{ model_copy.schema }}', {# schema #}
'{{ model_copy.name }}', {# name #}
'{{ tojson(model_copy.depends_on.nodes) }}', {# depends_on_nodes #}
'{{ model_copy.package_name }}', {# package_name #}
'{{ model_copy.original_file_path }}', {# path #}
'{{ model_copy.checksum.checksum }}', {# checksum #}
'{{ model_copy.config.materialized }}', {# materialization #}
'{{ tojson(model_copy.tags) }}', {# tags #}
'{{ tojson(model_copy.config.meta) | replace("'","''") }}', {# meta #}
'{{ model_copy.alias }}', {# alias #}
{% if var('dbt_artifacts_exclude_all_results', false) %}
null
{% else %}
'{{ tojson(model_copy) | replace("'","''") }}' {# all_results #}
{% endif %}
)
{%- if not loop.last %},{%- endif %}
{%- endfor %}

) v ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15")

{% endset %}
{{ model_values }}
{% else %}
{{ return("") }}
{% endif %}
{% endmacro -%}
47 changes: 47 additions & 0 deletions macros/upload_individual_datasets/upload_seed_executions.sql
Original file line number Diff line number Diff line change
Expand Up @@ -217,3 +217,50 @@
{{ return("") }}
{% endif %}
{% endmacro -%}

{% macro sqlserver__get_seed_executions_dml_sql(seeds) -%}
{% if seeds != [] %}
{% set seed_execution_values %}
select
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16"
from ( values
{% for model in seeds -%}
(
'{{ invocation_id }}', {# command_invocation_id #}
'{{ model.node.unique_id }}', {# node_id #}
'{{ run_started_at }}', {# run_started_at #}

{% set config_full_refresh = model.node.config.full_refresh %}
{% if config_full_refresh is none %}
{% set config_full_refresh = flags.FULL_REFRESH %}
{% endif %}
'{{ config_full_refresh }}', {# was_full_refresh #}

'{{ model.thread_id }}', {# thread_id #}
'{{ model.status }}', {# status #}

{% set compile_started_at = (model.timing | selectattr("name", "eq", "compile") | first | default({}))["started_at"] %}
{% if compile_started_at %}'{{ compile_started_at }}'{% else %}null{% endif %}, {# compile_started_at #}
{% set query_completed_at = (model.timing | selectattr("name", "eq", "execute") | first | default({}))["completed_at"] %}
{% if query_completed_at %}'{{ query_completed_at }}'{% else %}null{% endif %}, {# query_completed_at #}

{{ model.execution_time }}, {# total_node_runtime #}
null, -- rows_affected not available {# Only available in Snowflake #}
'{{ model.node.config.materialized }}', {# materialization #}
'{{ model.node.schema }}', {# schema #}
'{{ model.node.name }}', {# name #}
'{{ model.node.alias }}', {# alias #}
'{{ model.message | replace("'", "''") }}', {# message #}
'{{ tojson(model.adapter_response) | replace("'", "''") }}' {# adapter_response #}
)
{%- if not loop.last %},{%- endif %}
{%- endfor %}

) v ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16")

{% endset %}
{{ seed_execution_values }}
{% else %}
{{ return("") }}
{% endif %}
{% endmacro -%}
37 changes: 37 additions & 0 deletions macros/upload_individual_datasets/upload_seeds.sql
Original file line number Diff line number Diff line change
Expand Up @@ -109,3 +109,40 @@
{{ return("") }}
{% endif %}
{%- endmacro %}

{% macro sqlserver__get_seeds_dml_sql(seeds) -%}

{% if seeds != [] %}
{% set seed_values %}
select "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12"
from ( values
{% for seed in seeds -%}
(
'{{ invocation_id }}', {# command_invocation_id #}
'{{ seed.unique_id }}', {# node_id #}
'{{ run_started_at }}', {# run_started_at #}
'{{ seed.database }}', {# database #}
'{{ seed.schema }}', {# schema #}
'{{ seed.name }}', {# name #}
'{{ seed.package_name }}', {# package_name #}
'{{ seed.original_file_path }}', {# path #}
'{{ seed.checksum.checksum }}', {# checksum #}
'{{ tojson(seed.config.meta) | replace("'","''") }}', {# meta #}
'{{ seed.alias }}', {# alias #}
{% if var('dbt_artifacts_exclude_all_results', false) %}
null
{% else %}
'{{ tojson(seed) | replace("'","''") }}' {# all_results #}
{% endif %}
)
{%- if not loop.last %},{%- endif %}
{%- endfor %}

) v ("1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12")

{% endset %}
{{ seed_values }}
{% else %}
{{ return("") }}
{% endif %}
{% endmacro -%}
Loading
Loading