Skip to content

Commit 6df54c5

Browse files
committed
fix: local docker compose setup
Migrations are done automatically when starting the server and should not be applied in the local docker compose image itself Revert "fix: local docker compose setup" This reverts commit 96a82e3fe7b6fab9140788bef7eee9ded989f6ba. a
1 parent 9bdbcfc commit 6df54c5

File tree

4 files changed

+20
-27
lines changed

4 files changed

+20
-27
lines changed

docker/compose.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,3 @@ services:
1010
POSTGRES_PASSWORD: postgres
1111
volumes:
1212
- ./db/migrations/schema.sql:/docker-entrypoint-initdb.d/0000_schema.sql
13-
- ../db/migrations:/docker-entrypoint-initdb.d

src/server.ts

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import { Server, IncomingMessage, ServerResponse } from 'node:http'
33
import { runMigrations } from './utils/migrate'
44
import { createServer } from './app'
55
import { getConfig } from './utils/config'
6-
import { Client } from 'pg'
76
import { logger } from './logger'
87

98
const config = getConfig()
@@ -16,30 +15,10 @@ const main = async () => {
1615
requestIdHeader: 'Request-Id',
1716
})
1817

19-
// Init config
20-
const port = getConfig().PORT
21-
22-
// Init DB
23-
const dbConfig = {
24-
connectionString: config.DATABASE_URL,
25-
connectionTimeoutMillis: 10_000,
26-
}
27-
const client = new Client(dbConfig)
28-
29-
// Run migrations
30-
try {
31-
await client.connect()
32-
33-
// Ensure schema exists, not doing it via migration to not break current migration checksums
34-
await client.query(`CREATE SCHEMA IF NOT EXISTS ${config.SCHEMA};`)
35-
36-
await runMigrations(client)
37-
} finally {
38-
await client.end()
39-
}
18+
await runMigrations()
4019

4120
// Start the server
42-
app.listen({ port: Number(port), host: '0.0.0.0' }, (err, address) => {
21+
app.listen({ port: Number(config.PORT), host: '0.0.0.0' }, (err, address) => {
4322
if (err) {
4423
console.error(err)
4524
process.exit(1)

src/utils/migrate.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,26 @@ async function connectAndMigrate(client: Client, migrationsDirectory: string, lo
2727
}
2828
}
2929

30-
export async function runMigrations(client: Client): Promise<void> {
30+
export async function runMigrations(): Promise<void> {
31+
// Init DB
32+
const dbConfig = {
33+
connectionString: config.DATABASE_URL,
34+
connectionTimeoutMillis: 10_000,
35+
}
36+
const client = new Client(dbConfig)
37+
3138
try {
39+
// Run migrations
40+
await client.connect()
41+
42+
// Ensure schema exists, not doing it via migration to not break current migration checksums
43+
await client.query(`CREATE SCHEMA IF NOT EXISTS ${config.SCHEMA};`)
44+
3245
console.log('Running migrations')
46+
3347
await connectAndMigrate(client, './db/migrations')
34-
} catch (error) {
35-
throw error
3648
} finally {
49+
await client.end()
3750
console.log('Finished migrations')
3851
}
3952
}

test/webhooks.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { createServer } from '../src/app'
55
import { getConfig } from '../src/utils/config'
66
import stripeMock from './helpers/stripe'
77
import 'dotenv/config'
8+
import { runMigrations } from '../src/utils/migrate'
89

910
jest.doMock('stripe', () => {
1011
return jest.fn(() => stripeMock)
@@ -18,6 +19,7 @@ describe('/webhooks', () => {
1819

1920
beforeAll(async () => {
2021
server = await createServer()
22+
await runMigrations()
2123
})
2224

2325
afterAll(async () => {

0 commit comments

Comments
 (0)