cyrilgdn / terraform-provider-postgresql

Terraform PostgreSQL provider
https://www.terraform.io/docs/providers/postgresql/
Mozilla Public License 2.0
376 stars 194 forks source link

postgresql_publication has plan diff if values in table argument are not in alphabetical order #264

Open RoseateSpoonbill opened 1 year ago

RoseateSpoonbill commented 1 year ago

I am trying to add the configuration of an existing Postgres publication into Terraform. The import works but afterwards terraform plan shows a difference in the table order/sorting

Terraform Version

Terraform v0.14.5
+ provider registry.terraform.io/cyrilgdn/postgresql v1.17.1

Affected Resource(s)

Please list the resources as a list, for example:

Terraform Configuration Files

resource "postgresql_publication" "pub_test" {
  name                             = "test"
  tables                           = ["public.tableA", "public.tableB", "public.tableC", "billing.table1", "billing.table2"]
  owner                            = google_sql_user.x.name
  publish_param                    = ["insert", "update", "delete"]
  publish_via_partition_root_param = false
}

Expected Behavior

If the elements of the table list are the same in Terraform as in the database, there should be no change to the tables argument, regardless of what order the tables are listed in in the configuration.

Actual Behavior

Terraform tries to change the table order when the configuration's table order is not sorted, even though the actual elements are the same.

  # module.module1.postgresql_publication.pub_test will be updated in-place
  ~ resource "postgresql_publication" "pub_test" {
      + drop_cascade                     = false
        id                               = "database1.test"
        name                             = "test"
      ~ publish_param                    = [
            # (2 unchanged elements hidden)
            "delete",
          - "truncate",
        ]
      ~ tables                           = [
          - "billing.table1",
          - "billing.table2",
          - "public.tableC",
          + "public.tableC, billing.table1, billing.table2",
            # (2 unchanged elements hidden)
        ]
        # (4 unchanged attributes hidden)
    }

Steps to Reproduce

  1. terraform import module.module1.postgresql_publication.pub_test database1.test
  2. terraform plan

Important Factoids

This issue doesn't happen if the tables are listed in the configuration in alphabetical order

resource "postgresql_publication" "pub_test" {
  name                             = "test"
  tables                           = ["billing.table1", "billing.table2", "public.tableA", "public.tableB", "public.tableC"]
  owner                            = google_sql_user.x.name
  publish_param                    = ["insert", "update", "delete"]
  publish_via_partition_root_param = false
}

References

Suggestion

I don't know GO but I think that this issue could be resolved by sorting both lists of tables (from the config argument and from the database), the same way, before they were compared

nguyenhoaibao commented 1 year ago

I think this is fixed in https://github.com/cyrilgdn/terraform-provider-postgresql/pull/219, which was released in v1.17.1. Which version are you using?

RoseateSpoonbill commented 1 year ago

I am using v1.17.1. Updating to 1.17.1 solved part of the issue I was seeing, but I still see a diff if the table list in the Terraform configuration is not in alphabetical order