fix: postgres: do a deep comparison to see if the default value has changed for json types #7650
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.
Description of change
Hello. First of all, I am not a typeorm expert but this is my attempt at solving a problem that I encountered. Please feel free to update this branch to make any kind of change (maintainers can update the branch). I may very well have missed certain edge cases since I did not test this extensively.
Postgres sorts the
json
andjsonb
values before saving them. Additionally, postgres has different whitespacing thanJSON.serialize
so just pre-sorting the values will still cause persistent migrations. So if you have anything other than an empty object then you will always see a persistent migration. This PR adds a new method that parses the JSON default that is used in the table, and then does adeepCompare
on the two objects.This kind of thing is already done in another part of the code:
typeorm/src/persistence/SubjectChangedColumnsComputer.ts
Lines 103 to 109 in beea2e1
Example:
First migration:
But then any subsequent migrations will always contain:
Pull-Request Checklist
Sorry, I had issues running
npm install
because I'm on an Apple M1 computer. Got this error:So if someone else can do the polishing that would be awesome. Thank you!
master
branchnpm run lint
passes with this changenpm run test
passes with this changeFixes #0000