Advertisement
opexxx

mysql2sqlite.sh

Mar 30th, 2015
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.06 KB | None | 0 0
  1. #!/bin/sh
  2.  
  3. # Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
  4. # CREATE block and create them in separate commands _after_ all the INSERTs.
  5.  
  6. # Awk is choosen because it's fast and portable. You can use gawk, original awk or even the lightning fast mawk.
  7. # The mysqldump file is traversed only once.
  8.  
  9. # Usage: $ ./mysql2sqlite mysqldump-opts db-name | sqlite3 database.sqlite
  10. # Example: $ ./mysql2sqlite --no-data -u root -pMySecretPassWord myDbase | sqlite3 database.sqlite
  11.  
  12. # Thanks to and @artemyk and @gkuenning for their nice tweaks.
  13.  
  14. mysqldump  --compatible=ansi --skip-extended-insert --compact  "$@" | \
  15.  
  16. awk '
  17.  
  18. BEGIN {
  19.     FS=",$"
  20.     print "PRAGMA synchronous = OFF;"
  21.     print "PRAGMA journal_mode = MEMORY;"
  22.     print "BEGIN TRANSACTION;"
  23. }
  24.  
  25. # CREATE TRIGGER statements have funny commenting.  Remember we are in trigger.
  26. /^\/\*.*CREATE.*TRIGGER/ {
  27.     gsub( /^.*TRIGGER/, "CREATE TRIGGER" )
  28.     print
  29.     inTrigger = 1
  30.     next
  31. }
  32.  
  33. # The end of CREATE TRIGGER has a stray comment terminator
  34. /END \*\/;;/ { gsub( /\*\//, "" ); print; inTrigger = 0; next }
  35.  
  36. # The rest of triggers just get passed through
  37. inTrigger != 0 { print; next }
  38.  
  39. # Skip other comments
  40. /^\/\*/ { next }
  41.  
  42. # Print all `INSERT` lines. The single quotes are protected by another single quote.
  43. /INSERT/ {
  44.     gsub( /\\\047/, "\047\047" )
  45.     gsub(/\\n/, "\n")
  46.     gsub(/\\r/, "\r")
  47.     gsub(/\\"/, "\"")
  48.     gsub(/\\\\/, "\\")
  49.     gsub(/\\\032/, "\032")
  50.     print
  51.     next
  52. }
  53.  
  54. # Print the `CREATE` line as is and capture the table name.
  55. /^CREATE/ {
  56.     print
  57.     if ( match( $0, /\"[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
  58. }
  59.  
  60. # Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
  61. /^  [^"]+KEY/ && !/^  PRIMARY KEY/ { gsub( /.+KEY/, "  KEY" ) }
  62.  
  63. # Get rid of field lengths in KEY lines
  64. / KEY/ { gsub(/\([0-9]+\)/, "") }
  65.  
  66. # Print all fields definition lines except the `KEY` lines.
  67. /^  / && !/^(  KEY|\);)/ {
  68.     gsub( /AUTO_INCREMENT|auto_increment/, "" )
  69.     gsub( /(CHARACTER SET|character set) [^ ]+ /, "" )
  70.     gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|default current_timestamp on update current_timestamp/, "" )
  71.     gsub( /(COLLATE|collate) [^ ]+ /, "" )
  72.     gsub(/(ENUM|enum)[^)]+\)/, "text ")
  73.     gsub(/(SET|set)\([^)]+\)/, "text ")
  74.     gsub(/UNSIGNED|unsigned/, "")
  75.     if (prev) print prev ","
  76.     prev = $1
  77. }
  78.  
  79. # `KEY` lines are extracted from the `CREATE` block and stored in array for later print
  80. # in a separate `CREATE KEY` command. The index name is prefixed by the table name to
  81. # avoid a sqlite error for duplicate index name.
  82. /^(  KEY|\);)/ {
  83.     if (prev) print prev
  84.     prev=""
  85.     if ($0 == ");"){
  86.         print
  87.     } else {
  88.         if ( match( $0, /\"[^"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
  89.         if ( match( $0, /\([^()]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
  90.         key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
  91.     }
  92. }
  93.  
  94. # Print all `KEY` creation lines.
  95. END {
  96.     for (table in key) printf key[table]
  97.     print "END TRANSACTION;"
  98. }
  99. '
  100. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement