Found in the VM test: Prisma records a failed migration and then refuses every later migrate deploy (P3009), so one broken release left a church unable to update. On rollback, failed migrations the previous release doesn't have are marked rolled back (prisma migrate resolve); the backup still has the database as it was. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
145 lines
6.3 KiB
Bash
145 lines
6.3 KiB
Bash
# shellcheck shell=bash
|
|
# Cross Events' hooks for the crosscode script (from hope-events' scripts/deploy.sh 2.1.x).
|
|
# Every hook takes the product's checkout (EVENTS_DIR, a checkout of crosscode-platform) and the
|
|
# app user, and runs the app's commands as that user. Values come from church.env (CENV).
|
|
#
|
|
# Adding a product means adding a file like this one with the same hooks, and its name in
|
|
# SUPPORTED_PRODUCTS (lib/common.sh).
|
|
|
|
events_app() { printf '%s/apps/events' "$1"; }
|
|
|
|
events_pm2_names() { pvar events PM2; }
|
|
events_pm2_config() { printf '%s/ecosystem.config.js' "$(events_app "$1")"; }
|
|
|
|
# EVENTS_PORTS="<api> <web>"
|
|
events_api_port() { local p; read -r p _ <<< "$(pvar events PORTS)"; printf '%s' "${p:-5000}"; }
|
|
events_web_port() { local p; read -r _ p <<< "$(pvar events PORTS)"; printf '%s' "${p:-3000}"; }
|
|
events_health_url() { printf 'http://127.0.0.1:%s/health' "$(events_api_port)"; }
|
|
|
|
# The version /health reports once this checkout runs (the backend's package.json).
|
|
events_expected_version() {
|
|
node -p 'require(process.argv[1]).version' "$(events_app "$1")/backend/package.json"
|
|
}
|
|
|
|
events_backup() {
|
|
local dir="$1" user="$2"
|
|
# shellcheck disable=SC2016
|
|
run_as "$user" "$(events_app "$dir")/backend" node -e '
|
|
require("dotenv").config();
|
|
require("./src/utils/backupUtils").runBackup()
|
|
.then(r => console.log(" " + r.filename))
|
|
.catch(e => { console.error(" Backup failed: " + e.message); process.exit(1); });'
|
|
}
|
|
|
|
# Only this app's workspaces, never the other products'. --include=dev: the TypeScript and Next
|
|
# builds need type and build packages even where NODE_ENV=production.
|
|
events_install_deps() {
|
|
run_as "$2" "$1" npm ci --include=dev --no-audit --no-fund -w apps/events/backend -w apps/events/frontend
|
|
}
|
|
|
|
events_migrate() {
|
|
run_as "$2" "$(events_app "$1")/backend" npm run prisma:deploy
|
|
}
|
|
|
|
events_pending_migrations() {
|
|
# prisma migrate status exits non-zero when migrations are pending (or the DB can't be read).
|
|
! run_as "$2" "$(events_app "$1")/backend" npx prisma migrate status >/dev/null 2>&1
|
|
}
|
|
|
|
events_build() {
|
|
run_as "$2" "$(events_app "$1")/backend" npm run build &&
|
|
run_as "$2" "$(events_app "$1")/frontend" npm run build
|
|
}
|
|
|
|
# After going back to the previous tag: install and build it again. Migrations aren't undone
|
|
# (the backup taken first has the database as it was).
|
|
events_rollback_build() {
|
|
events_install_deps "$1" "$2" && events_build "$1" "$2"
|
|
}
|
|
|
|
# The install's own PM2 config (not in git). Created from the example the first time, with this
|
|
# church's PM2 names and web port.
|
|
events_ensure_pm2_config() {
|
|
local dir="$1" user="$2" app conf backend frontend
|
|
app="$(events_app "$dir")"
|
|
conf="$app/ecosystem.config.js"
|
|
[ -f "$conf" ] && return 0
|
|
read -r backend frontend <<< "$(events_pm2_names)"
|
|
sed -e "s/'hope-events-backend'/'${backend}'/" \
|
|
-e "s/'hope-events-frontend'/'${frontend}'/" \
|
|
-e "s/-p 3000/-p $(events_web_port)/" \
|
|
"$app/ecosystem.config.example.js" > "$conf"
|
|
chown "$user:$user" "$conf" 2>/dev/null || true
|
|
info "Created $conf from the example."
|
|
}
|
|
|
|
events_reload() {
|
|
events_ensure_pm2_config "$1" "$2"
|
|
run_as "$2" "$(events_app "$1")" pm2 reload ecosystem.config.js --update-env
|
|
}
|
|
|
|
# The church's data as crosscode.church-export/1 into <out dir> (owned by the app user). Releases
|
|
# from before the export (events@2.2.0) don't have the script.
|
|
events_export() {
|
|
local dir="$1" user="$2" out="$3" app
|
|
app="$(events_app "$dir")"
|
|
[ -f "$app/backend/scripts/export-church.ts" ] ||
|
|
die "This release of Events ($(checkout_ref "$dir" events)) has no export yet. Update it first."
|
|
run_as "$user" "$app/backend" npx tsx scripts/export-church.ts --out-dir "$out"
|
|
}
|
|
|
|
# Uploaded files into <dest> (WhatsApp ticket PDFs waiting to be sent are left out).
|
|
events_copy_uploads() {
|
|
local src
|
|
src="$(events_app "$1")/backend/public/uploads"
|
|
mkdir -p "$2"
|
|
[ -d "$src" ] || return 0
|
|
rsync -a --exclude tickets-temp/ "$src/" "$2/"
|
|
}
|
|
|
|
# What uninstall keeps for 30 days in <dest>: the backups (the last is the final one), the
|
|
# uploads, and the .env files (the backup's stored secrets are encrypted with its JWT_SECRET).
|
|
events_keep() {
|
|
local app dest="$2"
|
|
app="$(events_app "$1")"
|
|
mkdir -p "$dest"
|
|
[ ! -d "$app/backend/backups" ] || rsync -a "$app/backend/backups/" "$dest/backups/"
|
|
[ ! -d "$app/backend/public/uploads" ] || tar -czf "$dest/uploads.tar.gz" -C "$app/backend/public" uploads
|
|
cp -p "$app/backend/.env" "$dest/backend.env" 2>/dev/null || true
|
|
cp -p "$app/frontend/.env.production" "$dest/frontend.env.production" 2>/dev/null || true
|
|
chmod -R go-rwx "$dest"
|
|
}
|
|
|
|
# --var arguments for the env template (envtpl.cjs), from church.env.
|
|
events_env_vars() {
|
|
printf '%s\n' --var "domain=$(pvar events DOMAIN)" --var "apiPort=$(events_api_port)" --var "webPort=$(events_web_port)" --var "church=$(cenv CHURCH)"
|
|
}
|
|
|
|
# After a failed deploy, before going back to <previous ref>: migrations Prisma recorded as failed
|
|
# that the previous release doesn't have are marked as rolled back, so the next deploy isn't
|
|
# blocked (P3009). The database itself isn't changed: the backup taken first has it as it was.
|
|
events_resolve_failed_migrations() {
|
|
local dir="$1" user="$2" prev="$3" backend name
|
|
backend="$(events_app "$dir")/backend"
|
|
local -a failed
|
|
# shellcheck disable=SC2016 # JavaScript, not shell
|
|
mapfile -t failed < <(run_as "$user" "$backend" node -e '
|
|
require("dotenv").config();
|
|
const { Client } = require("pg");
|
|
const c = new Client({ connectionString: process.env.DATABASE_URL });
|
|
c.connect()
|
|
.then(() => c.query("select migration_name from _prisma_migrations where finished_at is null and rolled_back_at is null"))
|
|
.then(r => { r.rows.forEach(x => console.log(x.migration_name)); return c.end(); })
|
|
.catch(e => { console.error(e.message); process.exit(1); });')
|
|
for name in "${failed[@]}"; do
|
|
[ -n "$name" ] || continue
|
|
if git_in "$dir" cat-file -e "$prev:apps/events/backend/prisma/migrations/$name/migration.sql" 2>/dev/null; then
|
|
warn "events: $name failed but $prev has it too; left for a look (npx prisma migrate status)."
|
|
continue
|
|
fi
|
|
run_as "$user" "$backend" npx prisma migrate resolve --rolled-back "$name" >/dev/null
|
|
info "events: marked the failed migration $name as rolled back."
|
|
report_result "resolvedMigration" "$name"
|
|
done
|
|
}
|