Ruby on rails 使用rakedb:直接迁移普通SQL
使用Ruby on rails 使用rakedb:直接迁移普通SQL,ruby-on-rails,rake,rails-migrations,Ruby On Rails,Rake,Rails Migrations,使用rakedb:migrate加载香草SQL会涉及到什么问题 我正在处理的业务需求不允许我使用默认的Rails迁移。但我仍然需要跟踪更改,轻松更改数据库DDL,以及Rails迁移提供的其他功能 因此,迁移文件如下所示: class AddDateToPost < ActiveRecord::Migration def self.up ActiveRecord::Base.connection.execute("ALTER TABLE `posts` ADD COLUMN da
rakedb:migrate
加载香草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
class AddDateToPost
这是完全可以接受的,只要您确信您的上下功能相互映射,就不会有问题。为了便于阅读,我建议执行以下操作:
class AddDateToPost < ActiveRecord::Migration
def self.up
execute "ALTER TABLE `posts` ADD COLUMN date DATETIME NULL"
end
def self.down
execute "ALTER TABLE `posts` DROP COLUMN date"
end
end
类AddDateToPostRakefile
以启用rakedb:
任务:
require 'standalone_migrations'
StandaloneMigrations::Tasks.load_tasks
之后,您只需像通常那样设置迁移:
class AddDateToPost < ActiveRecord::Migration
def self.up
add_columm :posts, :date, :datetime, default: nil
end
def self.down
remove_columm :posts, :date
end
end
class AddDateToPost