Skip to content

Commit 7714e30

Browse files
committed
Fix(snowflake): Correctly handle COPY GRANTS property in materialized views
1 parent 815bd98 commit 7714e30

1 file changed

Lines changed: 65 additions & 0 deletions

File tree

tests/core/engine_adapter/test_snowflake.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
from sqlmesh.utils.errors import SQLMeshError
1616
from sqlmesh.utils import optional_import
1717
from tests.core.engine_adapter import to_sql_calls
18+
from sqlmesh.core.model.kind import ViewKind
1819

1920
pytestmark = [pytest.mark.engine, pytest.mark.snowflake]
2021

@@ -727,3 +728,67 @@ def test_table_format_iceberg(snowflake_mocked_engine_adapter: SnowflakeEngineAd
727728
'CREATE ICEBERG TABLE IF NOT EXISTS "test"."table" ("a" INT) CATALOG=\'snowflake\' EXTERNAL_VOLUME=\'test\'',
728729
'CREATE ICEBERG TABLE IF NOT EXISTS "test"."table" CATALOG=\'snowflake\' EXTERNAL_VOLUME=\'test\' AS SELECT CAST("a" AS INT) AS "a" FROM (SELECT CAST("a" AS INT) AS "a") AS "_subquery"',
729730
]
731+
732+
733+
def test_create_view_with_schema_and_grants(
734+
snowflake_mocked_engine_adapter: SnowflakeEngineAdapter,
735+
):
736+
adapter = snowflake_mocked_engine_adapter
737+
738+
model_v = load_sql_based_model(
739+
d.parse(f"""
740+
MODEL (
741+
name test.v,
742+
kind VIEW,
743+
description 'normal **view** from integration test',
744+
dialect 'snowflake'
745+
);
746+
747+
select 1 as "ID", 'foo' as "NAME";
748+
""")
749+
)
750+
751+
model_mv = load_sql_based_model(
752+
d.parse(f"""
753+
MODEL (
754+
name test.mv,
755+
kind VIEW (
756+
materialized true
757+
),
758+
description 'materialized **view** from integration test',
759+
dialect 'snowflake'
760+
);
761+
762+
select 1 as "ID", 'foo' as "NAME";
763+
""")
764+
)
765+
766+
assert isinstance(model_v.kind, ViewKind)
767+
assert isinstance(model_mv.kind, ViewKind)
768+
769+
adapter.create_view(
770+
"target_view",
771+
model_v.render_query_or_raise(),
772+
model_v.columns_to_types,
773+
materialized=model_v.kind.materialized,
774+
view_properties=model_v.render_physical_properties(),
775+
table_description=model_v.description,
776+
column_descriptions=model_v.column_descriptions,
777+
)
778+
779+
adapter.create_view(
780+
"target_materialized_view",
781+
model_mv.render_query_or_raise(),
782+
model_mv.columns_to_types,
783+
materialized=model_mv.kind.materialized,
784+
view_properties=model_mv.render_physical_properties(),
785+
table_description=model_mv.description,
786+
column_descriptions=model_mv.column_descriptions,
787+
)
788+
789+
assert to_sql_calls(adapter) == [
790+
# normal view - COPY GRANTS goes after the column list
791+
"""CREATE OR REPLACE VIEW "target_view" ("ID", "NAME") COPY GRANTS COMMENT='normal **view** from integration test' AS SELECT 1 AS "ID", 'foo' AS "NAME\"""",
792+
# materialized view - COPY GRANTS goes before the column list
793+
"""CREATE OR REPLACE MATERIALIZED VIEW "target_materialized_view" COPY GRANTS ("ID", "NAME") COMMENT='materialized **view** from integration test' AS SELECT 1 AS "ID", 'foo' AS "NAME\"""",
794+
]

0 commit comments

Comments
 (0)