Ruby on rails 3 如何在迁移中正确添加自定义SQL DML
今天我遇到了一个情况,我想在迁移中向表中添加一个部分postgres索引。当然,在使用Ruby on rails 3 如何在迁移中正确添加自定义SQL DML,ruby-on-rails-3,activerecord,Ruby On Rails 3,Activerecord,今天我遇到了一个情况,我想在迁移中向表中添加一个部分postgres索引。当然,在使用add\u index的rails中,这种东西还不可能实现 因此,我不得不在迁移中使用execute语句 现在,schema.rb的顶部有以下注释: # Note that this schema.rb definition is the authoritative source for your # database schema. If you need to create the application
add\u index
的rails中,这种东西还不可能实现
因此,我不得不在迁移中使用execute
语句
现在,schema.rb
的顶部有以下注释:
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
不幸的是,这个execute
语句没有在schema.rb
中跟踪。如果我有任何自定义的DML,这会使schema.rb
变得毫无用处
有没有办法强制包含DML的
execute
语句进入schema.rb
schema.rb无法实现您想要的功能,您要做的是切换到使用数据库的本机模式转储/检索代码
config.active_record.schema_format = :sql
现在,您将获得structure.sql文件,而不是schema.rb文件。这应该可以透明地处理所有rails的内置任务,并且由于它使用本机转储程序格式,它将支持您想要的任何疯狂的东西。为了补充这个问题,有没有一种方法可以使用rake来做到这一点,即每当rake创建数据库时,它都会运行一个包含所有自定义SQL DML的脚本?