pg-schema-diff icon indicating copy to clipboard operation
pg-schema-diff copied to clipboard

Support FK's switching between non-castable types

Open bplunkett-stripe opened this issue 2 years ago • 3 comments

Currently, if switching between incompatible types with a FK, plan validation will error. We need to identify if types are incompatible and drop and re-add the FK

This is probably a pretty rare operation , since the only incompatible type switches we currently support is BIGINT to TIMESTAMP

bplunkett-stripe avatar Aug 07 '23 20:08 bplunkett-stripe

I'd like to work on this issue!

sidyakinian avatar Sep 06 '23 16:09 sidyakinian

Sweet! This shouldn't be too much code, but it does get into the nitty gritty of the internal of the library.

There are couple parts to this:

  1. Identifying if types are castable. I think your best bet is to fetch casts from the systems catalog, but you should validate this is true
  2. I think all you need to do is check if the underlying column(s) of the FK are changing between non-castable types. You shouldn't need to check the foreign table, since the data type changes should inherently be identical. If they are non-castable, return requiresRecreate=true from this func

This ticket airs a bit more on the tricky side, so definitely let me know if you have any questions

bplunkett-stripe avatar Sep 07 '23 06:09 bplunkett-stripe

@bplunkett-stripe Hello, just made this draft PR: https://github.com/stripe/pg-schema-diff/pull/73. It only handles bigint to timestamp for now and the logic should probably be structured a bit differently, but please let me know if I'm going in the right direction! Sorry this took a while, didn't work a lot with Go and SQL so had a bit of a learning curve :)

sidyakinian avatar Oct 10 '23 18:10 sidyakinian