Skip to content

Commit

Permalink
Database conversion scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
yschaeff committed May 13, 2016
1 parent eb5a2f3 commit 7643113
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 0 deletions.
49 changes: 49 additions & 0 deletions enforcer/utils/convert_mysql_to_sqlite
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
set -e

# This scipt converts a MySQL to a SQLite database. It assumes both
# old and new databases live on the same host and are accessable by the same
# user.

SCHEMA=../../src/db/schema.sqlite

DB_IN=""
DB_OUT=""
DB_HOST="localhost"
DB_USR="test"
DB_PWD="test"

while getopts "i:o:h:u:p:" arg; do
case $arg in
i) DB_IN=$OPTARG ;;
o) DB_OUT=$OPTARG ;;
h) DB_HOST=$OPTARG ;;
u) DB_USR=$OPTARG ;;
p) DB_PWD=$OPTARG ;;
*)
echo "usage: "$0" -i DATABASE_1.4 -o DATABASE_2.0 [-h HOST] [-u USER] [-p PASSWORD]"
exit 1
;;
esac
done

if [ -z $DB_IN ]; then
echo "ERROR: No input database specified (-i DB_NAME)"
exit 1
fi

if [ -z $DB_OUT ]; then
echo "ERROR: No output database specified (-o DB_FILE)"
exit 1
fi

rm -f $DB_OUT
sqlite3 $DB_OUT < $SCHEMA
echo "attach '$DB_IN' as REMOTE;" |
cat - sqlite_convert.sql | sqlite3 $DB_OUT

echo "Converting database"

mysqldump -u $DB_USR -p$DB_PWD -h $DB_HOST $DB_IN |grep INSERT | sed -r 's/^INSERT INTO `([^"]+)`/INSERT INTO "\1"/' > tmp
sqlite3 $DB_OUT < tmp
rm tmp
51 changes: 51 additions & 0 deletions enforcer/utils/convert_sqlite_to_mysql
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash
set -e

# This scipt converts a SQLite3 to a MySQL database. It assumes both
# old and new databases live on the same host and are accessable by the same
# user.

SCHEMA=../../src/db/schema.mysql

DB_IN=""
DB_OUT=""
DB_HOST="localhost"
DB_USR="test"
DB_PWD="test"

while getopts "i:o:h:u:p:" arg; do
case $arg in
i) DB_IN=$OPTARG ;;
o) DB_OUT=$OPTARG ;;
h) DB_HOST=$OPTARG ;;
u) DB_USR=$OPTARG ;;
p) DB_PWD=$OPTARG ;;
*)
echo "usage: "$0" -i DATABASE_1.4 -o DATABASE_2.0 [-h HOST] [-u USER] [-p PASSWORD]"
exit 1
;;
esac
done

if [ -z $DB_IN ]; then
echo "ERROR: No input database specified (-i DB_FILE)"
exit 1
fi

if [ -z $DB_OUT ]; then
echo "ERROR: No output database specified (-o DB_NAME)"
exit 1
fi

echo "Creating database $DB_OUT (as user $DB_USR)"
echo "DROP DATABASE IF EXISTS $DB_OUT;CREATE DATABASE $DB_OUT;" |
mysql -u $DB_USR -p$DB_PWD -h $DB_HOST

echo "Creating tables in $DB_OUT (as user $DB_USR)"
mysql -u $DB_USR -p$DB_PWD -h $DB_HOST $DB_OUT < $SCHEMA

echo "Converting database"

sqlite3 $DB_IN .dump | grep INSERT | grep -v sqlite_sequence | sed -r 's/^INSERT INTO "([^"]+)"/INSERT INTO `\1`/' > tmp
mysql -u $DB_USR -p$DB_PWD -h $DB_HOST $DB_OUT < tmp
rm tmp

0 comments on commit 7643113

Please sign in to comment.