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
Is there a better way? I have found that this is also very picky. Your insert queries must follow up the line right after. It would was fun to create this, but not fun to look at :-/. Maybe there is a plugin somewhere?

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?

1 Response to “Importing PHPMyAdmin export files using migrations”

  1. Ryan Carmelo Briones Says:
    You could start by checking $? after the command completes.

Sorry, comments are closed for this article.