Problems while migrating from MySQL to SQLite

Having trouble installing serendipity?
Post Reply
theclaw
Posts: 2
Joined: Sun Mar 01, 2009 2:58 pm

Problems while migrating from MySQL to SQLite

Post 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!
garvinhicking
Core Developer
Posts: 30022
Joined: Tue Sep 16, 2003 9:45 pm
Location: Cologne, Germany
Contact:

Re: Problems while migrating from MySQL to SQLite

Post by garvinhicking »

Hi!

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

Regards,
Garvin
# Garvin Hicking (s9y Developer)
# Did I help you? Consider making me happy: http://wishes.garv.in/
# or use my PayPal account "paypal {at} supergarv (dot) de"
# My "other" hobby: http://flickr.garv.in/
theclaw
Posts: 2
Joined: Sun Mar 01, 2009 2:58 pm

Re: Problems while migrating from MySQL to SQLite

Post 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?
Post Reply