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:
@@ -101,7 +101,7 @@ services:
|
|||||||
target: /docker-entrypoint-initdb.d/migrations
|
target: /docker-entrypoint-initdb.d/migrations
|
||||||
consistency: cached
|
consistency: cached
|
||||||
- type: bind
|
- type: bind
|
||||||
source: ./worklenz-backend/database/init.sh
|
source: ./worklenz-backend/database/00_init.sh
|
||||||
target: /docker-entrypoint-initdb.d/00_init.sh
|
target: /docker-entrypoint-initdb.d/00_init.sh
|
||||||
consistency: cached
|
consistency: cached
|
||||||
- type: bind
|
- type: bind
|
||||||
@@ -114,12 +114,14 @@ services:
|
|||||||
elif command -v apk >/dev/null 2>&1; then
|
elif command -v apk >/dev/null 2>&1; then
|
||||||
apk add --no-cache dos2unix
|
apk add --no-cache dos2unix
|
||||||
fi
|
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
|
for f; do
|
||||||
dos2unix \"\$f\" 2>/dev/null || true
|
dos2unix "$f" 2>/dev/null || true
|
||||||
chmod +x \"\$f\"
|
chmod +x "$f"
|
||||||
done
|
done
|
||||||
" sh {} +
|
'"'"' sh {} +
|
||||||
|
|
||||||
exec docker-entrypoint.sh postgres
|
exec docker-entrypoint.sh postgres
|
||||||
'
|
'
|
||||||
db-backup:
|
db-backup:
|
||||||
@@ -139,7 +141,7 @@ services:
|
|||||||
bash -c "while true; do
|
bash -c "while true; do
|
||||||
sleep 86400;
|
sleep 86400;
|
||||||
PGPASSWORD=$$POSTGRES_PASSWORD pg_dump -h worklenz_db -U $$POSTGRES_USER -d $$POSTGRES_DB \
|
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;
|
> /pg_backups/worklenz_db_\$(date +%Y-%m-%d_%H-%M-%S).sql;
|
||||||
find /pg_backups -type f -name '*.sql' -mtime +30 -delete;
|
find /pg_backups -type f -name '*.sql' -mtime +30 -delete;
|
||||||
done"
|
done"
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
|
|||||||
@@ -1,55 +0,0 @@
|
|||||||
#!/bin/bash
|
|
||||||
set -e
|
|
||||||
|
|
||||||
# This script controls the order of SQL file execution during database initialization
|
|
||||||
echo "Starting database initialization..."
|
|
||||||
|
|
||||||
# Check if we have SQL files in expected locations
|
|
||||||
if [ -f "/docker-entrypoint-initdb.d/sql/0_extensions.sql" ]; then
|
|
||||||
SQL_DIR="/docker-entrypoint-initdb.d/sql"
|
|
||||||
echo "Using SQL files from sql/ subdirectory"
|
|
||||||
elif [ -f "/docker-entrypoint-initdb.d/0_extensions.sql" ]; then
|
|
||||||
# First time setup - move files to subdirectory
|
|
||||||
echo "Moving SQL files to sql/ subdirectory..."
|
|
||||||
mkdir -p /docker-entrypoint-initdb.d/sql
|
|
||||||
|
|
||||||
# Move all SQL files (except this script) to the subdirectory
|
|
||||||
for f in /docker-entrypoint-initdb.d/*.sql; do
|
|
||||||
if [ -f "$f" ]; then
|
|
||||||
cp "$f" /docker-entrypoint-initdb.d/sql/
|
|
||||||
echo "Copied $f to sql/ subdirectory"
|
|
||||||
fi
|
|
||||||
done
|
|
||||||
|
|
||||||
SQL_DIR="/docker-entrypoint-initdb.d/sql"
|
|
||||||
else
|
|
||||||
echo "SQL files not found in expected locations!"
|
|
||||||
exit 1
|
|
||||||
fi
|
|
||||||
|
|
||||||
# Execute SQL files in the correct order
|
|
||||||
echo "Executing 0_extensions.sql..."
|
|
||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/0_extensions.sql"
|
|
||||||
|
|
||||||
echo "Executing 1_tables.sql..."
|
|
||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/1_tables.sql"
|
|
||||||
|
|
||||||
echo "Executing indexes.sql..."
|
|
||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/indexes.sql"
|
|
||||||
|
|
||||||
echo "Executing 4_functions.sql..."
|
|
||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/4_functions.sql"
|
|
||||||
|
|
||||||
echo "Executing triggers.sql..."
|
|
||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/triggers.sql"
|
|
||||||
|
|
||||||
echo "Executing 3_views.sql..."
|
|
||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/3_views.sql"
|
|
||||||
|
|
||||||
echo "Executing 2_dml.sql..."
|
|
||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/2_dml.sql"
|
|
||||||
|
|
||||||
echo "Executing 5_database_user.sql..."
|
|
||||||
psql -v ON_ERROR_STOP=1 --username "$POSTGRES_USER" --dbname "$POSTGRES_DB" -f "$SQL_DIR/5_database_user.sql"
|
|
||||||
|
|
||||||
echo "Database initialization completed successfully"
|
|
||||||
Reference in New Issue
Block a user