forked from StellarSplit/StellarSplit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1769705000000-CreateBatchProcessingTables.ts
More file actions
106 lines (91 loc) · 4 KB
/
Copy path1769705000000-CreateBatchProcessingTables.ts
File metadata and controls
106 lines (91 loc) · 4 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
import { MigrationInterface, QueryRunner } from "typeorm";
export class CreateBatchProcessingTables1769705000000 implements MigrationInterface {
name = 'CreateBatchProcessingTables1769705000000'
public async up(queryRunner: QueryRunner): Promise<void> {
// Create enum types
await queryRunner.query(`
CREATE TYPE "batch_job_type_enum" AS ENUM ('split_creation', 'payment_processing', 'scheduled_task')
`);
await queryRunner.query(`
CREATE TYPE "batch_job_status_enum" AS ENUM ('pending', 'processing', 'completed', 'failed', 'cancelled', 'partial')
`);
await queryRunner.query(`
CREATE TYPE "batch_operation_status_enum" AS ENUM ('pending', 'processing', 'completed', 'failed', 'retrying', 'cancelled')
`);
// Create batch_jobs table
await queryRunner.query(`
CREATE TABLE "batch_jobs" (
"id" uuid NOT NULL DEFAULT uuid_generate_v4(),
"type" "batch_job_type_enum" NOT NULL,
"status" "batch_job_status_enum" NOT NULL DEFAULT 'pending',
"total_operations" integer NOT NULL DEFAULT 0,
"completed_operations" integer NOT NULL DEFAULT 0,
"failed_operations" integer NOT NULL DEFAULT 0,
"progress" decimal(5,2) DEFAULT 0,
"options" jsonb DEFAULT '{}',
"error_message" text,
"started_at" timestamp,
"completed_at" timestamp,
"created_at" timestamp NOT NULL DEFAULT now(),
"updated_at" timestamp NOT NULL DEFAULT now(),
CONSTRAINT "PK_batch_jobs" PRIMARY KEY ("id")
)
`);
// Create indexes for batch_jobs
await queryRunner.query(`
CREATE INDEX "IDX_batch_jobs_status" ON "batch_jobs" ("status")
`);
await queryRunner.query(`
CREATE INDEX "IDX_batch_jobs_type" ON "batch_jobs" ("type")
`);
await queryRunner.query(`
CREATE INDEX "IDX_batch_jobs_created_at" ON "batch_jobs" ("created_at")
`);
// Create batch_operations table
await queryRunner.query(`
CREATE TABLE "batch_operations" (
"id" uuid NOT NULL DEFAULT uuid_generate_v4(),
"batch_id" uuid NOT NULL,
"operation_index" integer NOT NULL,
"status" "batch_operation_status_enum" NOT NULL DEFAULT 'pending',
"payload" jsonb NOT NULL,
"result" jsonb,
"error_message" text,
"error_code" varchar(50),
"retry_count" integer DEFAULT 0,
"started_at" timestamp,
"completed_at" timestamp,
"created_at" timestamp NOT NULL DEFAULT now(),
"updated_at" timestamp NOT NULL DEFAULT now(),
CONSTRAINT "PK_batch_operations" PRIMARY KEY ("id"),
CONSTRAINT "FK_batch_operations_batch" FOREIGN KEY ("batch_id") REFERENCES "batch_jobs"("id") ON DELETE CASCADE
)
`);
// Create indexes for batch_operations
await queryRunner.query(`
CREATE INDEX "IDX_batch_operations_batch_id" ON "batch_operations" ("batch_id")
`);
await queryRunner.query(`
CREATE INDEX "IDX_batch_operations_status" ON "batch_operations" ("status")
`);
await queryRunner.query(`
CREATE INDEX "IDX_batch_operations_operation_index" ON "batch_operations" ("operation_index")
`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
// Drop indexes
await queryRunner.query(`DROP INDEX "IDX_batch_operations_operation_index"`);
await queryRunner.query(`DROP INDEX "IDX_batch_operations_status"`);
await queryRunner.query(`DROP INDEX "IDX_batch_operations_batch_id"`);
await queryRunner.query(`DROP INDEX "IDX_batch_jobs_created_at"`);
await queryRunner.query(`DROP INDEX "IDX_batch_jobs_type"`);
await queryRunner.query(`DROP INDEX "IDX_batch_jobs_status"`);
// Drop tables
await queryRunner.query(`DROP TABLE "batch_operations"`);
await queryRunner.query(`DROP TABLE "batch_jobs"`);
// Drop enum types
await queryRunner.query(`DROP TYPE "batch_operation_status_enum"`);
await queryRunner.query(`DROP TYPE "batch_job_status_enum"`);
await queryRunner.query(`DROP TYPE "batch_job_type_enum"`);
}
}