Page 1 of 1

Problems while migrating from MySQL to SQLite

Posted: Sun Mar 01, 2009 3:13 pm
by theclaw
Hello,

I used the following script to convert my MySQL database to SQLite:

Code: Select all

#!/bin/bash

if [ "x$1" == "x" ]; then
   echo "Usage: $0 <dbname>"
   exit
fi

if [ -e "$1.db" ]; then
   echo "$1.db already exists.  I will overwrite it in 15 seconds if you do not press CTRL-C."
   COUNT=15
   while [ $COUNT -gt 0 ]; do
      echo "$COUNT"
      sleep 1
      COUNT=$((COUNT - 1))
   done
   rm $1.db
fi

export KEYSFILE=$1.keys

mysqldump -u root -p --compact --compatible=ansi --default-character-set=binary $1 |
perl -pe '
BEGIN {
    open(KEYS, ">".$ENV{KEYSFILE}) 
}

if(/CREATE TABLE "(\w*)"/) {
    $tablename = $1
}

if(/^\s*KEY /) {
    my ($index_name) = m/^\s*KEY "(\w+)"/;
    $fields = $'\'';
    $fields =~ s/"//g;
    chomp $fields;
    {local $/=","; chomp $fields}
    print KEYS "CREATE INDEX $index_name ON $tablename $fields;\n";
}' |
grep -v ' KEY "' |
grep -v ' UNIQUE KEY "' |
grep -v ' PRIMARY KEY ' |
grep -v '^SET' |
sed 's/ unsigned / /g' |
sed 's/ auto_increment/ /gi' |
sed 's/ smallint([0-9]*) / integer /gi' |
sed 's/ tinyint([0-9]*) / integer /gi' |
sed 's/ int([0-9]*) / integer /gi' |
sed 's/ character set [^ ]* / /gi' |
sed 's/ enum([^)]*) / varchar(255) /gi' |
sed 's/ on update [^,]*//gi' |
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' |
perl -pe '
if (/^(INSERT.+?)\(/) {
   $a=$1;
   s/\\'\''/'\'\''/g;
   s/\\n/\n/g;
   s/\\r/\r/g;
   s/\\"/"/g;
   s/\),\(/\);\n$a\(/g;
}
' > $1.sql
cat $KEYSFILE >> $1.sql

cat $1.sql | sqlite $1.db > $1.err
ERRORS=`cat $1.err | wc -l`
if [ "$ERRORS" == "0" ]; then
   echo "Conversion completed without error. Output file: $1.db"
   rm $1.sql
   rm $1.err
else
   echo "There were errors during conversion.  Please review $1.err and $1.sql for details."
fi
(It's actually a script found on http://www.sqlite.org/cvstrac/wiki?p=ConverterTools, but I adapted it to work with SQLite 2 (instead of 3) and fixed some bugs.)

So this script seems to work, and my blog gets displayed correctly; however each time I try to add a blogentry, I get the following error:
Warning: sqlite_query() [function.sqlite-query]: serendipity_entries.id may not be NULL in /home/theclaw/www/theclaw/blog/include/db/sqlite.inc.php on line 229
Fehler: Einträge konnten nicht importiert werden!
(The last line is german an roughly translates to "Couldn't import entries")

I'm using s9y 1.4.1. Any idea what went wrong?

Thanks in advance!

Re: Problems while migrating from MySQL to SQLite

Posted: Mon Mar 02, 2009 9:36 am
by garvinhicking
Hi!

Sounds like the importer did not import the serendipity_entries.id column as an auto-increment primary key.

Regards,
Garvin

Re: Problems while migrating from MySQL to SQLite

Posted: Mon Mar 02, 2009 12:33 pm
by theclaw
Thanks, it's working now :)

Here's the script, if somebody needs it:

Code: Select all

#!/bin/bash

if [ "x$1" == "x" ]; then
   echo "Usage: $0 <dbname>"
   exit
fi

if [ -e "$1.db" ]; then
   echo "$1.db already exists.  I will overwrite it in 15 seconds if you do not press CTRL-C."
   COUNT=15
   while [ $COUNT -gt 0 ]; do
      echo "$COUNT"
      sleep 1
      COUNT=$((COUNT - 1))
   done
   rm $1.db
fi

export KEYSFILE=$1.keys

mysqldump -u root -p --compact --compatible=ansi --default-character-set=binary $1 |
perl -pe '
BEGIN {
    open(KEYS, ">".$ENV{KEYSFILE}) 
}

if(/CREATE TABLE "(\w*)"/) {
    $tablename = $1
}

if(/^\s*KEY /) {
    my ($index_name) = m/^\s*KEY "(\w+)"/;
    $fields = $'\'';
    $fields =~ s/"//g;
    chomp $fields;
    {local $/=","; chomp $fields}
    print KEYS "CREATE INDEX $index_name ON $tablename $fields;\n";
}' |
grep -v ' KEY "' |
grep -v ' UNIQUE KEY "' |
grep -v ' PRIMARY KEY ' |
grep -v '^SET' |
sed 's/ unsigned / /g' |
sed 's/int([0-9]*) NOT NULL auto_increment/INTEGER PRIMARY KEY/' |
sed 's/ smallint([0-9]*) / integer /gi' |
sed 's/ tinyint([0-9]*) / integer /gi' |
sed 's/ int([0-9]*) / integer /gi' |
sed 's/ character set [^ ]* / /gi' |
sed 's/ enum([^)]*) / varchar(255) /gi' |
sed 's/ on update [^,]*//gi' |
perl -e 'local $/;$_=<>;s/,\n\)/\n\)/gs;print "begin;\n";print;print "commit;\n"' |
perl -pe '
if (/^(INSERT.+?)\(/) {
   $a=$1;
   s/\\'\''/'\'\''/g;
   s/\\n/\n/g;
   s/\\r/\r/g;
   s/\\"/"/g;
   s/\),\(/\);\n$a\(/g;
}
' > $1.sql
cat $KEYSFILE >> $1.sql

cat $1.sql | sqlite $1.db > $1.err
ERRORS=`cat $1.err | wc -l`
if [ "$ERRORS" == "0" ]; then
   echo "Conversion completed without error. Output file: $1.db"
   rm $1.sql
   rm $1.err
else
   echo "There were errors during conversion.  Please review $1.err and $1.sql for details."
fi
Btw., does s9y also support sqlite3?