Skip to content

Commit 45f6f17

Browse files
committed
Auto-create databases in migrations, remove setup.sql dependency
- feed-requests: Add ensureDatabase: true to MikroORM config - user-feeds-next: Add ensureDatabaseExists() to create DB before migrations - Remove setup.sql volume mount from docker-compose.base.yml This allows the released compose file to work standalone without requiring local SQL files.
1 parent 20b5d16 commit 45f6f17

3 files changed

Lines changed: 39 additions & 1 deletion

File tree

docker-compose.base.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,6 @@ services:
4848
image: postgres:17-alpine
4949
volumes:
5050
- feed-requests-postgres17-data:/var/lib/postgresql/data
51-
- ./services/feed-requests/sql/setup.sql:/docker-entrypoint-initdb.d/setup.sql
5251
networks:
5352
- monitorss-default
5453

services/feed-requests/src/mikro-orm.config.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ const MikroOrmConfig: Options = {
1414
forceUtcTimezone: true,
1515
timezone: 'UTC',
1616
dbName,
17+
ensureDatabase: true,
1718
};
1819

1920
export default MikroOrmConfig;

services/user-feeds-next/src/scripts/run-migrations.ts

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,54 @@
11
import { config } from "dotenv";
2+
import { Client } from "pg";
23
import { initPool, closePool, runMigrations } from "../stores/postgres";
34
import { logger } from "../shared/utils";
45

56
config();
67

78
const POSTGRES_URI = process.env.USER_FEEDS_POSTGRES_URI;
89

10+
function parseDbNameFromUri(uri: string): { baseUri: string; dbName: string } {
11+
const url = new URL(uri);
12+
const dbName = url.pathname.slice(1);
13+
url.pathname = "/postgres";
14+
return { baseUri: url.toString(), dbName };
15+
}
16+
17+
async function ensureDatabaseExists(uri: string): Promise<void> {
18+
const { baseUri, dbName } = parseDbNameFromUri(uri);
19+
20+
const client = new Client({ connectionString: baseUri });
21+
22+
try {
23+
await client.connect();
24+
25+
const result = await client.query(
26+
"SELECT 1 FROM pg_database WHERE datname = $1",
27+
[dbName]
28+
);
29+
30+
if (result.rows.length === 0) {
31+
logger.info(`Database "${dbName}" does not exist, creating...`);
32+
await client.query(`CREATE DATABASE "${dbName}"`);
33+
logger.info(`Database "${dbName}" created`);
34+
}
35+
} catch (err) {
36+
logger.error(`Failed to ensure database "${dbName}" exists`, {
37+
error: (err as Error).stack,
38+
});
39+
throw err;
40+
} finally {
41+
await client.end();
42+
}
43+
}
44+
945
async function main() {
1046
if (!POSTGRES_URI) {
1147
throw new Error("USER_FEEDS_POSTGRES_URI is required");
1248
}
1349

50+
await ensureDatabaseExists(POSTGRES_URI);
51+
1452
const pool = initPool(POSTGRES_URI);
1553
logger.info("Connected to PostgreSQL, running migrations...");
1654

0 commit comments

Comments
 (0)