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

Removing a grant on a dropped table fails #362

Open
TPXP opened this issue Oct 12, 2023 · 3 comments
Open

Removing a grant on a dropped table fails #362

TPXP opened this issue Oct 12, 2023 · 3 comments

Comments

@TPXP
Copy link

TPXP commented Oct 12, 2023

Hi there,

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

Terraform Version

 terraform -v
Terraform v1.5.7
on darwin_arm64
+ provider registry.terraform.io/cyrilgdn/postgresql v1.21.0

Your version of Terraform is out of date! The latest version
is 1.6.1. You can update by downloading from https://www.terraform.io/downloads.html

Upgrading to the latest terraform version does not fix my issue

Affected Resource(s)

  • postgresql_grant
  • (possibly others, haven't checked)

Terraform Configuration Files

terraform {
  required_providers {
    postgresql = {
      source = "cyrilgdn/postgresql"
      version = "~> 1.21.0"
    }
  }
}

provider "postgresql" {
  host            = "127.0.0.1"
  port            = 5432
  database        = "postgres"
  username        = "postgres"
  password        = "postgres"
  connect_timeout = 15
  sslmode         = "disable"
}

resource "postgresql_role" "test" {
    name = "test"
    login = true
    password = "test"
}

# Drop lines below after dropping the "test" table
resource "postgresql_grant" "test" {
    database = "postgres"
    role = "test"
    schema = "public"
    object_type = "table"
    objects = ["test"]
    privileges = ["SELECT"]
}

Debug Output

https://pastebin.com/pt5V9ube

Panic Output

No panic

Expected Behavior

I expect the provider to silently ignore the resource removal since the ressource has already been removed when I dropped the table.

Actual Behavior

The module tried to remove the resource anyway and raised an error.

Error: could not execute revoke query: pq: relation "public.test" does not exist

Steps to Reproduce

  1. Start the database in a docker container and create the "test" table
docker run --rm -p 5432:5432 -e POSTGRES_PASSWORD=postgres postgres
# In another shell
psql -h 127.0.0.1 -p 5432 -U postgres
# password is postgres
create table test(id int);
\d
\q
terraform init
terraform apply
  1. Now, drop the "test" table and remove the "test" grant from the main.tf file
psql -h 127.0.0.1 -p 5432 -U postgres
# password is postgres
drop table test;
\q
terraform apply

Important Factoids

I was able to reproduce this on a docker instance as well as on RDS

References

None, I looked for the error message

@aibou
Copy link

aibou commented May 17, 2024

I encounted this issue that can be avoided by terraform state rm & terraform apply.

@TPXP
Copy link
Author

TPXP commented May 20, 2024

Indeed, state rm will work around the issue, but I'd prefer the terraform module to realize this grant does not exist anymore automatically

@dhruvatee
Copy link

This would be really useful if provider handle this automatically

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants