Importing PHPMyAdmin export files using migrations
August 27th, 2007
1 2 3 4 5 6 7 8 9 10 |
def self.up @first_query = true sql_file = File.dirname(__FILE__) + "/../some_crazy_file.sql" IO.readlines(sql_file).join.split(/;\nINSERT INTO /).each do |q| q = "INSERT INTO " + q unless @first_query execute q @first_query = false end end |
UPDATE: My friend Brandon, who chose not to comment on this blog post (shame on him!) said that running the sql file in command line could work as a more elegant solution there. The only problem I have with this approach is whether or not the rake task fails if the operation fails. So you code would like like this:
1 2 3 |
def self.up result = `mysql db_name < some_crazy_file.sql` end |
The only thing left is to make sure that result returns a favorable result, and if it doesn't then fail the migration. I'm not exactly sure how to do this (to busy to investigate). Any takers?