feat(db): support postgresql database (#3643)

Co-authored-by: Meier Lukas <meierschlumpf@gmail.com>
This commit is contained in:
Yuichi Nakai
2025-08-30 03:30:03 +09:00
committed by GitHub
parent a4aa2aea90
commit 5168cba8e4
22 changed files with 3603 additions and 32 deletions

View File

@@ -0,0 +1,46 @@
import path from "path";
import { PostgreSqlContainer } from "@testcontainers/postgresql";
import { drizzle } from "drizzle-orm/node-postgres";
import { migrate } from "drizzle-orm/node-postgres/migrator";
import { Pool } from "pg";
import { describe, test } from "vitest";
import * as pgSchema from "../schema/postgresql";
describe("PostgreSql Migration", () => {
test("should add all tables and keys specified in migration files", async () => {
const container = new PostgreSqlContainer("postgres:latest");
const postgreSqlContainer = await container.start();
const pool = new Pool({
user: postgreSqlContainer.getUsername(),
database: postgreSqlContainer.getDatabase(),
password: postgreSqlContainer.getPassword(),
port: postgreSqlContainer.getPort(),
host: postgreSqlContainer.getHost(),
keepAlive: true,
max: 0,
idleTimeoutMillis: 60000,
allowExitOnIdle: false,
});
const database = drizzle({
schema: pgSchema,
casing: "snake_case",
client: pool,
});
// Run migrations and check if it works
await migrate(database, {
migrationsFolder: path.join(__dirname, "..", "migrations", "postgresql"),
});
// Check if users table exists
await database.query.users.findMany();
// Close the pool to release resources
await pool.end();
// Stop the container
await postgreSqlContainer.stop();
}, 40_000);
});