Skip to content

Commit c8375de

Browse files
committed
Added an database automigration feature to the docker image
1 parent 594a577 commit c8375de

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

.docker/partdb-entrypoint.sh

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,48 @@ fi
4242
# Start PHP-FPM (the PHP_VERSION is replaced by the configured version in the Dockerfile)
4343
service phpPHP_VERSION-fpm start
4444

45+
46+
# Run migrations if automigration is enabled via env variable DB_AUTOMIGRATE
47+
if [ "$DB_AUTOMIGRATE" = "true" ]; then
48+
echo "Waiting for database to be ready..."
49+
ATTEMPTS_LEFT_TO_REACH_DATABASE=60
50+
until [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ] || DATABASE_ERROR=$(sudo -E -u www-data php bin/console dbal:run-sql -q "SELECT 1" 2>&1); do
51+
if [ $? -eq 255 ]; then
52+
# If the Doctrine command exits with 255, an unrecoverable error occurred
53+
ATTEMPTS_LEFT_TO_REACH_DATABASE=0
54+
break
55+
fi
56+
sleep 1
57+
ATTEMPTS_LEFT_TO_REACH_DATABASE=$((ATTEMPTS_LEFT_TO_REACH_DATABASE - 1))
58+
echo "Still waiting for database to be ready... Or maybe the database is not reachable. $ATTEMPTS_LEFT_TO_REACH_DATABASE attempts left."
59+
done
60+
61+
if [ $ATTEMPTS_LEFT_TO_REACH_DATABASE -eq 0 ]; then
62+
echo "The database is not up or not reachable:"
63+
echo "$DATABASE_ERROR"
64+
exit 1
65+
else
66+
echo "The database is now ready and reachable"
67+
fi
68+
69+
# Check if there are any available migrations to do, by executing doctrine:migrations:up-to-date
70+
# and checking if the exit code is 0 (up to date) or 1 (not up to date)
71+
if sudo -E -u www-data php bin/console doctrine:migrations:up-to-date --no-interaction; then
72+
echo "Database is up to date, no migrations necessary."
73+
else
74+
echo "Migrations available..."
75+
echo "Do backup of database..."
76+
77+
sudo -E -u www-data mkdir -p /var/www/html/uploads/.automigration-backup/
78+
# Backup the database
79+
sudo -E -u www-data php bin/console partdb:backup -n --database /var/www/html/uploads/.automigration-backup/backup-$(date +%Y-%m-%d_%H-%M-%S).zip
80+
81+
# Check if there are any migration files
82+
sudo -E -u www-data php bin/console doctrine:migrations:migrate --no-interaction
83+
fi
84+
85+
fi
86+
4587
# first arg is `-f` or `--some-option` (taken from https://github.com/docker-library/php/blob/master/8.2/bullseye/apache/docker-php-entrypoint)
4688
if [ "${1#-}" != "$1" ]; then
4789
set -- apache2-foreground "$@"

docs/installation/installation_docker.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ services:
4747
- DATABASE_URL=sqlite:///%kernel.project_dir%/var/db/app.db
4848
# In docker env logs will be redirected to stderr
4949
- APP_ENV=docker
50+
51+
# Uncomment this, if you want to use the automatic database migration feature. With this you have you do not have to
52+
# run the doctrine:migrations:migrate commands on installation or upgrade. A database backup is written to the uploads/
53+
# folder (under .automigration-backup), so you can restore it, if the migration fails.
54+
# This feature is currently experimental, so use it at your own risk!
55+
# - DB_AUTOMIGRATE=true
5056

5157
# You can configure Part-DB using environment variables
5258
# Below you can find the most essential ones predefined
@@ -130,6 +136,12 @@ services:
130136
# In docker env logs will be redirected to stderr
131137
- APP_ENV=docker
132138

139+
# Uncomment this, if you want to use the automatic database migration feature. With this you have you do not have to
140+
# run the doctrine:migrations:migrate commands on installation or upgrade. A database backup is written to the uploads/
141+
# folder (under .automigration-backup), so you can restore it, if the migration fails.
142+
# This feature is currently experimental, so use it at your own risk!
143+
# - DB_AUTOMIGRATE=true
144+
133145
# You can configure Part-DB using environment variables
134146
# Below you can find the most essential ones predefined
135147
# However you can add add any other environment configuration you want here

0 commit comments

Comments
 (0)