forked from KCEE0901/trustchain-escrow
-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy path20260528000000_webhooks.js
More file actions
64 lines (57 loc) · 2.13 KB
/
Copy path20260528000000_webhooks.js
File metadata and controls
64 lines (57 loc) · 2.13 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
/**
* Migration: Add webhook subscriptions and deliveries
* Version: 20260528000000_webhooks
*/
/**
* @param {import('@prisma/client').PrismaClient} prisma
*/
export async function up(prisma) {
await prisma.$executeRawUnsafe(`
CREATE TABLE IF NOT EXISTS webhook_subscriptions (
id TEXT PRIMARY KEY,
tenant_id TEXT NOT NULL,
url TEXT NOT NULL,
secret TEXT NOT NULL,
event_types TEXT[] NOT NULL DEFAULT '{}',
is_active BOOLEAN NOT NULL DEFAULT TRUE,
created_by TEXT,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT fk_webhook_subscription_tenant FOREIGN KEY (tenant_id) REFERENCES tenants(id) ON DELETE CASCADE
)
`);
await prisma.$executeRawUnsafe(`
CREATE INDEX IF NOT EXISTS webhook_subscriptions_tenant_id_idx ON webhook_subscriptions(tenant_id)
`);
await prisma.$executeRawUnsafe(`
CREATE INDEX IF NOT EXISTS webhook_subscriptions_created_by_idx ON webhook_subscriptions(created_by)
`);
await prisma.$executeRawUnsafe(`
CREATE TABLE IF NOT EXISTS webhook_deliveries (
id TEXT PRIMARY KEY,
subscription_id TEXT NOT NULL,
event_type TEXT NOT NULL,
payload JSONB NOT NULL,
status TEXT NOT NULL DEFAULT 'pending',
attempts INT NOT NULL DEFAULT 0,
response_code INT,
error_message TEXT,
last_attempt_at TIMESTAMPTZ,
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
CONSTRAINT fk_webhook_delivery_subscription FOREIGN KEY (subscription_id) REFERENCES webhook_subscriptions(id) ON DELETE CASCADE
)
`);
await prisma.$executeRawUnsafe(`
CREATE INDEX IF NOT EXISTS webhook_deliveries_subscription_id_idx ON webhook_deliveries(subscription_id)
`);
await prisma.$executeRawUnsafe(`
CREATE INDEX IF NOT EXISTS webhook_deliveries_event_type_idx ON webhook_deliveries(event_type)
`);
}
/**
* @param {import('@prisma/client').PrismaClient} prisma
*/
export async function down(prisma) {
await prisma.$executeRawUnsafe(`DROP TABLE IF EXISTS webhook_deliveries`);
await prisma.$executeRawUnsafe(`DROP TABLE IF EXISTS webhook_subscriptions`);
}