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

PostgreSQL check constraints are not updated when definition changes #2036

Closed
macdonsp opened this issue Aug 31, 2023 · 3 comments
Closed

PostgreSQL check constraints are not updated when definition changes #2036

macdonsp opened this issue Aug 31, 2023 · 3 comments
Assignees

Comments

@macdonsp
Copy link

When doing some testing, I discovered that if you update the definition of a check constraint when using PostgreSQL with HCL input, the change is not detected or applied. To reproduce, start with an initial schema:

schema "public" {
}
table "categories" {
  schema = schema.public
  column "id" {
    null = false
    type = int
  }
  column "name" {
    null = true
    type = varchar(100)
  }
  primary_key {
    columns = [column.id]
  }
}

Run a schema apply which creates the table:

$ atlas schema apply -u "postgresql://localhost:5432/testing?sslmode=disable" --dev-url "docker://postgres/14" -f schema.hcl --auto-approve
-- Planned Changes:
-- Create "categories" table
CREATE TABLE "public"."categories" ("id" integer NOT NULL, "name" character varying(100) NULL, PRIMARY KEY ("id"));

Add a constraint to the table definition:

schema "public" {
}
table "categories" {
  schema = schema.public
  column "id" {
    null = false
    type = int
  }
  column "name" {
    null = true
    type = varchar(100)
  }
  check "validate_id" {
    expr = "id >= 100"
  }
  primary_key {
    columns = [column.id]
  }
}

Run a schema apply which creates the constraint:

$ atlas schema apply -u "postgresql://localhost:5432/testing?sslmode=disable" --dev-url "docker://postgres/14" -f schema.hcl --auto-approve
-- Planned Changes:
-- Modify "categories" table
ALTER TABLE "public"."categories" ADD CONSTRAINT "validate_id" CHECK (id >= 100);

Modify the constraint definition in the schema, changing "id >= 100" to "id >= 500":

schema "public" {
}
table "categories" {
  schema = schema.public
  column "id" {
    null = false
    type = int
  }
  column "name" {
    null = true
    type = varchar(100)
  }
  check "validate_id" {
    expr = "id >= 500"
  }
  primary_key {
    columns = [column.id]
  }
}

Run a schema apply, which does not detect or apply the change:

$ atlas schema apply -u "postgresql://localhost:5432/testing?sslmode=disable" --dev-url "docker://postgres/14" -f schema.hcl --auto-approve
Schema is synced, no changes to be made

This was discovered using the latest version of Atlas. Please let me know if you would like any additional information.

Thanks!

@alexcwatt
Copy link

I found something similar with SQLite. I had a constraint like this

check "type" {
  expr = "type IN ('meeting', 'phone_call')"
}

and found that if I added more options to the constraint, it would not update the database.

@jpjoux
Copy link

jpjoux commented Nov 12, 2023

Same issue as described
I have to delete the constraint, apply the modification and create a new one ....
When can it be corrected ?

@a8m a8m self-assigned this Nov 12, 2023
@a8m
Copy link
Member

a8m commented Oct 31, 2024

Thanks for reporting this. Issue was resolved with #3208

@a8m a8m closed this as completed Oct 31, 2024
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

4 participants