chore(Justfile): allow regenerating migrations by commit hash

This commit is contained in:
Moritz 2025-05-28 19:51:34 +02:00
parent 723d9c7205
commit 3e2140fda7
Signed by: moritz
GPG key ID: 1020A035E5DD0824

View file

@ -41,19 +41,33 @@ build-docker-container:
run-docker-container: build-docker-container run-docker-container: build-docker-container
docker run -e "SECRET_KEY_BASE=ahK8BeiDaibaige1ahkooS0chie9lo7the7uuzar0eeBeeCh2iereteshee2Oosu" -e='DATABASE_URL=postgres://postgres@localhost:5432/mv_dev' -e='PORT=4040' -e='PHX_HOST=localhost' --network=host mitgliederverwaltung docker run -e "SECRET_KEY_BASE=ahK8BeiDaibaige1ahkooS0chie9lo7the7uuzar0eeBeeCh2iereteshee2Oosu" -e='DATABASE_URL=postgres://postgres@localhost:5432/mv_dev' -e='PORT=4040' -e='PHX_HOST=localhost' --network=host mitgliederverwaltung
regen-migrations migration_name: # Usage:
#!/bin/bash # just regen-migrations migration_name [commit_hash]
# If commit_hash is given, rollback & delete the migrations from that commit.
# Otherwise, rollback & delete all untracked migrations.
regen-migrations migration_name commit_hash='':
#!/usr/bin/env bash
set -euo pipefail set -euo pipefail
# Get count of untracked migrations # Pick migrations either from the given commit or untracked files
N_MIGRATIONS=$(git ls-files --others priv/repo/migrations | wc -l) if [ -n "{{commit_hash}}" ]; then
# Rollback untracked migrations echo "→ Rolling back migrations from commit {{commit_hash}}"
mix ash_postgres.rollback -n $N_MIGRATIONS MIG_FILES=$(git show --name-only --pretty=format: "{{commit_hash}}" \
# Delete untracked migrations and snapshots | grep -E "^priv/repo/migrations/|^priv/resource_snapshots")
git ls-files --others priv/repo/migrations | xargs rm else
git ls-files --others priv/resource_snapshots | xargs rm echo "→ Rolling back all untracked migrations"
# Regenerate migrations MIG_FILES=$(git ls-files --others priv/repo/migrations)
mix ash.codegen --name {{migration_name}}
# Run migrations if flag
if echo $* | grep -e "-m" -q; then
mix ash.migrate
fi fi
# Roll back in Ash
COUNT=$(echo "$MIG_FILES" | wc -l)
mix ash_postgres.rollback -n "$COUNT"
# Remove the migration files
echo removing $MIG_FILES
echo "$MIG_FILES" | xargs rm -f
# Also clean up any untracked resource snapshots
git ls-files --others priv/resource_snapshots | xargs rm -f
# Generate a fresh migration
mix ash.codegen --name "{{migration_name}}"