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

Forced Replacement of one resource renders another resource invalid #2397

Open
justin-ramirez-gametime opened this issue Jan 22, 2024 · 2 comments
Labels
category:resource feature-request Used to mark issues with provider's missing functionalities resource:materialized_view Issue connected to the snowflake_materialized_view resource

Comments

@justin-ramirez-gametime

Terraform CLI and Provider Versions

Terraform v1.7.0 on darwin_arm64
provider registry.terraform.io/snowflake-labs/snowflake v0.84.0

Terraform Configuration

resource "snowflake_materialized_view" "materialized_view" {
  provider   = snowflake.sys_admin
  database   = var.database_name
  schema     = var.schema_name
  name       = var.materialized_view_name
  warehouse  = var.warehouse_name
  comment    = var.description
  statement  = var.sql_statement
  or_replace = var.replace_if_exists
  is_secure  = var.is_secure
}

locals {
  materialized_views = [
    {
      database_name          = module.snowflake_external_table["<resource_name>"].database_name
      schema_name            = module.snowflake_external_table["<resource_name>"].schema_name
      materialized_view_name = "<resource_name>_MV"
      sql_statement          = <<SQL
      SELECT *
      FROM ${module.snowflake_external_table["<resource_name>"].database_name}.${module.snowflake_external_table["<resource_name>"].schema_name}.${module.snowflake_external_table["<resource_name>"].external_table_name};
      SQL
      warehouse_name         = module.team_warehouses["DATA_ENGINEER_LARGE"].warehouse_name
      description            = "Depends on table: ${module.snowflake_external_table["<resource_name>"].id}"  # our attempt to update whenever the source table changes
      is_secure              = false
      replace_if_exists      = true
    },
    #...
  ]
}

module "snowflake_materialized_view" {
  source = "../../../modules/snowflake/materialized-view"
  providers = {
    snowflake.sys_admin = snowflake.sys_admin
  }
  for_each = {
    for materialized_view in local.materialized_views :
    "${materialized_view.database_name}.${materialized_view.schema_name}.${materialized_view.materialized_view_name}"
    => materialized_view
  }
  database_name          = each.value.database_name
  schema_name            = each.value.schema_name
  materialized_view_name = each.value.materialized_view_name
  sql_statement          = each.value.sql_statement
  warehouse_name         = each.value.warehouse_name
  description            = lookup(each.value, "description", null)
  is_secure              = lookup(each.value, "is_secure", null)
  replace_if_exists      = lookup(each.value, "replace_if_exists", null)
}

Expected Behavior

Make changes to an external table(i.e. change comment field) -> it will be destroyed and replaced on apply(# forces replacement) -> if there is a materialized view which depends on it -> the materialized view is destroyed and recreated after the external table is recreated.

Actual Behavior

If any change is made to an external table, that table is destroyed and recreated on apply(# forces replacement).
Screenshot 2024-01-19 at 7 22 03 PM

When this happens, any materialized_view which is based on that table is marked as invalid because the resource it was based on has been dropped. We tried to make the materialized_view depend on the id of the external table(shown in the config code above). But this only led to an 'edit in place' for the materialized_view.
Screenshot 2024-01-19 at 7 22 16 PM

If any other changes are attempted on the invalid materialized_view on subsequent applies, you will get an error. The Materialized view needs to be destroyed and recreated in order to get it back.
Screenshot 2024-01-19 at 6 27 30 PM

Steps to Reproduce

  1. terraform apply
  2. query materialized view

How much impact is this issue causing?

High

Logs

No response

Additional Information

This same problem has affected external tables when a change was made to the stage which it depends on. It seems like this issue will affect any resource which depends on a particular underlying resource and is marked as invalid when that resource is dropped from the warehouse.

@justin-ramirez-gametime justin-ramirez-gametime added the bug Used to mark issues with provider's incorrect behavior label Jan 22, 2024
@sfc-gh-asawicki
Copy link
Collaborator

Hey @justin-ramirez-gametime. Thanks for reporting the issue.

Did you try to use https://developer.hashicorp.com/terraform/language/meta-arguments/lifecycle#replace_triggered_by?

We are currently working on updating the old underlying implementation of this resource to the new SDK. It won't change this behavior, though. We have a redesign of resources upcoming on our roadmap (https://github.com/Snowflake-Labs/terraform-provider-snowflake/blob/main/ROADMAP.md#19012024-roadmap-overview). I will add this as a topic to discuss then.

@sfc-gh-asawicki sfc-gh-asawicki added feature-request Used to mark issues with provider's missing functionalities and removed bug Used to mark issues with provider's incorrect behavior labels Jan 23, 2024
@justin-ramirez-gametime
Copy link
Author

This was helpful. Here is what we did for now:

resource "snowflake_materialized_view" "materialized_view" {
  provider   = snowflake.sys_admin
  database   = var.database_name
  schema     = var.schema_name
  name       = var.materialized_view_name
  warehouse  = var.warehouse_name
  comment    = var.description
  statement  = var.sql_statement
  or_replace = var.replace_if_exists
  is_secure  = var.is_secure
  # This is a workaround to force the materialized view to be replaced when the source table is changed/recreated
  lifecycle {
    replace_triggered_by = [terraform_data.replacement]
  }
}

resource "terraform_data" "replacement" {
  input = var.source_id
}

@sfc-gh-jcieslak sfc-gh-jcieslak added resource:materialized_view Issue connected to the snowflake_materialized_view resource category:resource labels May 20, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
category:resource feature-request Used to mark issues with provider's missing functionalities resource:materialized_view Issue connected to the snowflake_materialized_view resource
Projects
None yet
Development

No branches or pull requests

3 participants