A10: Fix Alembic head extraction in bootstrap script

Bug: 'alembic heads' returns '48094a7d1b92 (head)', but DB query returns
only '48094a7d1b92'. String comparison failed due to '(head)' suffix.

Fix:
- Extract revision ID using grep -oE '^[a-f0-9]+' before space
- Handle multiple heads: check if DB version matches any head
- Add validation for empty outputs with clear error messages
- Add success message showing head and DB version

Verification:
- Script syntax: bash -n passes
- Handles single head (exact match)
- Handles multiple heads (DB version matches any)
This commit is contained in:
ik
2026-09-10 17:57:46 +07:00
parent 5de8ea939a
commit 7386e7bea8
+32 -3
View File
@@ -26,9 +26,38 @@ curl -fsS "http://127.0.0.1:$BOOTSTRAP_API_PORT/ready" >/dev/null
curl -fsS "http://127.0.0.1:$BOOTSTRAP_WEB_PORT/" >/dev/null
curl -fsS -D - -o /dev/null "http://127.0.0.1:$BOOTSTRAP_API_PORT/health" | grep -qi '^x-frame-options: DENY'
curl -fsS -D - -o /dev/null "http://127.0.0.1:$BOOTSTRAP_API_PORT/health" | grep -qi '^cross-origin-opener-policy: same-origin'
# A10: Check actual Alembic head dynamically, not hardcoded revision
ALEMBIC_HEAD=$($compose exec -T api alembic heads 2>/dev/null | tail -1)
test "$($compose exec -T db psql -At -U rf4 -d rf4_spotter -c 'select version_num from alembic_version')" = "$ALEMBIC_HEAD"
# A10: Extract Alembic revision ID programmatically, handle multiple heads
ALEMBIC_HEADS_OUTPUT=$($compose exec -T api alembic heads 2>/dev/null || true)
# Extract revision IDs (first field before space or '(head)'), handle multiple heads
ALEMBIC_HEAD=$(echo "$ALEMBIC_HEADS_OUTPUT" | grep -oE '^[a-f0-9]+' | head -1)
DB_VERSION=$($compose exec -T db psql -At -U rf4 -d rf4_spotter -c 'select version_num from alembic_version')
if [ -z "$ALEMBIC_HEAD" ]; then
echo "ERROR: alembic heads returned empty or invalid output: $ALEMBIC_HEADS_OUTPUT" >&2
exit 1
fi
if [ -z "$DB_VERSION" ]; then
echo "ERROR: alembic_version table is empty" >&2
exit 1
fi
# Handle multiple heads: check if DB version matches any head
HEAD_COUNT=$(echo "$ALEMBIC_HEADS_OUTPUT" | grep -cE '^[a-f0-9]+' || true)
if [ "$HEAD_COUNT" -gt 1 ]; then
echo "WARNING: Multiple Alembic heads detected ($HEAD_COUNT), checking if DB version matches any..."
if ! echo "$ALEMBIC_HEADS_OUTPUT" | grep -q "^$DB_VERSION"; then
echo "ERROR: DB version $DB_VERSION does not match any head. Heads: $ALEMBIC_HEADS_OUTPUT" >&2
exit 1
fi
else
# Single head: exact match required
test "$DB_VERSION" = "$ALEMBIC_HEAD"
fi
echo "Alembic head: $ALEMBIC_HEAD, DB version: $DB_VERSION"
index_count=$($compose exec -T db psql -At -U rf4 -d rf4_spotter -c "select count(*) from pg_indexes where schemaname = 'public' and indexname in ('ix_catch_report_activity_lookup','ix_catch_report_spot_feed','ix_catch_report_moderation_queue','ix_catch_report_official_records','ix_official_import_source_status_started','ix_external_observation_review_queue','ix_submission_attempt_client_created','ix_moderation_event_created_at')")
test "$index_count" = "8"
test "$($compose exec -T db psql -At -U rf4 -d rf4_spotter -c 'select count(*) from fish')" = "2"