diff --git a/README.md b/README.md index fadf352..65adf15 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,8 @@ Converts MySQL dump to SQLite3 compatible dump (including MySQL `KEY xxxxx` stat ./mysql2sqlite dump_mysql.sql | sqlite3 mysqlite3.db ~~~~ +(both `mysql2sqlite` and `sqlite3` might write something to stdout and stderr - e.g. `memory` coming from `PRAGMA journal_mode = MEMORY;` is not harmful) + ## Development The script is written in *awk* (tested with gawk, but should work with original awk, and the lightning fast mawk) and shall be fully POSIX compliant. diff --git a/mysql2sqlite b/mysql2sqlite index deaa469..2a3e374 100755 --- a/mysql2sqlite +++ b/mysql2sqlite @@ -9,8 +9,13 @@ function printerr( s ){ print s | "cat >&2" } BEGIN { if( ARGC != 2 ){ printerr( \ - "USAGE: mysql2sqlite dump_mysql.sql > dump_sqlite3.sql\n" \ - " file name - (dash) is not supported, because - means stdin") + "USAGE:\n"\ + " mysql2sqlite dump_mysql.sql > dump_sqlite3.sql\n" \ + " OR\n" \ + " mysql2sqlite dump_mysql.sql | sqlite3 sqlite.db\n" \ + "\n" \ + "NOTES:\n" \ + " Dash in filename is not supported, because dash (-) means stdin." ) no_END = 1 exit 1 } @@ -136,7 +141,7 @@ inView != 0 { next } } # CREATE DATABASE is not supported -/^(CREATE.*DATABASE|create.*database)/ { next } +/^(CREATE DATABASE|create database)/ { next } # print the CREATE line as is and capture the table name /^(CREATE|create)/ { @@ -157,7 +162,7 @@ inView != 0 { next } } # Replace `FULLTEXT KEY` (probably other `XXXXX KEY`) -/^ (FULLTEXT KEY|fulltext key)/ { gsub( /.+(KEY|key)/, " KEY" ) } +/^ (FULLTEXT KEY|fulltext key)/ { gsub( /[A-Za-z ]+(KEY|key)/, " KEY" ) } # Get rid of field lengths in KEY lines / (PRIMARY |primary )?(KEY|key)/ { gsub( /\([0-9]+\)/, "" ) } @@ -189,12 +194,18 @@ aInc == 1 && /PRIMARY KEY|primary key/ { next } gsub( /(ENUM|enum)[^)]+\)/, "text " ) gsub( /(SET|set)\([^)]+\)/, "text " ) gsub( /UNSIGNED|unsigned/, "" ) + gsub( /_utf8mb3/, "" ) gsub( /` [^ ]*(INT|int|BIT|bit)[^ ]*/, "` integer" ) gsub( /" [^ ]*(INT|int|BIT|bit)[^ ]*/, "\" integer" ) ere_bit_field = "[bB]'[10]+'" if( match($0, ere_bit_field) ){ sub( ere_bit_field, bit_to_int( substr( $0, RSTART +2, RLENGTH -2 -1 ) ) ) } + + # remove USING BTREE and other suffixes for USING, for example: "UNIQUE KEY + # `hostname_domain` (`hostname`,`domain`) USING BTREE," + gsub( / USING [^, ]+/, "" ) + # field comments are not supported gsub( / (COMMENT|comment).+$/, "" ) # Get commas off end of line @@ -211,7 +222,7 @@ aInc == 1 && /PRIMARY KEY|primary key/ { next } else { # FIXME check if this is correct in all cases if( match( $1, - /(CONSTRAINT|constraint) \".*\" (FOREIGN KEY|foreign key)/ ) ){ + /(CONSTRAINT|constraint) ["].*["] (FOREIGN KEY|foreign key)/ ) ){ print "," } } diff --git a/unit_tests.sh b/unit_tests.sh index 46d1304..8ad82b0 100755 --- a/unit_tests.sh +++ b/unit_tests.sh @@ -121,5 +121,26 @@ SQLout cat <<\SQL +CREATE TABLE `scimag` ( + `ID` int(15) unsigned NOT NULL AUTO_INCREMENT, + `DOI` varchar(200) NOT NULL, + PRIMARY KEY (`ID`) USING BTREE, + UNIQUE KEY `DOIUNIQUE` (`DOI`) USING BTREE, +); SQL +cat <<\SQLin +CREATE TABLE `scimag` ( + `TEXTFIELD` text DEFAULT (_utf8mb3'text_value'), +); +SQLin + +cat <<\SQLout +PRAGMA synchronous = OFF; +PRAGMA journal_mode = MEMORY; +BEGIN TRANSACTION; +CREATE TABLE `scimag` ( + `TEXTFIELD` text DEFAULT ('text_value') +); +END TRANSACTION; +SQLout