yogthos / migratus

MIGRATE ALL THE THINGS!
638 stars 96 forks source link

How to baseline migrations #263

Open insano10 opened 2 weeks ago

insano10 commented 2 weeks ago

Can you please suggest a strategy for compressing a large timeline of migrations?

Given a large accumulation of migrations I would ideally like to create a single migration that contains all the combined schema changes so far and have that be the new latest applied patch in the schema table.

Is there any provision to do this through migratus commands, or would it involve manually updating the schema table data?

yogthos commented 2 weeks ago

There isn't a mechanism for merging existing migrations at the moment, so you'd have to manually update the schema table at the moment.

I do think having this as a feature would be a good idea. I've run into this scenario myself as well. One strategy could be to have a helper function where you can provide the ids for the start/end migrations for a list if migrations to be merged and then just concatenate them separated by --;;. This should produce a single migration file that behaves the same way, and then it could also run the sql query that updates the migrations table to match the new numbering scheme.

insano10 commented 2 weeks ago

I'll have a think on this.

A complication is the potential mixture of code-based and SQL based migrations. Currently I have a mixture of the 2 so creating a single baseline migration would have to be a code-based one.

It's still definitely possible, maybe something like:

  1. take in start/end IDs for the baseline
  2. compress all contiguous SQL patches into single merged migrations
  3. create a code based migration that iterates through the SQL batches and code migrations and applies them in order
yogthos commented 2 weeks ago

Yeah that sounds like it should work. Compressing SQL patches into a single migration could be a standalone step, and then if there only SQL migrations you just compress them and update the migration history. If there are also code migrations then the code migration would be the baseline and you'd do the steps you outlined.