Skip to content

refactor(migrations): add commands to make file to run migrations for api v2 #5169

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
4 changes: 2 additions & 2 deletions .github/workflows/migration-check.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ jobs:
shell: bash
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
run: diesel migration run --locked-schema
run: make migrate locked-schema=yes

- name: Verify `diesel migration redo`
shell: bash
env:
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
run: diesel migration redo -a --locked-schema
run: make redo_migrate locked-schema=yes
2 changes: 1 addition & 1 deletion .github/workflows/postman-collection-runner.yml
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ jobs:
shell: bash
env:
DATABASE_URL: postgres://db_user:db_pass@localhost:5432/hyperswitch_db
run: diesel migration run
run: make migrate locked-schema=yes

- name: Install newman from fork
run: npm ci
Expand Down
2 changes: 1 addition & 1 deletion INSTALL_dependencies.sh
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ fi

# run migrations
print_info "Running migrations"
MIGRATION_CMD="diesel migration --database-url postgres://${DB_USER}:${DB_PASS}@localhost:5432/${DB_NAME} run"
MIGRATION_CMD="make migrate database-url=postgres://${DB_USER}:${DB_PASS}@localhost:5432/${DB_NAME}"

if [[ -d "migrations" ]]; then
$MIGRATION_CMD
Expand Down
34 changes: 34 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,37 @@ precommit : fmt clippy test

hack:
cargo hack check --workspace --each-feature --all-targets

# Run database migrations using `diesel_cli`.
# Assumes `diesel_cli` is already installed.
#
# Usage :
# make migrate [database-url=<PSQL connection string>] [locked-schema=<yes|no>]

# This proceeds as follows:
# Creates a temporary migrations directory, cleans it up if it already exists
# Copies all migrations to the temporary migrations directory and runs migrations
# Cleans up migrations, removing tmp directory if empty, ignoring otherwise
migrate:
mkdir -p $(ROOT_DIR)/tmp/migrations
find $(ROOT_DIR)/tmp/migrations/ -mindepth 1 -delete

cp -r $(ROOT_DIR)/migrations/. $(ROOT_DIR)/v2_migrations/. $(ROOT_DIR)/tmp/migrations/
diesel migration run --migration-dir=$(ROOT_DIR)/tmp/migrations \
$(if $(strip $(database-url)),--database-url="$(database-url)",) \
$(if $(strip $(call eq,$(locked-schema),yes)),--locked-schema,)

rm -r $(ROOT_DIR)/tmp/migrations
rmdir $(ROOT_DIR)/tmp 2>/dev/null || true

redo_migrate:
mkdir -p $(ROOT_DIR)/tmp/migrations
find $(ROOT_DIR)/tmp/migrations/ -mindepth 1 -delete

cp -r $(ROOT_DIR)/migrations/. $(ROOT_DIR)/v2_migrations/. $(ROOT_DIR)/tmp/migrations/
diesel migration redo --all --migration-dir=$(ROOT_DIR)/tmp/migrations \
$(if $(strip $(database-url)),--database-url="$(database-url)",) \
$(if $(strip $(call eq,$(locked-schema),yes)),--locked-schema,)

rm -r $(ROOT_DIR)/tmp/migrations
rmdir $(ROOT_DIR)/tmp 2>/dev/null || true
11 changes: 4 additions & 7 deletions docker-compose-development.yml
Original file line number Diff line number Diff line change
Expand Up @@ -47,18 +47,15 @@ services:

migration_runner:
image: rust:latest
command: "bash -c 'cargo install diesel_cli --no-default-features --features postgres && diesel migration --database-url postgres://$${DATABASE_USER}:$${DATABASE_PASSWORD}@$${DATABASE_HOST}:$${DATABASE_PORT}/$${DATABASE_NAME} run'"
command: "bash -c 'cargo install diesel_cli --no-default-features --features postgres && make migrate'"
working_dir: /app
networks:
- router_net
volumes:
- ./:/app
environment:
- DATABASE_USER=db_user
- DATABASE_PASSWORD=db_pass
- DATABASE_HOST=pg
- DATABASE_PORT=5432
- DATABASE_NAME=hyperswitch_db
# format -> postgresql://DB_USER:DB_PASSWORD@HOST:PORT/DATABASE_NAME
- DATABASE_URL=postgresql://db_user:db_pass@pg:5432/hyperswitch_db

### Application services
hyperswitch-server:
Expand Down Expand Up @@ -321,7 +318,7 @@ services:
ports:
- "9000:9000"
environment:
apiBaseUrl: http://localhost:8080
apiBaseUrl: http://localhost:8080
sdkBaseUrl: http://localhost:9050/HyperLoader.js

hyperswitch-web-sdk:
Expand Down
9 changes: 3 additions & 6 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,15 @@ services:

migration_runner:
image: rust:latest
command: "bash -c 'cargo install diesel_cli --no-default-features --features postgres && diesel migration --database-url postgres://$${DATABASE_USER}:$${DATABASE_PASSWORD}@$${DATABASE_HOST}:$${DATABASE_PORT}/$${DATABASE_NAME} run'"
command: "bash -c 'cargo install diesel_cli --no-default-features --features postgres && make migrate'"
working_dir: /app
networks:
- router_net
volumes:
- ./:/app
environment:
- DATABASE_USER=db_user
- DATABASE_PASSWORD=db_pass
- DATABASE_HOST=pg
- DATABASE_PORT=5432
- DATABASE_NAME=hyperswitch_db
# format -> postgresql://DB_USER:DB_PASSWORD@HOST:PORT/DATABASE_NAME
- DATABASE_URL=postgresql://db_user:db_pass@pg:5432/hyperswitch_db

### Application services
hyperswitch-server:
Expand Down
18 changes: 10 additions & 8 deletions docs/try_local_system.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ Check the Table Of Contents to jump to the relevant section.
### Running additional services

The default behaviour for docker compose only runs the following services:

1. postgres
2. redis (standalone)
3. hyperswitch server
Expand Down Expand Up @@ -102,10 +103,11 @@ involved, check out the [architecture document][architecture].

- To run the data services (Clickhouse, Kafka and Opensearch) you can specify the `olap` profile

```shell
docker compose --profile olap up -d
```
You can read more about using the data services [here][data-docs]
```shell
docker compose --profile olap up -d
```

You can read more about using the data services [here][data-docs]

- You can also specify multiple profile names by specifying the `--profile` flag
multiple times.
Expand Down Expand Up @@ -226,10 +228,10 @@ for your distribution and follow along.
cargo install diesel_cli --no-default-features --features postgres
```

5. Make sure your system has the `pkg-config` package and OpenSSL installed:
5. Make sure your system has the `pkg-config` package, OpenSSL and make installed:

```shell
sudo apt install pkg-config libssl-dev
sudo apt install pkg-config libssl-dev make
```

Once you're done with setting up the dependencies, proceed with
Expand Down Expand Up @@ -476,10 +478,10 @@ Once you're done with setting up the dependencies, proceed with
cd hyperswitch
```

3. Run database migrations using `diesel_cli`:
3. Run database migrations:

```shell
diesel migration --database-url postgres://$DB_USER:$DB_PASS@localhost:5432/$DB_NAME run
make migrate database-url=postgres://$DB_USER:$DB_PASS@localhost:5432/$DB_NAME
```

Once you're done with setting up the database, proceed with
Expand Down
Loading