Would the ability to run sql before|after|around a migration be useful in rails?

One issue I stumble on often is Postgres sql dependencies for advanced structure and rails migrations.

Scenic is helpful if its just views, but… I actually think its approach can get in the way after you have something_vw_56.sql…

What would be REALLY helpful is if rails had built-in-hooks to just specify sql code that can run either:

  • before the migration sequence

  • after the migration sequence

  • before a schema load (but after database creation)

  • after schema load

  • around a migration that has a particular tag associated with it (so you can drop specific materialized views, indexes, and recreate them only for a particular migration, and not on every single migration).

I’d like to at least see support for SQL view migrations in rails, especially since ActiveRecord already supports read only models for SQL views fairly well. I feel that scenic personally does a fairly good job, and I’d understand if the majority of rails maintainers don’t want to add to the rails maintenance burden for SQL view migration support. What would wrapping the entire migrations sequence be used for? Better ways to wrap and augment schema generation could be improved IMO. Thoughts?

There is a similar gem for handling triggers and functions named fx (https://github.com/teoljungberg/fx)

I believe the rationale for keeping this out of rails core has to due with two pretty big knock on effects it would have:

  1. The mess of supporting across active record and endless false bug reports for things like “Custom function migration doesn’t work in development but works in prod” b/c they are using sqlite in dev, etc.

  2. It would lock apps to SQL-based dumps (db/schema.sql) in many cases, which is less than optimal from a code management point of view (at least imho) since the diffs tend to be very messy.

The migrator (including the command recorder and allowing for dynamically built reversals) would get terribly complicated if it had to support arbitrary sql getting injected