Created
December 22, 2015 00:16
-
-
Save amellnik/34d35161b36f6182b738 to your computer and use it in GitHub Desktop.
Convert from a sqlite3 dump to a MySQL one. This is a almost exact translation of a script from here: https://www.redmine.org/boards/2/topics/12793?r=24983
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if VERSION < v"0.4.0" | |
error("This uses some string functions only available in 0.4.0+") | |
end | |
#Get the file names from the arguments | |
if length(ARGS) >= 2 | |
inFileName = ARGS[1] | |
outFileName = ARGS[2] | |
elseif length(ARGS) == 1 | |
inFileName = ARGS[1] | |
outFileName = replace(inFileName, ".sql", ".mysql.sql") | |
else | |
error("Usage is SQLiteToMySQL.jl input.sql [output.sql]") | |
end | |
println(inFileName, " -- ", outFileName) | |
inFile = open(inFileName, "r") | |
outFile = open(outFileName, "w") | |
write(outFile, "SET sql_mode='NO_BACKSLASH_ESCAPES';\n") | |
for line in eachline(inFile) | |
if ( | |
startswith(line, "PRAGMA")| | |
startswith(line, "BEGIN TRANSACTION;")| | |
startswith(line, "COMMIT;")| | |
startswith(line, "DELETE FROM sqlite_sequence;")| | |
startswith(line, "INSERT INTO \"sqlite_sequence\"") | |
) continue end | |
line = replace(line, "AUTOINCREMENT", "AUTO_INCREMENT") | |
line = replace(line, "DEFAULT 't'", "DEFAULT '1'") | |
line = replace(line, "DEFAULT 'f'", "DEFAULT '0'") | |
line = replace(line, ",'t'", ",'1'") | |
line = replace(line, ",'f'", ",'0'") | |
in_string = false | |
newline = "" | |
for c in line | |
if !in_string | |
if c == '\'' #The backtick as char | |
in_string = true | |
elseif c == '"' #" as char | |
newline = string(newline, '`') | |
continue | |
end | |
elseif c == '\'' | |
in_string = false | |
end | |
newline = string(newline, c) | |
end | |
write(outFile, newline) | |
end | |
close(outFile) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment