Defensively drop constraints during migrations #480
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
When
PostgreSQL.prototype.getDropForeignKeysgets called with duplicated foreign keys, an invalidALTER TABLEstatement gets created. This is because the duplicates are mapped to multipleDROP CONSTRAINT fk_keySQL actions, e.g.:As the
DROP CONSTRAINTactions are run sequentially, when the first action gets executed by PostgreSQL, the next one (for the same, but already dropped key) will no longer cause an SQL error thanks to the added IF EXISTS check:Please note, this is really a hack that fixes
npm run migrateoperations I have experienced when working on my project. We still need to understand why thegetDropForeignKeysmethod gets called with duplicated foreign keys in certain conditions but I believe ensuring the generated SQL code is valid is still a step in the right direction.Checklist
npm testpasses on your machine (with the exception of 1-2 tests that keep timing out both on the upstream and my fork master branches)