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 = File.open("path_to_your_file/dataload.sql").read
sql.split('';'').each do |sql_statement|
DBConn.connection.execute(sql_statement)
end
puts "Empty Schema has been created ''#{Time.now}''
"
end
Remove blank lines from the end of the file, or else it complains. So, it is
a little brittle.
On Sat, Dec 13, 2008 at 7:20 PM, Patrick Doyle
<wpdster-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:
> On Sat, Dec 13, 2008 at 5:57 PM, Erwin
<yves_dufour-ee4meeAH724@public.gmane.org> wrote:
>
>>
>> I have a dump of some tables to be filled when the app is setup
>>
>> ''.../db/dataset/regions_dpt_cities_codes.sql''
>>
>> I would like to execute a task similar to the >mysql source
>> <dump_file>
>> 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''
> end
>
> Personally, I like to see the shell commands as they are executed (a legacy
> of my misspent youth with Makefiles), so I put
>
> verbose(true)
>
> at the top my my Rakefiles.
>
> --wpd
>
>
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---