views:

108

answers:

1

I have to create a Rails migration which creates many triggers and stored procedures.

Normally one would do that using the execute method, but because of the size of the statements, I'd rather keep them in an external file and reference it from the migration.

How can I do that? Is it even possible?

+1  A: 

You can just store them in a text file and read them in via a File object.

sql = ""
source = File.new("./sql/procedures.sql", "r")
while (line = file.gets)
  sql << line
end
file.close
execute sql

It's ugly, but works. I'd strongly recommend keeping the stored procedures/triggers inside migrations for easy rollbacks.

If you do the "external file" method, you'll need to maintain two additional files per migration, one for adding all the stuff, and one for dropping in in case of a:

rake db:rollback
Mike Trpcic