I had a situation today where I wanted to add a partial postgres index to a table in a migration. Naturally this kind of stuff is not possible yet in rails using add_index (it is coming some time)
So, I am forced to use execute statements in my migration.
Now, schema.rb has this comment at the top:
# 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).
#
Unfortunately this execute statement is not tracked in schema.rb the effect of this is making schema.rb pretty much useless if I have any custom DML.
Is there any way I can force an execute statement containing DML to find itself into schema.rb ?
There’s no way to achieve what you want with schema.rb, what you want to do is switch to using the database’s native schema dumping / retrieval code.
Now instead of a schema.rb file, you’ll get a structure.sql file. This should transparently work with all rails’ built in tasks and as it’s using the native dumper format, it’ll support whatever crazy stuff you want it to.