fix: resolve unbound variable error in deploy script migration check

The APPLIED_MIGRATIONS associative array triggered "unbound variable"
under set -u when empty (first run / seed-existing). Fix by initializing
with =() and using a safe helper function with ${:-} default syntax.

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-04-09 09:15:54 -04:00
parent e06ca74d1d
commit 2aad137bd7

View File

@@ -248,12 +248,19 @@ CREATE TABLE IF NOT EXISTS shared.schema_migrations (
SQL SQL
ok "Migration tracking table ready" ok "Migration tracking table ready"
# Helper: check if a migration has been applied (safe with set -u)
is_applied() {
local key="$1"
# Use a subshell test to avoid unbound variable with set -u on empty associative arrays
[[ -n "${APPLIED_MIGRATIONS[$key]:-}" ]]
}
# Step 5b: Get list of already-applied migrations # Step 5b: Get list of already-applied migrations
declare -A APPLIED_MIGRATIONS declare -A APPLIED_MIGRATIONS=()
while IFS= read -r fname; do while IFS= read -r fname; do
fname=$(echo "$fname" | xargs) # trim whitespace fname=$(echo "$fname" | xargs) # trim whitespace
[ -n "$fname" ] && APPLIED_MIGRATIONS["$fname"]=1 [ -n "$fname" ] && APPLIED_MIGRATIONS["$fname"]=1
done < <(run_sql -t -c "SELECT filename FROM shared.schema_migrations ORDER BY filename;") done < <(run_sql -t -c "SELECT filename FROM shared.schema_migrations ORDER BY filename;" 2>/dev/null || true)
APPLIED_COUNT=${#APPLIED_MIGRATIONS[@]} APPLIED_COUNT=${#APPLIED_MIGRATIONS[@]}
log "Previously applied migrations: $APPLIED_COUNT" log "Previously applied migrations: $APPLIED_COUNT"
@@ -304,7 +311,7 @@ PENDING_COUNT=0
APPLIED_THIS_RUN=0 APPLIED_THIS_RUN=0
for filename in "${MIGRATION_FILES[@]}"; do for filename in "${MIGRATION_FILES[@]}"; do
if [ -n "${APPLIED_MIGRATIONS[$filename]+x}" ]; then if is_applied "$filename"; then
continue continue
fi fi
((PENDING_COUNT++)) ((PENDING_COUNT++))
@@ -317,7 +324,7 @@ else
echo "" echo ""
for filename in "${MIGRATION_FILES[@]}"; do for filename in "${MIGRATION_FILES[@]}"; do
if [ -n "${APPLIED_MIGRATIONS[$filename]+x}" ]; then if is_applied "$filename"; then
continue continue
fi fi