I need to execute very similar sql statements (possibly with one parameter besides the table name), across multiple tables in a rails application. As a result, I get many similar migrations, for example:
class DoSomeSQLOnUser < ActiveRecord::Migration def up execute('some long sql that alters the user.field1') execute('some long sql that alters the user.field2') end def down execute('some sql that undoes the changes') end end
Then I have the same for customers, sales, etc.
I would like to extend ActiveRecord::Migration so that I can do this instead:
class DoSomeSQLOnUser < ActiveRecord::Migration def change do_custom_thing_on :users, :field1 do_custom_thing_on :users, :field2 end end
How can i do this? I think I know how to do this when operations are split up and down, for example:
class DoSomeSQLOnUser < ActiveRecord::Migration def up do_custom_thing_on :users, :field1 do_custom_thing_on :users, :field2 end def down undo_custom_thing_on :users, :field1 undo_custom_thing_on :users, :field2 end end
But doing it so that the change is "reversible" eluded me.
source share