Add a migrations check to CI

which can be skipped with SKIP_MIGRATION_CHECK=1
This commit is contained in:
Mike Dalessio
2025-10-21 09:15:46 -07:00
parent 083af17305
commit e7d59ff6d0
2 changed files with 84 additions and 0 deletions
+80
View File
@@ -0,0 +1,80 @@
#!/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
+4
View File
@@ -9,6 +9,10 @@ CI.run do
step "Security: Importmap audit", "bin/importmap audit"
step "Security: Brakeman audit", "bin/brakeman --quiet --no-pager --exit-on-warn --exit-on-error"
unless ENV["SKIP_MIGRATION_CHECK"]
step "Database: Check migrations", "bin/migration-audit"
end
step "Tests: Rails: SaaS config", "bin/rails test"
step "Tests: Rails: OSS config", "OSS_CONFIG=1 bin/rails test"
step "Tests: System", "bin/rails test:system"