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

Commit

Permalink
Merge branch 'main' into bug/41/applications-stake-change
Browse files Browse the repository at this point in the history
  • Loading branch information
nodiesBlade authored May 22, 2024
2 parents c8e16a4 + 93ffe84 commit b81dd88
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 8 deletions.
29 changes: 27 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,35 @@ This command will generate a up and down migration in `db_migrations`
DB Migrations are applied upon server start, but as well, it can be applied manually through:
```sh
./scripts/migration.sh {--down or --up} {number_of_times}
./scripts/migration.sh -d 1
./scripts/migration.sh -u 1
```

### Usage

#### Apply Migrations

- To apply all migrations:
```sh
./scripts/migration.sh --up
```

- To apply a specific number of migrations:
```sh
./scripts/migration.sh --up 2
```

#### Rollback Migrations
Make sure to provide either the number of migrations to rollback or the `--all` flag to rollback all migrations.

- To roll back a specific number of migrations:
```sh
./scripts/migration.sh --down 2
```

- To roll back all migrations:
```sh
./scripts/migration.sh --down --all
```

## Running Tests
Install Mockery with
```
Expand Down
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 b81dd88

Please sign in to comment.