cyrilgdn / terraform-provider-postgresql

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

fix: drop cascade for extensions and schemas #298

Closed FabioAntunes closed 4 months ago

FabioAntunes commented 1 year ago

When creating extensions or schemas we can provide a boolean value for the drop_cascade argument. Unfortunately, this value is never picked up and it always defaults to false.

If we have the following resource:

resource "postgresql_schema" "my_schema" {
  name  = "my_schema"
  owner = "postgres"
  drop_cascade = false
}

And change it to this

resource "postgresql_schema" "my_schema" {
  name  = "my_schema"
  owner = "postgres"
  drop_cascade = true
}

With the current version, there are no changes to plan.

I don't have a way to test this locally on my machine, so would be great if someone could test this.

FabioAntunes commented 11 months ago

Hey @cyrilgdn apologies for the tag, do you think you can have a look into this?

debu99 commented 10 months ago

Is this fix able to merge?

cyrilgdn commented 10 months ago

Hello @FabioAntunes,

Sorry for the late answer. Do you still encounter this issue. I try to reproduce the example you provided, and when I switch to true I have the right diff in the plan :thinking:

Also, unless I miss something your change is to set in the state the value you retrieved from the same state.

SGrondin commented 9 months ago

@cyrilgdn I can confirm that the problem is still happening with version 1.21.0

I'm having to remove the extension from the TF state manually to circumvent the problem.

cyrilgdn commented 4 months ago

Hi,

I allow myself to close this issue as for me both the resource update and the drop cascade work, also as I said this fix can't really fix anything

Here are the logs of what happens if I try to reproduce your issue:

$ cat test.tf 
resource "postgresql_schema" "my_schema" {
  name         = "my_schema"
  owner        = "postgres"
  drop_cascade = false
}

❯ terraform apply               
Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # postgresql_schema.my_schema will be created
  + resource "postgresql_schema" "my_schema" {
      + database      = (known after apply)
      + drop_cascade  = false
      + id            = (known after apply)
      + if_not_exists = true
      + name          = "my_schema"
      + owner         = "postgres"
    }

Plan: 1 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

postgresql_schema.my_schema: Creating...
postgresql_schema.my_schema: Creation complete after 0s [id=postgres.my_schema]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

❯ sed -i "s/false/true/" test.tf

❯ terraform apply               
postgresql_schema.my_schema: Refreshing state... [id=postgres.my_schema]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  ~ update in-place

Terraform will perform the following actions:

  # postgresql_schema.my_schema will be updated in-place
  ~ resource "postgresql_schema" "my_schema" {
      ~ drop_cascade  = false -> true
        id            = "postgres.my_schema"
        name          = "my_schema"
        # (3 unchanged attributes hidden)
    }

Plan: 0 to add, 1 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

postgresql_schema.my_schema: Modifying... [id=postgres.my_schema]
postgresql_schema.my_schema: Modifications complete after 0s [id=postgres.my_schema]

Apply complete! Resources: 0 added, 1 changed, 0 destroyed.

❯ psql -c "create table my_schema.test_table (id serial)" 
CREATE TABLE

❯ terraform destroy 
postgresql_schema.my_schema: Refreshing state... [id=postgres.my_schema]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # postgresql_schema.my_schema will be destroyed
  - resource "postgresql_schema" "my_schema" {
      - database      = "postgres" -> null
      - drop_cascade  = true -> null
      - id            = "postgres.my_schema" -> null
      - if_not_exists = true -> null
      - name          = "my_schema" -> null
      - owner         = "postgres" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

postgresql_schema.my_schema: Destroying... [id=postgres.my_schema]
postgresql_schema.my_schema: Destruction complete after 0s

Destroy complete! Resources: 1 destroyed.

If I do the same destroy after creating a table in the schema but without switching drop_cascade to true before, I have an error as expected:

❯ terraform destroy                                      
postgresql_schema.my_schema: Refreshing state... [id=postgres.my_schema]

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # postgresql_schema.my_schema will be destroyed
  - resource "postgresql_schema" "my_schema" {
      - database      = "postgres" -> null
      - drop_cascade  = false -> null
      - id            = "postgres.my_schema" -> null
      - if_not_exists = true -> null
      - name          = "my_schema" -> null
      - owner         = "postgres" -> null
    }

Plan: 0 to add, 0 to change, 1 to destroy.

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

postgresql_schema.my_schema: Destroying... [id=postgres.my_schema]
╷
│ Error: Error deleting schema: pq: cannot drop schema my_schema because other objects depend on it