Skip to content

Instantly share code, notes, and snippets.

@Appla
Forked from esperlu/mysql2sqlite.sh
Created June 6, 2019 02:40
Show Gist options
  • Save Appla/a30a2528614da76392c39f50a044ee7a to your computer and use it in GitHub Desktop.
Save Appla/a30a2528614da76392c39f50a044ee7a to your computer and use it in GitHub Desktop.

Revisions

  1. @esperlu esperlu renamed this gist Jun 22, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  2. @esperlu esperlu renamed this gist Jun 20, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. @esperlu esperlu renamed this gist Jun 20, 2011. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  4. @esperlu esperlu revised this gist Jun 20, 2011. No changes.
  5. @esperlu esperlu revised this gist Jun 20, 2011. 1 changed file with 47 additions and 12 deletions.
    59 changes: 47 additions & 12 deletions mysql2sqlite
    Original file line number Diff line number Diff line change
    @@ -1,27 +1,57 @@
    #!/bin/sh

    # Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
    # CREATE block and create them in separate commands _after_ all the INSERTs.

    # Awk is choosen because it's fast and portable. You can use gawk, original awk or even the lightning fast mawk.
    # The mysqldump file is traversed only once.

    # Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
    # CREATE block and create them in separate commands _after_ all the INSERTs.
    # Usage: $ ./mysql2sqlite mysqldump-opts db-name | sqlite3 database.sqlite
    # Example: $ ./mysql2sqlite --no-data -u root -pMySecretPassWord myDbase | sqlite3 database.sqlite

    # Usage: $ ./mysql2sqlite | sqlite3 database.sqlite
    # Thanks to and @artemyk and @gkuenning for their nice tweaks.

    mysqldump --compatible=ansi --skip-extended-insert --compact -u **** -p****** datbase_name | \
    mysqldump --compatible=ansi --skip-extended-insert --compact "$@" | \

    awk '
    awk -F ",$" '
    BEGIN {
    FS=",$"
    print "PRAGMA synchronous = OFF;"
    print "PRAGMA journal_mode = MEMORY;"
    print "BEGIN TRANSACTION;"
    }
    BEGIN{ FS=",$" }
    # CREATE TRIGGER statements have funny commenting. Remember we are in trigger.
    /^\/\*.*CREATE.*TRIGGER/ {
    gsub( /^.*TRIGGER/, "CREATE TRIGGER" )
    print
    inTrigger = 1
    next
    }
    # The end of CREATE TRIGGER has a stray comment terminator
    /END \*\/;;/ { gsub( /\*\//, "" ); print; inTrigger = 0; next }
    # The rest of triggers just get passed through
    inTrigger != 0 { print; next }
    # Skip comments
    # Skip other comments
    /^\/\*/ { next }
    # Print all `INSERT` lines. The single quotes are protected by another single quote.
    /INSERT/ { gsub( /\\\047/, "\047\047" ); print; next }
    /INSERT/ {
    gsub( /\\\047/, "\047\047" )
    gsub(/\\n/, "\n")
    gsub(/\\r/, "\r")
    gsub(/\\"/, "\"")
    gsub(/\\\\/, "\\")
    gsub(/\\\032/, "\032")
    print
    next
    }
    # Print the ´CREATE´ line as is and capture the table name.
    # Print the `CREATE` line as is and capture the table name.
    /^CREATE/ {
    print
    if ( match( $0, /\"[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
    @@ -30,6 +60,9 @@ BEGIN{ FS=",$" }
    # Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
    /^ [^"]+KEY/ && !/^ PRIMARY KEY/ { gsub( /.+KEY/, " KEY" ) }
    # Get rid of field lengths in KEY lines
    / KEY/ { gsub(/\([0-9]+\)/, "") }
    # Print all fields definition lines except the `KEY` lines.
    /^ / && !/^( KEY|\);)/ {
    gsub( /AUTO_INCREMENT|auto_increment/, "" )
    @@ -52,14 +85,16 @@ BEGIN{ FS=",$" }
    if ($0 == ");"){
    print
    } else {
    if ( match( $0, /\"[^\"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
    if ( match( $0, /\([^\)]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
    if ( match( $0, /\"[^"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
    if ( match( $0, /\([^()]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
    key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
    }
    }
    # Print all `KEY` creation lines.
    END {
    for (table in key) printf key[table]
    print "END TRANSACTION;"
    }
    '
    '
    exit 0
  6. @esperlu esperlu revised this gist Jun 7, 2011. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions mysql2sqlite
    Original file line number Diff line number Diff line change
    @@ -52,8 +52,8 @@ BEGIN{ FS=",$" }
    if ($0 == ");"){
    print
    } else {
    if ( match( $0, /\"[^\"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
    if ( match( $0, /\([^\)]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
    if ( match( $0, /\"[^"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
    if ( match( $0, /\([^()]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
    key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
    }
    }
  7. @esperlu esperlu revised this gist May 26, 2011. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion mysql2sqlite
    Original file line number Diff line number Diff line change
    @@ -42,7 +42,6 @@ BEGIN{ FS=",$" }
    if (prev) print prev ","
    prev = $1
    }
    }
    # `KEY` lines are extracted from the `CREATE` block and stored in array for later print
    # in a separate `CREATE KEY` command. The index name is prefixed by the table name to
  8. @esperlu esperlu revised this gist May 26, 2011. 1 changed file with 8 additions and 7 deletions.
    15 changes: 8 additions & 7 deletions mysql2sqlite
    Original file line number Diff line number Diff line change
    @@ -32,16 +32,17 @@ BEGIN{ FS=",$" }
    # Print all fields definition lines except the `KEY` lines.
    /^ / && !/^( KEY|\);)/ {
    gsub( /AUTO_INCREMENT/, "" )
    gsub( /CHARACTER SET [^ ]+ /, "" )
    gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP/, "" )
    gsub( /COLLATE [^ ]+ /, "" )
    gsub(/enum[^)]+\)/, "text ")
    gsub(/set\([^)]+\)/, "text ")
    gsub(/unsigned/, "")
    gsub( /AUTO_INCREMENT|auto_increment/, "" )
    gsub( /(CHARACTER SET|character set) [^ ]+ /, "" )
    gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP|default current_timestamp on update current_timestamp/, "" )
    gsub( /(COLLATE|collate) [^ ]+ /, "" )
    gsub(/(ENUM|enum)[^)]+\)/, "text ")
    gsub(/(SET|set)\([^)]+\)/, "text ")
    gsub(/UNSIGNED|unsigned/, "")
    if (prev) print prev ","
    prev = $1
    }
    }
    # `KEY` lines are extracted from the `CREATE` block and stored in array for later print
    # in a separate `CREATE KEY` command. The index name is prefixed by the table name to
  9. @esperlu esperlu revised this gist Apr 27, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mysql2sqlite
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    #!/bin/sh

    # Awk is choosen because it's fast and portable. You can use gawk, traditional awk or even the lightning fast mawk.
    # Awk is choosen because it's fast and portable. You can use gawk, original awk or even the lightning fast mawk.
    # The mysqldump file is traversed only once.

    # Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
  10. @esperlu esperlu revised this gist Apr 27, 2011. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion mysql2sqlite
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@
    # The mysqldump file is traversed only once.

    # Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
    # CREATE block and create them in a separate commands _after_ all the INSERTs.
    # CREATE block and create them in separate commands _after_ all the INSERTs.

    # Usage: $ ./mysql2sqlite | sqlite3 database.sqlite

  11. @esperlu esperlu created this gist Apr 27, 2011.
    65 changes: 65 additions & 0 deletions mysql2sqlite
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    #!/bin/sh

    # Awk is choosen because it's fast and portable. You can use gawk, traditional awk or even the lightning fast mawk.
    # The mysqldump file is traversed only once.

    # Converts a mysqldump file into a Sqlite 3 compatible file. It also extracts the MySQL `KEY xxxxx` from the
    # CREATE block and create them in a separate commands _after_ all the INSERTs.

    # Usage: $ ./mysql2sqlite | sqlite3 database.sqlite

    mysqldump --compatible=ansi --skip-extended-insert --compact -u **** -p****** datbase_name | \


    awk -F ",$" '
    BEGIN{ FS=",$" }
    # Skip comments
    /^\/\*/ { next }
    # Print all `INSERT` lines. The single quotes are protected by another single quote.
    /INSERT/ { gsub( /\\\047/, "\047\047" ); print; next }
    # Print the ´CREATE´ line as is and capture the table name.
    /^CREATE/ {
    print
    if ( match( $0, /\"[^\"]+/ ) ) tableName = substr( $0, RSTART+1, RLENGTH-1 )
    }
    # Replace `FULLTEXT KEY` or any other `XXXXX KEY` except PRIMARY by `KEY`
    /^ [^"]+KEY/ && !/^ PRIMARY KEY/ { gsub( /.+KEY/, " KEY" ) }
    # Print all fields definition lines except the `KEY` lines.
    /^ / && !/^( KEY|\);)/ {
    gsub( /AUTO_INCREMENT/, "" )
    gsub( /CHARACTER SET [^ ]+ /, "" )
    gsub( /DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP/, "" )
    gsub( /COLLATE [^ ]+ /, "" )
    gsub(/enum[^)]+\)/, "text ")
    gsub(/set\([^)]+\)/, "text ")
    gsub(/unsigned/, "")
    if (prev) print prev ","
    prev = $1
    }
    # `KEY` lines are extracted from the `CREATE` block and stored in array for later print
    # in a separate `CREATE KEY` command. The index name is prefixed by the table name to
    # avoid a sqlite error for duplicate index name.
    /^( KEY|\);)/ {
    if (prev) print prev
    prev=""
    if ($0 == ");"){
    print
    } else {
    if ( match( $0, /\"[^\"]+/ ) ) indexName = substr( $0, RSTART+1, RLENGTH-1 )
    if ( match( $0, /\([^\)]+/ ) ) indexKey = substr( $0, RSTART+1, RLENGTH-1 )
    key[tableName]=key[tableName] "CREATE INDEX \"" tableName "_" indexName "\" ON \"" tableName "\" (" indexKey ");\n"
    }
    }
    # Print all `KEY` creation lines.
    END {
    for (table in key) printf key[table]
    }
    '