Skip to content
This repository has been archived by the owner on Nov 13, 2024. It is now read-only.

Commit

Permalink
Enhance migration script to support specific and complete migrations/…
Browse files Browse the repository at this point in the history
…rollbacks
  • Loading branch information
Maxitosh committed May 8, 2024
1 parent 214d195 commit 7c0a897
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions scripts/migration.sh
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ if [[ $# -eq 0 ]]; then
exit 1
fi

UP_MIGRATION_NUMBER="" # Default to applying all migrations for up
DOWN_MIGRATION_NUMBER="" # No default for down, must be explicitly provided

while [[ $# -gt 0 ]]
do
key="$1"
Expand All @@ -21,10 +24,21 @@ case $key in
;;
-u|--up)
UP="true"
if [[ -n "$2" ]] && ! [[ "$2" =~ ^- ]]; then
UP_MIGRATION_NUMBER="$2"
shift
fi
shift # past argument
;;
-d|--down)
DOWN="true"
if [[ -n "$2" ]]; then
DOWN_MIGRATION_NUMBER="$2"
shift
else
echo "Error: Down migration requires a specific number of migrations or -all flag to revert all migrations."
exit 1
fi
shift # past argument
;;
*) # unknown option
Expand All @@ -42,19 +56,18 @@ else
exit 1
fi


# Check if migrating up, down, or creating a new migration
if [ "$UP" = "true" ]; then
# Migrate up
migrate -database "$DB_CONNECTION_URL" -path "db_migrations" up 1
# Migrate up to a number of steps or to the latest version
migrate -database "$DB_CONNECTION_URL" -path "db_migrations" up ${UP_MIGRATION_NUMBER}
elif [ "$DOWN" = "true" ]; then
# Migrate down
migrate -database "$DB_CONNECTION_URL" -path "db_migrations" down 1
# Migrate down to a number of steps or to the initial version
migrate -database "$DB_CONNECTION_URL" -path "db_migrations" down ${DOWN_MIGRATION_NUMBER}
else
# Create new migration
if [ -z "$NAME" ]; then
echo "Error: Missing required argument --name"
exit 1
fi
migrate -database "$DB_CONNECTION_URL" create -ext sql -seq -dir "db_migrations" "$NAME"
fi
fi

0 comments on commit 7c0a897

Please sign in to comment.