Files
fizzy/bin/migration-audit
T
Mike Dalessio e7d59ff6d0 Add a migrations check to CI
which can be skipped with SKIP_MIGRATION_CHECK=1
2025-10-21 09:27:56 -07:00

81 lines
2.0 KiB
Bash
Executable File

#!/usr/bin/env bash
set -eo pipefail
# Prefer app executables
app_root="$(cd "$(dirname "$0")/.."; pwd)"
export PATH="$app_root/bin:$PATH"
# Install gum if needed
if ! command -v gum &> /dev/null; then
echo
echo "▸ Installing gum"
if command -v pacman &> /dev/null; then
sudo pacman -S --noconfirm gum
elif command -v brew &> /dev/null; then
brew install gum
else
echo "Please install gum: https://github.com/charmbracelet/gum"
exit 1
fi
echo
fi
step() {
local spin=false
if [[ $1 == "--spin" ]] ; then
spin=true
spin_title=$2
shift
shift
fi
local step_name="$1"
shift
gum style --foreground 135 --bold "$step_name"
gum style --foreground 240 "$*"
if $spin; then
gum spin --spinner dot --title "$spin_title" --show-error -- "$@"
else
"$@"
fi
local exit_code=$?
echo
return $exit_code
}
echo
# Check if any migration or schema files have changed between origin/main and current working tree
changed_files=$(git diff --name-only origin/main...HEAD -- 'db/migrate/*' 'db/schema.rb' 'db/schema_cache.yml')
# If no relevant files changed, exit successfully
if [ -z "$changed_files" ]; then
gum style --foreground 240 "No migration or schema files changed. Skipping audit."
exit 0
fi
gum style --foreground 135 --bold "Migration or schema files changed:"
gum style --foreground 240 "$changed_files"
echo
step "Deleting schema files" rm -f db/schema.rb db/schema_cache.yml
step "Deleting tenant directory" rm -rf storage/tenants/development/175932900
step --spin "Migrating..." "Regenerating schema files" bin/rails db:migrate
# Check if the schema files have any git changes
if git diff --quiet db/schema.rb db/schema_cache.yml; then
gum style --foreground 46 "✓ Schema files are in sync with migrations"
exit 0
else
echo
gum style --foreground 196 --bold "✗ Schema files are out of sync with migrations!"
echo
gum style --foreground 135 --bold "Differences:"
git --no-pager diff db/schema.rb db/schema_cache.yml
exit 1
fi