[Feature proposal] Adding callbacks to ActiveRecord migrator

What do you think about the idea of adding some callbacks to extend ActiveRecord migrations. It originates from the following challenge.

One of the blockers for switching from schema.rb to more powerful and low-level schema.sql format is that the resulting SQL depends on the exact environment in which migrations are run. In big application with dozens of remote developers of different level, it can be hard to enforce the same environment context (like the version of the database, the current locale etc.).

To address the problem I’d like to have some mechanizm like callbacks (or some middleware) for running migrator. Having this option I could just add validations to be invoked by the Migrator.run. AFAIK, the only way of doing it now is monkey patching of the ActiveRecord::Migrator which is a durty, hacky and brittle way of doing things.

Do you think the problem worths adding callbacks, or you can recommend a better way of solving the problem?

We have typically handled this by writing new rake tasks and setting them to run before db:migrate. Here’s an example https://gist.github.com/ghiculescu/234da1a8aada56d543e6b876a17859ff

1 Like

Thank you, this can be a solution for small dev teams where there’s a chance anybody uses the task. In my case I’d like to enforce stricter checks before migration – to make it kind of “inavoidable” (by just running rails db:migrate).

UPD. Ahh, I missed the point with enhance-ing. Interesting! Thank you once again