feat: add portal database schema
This commit is contained in:
1 parent
763098b129
commit
7acaacb92e
17 files changed
+4410
-111
No files matched your search
@@ -0,0 +1,97 @@
|
||||
import { randomUUID } from "node:crypto";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import path from "node:path";
|
||||
|
||||
import { PostgreSqlContainer, type StartedPostgreSqlContainer } from "@testcontainers/postgresql";
|
||||
import { drizzle, type NodePgDatabase } from "drizzle-orm/node-postgres";
|
||||
import { Pool } from "pg";
|
||||
|
||||
import * as schema from "@/db/schema";
|
||||
|
||||
type DownloadSeed = {
|
||||
editorialId: string;
|
||||
usageId: string;
|
||||
assetId: string;
|
||||
};
|
||||
|
||||
export type PostgresTestDatabase = {
|
||||
seedEditorialWithUsage(): Promise<DownloadSeed>;
|
||||
insertDownload(input: DownloadSeed & { idempotencyKey: string }): Promise<void>;
|
||||
stop(): Promise<void>;
|
||||
};
|
||||
|
||||
export async function startPostgres(): Promise<PostgresTestDatabase> {
|
||||
const container = await new PostgreSqlContainer("postgres:17-alpine")
|
||||
.withDatabase("portal")
|
||||
.withUsername("portal")
|
||||
.withPassword("portal")
|
||||
.start();
|
||||
const pool = new Pool({ connectionString: container.getConnectionUri() });
|
||||
|
||||
await applyMigration(pool);
|
||||
|
||||
const db = drizzle({ client: pool, schema });
|
||||
|
||||
return {
|
||||
async seedEditorialWithUsage() {
|
||||
return seedEditorialWithUsage(db);
|
||||
},
|
||||
async insertDownload(input) {
|
||||
await db.insert(schema.downloadEvents).values({
|
||||
editorialAccountId: input.editorialId,
|
||||
usageId: input.usageId,
|
||||
immichAssetId: input.assetId,
|
||||
requestId: randomUUID(),
|
||||
idempotencyKey: input.idempotencyKey,
|
||||
});
|
||||
},
|
||||
async stop() {
|
||||
await pool.end();
|
||||
await stopContainer(container);
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
async function applyMigration(pool: Pool) {
|
||||
const migration = await readFile(path.join(process.cwd(), "drizzle/0000_initial.sql"), "utf8");
|
||||
await pool.query(migration);
|
||||
}
|
||||
|
||||
async function seedEditorialWithUsage(db: NodePgDatabase<typeof schema>): Promise<DownloadSeed> {
|
||||
const userId = randomUUID();
|
||||
const editorialId = randomUUID();
|
||||
const usageId = randomUUID();
|
||||
const assetId = "immich-asset-1";
|
||||
|
||||
await db.insert(schema.users).values({
|
||||
id: userId,
|
||||
name: "Test Redaktion",
|
||||
email: `test-${userId}@example.com`,
|
||||
emailVerified: true,
|
||||
});
|
||||
await db.insert(schema.editorialAccounts).values({
|
||||
id: editorialId,
|
||||
userId,
|
||||
name: "Test Redaktion",
|
||||
contactName: "Testkontakt",
|
||||
loginEmail: `test-${userId}@example.com`,
|
||||
status: "active",
|
||||
currentPriceCents: 1500,
|
||||
});
|
||||
await db.insert(schema.usages).values({
|
||||
id: usageId,
|
||||
editorialAccountId: editorialId,
|
||||
immichAssetId: assetId,
|
||||
filenameSnapshot: "bild.jpg",
|
||||
immichAlbumIdSnapshot: "immich-album-1",
|
||||
albumNameSnapshot: "Presse",
|
||||
purpose: "Artikel",
|
||||
priceCents: 1500,
|
||||
});
|
||||
|
||||
return { editorialId, usageId, assetId };
|
||||
}
|
||||
|
||||
async function stopContainer(container: StartedPostgreSqlContainer) {
|
||||
await container.stop();
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { afterAll, expect, test } from "vitest";
|
||||
import { startPostgres } from "./postgres";
|
||||
|
||||
const testDb = await startPostgres();
|
||||
|
||||
afterAll(() => testDb.stop());
|
||||
|
||||
test("rejects duplicate download idempotency keys for one editorial", async () => {
|
||||
const seed = await testDb.seedEditorialWithUsage();
|
||||
|
||||
await testDb.insertDownload({ ...seed, idempotencyKey: "same-request" });
|
||||
|
||||
await expect(
|
||||
testDb.insertDownload({ ...seed, idempotencyKey: "same-request" }),
|
||||
).rejects.toThrow();
|
||||
});
|
||||
Reference in new issue
Block a user