forked from MergeFi/backend
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1784304511000-CreateIdempotencyKeys.ts
More file actions
57 lines (51 loc) · 2.09 KB
/
Copy path1784304511000-CreateIdempotencyKeys.ts
File metadata and controls
57 lines (51 loc) · 2.09 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { MigrationInterface, QueryRunner } from 'typeorm';
/**
* Creates the idempotency_keys table backing the Idempotency-Key header
* support added in #16 — see IdempotencyKey (entity), IdempotencyInterceptor,
* and IdempotencyCleanupService.
*/
export class CreateIdempotencyKeys1784304511000 implements MigrationInterface {
name = 'CreateIdempotencyKeys1784304511000';
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`
CREATE TYPE "idempotency_keys_status_enum" AS ENUM ('processing', 'completed')
`);
await queryRunner.query(`
CREATE TABLE "idempotency_keys" (
"id" uuid NOT NULL DEFAULT gen_random_uuid(),
"key" character varying NOT NULL,
"scope" character varying NOT NULL,
"callerId" character varying NOT NULL,
"status" "idempotency_keys_status_enum" NOT NULL DEFAULT 'processing',
"responseStatus" integer,
"responseBody" jsonb,
"createdAt" TIMESTAMP NOT NULL DEFAULT now(),
"updatedAt" TIMESTAMP NOT NULL DEFAULT now(),
"expiresAt" TIMESTAMPTZ NOT NULL,
CONSTRAINT "PK_idempotency_keys" PRIMARY KEY ("id")
)
`);
// The concurrency-safety primitive: see IdempotencyKey's doc comment.
await queryRunner.query(`
CREATE UNIQUE INDEX "IDX_idempotency_keys_key_scope_callerId"
ON "idempotency_keys" ("key", "scope", "callerId")
`);
// Supports IdempotencyCleanupService's periodic DELETE ... WHERE
// "expiresAt" < now() sweep without a sequential scan.
await queryRunner.query(`
CREATE INDEX "IDX_idempotency_keys_expiresAt" ON "idempotency_keys" ("expiresAt")
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(
`DROP INDEX IF EXISTS "IDX_idempotency_keys_expiresAt"`,
);
await queryRunner.query(
`DROP INDEX IF EXISTS "IDX_idempotency_keys_key_scope_callerId"`,
);
await queryRunner.query(`DROP TABLE IF EXISTS "idempotency_keys"`);
await queryRunner.query(
`DROP TYPE IF EXISTS "idempotency_keys_status_enum"`,
);
}
}