From 70599083b810103ea82e49490246b31607f4d601 Mon Sep 17 00:00:00 2001 From: Stefan Hardegger Date: Mon, 20 Oct 2025 12:43:58 +0200 Subject: [PATCH] db migration --- apply_migration_production.sh | 45 +++++++++++++++++++++++++++++++++++ docker-compose.yml | 28 ++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100755 apply_migration_production.sh diff --git a/apply_migration_production.sh b/apply_migration_production.sh new file mode 100755 index 0000000..3622da6 --- /dev/null +++ b/apply_migration_production.sh @@ -0,0 +1,45 @@ +#!/bin/bash + +# Run this script on your production server to apply the backup_jobs table migration +# to all library databases + +echo "Applying backup_jobs table migration to all databases..." +echo "" + +# Apply to each database +for DB in storycove storycove_afterdark storycove_clas storycove_secret; do + echo "Applying to $DB..." + docker-compose exec -T postgres psql -U storycove -d "$DB" <<'SQL' +CREATE TABLE IF NOT EXISTS backup_jobs ( + id UUID PRIMARY KEY, + library_id VARCHAR(255) NOT NULL, + type VARCHAR(50) NOT NULL CHECK (type IN ('DATABASE_ONLY', 'COMPLETE')), + status VARCHAR(50) NOT NULL CHECK (status IN ('PENDING', 'IN_PROGRESS', 'COMPLETED', 'FAILED', 'EXPIRED')), + file_path VARCHAR(1000), + file_size_bytes BIGINT, + progress_percent INTEGER, + error_message VARCHAR(1000), + created_at TIMESTAMP NOT NULL, + started_at TIMESTAMP, + completed_at TIMESTAMP, + expires_at TIMESTAMP +); + +CREATE INDEX IF NOT EXISTS idx_backup_jobs_library_id ON backup_jobs(library_id); +CREATE INDEX IF NOT EXISTS idx_backup_jobs_status ON backup_jobs(status); +CREATE INDEX IF NOT EXISTS idx_backup_jobs_expires_at ON backup_jobs(expires_at); +CREATE INDEX IF NOT EXISTS idx_backup_jobs_created_at ON backup_jobs(created_at DESC); +SQL + echo "✓ Done with $DB" + echo "" +done + +echo "Migration complete! Verifying..." +echo "" + +# Verify tables exist +for DB in storycove storycove_afterdark storycove_clas storycove_secret; do + echo "Checking $DB:" + docker-compose exec -T postgres psql -U storycove -d "$DB" -c "\d backup_jobs" 2>&1 | grep -E "Table|does not exist" || echo " ✓ Table exists" + echo "" +done diff --git a/docker-compose.yml b/docker-compose.yml index d1614cf..72f079c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -66,6 +66,34 @@ services: networks: - storycove-network + migrations: + image: postgres:15-alpine + depends_on: + - postgres + volumes: + - ./backend/create_backup_jobs_table.sql:/migrations/create_backup_jobs_table.sql:ro + networks: + - storycove-network + entrypoint: /bin/sh + command: > + -c " + echo 'Waiting for postgres to be ready...'; + sleep 5; + echo 'Applying migrations to all databases...'; + for DB in storycove storycove_afterdark storycove_clas storycove_secret; do + echo \"Checking if database \$$DB exists...\"; + if PGPASSWORD=${DB_PASSWORD} psql -h postgres -U storycove -lqt | cut -d \\| -f 1 | grep -qw \$$DB; then + echo \"Applying migration to \$$DB...\"; + PGPASSWORD=${DB_PASSWORD} psql -h postgres -U storycove -d \$$DB -f /migrations/create_backup_jobs_table.sql; + echo \"✓ Migration applied to \$$DB\"; + else + echo \"⚠ Database \$$DB does not exist, skipping...\"; + fi; + done; + echo 'All migrations complete!'; + " + restart: "no" + solr: build: