如何在迁移中正确添加自定义SQL DML

时间:2012-08-10 01:14:31

标签: ruby-on-rails-3 activerecord

今天我遇到了一种情况,我希望在迁移中向表中添加部分postgres索引。当然,使用add_indexcoming some time}

在轨道中无法使用此类内容

因此,我在迁移中被迫使用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

1 个答案:

答案 0 :(得分:6)

没有办法用schema.rb实现你想要的,你想要做的就是切换到使用数据库的本机模式转储/检索代码。

config.active_record.schema_format = :sql

现在,您将获得一个structure.sql文件,而不是schema.rb文件。这应该透明地适用于所有导轨'内置任务,并且使用原生转储器格式,它可以支持您想要的任何疯狂的东西。