#!/bin/sh FIXTURE=$1 COMMIT=$2 FORCE=$3 help() { echo "USAGE: scripts/reset-database.sh [FIXTURE] [COMMIT]" echo " " echo "Parameters:" echo " FIXTURE A timestamp used in fixture name" echo " COMMIT A commit ID used by git checkout" echo " " echo "Example:" echo " scripts/reset-database.sh 20241110 cb69ece6ca5ba04e94dcc2758f53869c70224592" } bold=$(tput bold) normal=$(tput sgr0) echobold() { echo "${bold}$1${normal}" } if ! [ -n "$FORCE" ]; then nginx=`docker ps|grep nginx` if [ -n "$nginx" ]; then echo "WARNING: this script is probably run on a production server. Use a third parameter if you really want to run it." exit 3 fi fi if ! [ -n "$FIXTURE" ]; then echo "No fixture defined. Abort." help exit 1 fi if ! [ -n "$COMMIT" ]; then echo "No commit version defined. Abort." help exit 1 fi FFILE=fixtures/postgres-backup-$FIXTURE.json if ! [ -f "src/$FFILE" ]; then echo "ERROR: missing fixture file ($FFILE)" exit 2 fi echo " " echobold "WARNING: use Ctrl+C to stop the reset process since a 'no' answer cannot be detected." echo " " # remove all elements in database echobold "Flush database" docker exec -i agenda_culturel-backend python3 manage.py flush # move back database structure to the original echobold "Setup database structure to zero" docker exec -i agenda_culturel-backend python3 manage.py migrate agenda_culturel zero # reset code depending on a specific commit echobold "Move back to the desired commit" git checkout $COMMIT # change database to reach this specific version echobold "Setup database stucture according to the selected commit" docker exec -i agenda_culturel-backend python3 manage.py migrate agenda_culturel # import data echobold "Import data" docker exec -i agenda_culturel-backend python3 manage.py loaddata --format=json $FFILE # reset code to uptodate version echobold "Move back to last commit" git checkout main # update database structure echobold "Update database" docker exec -i agenda_culturel-backend python3 manage.py migrate agenda_culturel # create superuser echobold "Create superuser" docker exec -ti agenda_culturel-backend python3 manage.py createsuperuser