Skip to content

Commit

Permalink
Updated tests and drop_relation
Browse files Browse the repository at this point in the history
  • Loading branch information
prdpsvs committed May 23, 2024
1 parent 36187bf commit 35110fe
Show file tree
Hide file tree
Showing 17 changed files with 23 additions and 52 deletions.
4 changes: 2 additions & 2 deletions dbt/include/fabric/macros/adapters/columns.sql
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@

{% macro fabric__alter_column_type(relation, column_name, new_column_type) %}

{%- set table_name= tmp_relation.include(database=False).include(schema=False)-%}
{%- set schema_name = tmp_relation.include(database=False).include(identifier=False) -%}
{%- set table_name= relation.include(database=False).include(schema=False)-%}
{%- set schema_name = relation.include(database=False).include(identifier=False) -%}

{% set generate_tmp_relation_script %}
SELECT TRIM(REPLACE(STRING_AGG(ColumnName + ' ', ',-'), '-', CHAR(10))) AS ColumnDef
Expand Down
12 changes: 5 additions & 7 deletions dbt/include/fabric/macros/adapters/relation.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
{{ return(temp_relation) }}
{% endmacro %}

{% macro fabric__drop_relation(relation) -%}
{% macro fabric__get_drop_sql(relation) -%}
{% if relation.type == 'view' -%}
{% call statement('find_references', fetch_result=true) %}
{{ get_use_database_sql(relation.database) }}
Expand All @@ -27,19 +27,17 @@
{% set references = load_result('find_references')['data'] %}
{% for reference in references -%}
-- dropping referenced view {{ reference[0] }}.{{ reference[1] }}
{{ drop_relation_if_exists(relation.incorporate(
{% do adapter.drop_relation(relation.incorporate(
type="view",
path={"schema": reference[0], "identifier": reference[1]})) }}
path={"schema": reference[0], "identifier": reference[1]})) %}
{% endfor %}
{{ get_use_database_sql(relation.database) }}
EXEC('DROP VIEW IF EXISTS {{ relation.include(database=False) }};');
{% elif relation.type == 'table'%}
{% set object_id_type = 'U' %}
{{ get_use_database_sql(relation.database) }}
EXEC('DROP TABLE IF EXISTS {{ relation.include(database=False) }};');
{%- else -%}
{{ exceptions.raise_not_implemented('Invalid relation being dropped: ' ~ relation) }}
{% endif %}
{{ get_use_database_sql(relation.database) }}
EXEC('DROP {{ relation.type }} IF EXISTS {{ relation.include(database=False) }};');
{% endmacro %}

{% macro fabric__rename_relation(from_relation, to_relation) -%}
Expand Down
2 changes: 1 addition & 1 deletion dbt/include/fabric/macros/adapters/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
identifier=row[1],
type=row[3]
) -%}
{% do drop_relation_if_exists(schema_relation) %}
{% do adapter.drop_relation(schema_relation) %}
{%- endfor %}

{% call statement('drop_schema') -%}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
{% elif existing_relation.is_view %}
{#-- Can't overwrite a view with a table - we must drop --#}
{{ log("Dropping relation " ~ target_relation ~ " because it is a view and this model is a table.") }}
{{ drop_relation_if_exists(existing_relation) }}
{% do adapter.drop_relation(existing_relation) %}

{%- call statement('main') -%}
{{ get_create_table_as_sql(False, target_relation, sql)}}
Expand Down Expand Up @@ -79,7 +79,7 @@
{%- endcall -%}
{% endif %}

{% do drop_relation_if_exists(temp_relation) %}
{% do adapter.drop_relation(temp_relation) %}
{{ run_hooks(post_hooks, inside_transaction=True) }}

{% set target_relation = target_relation.incorporate(type='table') %}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
{%- set target_relation = this.incorporate(type='table') -%}

{% call statement('main') %}
{{ drop_relation_if_exists(target_relation) }}
{% do adapter.drop_relation(target_relation) %}
{{ create_or_replace_clone(target_relation, defer_relation) }}
{% endcall %}
{{ return({'relations': [target_relation]}) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
{% set tmp_relation = relation.incorporate(
path={"identifier": relation.identifier.replace("#", "") ~ '_temp_view'},
type='view')-%}
{% do run_query(drop_relation(tmp_relation)) %}

{% do adapter.drop_relation(tmp_relation) %}
{% set contract_config = config.get('contract') %}

{{ get_create_view_as_sql(tmp_relation, sql) }}
Expand All @@ -27,6 +26,6 @@
EXEC('CREATE TABLE [{{relation.database}}].[{{relation.schema}}].[{{relation.identifier}}] AS (SELECT * FROM [{{tmp_relation.database}}].[{{tmp_relation.schema}}].[{{tmp_relation.identifier}}]);');
{% endif %}

{{ drop_relation(tmp_relation) }}
{% do adapter.drop_relation(tmp_relation) %}

{% endmacro %}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@

{% if (existing_relation != none) %}
-- drop the temp relations if they exist already in the database
{{ drop_relation_if_exists(backup_relation) }}

{% do adapter.drop_relation(backup_relation) %}
-- Rename target relation as backup relation
{{ adapter.rename_relation(existing_relation, backup_relation) }}
{% endif %}
Expand Down Expand Up @@ -41,7 +42,7 @@
-- finally, drop the foreign key references if exists
{{ drop_fk_indexes_on_table(backup_relation) }}
-- drop existing/backup relation after the commit
{{ drop_relation_if_exists(backup_relation) }}
{% do adapter.drop_relation(backup_relation) %}
{% endif %}
-- Add constraints including FK relation.
{{ fabric__build_model_constraints(target_relation) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

{% if (existing_relation != none) %}
-- drop the temp relations if they exist already in the database
{{ drop_relation_if_exists(backup_relation) }}
{% do adapter.drop_relation(backup_relation) %}
-- Rename target relation as backup relation
{{ adapter.rename_relation(existing_relation, backup_relation) }}
{% endif %}
Expand All @@ -35,7 +35,7 @@
{{ run_hooks(post_hooks, inside_transaction=True) }}
{{ adapter.commit() }}
{% if (backup_relation != none) %}
{{ drop_relation_if_exists(backup_relation) }}
{% do adapter.drop_relation(backup_relation) %}
{% endif %}
{{ run_hooks(post_hooks, inside_transaction=False) }}
{{ return({'relations': [target_relation]}) }}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
{{ final_sql }}
{% endcall %}

{{ drop_relation_if_exists(temp_snapshot_relation) }}
{% do adapter.drop_relation(temp_snapshot_relation) %}
{% set should_revoke = should_revoke(target_relation_exists, full_refresh_mode=False) %}
{% do apply_grants(target_relation, grant_config, should_revoke=should_revoke) %}

Expand Down
3 changes: 0 additions & 3 deletions tests/functional/adapter/test_aliases.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
)


@pytest.mark.skip(reason="CTAS is not supported without a table.")
class TestAliasesFabric(BaseAliases):
@pytest.fixture(scope="class")
def macros(self):
Expand All @@ -21,14 +20,12 @@ def macros(self):
return {"expect_value.sql": MACROS__EXPECT_VALUE_SQL}


@pytest.mark.skip(reason="Test audit tables are using CTAS on View without a table definition.")
class TestSameAliasDifferentSchemasFabric(BaseSameAliasDifferentSchemas):
@pytest.fixture(scope="class")
def macros(self):
return {"expect_value.sql": MACROS__EXPECT_VALUE_SQL}


@pytest.mark.skip(reason="Test audit tables are using CTAS on View without a table definition.")
class TestSameAliasDifferentDatabasesFabric(BaseSameAliasDifferentDatabases):
@pytest.fixture(scope="class")
def macros(self):
Expand Down
6 changes: 1 addition & 5 deletions tests/functional/adapter/test_basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,11 @@ class TestSimpleMaterializations(BaseSimpleMaterializations):
pass


@pytest.mark.skip(reason="CTAS is not supported without a table.")
class TestSingularTestsFabric(BaseSingularTests):
pass


@pytest.mark.skip(reason="ephemeral not supported")
@pytest.mark.skip(reason="Nested CTE is not supported")
class TestSingularTestsEphemeralFabric(BaseSingularTestsEphemeral):
pass

Expand All @@ -41,8 +40,6 @@ class TestIncrementalFabric(BaseIncremental):
pass


# Modified incremental_not_schema_change.sql file to handle DATETIME compatibility issues.
@pytest.mark.skip(reason="CTAS is not supported without a table.")
class TestIncrementalNotSchemaChangeFabric(BaseIncrementalNotSchemaChange):
@pytest.fixture(scope="class")
def models(self):
Expand Down Expand Up @@ -73,7 +70,6 @@ class TestSnapshotTimestampFabric(BaseSnapshotTimestamp):
pass


# Assertion Failed.
class TestBaseCachingFabric(BaseAdapterMethod):
pass

Expand Down
2 changes: 0 additions & 2 deletions tests/functional/adapter/test_changing_relation_type.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import pytest
from dbt.tests.adapter.relations.test_changing_relation_type import BaseChangeRelationTypeValidator


@pytest.mark.skip(reason="CTAS is not supported without a underlying table definition.")
class TestChangeRelationTypesFabric(BaseChangeRelationTypeValidator):
pass
1 change: 0 additions & 1 deletion tests/functional/adapter/test_empty.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
# from dbt.tests.adapter.empty.test_empty import BaseTestEmpty


# class TestEmpty(BaseTestEmpty):
# pass
7 changes: 3 additions & 4 deletions tests/functional/adapter/test_incremental.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,13 @@
"""


@pytest.mark.skip(reason="CTAS is not supported without a table.")
class TestBaseIncrementalUniqueKeyFabric(BaseIncrementalUniqueKey):
pass


@pytest.mark.skip(reason="CTAS is not supported without a table.")
@pytest.mark.skip(
"The specified ALTER TABLE statement is not supported in this edition of SQL Server."
)
class TestIncrementalOnSchemaChangeFabric(BaseIncrementalOnSchemaChange):
@pytest.fixture(scope="class")
def models(self):
Expand All @@ -112,12 +113,10 @@ def models(self):
}


@pytest.mark.skip(reason="CTAS is not supported without a table.")
class TestIncrementalPredicatesDeleteInsertFabric(BaseIncrementalPredicates):
pass


@pytest.mark.skip(reason="CTAS is not supported without a table.")
class TestPredicatesDeleteInsertFabric(BaseIncrementalPredicates):
@pytest.fixture(scope="class")
def project_config_update(self):
Expand Down
14 changes: 0 additions & 14 deletions tests/functional/adapter/test_list_relations_without_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,6 @@ def test__fabric__list_relations_without_caching(self, project):
schemas = project.created_schemas

for schema in schemas:
# schema_relation = BaseRelation.create(schema=schema, database=database)
# schema_relation = f"{database}.{schema}"
kwargs = {"schema_relation": schema}
_, log_output = run_dbt_and_capture(
[
Expand All @@ -121,13 +119,6 @@ def test__fabric__list_relations_without_caching(self, project):
str(kwargs),
]
)

# parsed_logs = parse_json_logs(log_output)
# print(parsed_logs)
# n_relations = find_result_in_parsed_logs(parsed_logs, "n_relations")

# assert n_relations == "n_relations: 1"
# assert "n_relations: 1" in log_output
assert "n_relations: 2" in log_output


Expand Down Expand Up @@ -166,9 +157,4 @@ def test__fabric__list_relations_without_caching(self, project):
str(kwargs),
]
)

# parsed_logs = parse_json_logs(log_output)
# n_relations = find_result_in_parsed_logs(parsed_logs, "n_relations")

# assert n_relations == f"n_relations: {NUM_EXPECTED_RELATIONS}"
assert "n_relations: 12" in log_output
1 change: 0 additions & 1 deletion tests/functional/adapter/test_query_comment.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
)


# Tests #
class TestQueryComments(BaseQueryComments):
pass

Expand Down
1 change: 0 additions & 1 deletion tests/functional/adapter/test_store_test_failures.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
# TestStoreTestFailures,
# )


tests__passing_test = """
select * from {{ ref('fine_model') }}
where 1=2
Expand Down

0 comments on commit 35110fe

Please sign in to comment.