问题
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 MySQL-Dump into a sqlite DB?
回答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
回答2:
I solved the Problem with a DATA DB Dump (not the schema) and create the tables with another migration file and create_table
.
来源:https://stackoverflow.com/questions/3613235/import-mysql-db-dump-into-a-rails-app-using-a-migration-file