Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.22 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. # MAMP Example:
  15. #/Applications/MAMP/Library/bin/mysqldump --compatible=ansi --skip-extended-insert --compact "$@" | \
  16.  
  17. #awk '
  18.  
  19. # XAMPP Example
  20. #/Applications/XAMPP/Library/bin/mysqldump --compatible=ansi --skip-extended-insert --compact "$@" | \
  21. #awk '
  22.  
  23. mysqldump --compatible=ansi --skip-extended-insert --compact "$@" | \
  24. awk '
  25.  
  26. BEGIN {
  27. FS=",$"
  28. print "PRAGMA synchronous = OFF;"
  29. print "PRAGMA journal_mode = MEMORY;"
  30. print "BEGIN TRANSACTION;"
  31. }
  32.  
  33. # CREATE TRIGGER statements have funny commenting. Remember we are in trigger.
  34. /^\/\*.*CREATE.*TRIGGER/ {
  35. gsub( /^.*TRIGGER/, "CREATE TRIGGER" )
  36. print
  37. inTrigger = 1
  38. next
  39. }
  40.  
  41. # The end of CREATE TRIGGER has a stray comment terminator
  42. /END \*\/;;/ { gsub( /\*\//, "" ); print; inTrigger = 0; next }
  43.  
  44. # The rest of triggers just get passed through
  45. inTrigger != 0 { print; next }
  46.  
  47. # Skip other comments
  48. /^\/\*/ { next }
  49.  
  50. # Print all `INSERT` lines. The single quotes are protected by another single quote.
  51. /INSERT/ {
  52. gsub( /\\\047/, "\047\047" )
  53. gsub(/\\n/, "\n")
  54. gsub(/\\r/, "\r")
  55. gsub(/\\"/, "\"")
  56. gsub(/\\\\/, "\\")
  57. gsub(/\\\032/, "\032")
  58. print
  59. next
  60. }
  61.  
  62. # Print the `CREATE` line as is and capture the table name.
  63. /^CREATE/ {
  64. print
  65. if ( match( $0, /\"[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
  66. }
  67.  
  68. # Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
  69. /^ [^"]+KEY/ && !/^ PRIMARY KEY/ { gsub( /.+KEY/, " KEY" ) }
  70.  
  71. # Get rid of field lengths in KEY lines
  72. / KEY/ { gsub(/\([0-9]+\)/, "") }
  73.  
  74. # Print all fields definition lines except the `KEY` lines.
  75. /^ / && !/^( KEY|\);)/ {
  76. gsub( /AUTO_INCREMENT|auto_increment/, "" )
  77. gsub( /(CHARACTER SET|character set) [^ ]+ /, "" )
  78. gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|default current_timestamp on update current_timestamp/, "" )
  79. gsub( /(COLLATE|collate) [^ ]+ /, "" )
  80. gsub(/(ENUM|enum)[^)]+\)/, "text ")
  81. gsub(/(SET|set)\([^)]+\)/, "text ")
  82. gsub(/UNSIGNED|unsigned/, "")
  83. if (prev) print prev ","
  84. prev = $1
  85. }
  86.  
  87. # `KEY` lines are extracted from the `CREATE` block and stored in array for later print
  88. # in a separate `CREATE KEY` command. The index name is prefixed by the table name to
  89. # avoid a sqlite error for duplicate index name.
  90. /^( KEY|\);)/ {
  91. if (prev) print prev
  92. prev=""
  93. if ($0 == ");"){
  94. print
  95. } else {
  96. if ( match( $0, /\"[^"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
  97. if ( match( $0, /\([^()]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
  98. key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
  99. }
  100. }
  101.  
  102. # Print all `KEY` creation lines.
  103. END {
  104. for (table in key) printf key[table]
  105. print "END TRANSACTION;"
  106. }
  107. '
  108. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement