diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f45de67..e944d9ce 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,9 +1,15 @@ # Changelog +### v0.19.1 -### v0.19.0.3 +#### features: + +- users can now delcare a model's database to be other than the one specified in the profile. This will only work for on-premise SQL Server and Azure SQL Managed Instance. [#126](https://github.com/dbt-msft/dbt-sqlserver/issues/126) thanks [@semcha](https://github.com/semcha)! #### under the hood + +- abandon four-part version names (`v0.19.0.2`) in favor of three-part version names because it isn't [SemVer](https://semver.org/) and it causes problems with the `~=` pip operator used dbt-synapse, a pacakge that depends on dbt-sqlserver - allow CI to work with the lower-cost serverless Azure SQL [#132](https://github.com/dbt-msft/dbt-sqlserver/pull/132) + ### v0.19.0.2 #### fixes diff --git a/dbt/adapters/sqlserver/__version__.py b/dbt/adapters/sqlserver/__version__.py index c86c5243..22e18d53 100644 --- a/dbt/adapters/sqlserver/__version__.py +++ b/dbt/adapters/sqlserver/__version__.py @@ -1 +1 @@ -version = '0.19.0.2' \ No newline at end of file +version = '0.19.1' \ No newline at end of file diff --git a/dbt/include/sqlserver/macros/adapters.sql b/dbt/include/sqlserver/macros/adapters.sql index 8821983e..b5af3044 100644 --- a/dbt/include/sqlserver/macros/adapters.sql +++ b/dbt/include/sqlserver/macros/adapters.sql @@ -26,15 +26,15 @@ else table_type end as table_type - from information_schema.tables + from [{{ schema_relation.database }}].information_schema.tables where table_schema like '{{ schema_relation.schema }}' - and table_catalog like '{{ schema_relation.database }}' {% endcall %} {{ return(load_result('list_relations_without_caching').table) }} {% endmacro %} {% macro sqlserver__list_schemas(database) %} {% call statement('list_schemas', fetch_result=True, auto_begin=False) -%} + USE {{ database }}; select name as [schema] from sys.schemas {% endcall %} @@ -43,7 +43,7 @@ {% macro sqlserver__create_schema(relation) -%} {% call statement('create_schema') -%} - USE [{{ relation.database }}] + USE [{{ relation.database }}]; IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.without_identifier().schema }}') BEGIN EXEC('CREATE SCHEMA {{ relation.without_identifier().schema }}') @@ -72,17 +72,8 @@ {% endmacro %} {% macro sqlserver__drop_relation(relation) -%} - {% if relation.type == 'view' -%} - {% set object_id_type = 'V' %} - {% elif relation.type == 'table'%} - {% set object_id_type = 'U' %} - {%- else -%} invalid target name - {% endif %} {% call statement('drop_relation', auto_begin=False) -%} - if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null - begin - drop {{ relation.type }} {{ relation.include(database=False) }} - end + {{ sqlserver__drop_relation_script(relation) }} {%- endcall %} {% endmacro %} @@ -93,6 +84,7 @@ {% set object_id_type = 'U' %} {%- else -%} invalid target name {% endif %} + USE [{{ relation.database }}]; if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null begin drop {{ relation.type }} {{ relation.include(database=False) }} @@ -107,14 +99,24 @@ {{ return(load_result('check_schema_exists').table) }} {% endmacro %} + +{% macro sqlserver__create_view_exec(relation, sql) -%} + {%- set temp_view_sql = sql.replace("'", "''") -%} + execute('create view {{ relation.include(database=False) }} as + {{ temp_view_sql }} + '); +{% endmacro %} + + {% macro sqlserver__create_view_as(relation, sql) -%} - create view {{ relation.schema }}.{{ relation.identifier }} as - {{ sql }} + USE [{{ relation.database }}]; + {{ sqlserver__create_view_exec(relation, sql) }} {% endmacro %} {% macro sqlserver__rename_relation(from_relation, to_relation) -%} {% call statement('rename_relation') -%} + USE [{{ to_relation.database }}]; EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}' IF EXISTS( SELECT * @@ -128,6 +130,7 @@ {%- set cci_name = relation.schema ~ '_' ~ relation.identifier ~ '_cci' -%} {%- set relation_name = relation.schema ~ '_' ~ relation.identifier -%} {%- set full_relation = relation.schema ~ '.' ~ relation.identifier -%} + use [{{ relation.database }}]; if EXISTS ( SELECT * FROM sys.indexes WHERE name = '{{cci_name}}' @@ -149,12 +152,13 @@ {{ sqlserver__drop_relation_script(relation) }} - EXEC('create view {{ tmp_relation.schema }}.{{ tmp_relation.identifier }} as + USE [{{ relation.database }}]; + EXEC('create view {{ tmp_relation.include(database=False) }} as {{ temp_view_sql }} '); - SELECT * INTO {{ relation.schema }}.{{ relation.identifier }} FROM - {{ tmp_relation.schema }}.{{ tmp_relation.identifier }} + SELECT * INTO {{ relation }} FROM + {{ tmp_relation }} {{ sqlserver__drop_relation_script(tmp_relation) }} @@ -165,11 +169,7 @@ {% endmacro %}_ {% macro sqlserver__insert_into_from(to_relation, from_relation) -%} - {%- set full_to_relation = to_relation.schema ~ '.' ~ to_relation.identifier -%} - {%- set full_from_relation = from_relation.schema ~ '.' ~ from_relation.identifier -%} - - SELECT * INTO {{full_to_relation}} FROM {{full_from_relation}} - + SELECT * INTO {{ to_relation }} FROM {{ from_relation }} {% endmacro %} {% macro sqlserver__current_timestamp() -%} @@ -192,7 +192,7 @@ character_maximum_length, numeric_precision, numeric_scale - from INFORMATION_SCHEMA.COLUMNS + from [{{ relation.database }}].INFORMATION_SCHEMA.COLUMNS where table_name = '{{ relation.identifier }}' and table_schema = '{{ relation.schema }}' UNION ALL diff --git a/setup.py b/setup.py index 3b5e7560..bab24a3c 100644 --- a/setup.py +++ b/setup.py @@ -28,7 +28,7 @@ def _dbt_sqlserver_version(): package_version = _dbt_sqlserver_version() description = """A sqlserver adapter plugin for dbt (data build tool)""" -dbt_version = '0.19.0' +dbt_version = '0.19' # the package version should be the dbt version, with maybe some things on the # ends of it. (0.18.1 vs 0.18.1a1, 0.18.1.1, ...) if not package_version.startswith(dbt_version):