Bash · 1051 bytes Raw Blame History
1 #!/usr/bin/env bash
2 # SPDX-License-Identifier: AGPL-3.0-or-later
3 #
4 # Goose keys migrations by numeric version, not the rest of the filename.
5 # Duplicate NNNN prefixes panic at runtime before any migration runs, so
6 # catch them in CI before a deploy has to discover the conflict.
7
8 set -euo pipefail
9
10 cd "$(git rev-parse --show-toplevel)"
11
12 migrations=$(find internal/migrationsfs/migrations -maxdepth 1 -type f -name '[0-9][0-9][0-9][0-9]_*.sql' | sort)
13 versions=$(printf '%s\n' "$migrations" | sed -E 's#^.*/([0-9][0-9][0-9][0-9])_.*$#\1#')
14 duplicates=$(printf '%s\n' "$versions" | sort | uniq -d)
15
16 if [ -n "$duplicates" ]; then
17 echo "lint-migration-versions: duplicate migration versions found:" >&2
18 echo >&2
19 while IFS= read -r version; do
20 [ -z "$version" ] && continue
21 echo " version $version:" >&2
22 printf '%s\n' "$migrations" | grep "/${version}_" | sed 's/^/ /' >&2
23 done <<< "$duplicates"
24 echo >&2
25 echo "Goose requires each numeric NNNN prefix to be globally unique." >&2
26 exit 1
27 fi
28
29 echo "lint-migration-versions: ok"