Skip to content Skip to sidebar Skip to footer

Import Mysql-db-dump Into A Rails App Using A Migration File

I have an old PHP application with a bunch of MySQL tables. I want to rewrite it with Rails(3) and want to import the old data. How can I write a migration-script to import the MyS

Solution 1:

It's out of my merit to judge why migrate from mysql to sqlite3 db. Why not run your dev environment as well in mysql? anyway, following is the script.

#!/bin/sh 

mysqldump --compact --compatible=ansi --default-character-set=binary mydbname | 
grep -v ' KEY "' | 
grep -v ' UNIQUE KEY "' | 
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' | 
perl -pe ' 
if (/^(INSERT.+?)\(/) { 
$a=$1; 
s/\\'\''/'\'\''/g; 
s/\\n/\n/g; 
s/\),\(/\);\n$a\(/g; 
} ' | sqlite3 output.db

Solution 2:

I solved the Problem with a DATA DB Dump (not the schema) and create the tables with another migration file and create_table.

Post a Comment for "Import Mysql-db-dump Into A Rails App Using A Migration File"