refactor(database): improve database initialization and backup handling

- Rename init script from init.sh to 00_init.sh for better ordering
- Rewrite database initialization script to support backup restoration
- Add proper migration handling with schema_migrations table
- Fix indentation and quoting issues in docker-compose.yml
- Remove old 00-init-db.sh script as it's replaced by new implementation
This commit is contained in:
kithmina1999
2025-06-06 16:26:11 +05:30
3 changed files with 13 additions and 66 deletions

View File

@@ -101,7 +101,7 @@ services:
target: /docker-entrypoint-initdb.d/migrations
consistency: cached
- type: bind
source: ./worklenz-backend/database/init.sh
source: ./worklenz-backend/database/00_init.sh
target: /docker-entrypoint-initdb.d/00_init.sh
consistency: cached
- type: bind
@@ -114,12 +114,14 @@ services:
elif command -v apk >/dev/null 2>&1; then
apk add --no-cache dos2unix
fi
find /docker-entrypoint-initdb.d -type f -name "*.sh" -exec sh -c "
find /docker-entrypoint-initdb.d -type f -name "*.sh" -exec sh -c '"'"'
for f; do
dos2unix \"\$f\" 2>/dev/null || true
chmod +x \"\$f\"
dos2unix "$f" 2>/dev/null || true
chmod +x "$f"
done
" sh {} +
'"'"' sh {} +
exec docker-entrypoint.sh postgres
'
db-backup:
@@ -136,12 +138,12 @@ services:
- ./pg_backups:/pg_backups #host dir for backups files
#setup bassh loop to backup data evey 24h
command: >
bash -c "while true; do
sleep 86400;
PGPASSWORD=$$POSTGRES_PASSWORD pg_dump -h worklenz_db -U $$POSTGRES_USER -d $$POSTGRES_DB \
> /pg_backups/worklenz_db_$(date +%Y-%m-%d_%H-%M-%S).sql;
find /pg_backups -type f -name '*.sql' -mtime +30 -delete;
done"
bash -c "while true; do
sleep 86400;
PGPASSWORD=$$POSTGRES_PASSWORD pg_dump -h worklenz_db -U $$POSTGRES_USER -d $$POSTGRES_DB \
> /pg_backups/worklenz_db_\$(date +%Y-%m-%d_%H-%M-%S).sql;
find /pg_backups -type f -name '*.sql' -mtime +30 -delete;
done"
restart: unless-stopped
networks:
- worklenz