Skip to content

Commit 13d1640

Browse files
refactor(migrations): add commands to make file to run migrations for api v2 (#5205)
Co-authored-by: Sanchith Hegde <[email protected]>
1 parent 1fb42b5 commit 13d1640

File tree

9 files changed

+55
-25
lines changed

9 files changed

+55
-25
lines changed

.github/workflows/migration-check.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,10 @@ jobs:
5757
shell: bash
5858
env:
5959
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
60-
run: diesel migration run --locked-schema
60+
run: make migrate locked-schema=yes
6161

6262
- name: Verify `diesel migration redo`
6363
shell: bash
6464
env:
6565
DATABASE_URL: postgres://postgres:postgres@localhost:5432/postgres
66-
run: diesel migration redo -a --locked-schema
66+
run: make redo_migrate locked-schema=yes

.github/workflows/postman-collection-runner.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ jobs:
107107
shell: bash
108108
env:
109109
DATABASE_URL: postgres://db_user:db_pass@localhost:5432/hyperswitch_db
110-
run: diesel migration run
110+
run: make migrate locked-schema=yes
111111

112112
- name: Install newman from fork
113113
run: npm ci

INSTALL_dependencies.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ fi
252252

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

257257
if [[ -d "migrations" ]]; then
258258
$MIGRATION_CMD

Makefile

+34
Original file line numberDiff line numberDiff line change
@@ -113,3 +113,37 @@ precommit : fmt clippy test
113113

114114
hack:
115115
cargo hack check --workspace --each-feature --all-targets
116+
117+
# Run database migrations using `diesel_cli`.
118+
# Assumes `diesel_cli` is already installed.
119+
#
120+
# Usage :
121+
# make migrate [database-url=<PSQL connection string>] [locked-schema=<yes|no>]
122+
123+
# This proceeds as follows:
124+
# Creates a temporary migrations directory, cleans it up if it already exists
125+
# Copies all migrations to the temporary migrations directory and runs migrations
126+
# Cleans up migrations, removing tmp directory if empty, ignoring otherwise
127+
migrate:
128+
mkdir -p $(ROOT_DIR)/tmp/migrations
129+
find $(ROOT_DIR)/tmp/migrations/ -mindepth 1 -delete
130+
131+
cp -r $(ROOT_DIR)/migrations/. $(ROOT_DIR)/v2_migrations/. $(ROOT_DIR)/tmp/migrations/
132+
diesel migration run --migration-dir=$(ROOT_DIR)/tmp/migrations \
133+
$(if $(strip $(database-url)),--database-url="$(database-url)",) \
134+
$(if $(strip $(call eq,$(locked-schema),yes)),--locked-schema,)
135+
136+
rm -r $(ROOT_DIR)/tmp/migrations
137+
rmdir $(ROOT_DIR)/tmp 2>/dev/null || true
138+
139+
redo_migrate:
140+
mkdir -p $(ROOT_DIR)/tmp/migrations
141+
find $(ROOT_DIR)/tmp/migrations/ -mindepth 1 -delete
142+
143+
cp -r $(ROOT_DIR)/migrations/. $(ROOT_DIR)/v2_migrations/. $(ROOT_DIR)/tmp/migrations/
144+
diesel migration redo --all --migration-dir=$(ROOT_DIR)/tmp/migrations \
145+
$(if $(strip $(database-url)),--database-url="$(database-url)",) \
146+
$(if $(strip $(call eq,$(locked-schema),yes)),--locked-schema,)
147+
148+
rm -r $(ROOT_DIR)/tmp/migrations
149+
rmdir $(ROOT_DIR)/tmp 2>/dev/null || true

docker-compose-development.yml

+4-7
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,15 @@ services:
4747

4848
migration_runner:
4949
image: rust:latest
50-
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'"
50+
command: "bash -c 'cargo install diesel_cli --no-default-features --features postgres && make migrate'"
5151
working_dir: /app
5252
networks:
5353
- router_net
5454
volumes:
5555
- ./:/app
5656
environment:
57-
- DATABASE_USER=db_user
58-
- DATABASE_PASSWORD=db_pass
59-
- DATABASE_HOST=pg
60-
- DATABASE_PORT=5432
61-
- DATABASE_NAME=hyperswitch_db
57+
# format -> postgresql://DB_USER:DB_PASSWORD@HOST:PORT/DATABASE_NAME
58+
- DATABASE_URL=postgresql://db_user:db_pass@pg:5432/hyperswitch_db
6259

6360
### Application services
6461
hyperswitch-server:
@@ -321,7 +318,7 @@ services:
321318
ports:
322319
- "9000:9000"
323320
environment:
324-
apiBaseUrl: http://localhost:8080
321+
apiBaseUrl: http://localhost:8080
325322
sdkBaseUrl: http://localhost:9050/HyperLoader.js
326323

327324
hyperswitch-web-sdk:

docker-compose.yml

+3-6
Original file line numberDiff line numberDiff line change
@@ -42,18 +42,15 @@ services:
4242

4343
migration_runner:
4444
image: rust:latest
45-
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'"
45+
command: "bash -c 'cargo install diesel_cli --no-default-features --features postgres && make migrate'"
4646
working_dir: /app
4747
networks:
4848
- router_net
4949
volumes:
5050
- ./:/app
5151
environment:
52-
- DATABASE_USER=db_user
53-
- DATABASE_PASSWORD=db_pass
54-
- DATABASE_HOST=pg
55-
- DATABASE_PORT=5432
56-
- DATABASE_NAME=hyperswitch_db
52+
# format -> postgresql://DB_USER:DB_PASSWORD@HOST:PORT/DATABASE_NAME
53+
- DATABASE_URL=postgresql://db_user:db_pass@pg:5432/hyperswitch_db
5754

5855
### Application services
5956
hyperswitch-server:

docs/try_local_system.md

+10-8
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ Check the Table Of Contents to jump to the relevant section.
7171
### Running additional services
7272

7373
The default behaviour for docker compose only runs the following services:
74+
7475
1. postgres
7576
2. redis (standalone)
7677
3. hyperswitch server
@@ -102,10 +103,11 @@ involved, check out the [architecture document][architecture].
102103

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

105-
```shell
106-
docker compose --profile olap up -d
107-
```
108-
You can read more about using the data services [here][data-docs]
106+
```shell
107+
docker compose --profile olap up -d
108+
```
109+
110+
You can read more about using the data services [here][data-docs]
109111

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

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

231233
```shell
232-
sudo apt install pkg-config libssl-dev
234+
sudo apt install pkg-config libssl-dev make
233235
```
234236

235237
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
476478
cd hyperswitch
477479
```
478480

479-
3. Run database migrations using `diesel_cli`:
481+
3. Run database migrations:
480482

481483
```shell
482-
diesel migration --database-url postgres://$DB_USER:$DB_PASS@localhost:5432/$DB_NAME run
484+
make migrate database-url=postgres://$DB_USER:$DB_PASS@localhost:5432/$DB_NAME
483485
```
484486

485487
Once you're done with setting up the database, proceed with

0 commit comments

Comments
 (0)