Closed kaaveland closed 7 months ago
It seems like the easiest way to do this is to snapshot queries against pg_attribute
and pg_constraint
, similarly to how we snapshot pg_class
. We'd want to track:
CHECK
-constraint and whether they are VALID
FOREIGN KEY
-contraints and whether they are VALID
This should let us match against these patterns from the strong migrations documentation:
VALID
check constraintVALID
foreign keyjson
columnNOT NULL
type_change_requires_table_rewrite
needs to be fixed to actually understand what type changes that forces a rewrite.
We should definitely make it possible to "fail the trace" if we find some of these patterns, because at that point, we know of better ways to do the migration.
We need to track indexes too, so we can recommend to add them concurrently. There's also a nice hint we could give if people add a unique constraint, where it's safer if they create a unique index concurrently.
Altering a check constraint body is probably often not safe. 🤔
The documentation for https://github.com/ankane/strong_migrations is awesome and it might be possible to recognize a bunch of the same things, either by parsing the sql with a proper parser, or by inspecting the locks and potentially generate some recommendations or "fixing up" the migration programmatically.