jackc / tern

The SQL Fan's Migrator

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Roll up migrations

jackc opened this issue · comments

Sometimes it is convenient to roll up or flatten a range of migrations to a single migration. See #50 (comment) for initial request. I have also personally flattened migrations by hand multiple times.

There are two advantages to this feature.

  1. Initial database setup may be quicker.
  2. If the original migration files are removed it would declutter the migration directory. However, #50 could also solve this.

There are also a few downsides or tricky issues.

There is no way to perfectly roll up a set of migrations. When I have done this by hand I have used pg_dump to generate the flattened migration. However, migrations can depend on the database settings (default collations, permission grants, etc.), tern config variables, and environment variables. pg_dump freezes the results. This means the roll up migration could do something different than the original migrations.

The other is what to do with down migrations. If migrations 1 - 50 are rolled up into one migration what happens when we migrate down to 49. Or for that matter try to migrate up from 0 to a version less than 50. That would require keeping the old migrations around and deciding whether it is possible to use a roll up migration or fall back to the original migrations. Possible, but messy. Hmm... maybe down migrations always use the original migrations and up migrations only use a roll up if starting from 0 and going at least up to the end of the roll up.

Definitely a nice to have, but I'm not sure how much complexity it would introduce and if that complexity would be worth it.

I'd like to request this feature, but not for archiving purposes. I'm using sqlc to generate structs from my database code, but it doesn't like the variable interpolation (for obvious reasons). It would be nice to be able to dump one file that is all valid SQL for other tools to run against.

@Bindernews For your use case pg_dump --schema-only might work.