Supose that I already have defined my database with a lot of tables (around 40). I realize now that I want to add certain columns to each table. For the sake of the example let it be
created_by
and updated_by
.
Is there any way to do this painless without going through 40 migrations and update each of them manually?
I am using rails 2.3.8
You could generate a single migration and put this code in it. It will give you an array of all the tables (minus the "schema_migrations" table that Rails automatically creates) and than add the column(s) to each one.
tables = ActiveRecord::Base.connection.tables - ["schema_migrations"]
tables.each do |table|
add_column table, :created_by, :integer
end
You don't need forty migrations. You can make just one migration, for example:
def self.up
%w(table1 table2 table3).each do |table_name|
ActiveRecord::Base.connection.execute "ALTER TABLE #{table_name} ADD created_by int, updated_by int"
end
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With