Multiple migration_template calls in Rails (2.3x) generator manifest
Tuesday, April 26th, 2011If you have created a Rails generator that needs to include more than one migration_template in its record block, I’ve found a trick so you don’t run into a “Multiple migrations have the version number” error when running db:migrate.
You need to tell the generator to take a one second snooze, so that the next_migration_string method returns a timestamp that is one second later.
You would think a simple call to the sleep method would do the trick, but because the generator manifest’s record has a special syntax (that relies on method_missing to define recorded actions), you need to do a small tweek by calling the sleep method on the block’s object, i.e. “m.sleep(1)”.
Here’s what it looks like in practice:
class TrolliedMigrationsGenerator < Rails::Generator::Base def manifest record do |m| m.migration_template 'trolleys_migration.rb', 'db/migrate', { :migration_file_name => "create_trolleys" } m.sleep(1) m.migration_template 'purchase_orders_migration.rb', 'db/migrate', { :migration_file_name => "create_purchase_orders" } m.sleep(1) m.migration_template 'line_items_migration.rb', 'db/migrate', { :migration_file_name => "create_line_items" } end end end