読者です 読者をやめる 読者になる 読者になる

へっぽこびんぼう野郎のnewbie日記

けろけーろ(´・ω・`)!

Djangoでmysqlのデータを取り込み直してmigrateするシェルスクリプト書いた

発端

Model変更→データかまう→やっぱやめた→dumpファイル入れなおす→migrate→Model変更→……→おぅふ、これ毎回やるのか……→めんどくさい!!!(´・ω・`)


必要におうじて編集してつかってほしいっ…!

recreate_db.sh

#!/bin/sh
# $1 -> filename


# Setting
dir="/path/to/django_project"
db_name="hoge_db"


if [ $# -lt 1 ]; then
echo "Usage: recreate_db {dumpfile} [is_migrate(y)]"
exit
fi

mysql -uroot -e "DROP DATABASE `$db_name`;" && echo "$db_name was dropped"
mysql -uroot -e "CREATE DATABASE `$db_name`;" && echo "$db_name was created"
mysql -u root "$db_name" < "$1" && echo 'Data was inserted'

cd $dir
is_migrate='n'

if [ $2 = 'y' ];then
is_migrate=$2
else
read -e -p "migrate?(y/n)" is_migrate;
fi

case $is_migrate in
y) python manage.py makemigrations; python manage.py migrate; break;;
*) exit;;
esac

$ sh recreate_db.sh ~/Downloads/dumpfile y
hoge_db was dropped
hoge_db was created
Data was inserted
No changes detected
Operations to perform:
Synchronize unmigrated apps: debug_toolbar, django_extensions
Apply all migrations: default, core, auth, contenttypes, sessions, admin
Synchronizing apps without migrations:
Creating tables...
Installing custom SQL...
Installing indexes...
Running migrations:
Applying core.0052_auto_20150722_1615... OK
Applying default.0004_auto_20150508_2308... OK