yogthos / migratus

MIGRATE ALL THE THINGS!
651 stars 95 forks source link

How to baseline migrations #263

Closed insano10 closed 1 month ago

insano10 commented 5 months 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 5 months 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 5 months 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 5 months 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.

Gwonwoo-Nam commented 1 month ago

I’ve experienced the same need for this feature, and I think it would be very useful. Is anyone currently working on this? If not, I’d be happy to submit a PR to help address it.

yogthos commented 1 month ago

I don't think anybody is looking at this at the moment. If you'd be up to do a PR that would be very welcome!

insano10 commented 1 month ago

Yeh sorry I've been too busy to progress this since opening the issue 🙈 if you'd like to implement this that would be fab.

yogthos commented 1 month ago

Just pushed out 1.6.1 with the features, amazing work @Gwonwoo-Nam !