how to import a dump .sql file in a rake task

I have a dump of some tables to be filled when the app is setup


I would like to execute a task similar to the >mysql source
like :
ActiveRecord::Base.connection.execute("source #{path};") (which
doesn't run fine, of course....

is it possible in a rake task ? if yes, any suggestion ?

thanks for your suggestions


How about:

task :load_sql_dump
sh ‘mysql -u user -p password < regions_dpt_cities_codes.sql’

Personally, I like to see the shell commands as they are executed (a legacy of my misspent youth with Makefiles), so I put


at the top my my Rakefiles.


I had a similar problem, and started out declaring the username and password in every line of the rake tasks. Did not like that, since I had many rake tasks, and changing a database connection meant changing every line in the rake code.

A better way is as follows (I read this from some google post, but cannot remember who it was, so sorry about not giving credit where credit is due):

1> DBConn is an active record connection.
2> Parse the sql file by splitting on ‘;’.
task :create_empty_schema do
sql =“path_to_your_file/dataload.sql”).read
sql.split(’;’).each do |sql_statement|
puts "Empty Schema has been created ‘#{}’ "

Remove blank lines from the end of the file, or else it complains. So, it is a little brittle.