Ajout d'un script pour faciliter les expérimentations

This commit is contained in:
Jean-Marie Favreau 2024-11-11 11:11:54 +01:00
parent 6e37828f90
commit 8cd891ad3a
2 changed files with 93 additions and 9 deletions

View File

@ -38,14 +38,7 @@ Pour ajouter une nouvelle source custom:
* sur le serveur de dev:
* ```docker exec -i agenda_culturel-backend python3 manage.py dumpdata --format=json --exclude=admin.logentry --exclude=auth.group --exclude=auth.permission --exclude=auth.user --exclude=contenttypes --indent=2 > fixtures/postgres-backup-20241101.json``` (à noter qu'ici on oublie les comptes, qu'il faudra recréer)
* sur le serveur de prod:
* On supprime la base de données: ```docker exec -i agenda_culturel-backend python3 manage.py flush```
* On réinitialise l'état de la base de données: ```docker exec -i agenda_culturel-backend python3 manage.py migrate agenda_culturel zero```
* On reprend à un état de migration équivalent au serveur: ```docker exec -i agenda_culturel-backend python3 manage.py migrate agenda_culturel [00xx]```
* On récupère le dump json ```scp $SERVEUR:$PATH/fixtures/postgres-backup-20241101.json src/fixtures/```
* On l'importe: ```docker exec -i agenda_culturel-backend python3 manage.py loaddata --format=json fixtures/postgres-backup-20241101.json```
* ```scripts/reset-database.sh FIXTURE COMMIT``` où ```FIXTURE``` est le timestamp dans le nom de la fixture, et ```COMMIT``` est l'ID du commit git correspondant à celle en prod sur le serveur au moment de la création de la fixture
À noter qu'on a supprimé de la base de donnée les comptes, et qu'il faudra les recréer:
* ```docker exec -ti agenda_culturel-backend python3 manage.py createsuperuser```
De plus, les images ne sont pas récupérées.
À noter que les images ne sont pas récupérées.

91
scripts/reset-database.sh Executable file
View File

@ -0,0 +1,91 @@
#!/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