Add script for migrations

This commit is contained in:
Aurélien Delobelle 2018-06-17 18:38:51 +02:00
parent c297c7b545
commit bb2868a583
3 changed files with 154 additions and 0 deletions

60
provisioning/move_apps.sh Executable file
View file

@ -0,0 +1,60 @@
#!/bin/sh
cp cof/settings/secret.py gestioCOF/settings/secret.py
# Exit on error
set -e
. provisioning/move_apps_helpers.sh
echo "-----"
echo "»»» Stopping services…"
systemctl stop daphne
systemctl stop worker
echo "»»» Updating database…"
_psql <<SQL
BEGIN;
\echo Updating 'migrations' table…
UPDATE django_migrations SET app='cof' WHERE app='gestioncof';
\echo Updating 'content_type' table…
UPDATE django_content_type SET app_label='cof' WHERE app_label='gestioncof';
\echo Renaming gestioncof tables…
DO
\$\$
DECLARE
r record;
BEGIN
FOR r IN
SELECT tablename FROM pg_tables
WHERE schemaname='public' AND tablename LIKE 'gestioncof_%'
LOOP
EXECUTE '
ALTER TABLE ' || quote_ident(r.tablename) || '
RENAME TO ' || quote_ident(replace(r.tablename, 'gestioncof', 'cof'))
;
END LOOP;
END
\$\$;
COMMIT;
SQL
echo "»»» Symlink secrets…"
echo "»»» Updating services…"
sed -i.bak -e 's/cof.settings/gestioCOF.settings/g; s/cof.asgi/gestioCOF.asgi/g' ${SERVICE_DAPHNE}
sed -i.bak -e 's/cof.settings/gestioCOF.settings/g' ${SERVICE_WORKER}
echo "»»» Restarting services…"
systemctl daemon-reload
systemctl start worker
systemctl start daphne
echo "-----"
echo "Looks Good To Me!!"
echo "If ok, you can delete '${SERVICE_DAPHNE}.bak' and '${SERVICE_WORKER}.bak'."

View file

@ -0,0 +1,37 @@
#!/bin/sh
_django_settings() {
# Returns the value of a Django settings.
echo $(python3 -c "
import ${DJANGO_SETTINGS_MODULE} as settings
print(settings.${1})")
}
DJANGO_SETTINGS_MODULE=${DJANGO_SETTINGS_MODULE:-gestioCOF.settings.dev}
# Tests that module exists
python3 -c "import ${DJANGO_SETTINGS_MODULE}"
echo "Django settings module: ${DJANGO_SETTINGS_MODULE}"
DBHOST=$(_django_settings DATABASES[\'default\'][\'HOST\'])
DBNAME=$(_django_settings DATABASES[\'default\'][\'NAME\'])
DBUSER=$(_django_settings DATABASES[\'default\'][\'USER\'])
DBPASSWORD=$(_django_settings DATABASES[\'default\'][\'PASSWORD\'])
_psql() {
PGPASSWORD=${DBPASSWORD} psql -h ${DBHOST} -d ${DBNAME} -U ${DBUSER} "$@"
}
echo "Database: ${DBNAME}"
SERVICE_DIR=${SERVICES_DIR:-/etc/systemd/system}
SERVICE_DAPHNE=${SERVICE_DIR}/${SERVICE_DAPHNE_FILENAME}
SERVICE_WORKER=${SERVICE_DIR}/${SERVICE_WORKER_FILENAME}
[ -e ${SERVICE_DAPHNE} ] || exit 1
[ -e ${SERVICE_WORKER} ] || exit 1
echo "Services:"
echo " - Daphne: ${SERVICE_DAPHNE}"
echo " - Worker: ${SERVICE_WORKER}"

View file

@ -0,0 +1,57 @@
#!/bin/bash
BRANCH_PRODUCTION="Production"
BRANCH_WIP="aureplop/restructuring"
export SERVICE_DAPHNE_FILENAME="daphne.service"
export SERVICE_WORKER_FILENAME="worker.service"
export VENV_DIR="/home/ubuntu"
# Exit on error
set -e
. provisioning/move_apps_helpers.sh
if [[ "$DJANGO_SETTINGS_MODULE" =~ "prod" ]] ;
then
echo "!!! Hint: Don't test me in production"
exit 1
fi
echo "Notes:"
echo " - The Vagrant DB data will be lost."
echo "-----"
echo "»»» Setup state, as of Production"
echo "- Checkout '$BRANCH_PRODUCTION'…"
git checkout "$BRANCH_PRODUCTION"
echo "- Prepare database…"
sudo -u postgres dropdb --if-exists $DBNAME
sudo -u postgres createdb $DBNAME
sudo -u postgres psql -c "GRANT ALL PRIVILEGES ON DATABASE $DBNAME TO $DBUSER"
echo "- Prepare Django…"
ln -s secret_example.py cof/settings/secret.py
. ${VENV_DIR}/venv/bin/activate
export DJANGO_SETTINGS_MODULE="cof.settings.dev"
bash provisioning/prepare_django.sh
echo "- Checkout '$BRANCH_WIP'…"
git checkout "$BRANCH_WIP"
export DJANGO_SETTINGS_MODULE="gestioCOF.settings.dev"
echo "##### RUNNING move_apps.sh #####"
sh provisioning/move_apps.sh
# For production, use:
# $ git pull
# $ export DJANGO_SETTINGS_MODULE="gestioCOF.settings.prod"
# $ sh provisioning/move_apps.sh
echo "############ ENDED #############"
# Add checks below this line