@Vasily 感谢您的回复。在阅读了它和来自stackoverflow的更多问题之后,我想出了这个解决方案:
由于我编写了自己的生成器来创建用户表,因此我在其中包含了 Rails::Generators::Migration,因此我可以像这样覆盖 next_migration_number 方法:
def self.next_migration_number(dirname)
if ActiveRecord::Base.timestamped_migrations
Time.now.utc.strftime("custom/%Y%m%d%H%M%S")
else
"custom/%.3d" % (current_migration_number(dirname) + 1)
end
end
现在用户生成的所有迁移都在 db/migrations/custom 目录中创建。
然后我编写了正常的 rails 迁移,它从 db/migrations/custom 目录执行所有迁移:
class ExecuteCustomMigrations < ActiveRecord::Migration
MIGRATIONS_PATH='db/migrate/custom'
def self.up
Dir["#{MIGRATIONS_PATH}/[0-9]*_*.rb"].
sort.map{|filename|require filename}.flatten.
each{|class_name| const_get(class_name).up}
end
def self.down
Dir["#{MIGRATIONS_PATH}/[0-9]*_*.rb"].sort.reverse.
map{|filename|require filename}.flatten.
each{|class_name| const_get(class_name).down}
end
end
用户创建自定义表后,我使用以下代码调用此迁移:
Rake::Task["db:migrate:redo"].execute("VERSION=20110108213453")