From c8d46ef2d9b29e7d3bfc187139913a79eb06c37f Mon Sep 17 00:00:00 2001 From: Joshua Oosthuizen Date: Tue, 17 Nov 2026 15:15:09 +0200 Subject: [PATCH] adopt: apply the release's new migrations in the switch-over (events@2.2.0 has one) Found in the VM test: adopt refused events@2.2.0 because the AI tokens migration made it into the release, and adopt only moved installs with no migrations. Now the migrations the release has and the old install doesn't run after the backup, while neither version runs; a failure starts the old install again. They're recorded (EVENTS_ADOPT_MIGRATIONS) so adopt --undo can still go back past them (they only add columns). Co-Authored-By: Claude Opus 5.5 --- scripts/lib/adopt.sh | 41 +++++++++++++++++++++++++++++++++-------- 1 file changed, 33 insertions(+), 8 deletions(-) diff --git a/scripts/lib/adopt.sh b/scripts/lib/adopt.sh index c61466f..f3e977b 100644 --- a/scripts/lib/adopt.sh +++ b/scripts/lib/adopt.sh @@ -61,6 +61,14 @@ rewrite_ecosystem() { sed -E "s#([\"'\`])${old}(/|[\"'\`])#\\1${new}\\2#g" } +# migrations_only_in : the Prisma migrations the first has and +# the second doesn't, one per line, sorted. +migrations_only_in() { + comm -13 \ + <(find "$2/prisma/migrations" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null | sort) \ + <(find "$1/prisma/migrations" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' 2>/dev/null | sort) +} + # Ignored files in a checkout's folder (the install's own data, not in git), one per line. ignored_files_in() { git_in "$1" ls-files --others --ignored --exclude-standard -- "$2"; } @@ -169,11 +177,16 @@ adopt_run() { step "Installing and building $version (the old install keeps serving)" events_install_deps "$new" "$user" events_build "$new" "$user" - local expected + local expected newmig expected="$(events_expected_version "$new")" - if events_pending_migrations "$new" "$user"; then - die "$version has database migrations that aren't applied (or the database can't be read). adopt only moves the install; run it at a 2.2.x without migrations. Remove $new; nothing else changed." + # Migrations the release has and the old install doesn't (2.2.0 has one: AI tokens). They run + # in the switch-over, after the backup, while neither version is running. Anything else + # pending (or a database that can't be read) stops here, before anything changes. + newmig="$(migrations_only_in "$app/backend" "$old/backend")" + if events_pending_migrations "$new" "$user" && [ -z "$newmig" ]; then + die "The database has migrations pending that $version doesn't explain (or it can't be read). Remove $new; nothing else changed." fi + [ -z "$newmig" ] || info "Migrations to apply in the switch-over: $(echo "$newmig" | tr '\n' ' ')" # ── 6. Switch over: the only downtime ───────────────────────────────────────────────────── step "Switching over" @@ -186,6 +199,10 @@ adopt_run() { ( set -e run_as "$user" "$new" pm2 delete "$backend" "$frontend" + if [ -n "$newmig" ]; then + info "Migrations" + events_migrate "$new" "$user" + fi run_as "$user" "$app" pm2 start ecosystem.config.js run_as "$user" "$app" pm2 save wait_for_version "$(events_health_url)" "$expected" 60 @@ -208,6 +225,9 @@ adopt_run() { church_env_set EVENTS_OLD_DIR "$old$ADOPT_SUFFIX" church_env_set EVENTS_OLD_REF "$oldref" church_env_set EVENTS_ADOPTED_AT "$(date +%F)" + # The migrations adopt applied: adopt --undo may go back past these (they only add to the + # database, which the old release ignores), but not past any applied later. + [ -z "$newmig" ] || church_env_set EVENTS_ADOPT_MIGRATIONS "$(echo "$newmig" | tr '\n' ' ' | sed 's/ $//')" church_env_write "$(church_env_path "$church")" step "Adopted: $church runs $version from $new" info "The old folder is $old$ADOPT_SUFFIX, kept $ADOPT_KEEP_DAYS days for \`crosscode adopt $church --undo\`." @@ -246,11 +266,16 @@ adopt_undo() { [ ! -e "$orig" ] || die "$orig exists again; move it away first." step "Checks" - # The database must still suit 2.1.x: no migrations the old release doesn't have. - extra="$(comm -13 \ - <(find "$oldkept/backend/prisma/migrations" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort) \ - <(find "$app/backend/prisma/migrations" -mindepth 1 -maxdepth 1 -type d -printf '%f\n' | sort))" - [ -z "$extra" ] || die "$(checkout_ref "$new" events) has database migrations $oldref doesn't know ($(echo "$extra" | tr '\n' ' ')). Going back would break it; restore a backup instead." + # The database must still suit 2.1.x: no migrations the old release doesn't have, apart from + # the ones adopt itself applied (they only add to the database, which 2.1.x ignores). + local adopted m + adopted=" $(pvar events ADOPT_MIGRATIONS) " + extra="" + while IFS= read -r m; do + [ -n "$m" ] && [[ "$adopted" != *" $m "* ]] && extra+="$m " + done < <(migrations_only_in "$app/backend" "$oldkept/backend") + [ -z "$extra" ] || die "$(checkout_ref "$new" events) has database migrations $oldref doesn't know ($extra), applied after the move. Going back would break it; restore a backup instead." + [ "$adopted" = " " ] || info "The columns added by $(pvar events ADOPT_MIGRATIONS) stay in the database; $oldref ignores them." step "Database backup" events_backup "$new" "$user"