heroku db:pull loses foreign keys

So here’s a little rake task to bring them back, assuming that your environment is exactly like mine.

namespace :db do
task :restore_foreign_keys => :environment do
schema = File.open("db/schema.rb").readlines
foreign_key_statements = schema.grep /add_foreign_key/
foreign_key_statements.each do |statement|
ActiveRecord::Base.connection.instance_eval statement
end
end
end

That code is really very ruby. Open the file, read the relevant lines, then.. just eval them in the context of the connection object and hope for the best.

-Iain

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s