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

Validate that unique constraint name is unique #427

Merged
merged 2 commits into from
Oct 23, 2024

Conversation

ryanslade
Copy link
Collaborator

I'm not sure this is exactly what we want, but putting it up here for review.

This will now catch the case when a unique constraint is added with a name that already exists on the table.
All this really does in shift the check to the Start operation instead of the Complete operation.

Before this changed we get this error:

Failed to complete migration: unable to execute complete operation: pq: index "reviews_username_key" is already associated with a constraint

Afterwards we get this:

Failed to start migration: migration is invalid: constraint "reviews_username_key" on table "reviews" already exists

Part of #105

@exekias
Copy link
Member

exekias commented Oct 23, 2024

good catch! I guess we want the same for the other constraint types? checks & foreign keys

@ryanslade
Copy link
Collaborator Author

Yep, those are mentioned in this issue too. I just wanted to make sure this was the correct approach before doing the others.

@ryanslade
Copy link
Collaborator Author

On the one hand, this is nice because we are catching issues early. On the other hand, it means we need to manually think of all the cases where things could go wrong and check for them during validation. I wonder if there is maybe a way to let Postgres find these issues early by running our complete step in an isolated transaction with no intention of committing it first?

@exekias
Copy link
Member

exekias commented Oct 23, 2024

I guess this depends on how likely this is, detecting an issue at validate time is more valuable as we don't even touch the DB and can potentially be used while editing a migration.

It feels like this doesn't get much more complex? we are probably covering all constraint types in that issue

@ryanslade
Copy link
Collaborator Author

I should have been a little clearer, I didn't mean for just the constraint checks. I'm sure there are a range of things that could go wrong at completion time that we might be able to have Postgres check for us. Something to keep in mind, but agree for this specific issue we can do it manually.

@ryanslade ryanslade merged commit d00dd1e into main Oct 23, 2024
52 checks passed
@ryanslade ryanslade deleted the rs/unique-constraint-name-validation branch October 23, 2024 08:18
@exekias
Copy link
Member

exekias commented Oct 23, 2024

ah yes, definitely we should try to detect any possible issue at validate or start time. Ideally, a successful start should guarantee complete! so that's something to take into account

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

Successfully merging this pull request may close these issues.

2 participants