Skip to content

Open source, composable payments platform | PCI compliant | SaaS and Self-host options | Enables connectivity to multiple payment, payout, fraud, vault and tokenization providers | Uplifts authorization with intelligent routing and revenue recovery | Reduce payment processing costs with cost observability | Reduces payment ops with reconciliation

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

Narayan Bhatff23e2f7d3de
diff --git a/.github/workflows/migration-check.yaml b/.github/workflows/migration-check.yaml
index b740bd3a5b..baab2b7bc9 100644
--- a/.github/workflows/migration-check.yaml
+++ b/.github/workflows/migration-check.yaml
@@ -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
diff --git a/.github/workflows/postman-collection-runner.yml b/.github/workflows/postman-collection-runner.yml
index 57ca74fbc4..0c2220d6e6 100644
--- a/.github/workflows/postman-collection-runner.yml
+++ b/.github/workflows/postman-collection-runner.yml
@@ -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
diff --git a/INSTALL_dependencies.sh b/INSTALL_dependencies.sh
index 8f893f5445..cade0a4e02 100755
--- a/INSTALL_dependencies.sh
+++ b/INSTALL_dependencies.sh
@@ -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
diff --git a/Makefile b/Makefile
index 4d12fdafb5..760f2d3fc6 100644
--- a/Makefile
+++ b/Makefile
@@ -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
diff --git a/docker-compose-development.yml b/docker-compose-development.yml
index 868eeba243..5086a365f7 100644
--- a/docker-compose-development.yml
+++ b/docker-compose-development.yml
@@ -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:
@@ -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:
diff --git a/docker-compose.yml b/docker-compose.yml
index 569ef04506..3a6ff1e404 100644
--- a/docker-compose.yml
+++ b/docker-compose.yml
@@ -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:
diff --git a/docs/try_local_system.md b/docs/try_local_system.md
index d45049dfd6..12aaad8a58 100644
--- a/docs/try_local_system.md
+++ b/docs/try_local_system.md
@@ -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
@@ -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.
@@ -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
@@ -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
diff --git a/migrations/2024-06-03-133421_remove_id_from_payment_table/down.sql b/v2_migrations/2024-06-03-133421_remove_id_from_payment_table/down.sql
similarity index 100%
rename from migrations/2024-06-03-133421_remove_id_from_payment_table/down.sql
rename to v2_migrations/2024-06-03-133421_remove_id_from_payment_table/down.sql
diff --git a/migrations/2024-06-03-133421_remove_id_from_payment_table/up.sql b/v2_migrations/2024-06-03-133421_remove_id_from_payment_table/up.sql
similarity index 100%
rename from migrations/2024-06-03-133421_remove_id_from_payment_table/up.sql
rename to v2_migrations/2024-06-03-133421_remove_id_from_payment_table/up.sql