Commit d3aec5b8 authored by BERJON Matthieu's avatar BERJON Matthieu

Add of a database migration file

This is a small bash script that dump the rails database table by table
and then import them in the new Django database.
Signed-off-by: BERJON Matthieu's avatarMatthieu Berjon <matthieu.berjon@inria.fr>
parent e6bc0044
#!/bin/sh
#
# Extract each table
USER=root
HOST=localhost
DB_SRC=allgo
DB_TGT=allgopy
TEMP_DIR=/tmp/sql
TABLES="docker_os webapps webapp_parameters webapp_versions users quotas \
job_queues jobs job_uploads"
DumpDb () {
# Dump the table without the creation schema
# using the argument -n and -t
mkdir -p $TEMP_DIR
for t in $TABLES
do
mysqldump -n -t --extended-insert=FALSE -u $USER -h $HOST $DB_SRC $t > $TEMP_DIR/db_$t.sql
done
}
LoadDb () {
# We have to first get all the tables (including the ones created by Django)
TABLES_TO_DROP=$(mysql -u $USER $DB_TGT -e 'SHOW TABLES;')
TO_DROP=$(echo $TABLES_TO_DROP | awk '{for(i=2;i<=NF;i++)print $i}')
for t in $TO_DROP
do
# We disable the foreign key constraint check in order to avoid dropping
# issues. We reactivate it just afterwards
mysql -u $USER -h $HOST $DB_TGT -e \
"SET FOREIGN_KEY_CHECKS = 0; DROP TABLE $t; SET FOREIGN_KEY_CHECKS = 1;"
done
# Use Django to recreate the tables
# Ensure that the virtual environment is activated
#
# In production, there is no need of virtual environment
cd ..
python manage.py migrate poc 0001_initial
# Insert the data into the DB
for t in $TABLES
do
mysql -u $USER -h $HOST $DB_TGT < $TEMP_DIR/db_$t.sql
done
# rm -rf $TEMP_DIR
}
Help() {
cat << EOF
Use: $0 [options]
Options:
-d, --dump Dump the tables of the allgo database into the sql folder
-i, --init Dump the tables of the allgo database, make the migration
and delete the dump (-d and -l at the same time)
-l, --load Load the tables into the allgopy database
-h, --help Show this help
EOF
}
while getopts dilh OPTIONS
do
case $OPTIONS in
d | --dump)
DumpDb
;;
i | --init)
DumpDb
LoadDb
rm -rf $TEMP_DIR
;;
l | --load)
LoadDb
;;
h | --help)
Help
;;
esac
done
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment