rake db:migrate
使用加载 vanilla SQL会涉及哪些问题?
我正在处理的业务需求不允许我使用默认的 Rails 迁移。但我仍然需要跟踪更改、轻松更改数据库 DDL 以及 Rails 迁移提供的其他内容。
所以迁移文件看起来像:
class AddDateToPost < ActiveRecord::Migration
def self.up
ActiveRecord::Base.connection.execute("ALTER TABLE `posts` ADD COLUMN date DATETIME NULL")
end
def self.down
ActiveRecord::Base.connection.execute("ALTER TABLE `posts` DROP COLUMN date")
end
end