fix: harden usage accounting idempotency
This commit is contained in:
1 parent
22fe6bf659
commit
3de1530074
14 files changed
+708
-107
No files matched your search
+19
-6
@@ -19,10 +19,11 @@ export type PostgresTestDatabase = {
|
||||
pool: Pool;
|
||||
seedEditorialWithUsage(): Promise<DownloadSeed>;
|
||||
insertDownload(input: DownloadSeed & { idempotencyKey: string }): Promise<void>;
|
||||
applyRemainingMigrations(): Promise<void>;
|
||||
stop(): Promise<void>;
|
||||
};
|
||||
|
||||
export async function startPostgres(): Promise<PostgresTestDatabase> {
|
||||
export async function startPostgres({ through }: { through?: string } = {}): Promise<PostgresTestDatabase> {
|
||||
const container = await new PostgreSqlContainer("postgres:17-alpine")
|
||||
.withDatabase("portal")
|
||||
.withUsername("portal")
|
||||
@@ -30,7 +31,11 @@ export async function startPostgres(): Promise<PostgresTestDatabase> {
|
||||
.start();
|
||||
const pool = new Pool({ connectionString: container.getConnectionUri() });
|
||||
|
||||
await applyMigration(pool);
|
||||
const migrationFiles = await listMigrationFiles();
|
||||
const throughIndex = through ? migrationFiles.indexOf(through) : migrationFiles.length - 1;
|
||||
if (through && throughIndex < 0) throw new Error(`Unknown migration: ${through}`);
|
||||
let appliedMigrationCount = throughIndex + 1;
|
||||
await applyMigrationFiles(pool, migrationFiles.slice(0, appliedMigrationCount));
|
||||
|
||||
const db = drizzle({ client: pool, schema });
|
||||
|
||||
@@ -50,6 +55,10 @@ export async function startPostgres(): Promise<PostgresTestDatabase> {
|
||||
requestFingerprint: "0".repeat(64),
|
||||
});
|
||||
},
|
||||
async applyRemainingMigrations() {
|
||||
await applyMigrationFiles(pool, migrationFiles.slice(appliedMigrationCount));
|
||||
appliedMigrationCount = migrationFiles.length;
|
||||
},
|
||||
async stop() {
|
||||
await pool.end();
|
||||
await stopContainer(container);
|
||||
@@ -57,11 +66,15 @@ export async function startPostgres(): Promise<PostgresTestDatabase> {
|
||||
};
|
||||
}
|
||||
|
||||
async function applyMigration(pool: Pool) {
|
||||
async function listMigrationFiles() {
|
||||
const migrationDirectory = path.join(process.cwd(), "drizzle");
|
||||
const migrationFiles = (await readdir(migrationDirectory))
|
||||
return (await readdir(migrationDirectory))
|
||||
.filter((file) => /^\d+_.*\.sql$/.test(file))
|
||||
.sort();
|
||||
}
|
||||
|
||||
async function applyMigrationFiles(pool: Pool, migrationFiles: string[]) {
|
||||
const migrationDirectory = path.join(process.cwd(), "drizzle");
|
||||
for (const file of migrationFiles) {
|
||||
await pool.query(await readFile(path.join(migrationDirectory, file), "utf8"));
|
||||
}
|
||||
@@ -71,7 +84,7 @@ async function seedEditorialWithUsage(db: NodePgDatabase<typeof schema>): Promis
|
||||
const userId = randomUUID();
|
||||
const editorialId = randomUUID();
|
||||
const usageId = randomUUID();
|
||||
const assetId = "immich-asset-1";
|
||||
const assetId = "aaaaaaaa-aaaa-4aaa-8aaa-aaaaaaaaaaaa";
|
||||
|
||||
await db.insert(schema.users).values({
|
||||
id: userId,
|
||||
@@ -93,7 +106,7 @@ async function seedEditorialWithUsage(db: NodePgDatabase<typeof schema>): Promis
|
||||
editorialAccountId: editorialId,
|
||||
immichAssetId: assetId,
|
||||
filenameSnapshot: "bild.jpg",
|
||||
immichAlbumIdSnapshot: "immich-album-1",
|
||||
immichAlbumIdSnapshot: "bbbbbbbb-bbbb-4bbb-8bbb-bbbbbbbbbbbb",
|
||||
albumNameSnapshot: "Presse",
|
||||
purpose: "Artikel",
|
||||
priceCents: 1500,
|
||||
|
||||
Reference in new issue
Block a user