Re:Zero-downtime schema migrations in Postgres using Reshape
by magicalhippo on Friday January 07, 2022 @10:03AM
(#29839877)
> These are SQL expressions which specify how to convert between the old and new schema
Right, I get how that would work with a read-only view, I just didn't know you could do updates against such a view. Nifty!
In the example, you can see "up" and "down" settings on the migration. These are SQL expressions which specify how to convert between the old and new schema, in this case casting from TEXT to INTEGER and back. So in this case we have:
up = "CAST(reference AS TEXT)" down = "CAST(reference AS INTEGER)"
which is just a standard Postgres function for casting.
> In our case we just delete the offending values if the constraint has "on cascade delete", which most do.
Interesting! I might add some options to a future `add_foreign_key` migration which can achieve behavior like this automatically.