pulumi / pulumi-snowflake

Apache License 2.0
17 stars 5 forks source link

Cannot create materialized view with same name in different schemas #144

Open adrienkergastel opened 2 years ago

adrienkergastel commented 2 years ago

What happened?

I have one snowflake database, with two schemas. I want to create one materialized view in each of those schemas, and I want those materialized views to have the same name.

Steps to reproduce

import pulumi_snowflake as snowflake
import pulumi

MY_DATABASE = snowflake.Database(resource_name="test_db", name="test_db")

SCHEMA_1_NAME = "schema_1"
SCHEMA_2_NAME = "schema_2"

MATERIALIZED_VIEW_NAME = "my_materialized_view"

# -- Schema 1

schema_1 = snowflake.Schema(
    resource_name=SCHEMA_1_NAME, database=MY_DATABASE.name, name=SCHEMA_1_NAME
)

table_1 = snowflake.Table(
    resource_name=f"{SCHEMA_1_NAME}-my-table",
    name="my_table",
    schema=schema_1.name,
    database=MY_DATABASE.name,
    columns=[snowflake.TableColumnArgs(name="record", type="VARIANT", nullable=False)],
)

materialized_view_1 = snowflake.MaterializedView(
    resource_name=f"{SCHEMA_1_NAME}-my-mat-view",
    name=MATERIALIZED_VIEW_NAME,
    database=MY_DATABASE.name,
    schema=schema_1.name,
    statement='select * FROM "my_table"',
    or_replace=False,
    is_secure=False,
    warehouse="COMPUTE_WH",
    opts=pulumi.ResourceOptions(depends_on=[table_1]),
)

# -- Schema 2

schema_2 = snowflake.Schema(
    resource_name=SCHEMA_2_NAME, database=MY_DATABASE.name, name=SCHEMA_2_NAME
)

table_2 = snowflake.Table(
    resource_name=f"{SCHEMA_2_NAME}-my-table",
    name="my_table",
    schema=schema_2.name,
    database=MY_DATABASE.name,
    columns=[snowflake.TableColumnArgs(name="record", type="VARIANT", nullable=False)],
)

materialized_view_2 = snowflake.MaterializedView(
    resource_name=f"{SCHEMA_2_NAME}-my-mat-view",
    name=MATERIALIZED_VIEW_NAME,
    database=MY_DATABASE.name,
    schema=schema_2.name,
    statement='select * FROM "my_table"',
    or_replace=False,
    is_secure=False,
    warehouse="COMPUTE_WH",
    opts=pulumi.ResourceOptions(depends_on=[table_2]),
)

When doing pulumi up, everything seem to work fine. When doing pulumi up again, pulumi tries to replace the second materialized view because the schema would have apparently changed

pulumi:pulumi:Stack: (same)
    [urn=urn:pulumi:<...>::pulumi:pulumi:Stack::<...>]
    --snowflake:index/materializedView:MaterializedView: (delete-replaced)
        [id=test_db|schema_2|my_materialized_view]
        [urn=urn:pulumi:<...>::snowflake:index/materializedView:MaterializedView::schema_2-my-mat-view]
        [provider=urn:pulumi:<...>::pulumi:providers:snowflake::default_0_13_0::e3992485-58fa-43b0-a2ff-d55c341778ee]
    +-snowflake:index/materializedView:MaterializedView: (replace)
        [id=test_db|schema_2|my_materialized_view]
        [urn=urn:<...>::snowflake:index/materializedView:MaterializedView::schema_2-my-mat-view]
        [provider=urn:pulumi:<...>::pulumi:providers:snowflake::default_0_13_0::e3992485-58fa-43b0-a2ff-d55c341778ee]
      ~ schema: "schema_1" => "schema_2"
    ++snowflake:index/materializedView:MaterializedView: (create-replacement)
        [id=test_db|schema_2|my_materialized_view]
        [urn=urn:pulumi:<...>::snowflake:index/materializedView:MaterializedView::schema_2-my-mat-view]
        [provider=urn:pulumi:<...>::pulumi:providers:snowflake::default_0_13_0::e3992485-58fa-43b0-a2ff-d55c341778ee]
      ~ schema: "schema_1" => "schema_2"

Expected Behavior

I expect Pulumi to not try to replace the second materialized view when doing pulumi a second time in a row

Actual Behavior

Pulumi tries to replace the second materialized view

Output of pulumi about

❯ pulumi about
CLI          
Version      3.40.1
Go Version   go1.19.1
Go Compiler  gc

Plugins
NAME        VERSION
aiven       4.3.0
docker      3.1.0
gcp         6.12.0
kubernetes  3.16.0
python      unknown
snowflake   0.13.0

Host     
OS       darwin
Version  12.5.1
Arch     x86_64

This project is written in python: executable='/Users/adrien.kergastel/opt/miniconda3/envs/<...>/bin/python3' version='3.8.5
'

Current Stack: my-stack

TYPE                                               URN
pulumi:pulumi:Stack                                urn:pulumi:<..>::pulumi:pulumi:Stack::<...>
pulumi:providers:snowflake                         urn:pulumi:<..>::pulumi:providers:snowflake::default_0_13_0
snowflake:index/database:Database                  urn:pulumi:<..>:::snowflake:index/database:Database::test_db
snowflake:index/schema:Schema                      urn:pulumi:<..>::snowflake:index/schema:Schema::schema_1
snowflake:index/table:Table                        urn:pulumi:<..>::snowflake:index/table:Table::schema_1-my-table
snowflake:index/materializedView:MaterializedView  urn:pulumi:<..>::snowflake:index/materializedView:MaterializedView::schema_1-my-mat-view
snowflake:index/schema:Schema                      urn:pulumi:<..>::snowflake:index/schema:Schema::schema_2
snowflake:index/table:Table                        urn:pulumi<..>:snowflake:index/table:Table::schema_2-my-table
snowflake:index/materializedView:MaterializedView  urn:pulumi:<..>::snowflake:index/materializedView:MaterializedView::schema_2-my-mat-view

Additional context

Manually looking in the stack, I found weird things for the second materialized view The output seem to be inconsistent with the input.

            {
                "urn": "urn:pulumi:<...>::snowflake:index/materializedView:MaterializedView::schema_2-my-mat-view",
                "custom": true,
                "id": "test_db|schema_2|my_materialized_view",
                "type": "snowflake:index/materializedView:MaterializedView",
                "inputs": {
                    "__defaults": [],
                    "database": "test_db",
                    "isSecure": false,
                    "name": "my_materialized_view",
                    "orReplace": false,
                    "schema": "schema_2",
                    "statement": "select * FROM \"my_table\"",
                    "warehouse": "COMPUTE_WH"
                },
                "outputs": {
                    "comment": "",
                    "database": "test_db",
                    "id": "test_db|schema_2|my_materialized_view",
                    "isSecure": false,
                    "name": "my_materialized_view",
                    "orReplace": false,
                    "schema": "schema_1",
                    "statement": "select * FROM \"my_table\"",
                    "warehouse": "COMPUTE_WH"
                },
                "parent": "urn:pulumi:<...>::pulumi:pulumi:Stack::<...>",
                "dependencies": [
                    "urn:pulumi:<...>::snowflake:index/table:Table::schema_2-my-table",
                    "urn:pulumi:<...>::snowflake:index/schema:Schema::schema_2",
                    "urn:pulumi:<...>::snowflake:index/database:Database::test_db"
                ],
                "provider": "urn:pulumi:<...>::pulumi:providers:snowflake::default_0_13_0::e3992485-58fa-43b0-a2ff-d55c341778ee",
                "propertyDependencies": {
                    "database": [
                        "urn:pulumi:<...>::snowflake:index/database:Database::test_db"
                    ],
                    "isSecure": null,
                    "name": null,
                    "orReplace": null,
                    "schema": [
                        "urn:pulumi:<...>::snowflake:index/schema:Schema::schema_2"
                    ],
                    "statement": null,
                    "warehouse": null
                }
            }

Contributing

Vote on this issue by adding a 👍 reaction. To contribute a fix for this issue, leave a comment (and link to your pull request, if you've opened one already).

danielrbradley commented 2 years ago

Thanks for being so thorough with your report @adrienkergastel!

Could I confirm that when you just change the name of the second materialized view, that all the resources deploy as expected? This is just to confirm that the issue you're seeing is definitely a result of the duplicate name rather than any other peripheral issues. Thanks!

adrienkergastel commented 2 years ago

It works fine if I change the name of the second materialized view.

Thank you for your quick response!

mjeffryes commented 4 days ago

Unfortunately, it looks like this issue hasn't seen any updates in a while. If you're still encountering this problem, could you leave a quick comment to let us know so we can prioritize it?