Importing a large sql file directly into mysql via a rails migration

Posted by Tim Connor Sun, 12 Oct 2008 23:12:00 GMT

or otherwise running a sql file through the mysql commandline client, with the current connection details:


class CreateZipCodes < ActiveRecord::Migration
def self.up
#Loading that large of a YAML file can cause problems
zip_code_file = File.expand_path(File.join(File.dirname(FILE), “..”, “zip_codes.sql”))
config = ActiveRecord::Base.connection.instance_variable_get(‘@config’)
`mysql -u#{config[:username]} -p#{config[:password]} #{config[:database]} < #{zip_code_file}`
end

def self.down drop_table :zip_codes end

end