petoju / terraform-provider-mysql

Terraform MySQL provider – unofficial fork
https://registry.terraform.io/providers/petoju/mysql
Mozilla Public License 2.0
63 stars 40 forks source link

Reasoning of not supporting CREATE IF NOT EXISTS for users and databases? #67

Closed xiaxuantan closed 1 year ago

xiaxuantan commented 1 year ago

Terraform Version

$ terraform -v
Terraform v1.3.7
on darwin_arm64

Affected Resource(s)

Mainly mysql_users and mysql_database.

Terraform Configuration Files

resource "mysql_user" "user" {
  user               = var.user
  host               = var.host
  plaintext_password = ...
}

Debug Output

Panic Output

Expected Behavior

When trying to create a user or database for an imported database or for a database restored from a snapshot where the users/databases already exist, we expect the creation to succeed anyway.

Actual Behavior

We got errors saying the users already exist.

While it might sound like a terraform import issue, it's hard to include it in our automation and I wonder if it's possible to add a new boolean parameter adopt_if_exists to these resources. If it's specified, the provider uses verb CREATE xx IF NOT EXISTS.

Suggested change

resource "mysql_user" "user" {
  user               = var.user
  host               = var.host
  plaintext_password = ...
  adopt_if_exists    = true
}

so that the creation can succeed regardless of the pre-existence of the resources.

Steps to Reproduce

Important Factoids

References

petoju commented 1 year ago

The main reasons not to support it:

  1. Adopting makes it possible to include one user twice. Deleting one of them leads to deletion of a user and creation again on the next apply. That's not something we want.
  2. terraform import is really easy and it just works in cases one wants to use a pre-existing resource.
  3. It's more coding and more tests. That's a price we're not willing to pay without obvious benefits.
xiaxuantan commented 1 year ago
  • Adopting makes it possible to include one user twice. Deleting one of them leads to deletion of a user and creation again on the next apply. That's not something we want.

Cool thanks for the explanation.