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

clean up relation usage and default behavior #92

Closed
wants to merge 23 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions dbt/adapters/sqlserver/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from dbt.adapters.sqlserver.connections import SQLServerConnectionManager
from dbt.adapters.sqlserver.connections import SQLServerCredentials
from dbt.adapters.sqlserver.impl import SQLServerAdapter
from dbt.adapters.sqlserver.relation import SQLServerRelation

from dbt.adapters.base import AdapterPlugin
from dbt.include import sqlserver
Expand Down
3 changes: 2 additions & 1 deletion dbt/adapters/sqlserver/impl.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from dbt.adapters.sql import SQLAdapter
from dbt.adapters.sqlserver import SQLServerConnectionManager
from dbt.adapters.sqlserver import SQLServerRelation
from dbt.adapters.base.relation import BaseRelation
import agate
from typing import (
Expand All @@ -10,7 +11,7 @@

class SQLServerAdapter(SQLAdapter):
ConnectionManager = SQLServerConnectionManager

Relation = SQLServerRelation
@classmethod
def date_function(cls):
return "getdate()"
Expand Down
27 changes: 27 additions & 0 deletions dbt/adapters/sqlserver/relation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import Optional

from dataclasses import dataclass

from dbt.adapters.base.relation import BaseRelation, Policy
from dbt.exceptions import RuntimeException


@dataclass
class SQLServerQuotePolicy(Policy):
database: bool = False
schema: bool = False
identifier: bool = False


@dataclass
class SQLServerIncludePolicy(Policy):
database: bool = False
schema: bool = True
identifier: bool = True


@dataclass(frozen=True, eq=False, repr=False)
class SQLServerRelation(BaseRelation):
quote_policy: SQLServerQuotePolicy = SQLServerQuotePolicy()
include_policy: SQLServerIncludePolicy = SQLServerIncludePolicy()
quote_character: str = '\''
34 changes: 16 additions & 18 deletions dbt/include/sqlserver/macros/adapters.sql
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@
{% macro sqlserver__create_schema(relation) -%}
{% call statement('create_schema') -%}
USE [{{ relation.database }}]
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.without_identifier().schema }}')
IF NOT EXISTS (SELECT * FROM sys.schemas WHERE name = '{{ relation.schema }}')
BEGIN
EXEC('CREATE SCHEMA {{ relation.without_identifier().schema }}')
EXEC('CREATE SCHEMA {{ relation.schema }}')
END
{% endcall %}
{% endmacro %}
Expand Down Expand Up @@ -79,9 +79,9 @@
{%- 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
if object_id ('{{ relation }}','{{ object_id_type }}') is not null
begin
drop {{ relation.type }} {{ relation.include(database=False) }}
drop {{ relation.type }} {{ relation }}
end
{%- endcall %}
{% endmacro %}
Expand All @@ -93,9 +93,9 @@
{% set object_id_type = 'U' %}
{%- else -%} invalid target name
{% endif %}
if object_id ('{{ relation.include(database=False) }}','{{ object_id_type }}') is not null
if object_id ('{{ relation }}','{{ object_id_type }}') is not null
begin
drop {{ relation.type }} {{ relation.include(database=False) }}
drop {{ relation.type }} {{ relation }}
end
{% endmacro %}

Expand All @@ -108,19 +108,19 @@
{% endmacro %}

{% macro sqlserver__create_view_as(relation, sql) -%}
create view {{ relation.schema }}.{{ relation.identifier }} as
create view {{ relation }} as
{{ sql }}
{% endmacro %}


{% macro sqlserver__rename_relation(from_relation, to_relation) -%}
{% call statement('rename_relation') -%}
EXEC sp_rename '{{ from_relation.schema }}.{{ from_relation.identifier }}', '{{ to_relation.identifier }}'
EXEC sp_rename '{{ from_relation }}', '{{ to_relation.identifier }}'
IF EXISTS(
SELECT *
FROM sys.indexes
WHERE name='{{ from_relation.schema }}_{{ from_relation.identifier }}_cci' and object_id = OBJECT_ID('{{ from_relation.schema }}.{{ to_relation.identifier }}'))
EXEC sp_rename N'{{ from_relation.schema }}.{{ to_relation.identifier }}.{{ from_relation.schema }}_{{ from_relation.identifier }}_cci', N'{{ from_relation.schema }}_{{ to_relation.identifier }}_cci', N'INDEX'
WHERE name='{{ from_relation.schema }}_{{ from_relation.identifier }}_cci' and object_id = OBJECT_ID('{{ from_relation }}'))
EXEC sp_rename N'{{ from_relation }}.{{ from_relation.schema }}_{{ from_relation.identifier }}_cci', N'{{ from_relation.schema }}_{{ to_relation.identifier }}_cci', N'INDEX'

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are we prefixing with the entire relation now? That includes double-quoted schema and relation names, which is creating really bizarre output in my run:

EXEC sp_rename N'"dbo"."APPT_COMPLETED_MONTHLY__dbt_tmp".dbo_APPT_COMPLETED_MONTHLY__dbt_tmp_cci', N'dbo_APPT_COMPLETED_MONTHLY_cci', N'INDEX'

{%- endcall %}
{% endmacro %}

Expand All @@ -133,9 +133,9 @@
sys.indexes WHERE name = '{{cci_name}}'
AND object_id=object_id('{{relation_name}}')
)
DROP index {{full_relation}}.{{cci_name}}
DROP index {{relation}}.{{cci_name}}
CREATE CLUSTERED COLUMNSTORE INDEX {{cci_name}}
ON {{full_relation}}
ON {{relation}}
{% endmacro %}

{% macro sqlserver__create_table_as(temporary, relation, sql) -%}
Expand All @@ -149,12 +149,12 @@

{{ sqlserver__drop_relation_script(relation) }}

EXEC('create view {{ tmp_relation.schema }}.{{ tmp_relation.identifier }} as
EXEC('create view {{ tmp_relation }} 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) }}

Expand All @@ -165,10 +165,8 @@
{% 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 %}

Expand Down