cyrilgdn / terraform-provider-postgresql

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

postgresql_publication has plan diff when tables names are in uppercase #347

Closed nlamirault closed 4 months ago

nlamirault commented 10 months ago

Hi there, we try to create a postgresql_publication with Uppercase tables names.

Like that:

resource "postgresql_publication" "publication" {
  for_each = var.db
  name     = each.value.publication_name
  tables   = [ "public.\"Nationality\"" ]
  database = each.key
}

It works:

iam=> select * from pg_publication_tables;
              pubname               | schemaname |       tablename
------------------------------------+------------+------------------------
...
airbyte_publication_prod_iam_admin | public     | Nationality

but we have always a change in the plan:

# module.postgres-shared-prod-live.postgresql_publication.publication["iam"] will be updated in-place
  ~ resource "postgresql_publication" "publication" {
        id                                   = "iam.airbyte_publication_prod_iam_admin"
        name                             = "airbyte_publication_prod_iam_admin"
      ~ tables                           = [
          - "public.Nationality",
          + "public.\"Nationality\"",
            # (16 unchanged elements hidden)
        ]
        # (6 unchanged attributes hidden)
    }

If we set the table name like that: "public.Nationality", we have that:

iam=> select * from pg_publication_tables;
              pubname               | schemaname |       tablename
------------------------------------+------------+------------------------
...
airbyte_publication_prod_iam_admin | public     | nationality

we make a dirty fix to not be blocked in our provisioning:

re := regexp.MustCompile(`public\.(\w+)`)
for _, p := range added {
        query := fmt.Sprintf("ALTER PUBLICATION %s ADD TABLE %s", pubName, re.ReplaceAllString(p.(string), "public.\"$1\""))
        queries = append(queries, query)
    }

for _, p := range dropped {
        query := fmt.Sprintf("ALTER PUBLICATION %s DROP TABLE %s", pubName, re.ReplaceAllString(p.(string), "public.\"$1\""))
        log.Printf(query)
        queries = append(queries, query)
       }

Do you have any idea how we can fix that ? Thanks.

Thank you for opening an issue. Please provide the following information:

Terraform Version

Run terraform -v to show the version. If you are not running the latest version of Terraform, please upgrade because your issue may have already been fixed.

v1.5.6

Affected Resource(s)

Please list the resources as a list, for example:

If this issue appears to affect multiple resources, it may be an issue with Terraform's core, so please mention this.

Terraform Configuration Files

Debug Output

Panic Output

Expected Behavior

Actual Behavior

Steps to Reproduce

References

jrmbrgs commented 9 months ago

Hello, Just got the same issue, maybe using pq.QuoteIdentifier() (e.g. here) could solve, did not try yet.