Files
configcenter/scripts/check-migrations.sh

43 lines
1.4 KiB
Bash

#!/usr/bin/env sh
set -eu
migration_dir="internal/store/postgres/migrations"
previous=0
for path in "$migration_dir"/*.sql; do
name="$(basename "$path")"
case "$name" in
[0-9][0-9][0-9]_[a-z0-9_]*.sql) ;;
*)
printf 'invalid migration filename: %s\n' "$name" >&2
exit 1
;;
esac
number="${name%%_*}"
number="$(printf '%s' "$number" | sed 's/^0*//')"
[ -n "$number" ] || number=0
if [ "$number" -ne $((previous + 1)) ]; then
printf 'migration sequence must be contiguous: expected %03d, found %03d (%s)\n' "$((previous + 1))" "$number" "$name" >&2
exit 1
fi
previous="$number"
done
base_ref="${MIGRATION_BASE_REF:-}"
if [ -n "$base_ref" ] && git cat-file -e "$base_ref^{commit}" 2>/dev/null; then
changed="$(git diff --name-status "$base_ref"...HEAD -- "$migration_dir" || true)"
if [ -n "$changed" ]; then
printf '%s\n' "$changed" | while IFS="$(printf '\t')" read -r status path rest; do
case "$status" in
A) ;;
*)
printf 'existing migrations are immutable; only new migration files may be added: %s %s %s\n' "$status" "$path" "$rest" >&2
exit 1
;;
esac
done
fi
fi
printf 'migration policy check passed (%d migrations)\n' "$previous"