@voyant-travel/apps 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (64) hide show
  1. package/dist/access-boundary.d.ts +29 -0
  2. package/dist/access-boundary.js +33 -0
  3. package/dist/api-runtime.d.ts +10 -0
  4. package/dist/api-runtime.js +8 -0
  5. package/dist/app-api-contracts.d.ts +51 -0
  6. package/dist/app-api-contracts.js +50 -0
  7. package/dist/app-api-routes.d.ts +26 -0
  8. package/dist/app-api-routes.js +133 -0
  9. package/dist/app-api-service.d.ts +1263 -0
  10. package/dist/app-api-service.js +231 -0
  11. package/dist/app-api-service.test.d.ts +1 -0
  12. package/dist/app-api-service.test.js +105 -0
  13. package/dist/compiler.d.ts +38 -0
  14. package/dist/compiler.js +118 -0
  15. package/dist/compiler.test.d.ts +1 -0
  16. package/dist/compiler.test.js +99 -0
  17. package/dist/consent.d.ts +15 -0
  18. package/dist/consent.js +50 -0
  19. package/dist/consent.test.d.ts +1 -0
  20. package/dist/consent.test.js +80 -0
  21. package/dist/contracts.d.ts +263 -0
  22. package/dist/contracts.js +273 -0
  23. package/dist/index.d.ts +13 -0
  24. package/dist/index.js +12 -0
  25. package/dist/ingestion.d.ts +15 -0
  26. package/dist/ingestion.js +232 -0
  27. package/dist/ingestion.test.d.ts +1 -0
  28. package/dist/ingestion.test.js +47 -0
  29. package/dist/installation-reconciliation.d.ts +18 -0
  30. package/dist/installation-reconciliation.js +254 -0
  31. package/dist/installation-service.d.ts +351 -0
  32. package/dist/installation-service.js +328 -0
  33. package/dist/installation-state.d.ts +15 -0
  34. package/dist/installation-state.js +17 -0
  35. package/dist/installation-state.test.d.ts +1 -0
  36. package/dist/installation-state.test.js +38 -0
  37. package/dist/oauth-crypto.d.ts +8 -0
  38. package/dist/oauth-crypto.js +23 -0
  39. package/dist/oauth-crypto.test.d.ts +1 -0
  40. package/dist/oauth-crypto.test.js +11 -0
  41. package/dist/oauth-service.d.ts +65 -0
  42. package/dist/oauth-service.js +381 -0
  43. package/dist/oauth-service.test.d.ts +1 -0
  44. package/dist/oauth-service.test.js +8 -0
  45. package/dist/routes.d.ts +17 -0
  46. package/dist/routes.js +131 -0
  47. package/dist/schema.d.ts +2937 -0
  48. package/dist/schema.js +342 -0
  49. package/dist/service.d.ts +95 -0
  50. package/dist/service.js +172 -0
  51. package/dist/service.test.d.ts +1 -0
  52. package/dist/service.test.js +178 -0
  53. package/dist/test-fixtures.d.ts +80 -0
  54. package/dist/test-fixtures.js +78 -0
  55. package/dist/voyant.d.ts +2 -0
  56. package/dist/voyant.js +115 -0
  57. package/dist/webhook-delivery.d.ts +69 -0
  58. package/dist/webhook-delivery.js +262 -0
  59. package/migrations/20260717000100_app_registry_foundation.sql +87 -0
  60. package/migrations/20260717111938_breezy_karma.sql +136 -0
  61. package/migrations/20260717123000_app_webhook_delivery_health.sql +4 -0
  62. package/migrations/20260717143000_app_oauth_authorization.sql +47 -0
  63. package/migrations/meta/_journal.json +34 -0
  64. package/package.json +159 -0
@@ -0,0 +1,262 @@
1
+ import { isExternalWebhookPayloadSchema } from "@voyant-travel/core/project";
2
+ import { newId } from "@voyant-travel/db/lib/typeid";
3
+ import { infraWebhookDeliveriesTable, infraWebhookDeliverySelectSchema, } from "@voyant-travel/db/schema/infra";
4
+ import { createAppWebhookDeliveryEnvelope, createSelectedExternalWebhookQueue, hashWebhookPayload, isAppWebhookDeliveryEnvelope, webhookBodyExcerpt, } from "@voyant-travel/webhook-delivery";
5
+ import { and, asc, eq, isNull, lte, or, sql } from "drizzle-orm";
6
+ import { audit } from "./installation-reconciliation.js";
7
+ import { appInstallations, appWebhookSubscriptions } from "./schema.js";
8
+ export function createAppWebhookDeliveryStore(db, options) {
9
+ const threshold = options.terminalFailureThreshold ?? 5;
10
+ const now = options.now ?? (() => new Date());
11
+ return {
12
+ async listActiveSubscriptions(eventName) {
13
+ const rows = await selectSubscriptionRows(db, eventName);
14
+ return Promise.all(rows.map((row) => toWebhookSubscription(row, options.resolveSigningKey)));
15
+ },
16
+ async getSubscription(id) {
17
+ const rows = await selectSubscriptionRows(db, undefined, id);
18
+ const row = rows[0];
19
+ return row ? toWebhookSubscription(row, options.resolveSigningKey) : null;
20
+ },
21
+ async enqueueAttempt(input) {
22
+ const existing = await db
23
+ .select()
24
+ .from(infraWebhookDeliveriesTable)
25
+ .where(and(eq(infraWebhookDeliveriesTable.idempotencyKey, input.idempotencyKey), eq(infraWebhookDeliveriesTable.attemptNumber, input.attemptNumber)))
26
+ .limit(1);
27
+ if (existing[0]) {
28
+ return { attempt: infraWebhookDeliverySelectSchema.parse(existing[0]), created: false };
29
+ }
30
+ const [row] = await db.insert(infraWebhookDeliveriesTable).values(pending(input)).returning();
31
+ if (!row)
32
+ throw new Error("App webhook attempt insert returned no row");
33
+ return { attempt: infraWebhookDeliverySelectSchema.parse(row), created: true };
34
+ },
35
+ async listReadyAttemptIds(at, staleBefore, limit) {
36
+ const rows = await db
37
+ .select({ id: infraWebhookDeliveriesTable.id })
38
+ .from(infraWebhookDeliveriesTable)
39
+ .where(and(eq(infraWebhookDeliveriesTable.sourceModule, "apps"), or(and(eq(infraWebhookDeliveriesTable.status, "pending"), or(isNull(infraWebhookDeliveriesTable.scheduledFor), lte(infraWebhookDeliveriesTable.scheduledFor, at))), and(eq(infraWebhookDeliveriesTable.status, "in_flight"), lte(infraWebhookDeliveriesTable.startedAt, staleBefore)))))
40
+ .orderBy(asc(infraWebhookDeliveriesTable.scheduledFor), asc(infraWebhookDeliveriesTable.id))
41
+ .limit(limit);
42
+ return rows.map(({ id }) => id);
43
+ },
44
+ async claimAttempt(id, at, staleBefore) {
45
+ const rows = await db
46
+ .update(infraWebhookDeliveriesTable)
47
+ .set({ status: "in_flight", startedAt: at, updatedAt: at })
48
+ .where(and(eq(infraWebhookDeliveriesTable.id, id), eq(infraWebhookDeliveriesTable.sourceModule, "apps"), or(eq(infraWebhookDeliveriesTable.status, "pending"), and(eq(infraWebhookDeliveriesTable.status, "in_flight"), lte(infraWebhookDeliveriesTable.startedAt, staleBefore)))))
49
+ .returning();
50
+ return rows[0] ? infraWebhookDeliverySelectSchema.parse(rows[0]) : null;
51
+ },
52
+ async completeAttempt(input) {
53
+ const [row] = await db
54
+ .update(infraWebhookDeliveriesTable)
55
+ .set({ ...input, updatedAt: input.finishedAt })
56
+ .where(eq(infraWebhookDeliveriesTable.id, input.id))
57
+ .returning();
58
+ if (!row)
59
+ throw new Error(`Webhook attempt ${input.id} was not in flight`);
60
+ return infraWebhookDeliverySelectSchema.parse(row);
61
+ },
62
+ async completeAndEnqueueRetry(completion, retry) {
63
+ return db.transaction(async (tx) => {
64
+ const completed = await createAppWebhookDeliveryStore(tx, options).completeAttempt(completion);
65
+ const enqueued = await createAppWebhookDeliveryStore(tx, options).enqueueAttempt(retry);
66
+ return { completed, retry: enqueued.attempt };
67
+ });
68
+ },
69
+ async recordSubscriptionOutcome(subscriptionId, succeeded, at) {
70
+ const patch = succeeded
71
+ ? { lastDeliveryAt: at, failureCount: 0, signingKeyId: null, updatedAt: at }
72
+ : {
73
+ lastDeliveryAt: at,
74
+ // agent-quality: raw-sql reviewed -- owner: apps; Drizzle supplies the column identifier and no caller-controlled SQL is interpolated.
75
+ failureCount: sql `${appWebhookSubscriptions.failureCount} + 1`,
76
+ updatedAt: at,
77
+ };
78
+ const [subscription] = await db
79
+ .update(appWebhookSubscriptions)
80
+ .set(patch)
81
+ .where(eq(appWebhookSubscriptions.id, subscriptionId))
82
+ .returning();
83
+ if (!succeeded && subscription && subscription.failureCount >= threshold) {
84
+ const [installation] = await db
85
+ .update(appInstallations)
86
+ .set({ status: "degraded", degradedAt: now(), updatedAt: now() })
87
+ .where(and(eq(appInstallations.id, subscription.installationId), eq(appInstallations.status, "active")))
88
+ .returning();
89
+ await db
90
+ .update(appWebhookSubscriptions)
91
+ .set({ status: "failed", pausedAt: now(), deactivatedAt: now() })
92
+ .where(eq(appWebhookSubscriptions.id, subscriptionId));
93
+ if (installation) {
94
+ await audit(db, installation, "system", "reconciliation", "webhooks.degraded", {
95
+ subscriptionId,
96
+ failureCount: subscription.failureCount,
97
+ });
98
+ }
99
+ }
100
+ },
101
+ };
102
+ }
103
+ export function createAppWebhookEventQueue(db, options) {
104
+ return createSelectedExternalWebhookQueue({
105
+ contracts: options.contracts,
106
+ store: createAppWebhookDeliveryStore(db, options),
107
+ now: options.now,
108
+ });
109
+ }
110
+ export async function enqueueAppWebhookEvent(db, event, options) {
111
+ return createAppWebhookEventQueue(db, options).enqueue(event);
112
+ }
113
+ export async function listAppWebhookHealth(db, installationId) {
114
+ const subscriptions = await db
115
+ .select()
116
+ .from(appWebhookSubscriptions)
117
+ .where(eq(appWebhookSubscriptions.installationId, installationId))
118
+ .orderBy(appWebhookSubscriptions.eventType, appWebhookSubscriptions.eventVersion);
119
+ return { data: subscriptions };
120
+ }
121
+ export async function replayAppWebhookDelivery(db, input) {
122
+ return db.transaction(async (tx) => {
123
+ const [original] = await tx
124
+ .select()
125
+ .from(infraWebhookDeliveriesTable)
126
+ .where(eq(infraWebhookDeliveriesTable.id, input.deliveryId))
127
+ .limit(1);
128
+ if (!original || !isAppWebhookDeliveryEnvelope(original.requestPayload)) {
129
+ throw new Error("Replay requires a retained app webhook delivery.");
130
+ }
131
+ const [installation] = await tx
132
+ .select()
133
+ .from(appInstallations)
134
+ .where(eq(appInstallations.id, original.requestPayload.installationId))
135
+ .limit(1);
136
+ if (installation?.status !== "active") {
137
+ throw new Error("Replay requires an active app installation.");
138
+ }
139
+ const parsedOriginal = infraWebhookDeliverySelectSchema.parse(original);
140
+ const replayed = replayInput(parsedOriginal, original.requestPayload);
141
+ const store = createAppWebhookDeliveryStore(tx, {
142
+ resolveSigningKey: async () => input.signingKey,
143
+ });
144
+ const enqueued = await store.enqueueAttempt(replayed);
145
+ await audit(tx, installation, input.actorId, "reconciliation", "webhooks.replay", {
146
+ originalDeliveryId: original.id,
147
+ replayDeliveryId: enqueued.attempt.id,
148
+ });
149
+ return enqueued.attempt;
150
+ });
151
+ }
152
+ async function selectSubscriptionRows(db, eventName, subscriptionId) {
153
+ return db
154
+ .select({
155
+ id: appWebhookSubscriptions.id,
156
+ installationId: appWebhookSubscriptions.installationId,
157
+ appId: appInstallations.appId,
158
+ eventVersion: appWebhookSubscriptions.eventVersion,
159
+ endpointUrl: appWebhookSubscriptions.endpointUrl,
160
+ })
161
+ .from(appWebhookSubscriptions)
162
+ .innerJoin(appInstallations, eq(appInstallations.id, appWebhookSubscriptions.installationId))
163
+ .where(and(eventName ? eq(appWebhookSubscriptions.eventType, eventName) : undefined, subscriptionId ? eq(appWebhookSubscriptions.id, subscriptionId) : undefined, eq(appWebhookSubscriptions.status, "active"), eq(appInstallations.status, "active")));
164
+ }
165
+ async function toWebhookSubscription(row, resolveSigningKey) {
166
+ const key = await resolveSigningKey({ appId: row.appId, installationId: row.installationId });
167
+ return {
168
+ id: row.id,
169
+ url: row.endpointUrl,
170
+ secret: key.secret,
171
+ keyId: key.id,
172
+ headers: null,
173
+ maxRetries: 5,
174
+ active: true,
175
+ app: {
176
+ installationId: row.installationId,
177
+ appId: row.appId,
178
+ eventVersion: row.eventVersion,
179
+ },
180
+ };
181
+ }
182
+ function pending(input) {
183
+ return {
184
+ id: input.id ?? newId("webhook_deliveries"),
185
+ sourceModule: input.sourceModule,
186
+ sourceEvent: input.sourceEvent,
187
+ sourceEntityModule: input.sourceEntityModule,
188
+ sourceEntityId: input.sourceEntityId,
189
+ subscriptionId: input.subscriptionId,
190
+ targetUrl: input.targetUrl,
191
+ targetKind: "app",
192
+ targetRef: input.subscriptionId,
193
+ requestMethod: input.requestMethod,
194
+ requestHeaders: input.requestHeaders,
195
+ requestBodyHash: input.requestBodyHash,
196
+ requestBodyExcerpt: input.requestBodyExcerpt,
197
+ requestPayload: objectRecord(input.requestPayload),
198
+ deliveryContract: objectRecord(input.deliveryContract),
199
+ attemptNumber: input.attemptNumber,
200
+ parentDeliveryId: input.parentDeliveryId,
201
+ idempotencyKey: input.idempotencyKey,
202
+ status: "pending",
203
+ scheduledFor: input.scheduledFor,
204
+ startedAt: null,
205
+ };
206
+ }
207
+ function replayInput(original, envelope) {
208
+ const contract = parseDeliveryContract(original.deliveryContract);
209
+ const deliveryId = newId("webhook_deliveries");
210
+ const replay = createAppWebhookDeliveryEnvelope({
211
+ deliveryId,
212
+ installationId: envelope.installationId,
213
+ appId: envelope.appId,
214
+ event: {
215
+ name: envelope.event.type,
216
+ data: envelope.payload,
217
+ emittedAt: envelope.event.occurredAt,
218
+ metadata: envelope.metadata,
219
+ },
220
+ contract,
221
+ deliveredAt: new Date(),
222
+ attemptNumber: 1,
223
+ maxRetries: envelope.attempt.maxRetries,
224
+ idempotencyKey: `app-webhook-replay:${original.id}:${deliveryId}`,
225
+ originalDeliveryId: original.id,
226
+ });
227
+ const body = JSON.stringify(replay);
228
+ return {
229
+ id: deliveryId,
230
+ sourceModule: "apps",
231
+ sourceEvent: envelope.event.type,
232
+ sourceEntityModule: envelope.subject.module,
233
+ sourceEntityId: envelope.subject.id,
234
+ subscriptionId: original.subscriptionId ?? "",
235
+ targetUrl: original.targetUrl,
236
+ requestMethod: "POST",
237
+ requestHeaders: original.requestHeaders ?? {},
238
+ requestBodyHash: hashWebhookPayload(body),
239
+ requestBodyExcerpt: webhookBodyExcerpt(body),
240
+ requestPayload: replay,
241
+ deliveryContract: contract,
242
+ attemptNumber: 1,
243
+ parentDeliveryId: original.id,
244
+ idempotencyKey: replay.attempt.idempotencyKey,
245
+ scheduledFor: new Date(),
246
+ };
247
+ }
248
+ function objectRecord(value) {
249
+ return Object.fromEntries(Object.entries(value));
250
+ }
251
+ function parseDeliveryContract(value) {
252
+ if (value &&
253
+ typeof value === "object" &&
254
+ !Array.isArray(value) &&
255
+ typeof value.eventId === "string" &&
256
+ typeof value.eventType === "string" &&
257
+ typeof value.eventVersion === "string" &&
258
+ isExternalWebhookPayloadSchema(value.payloadSchema)) {
259
+ return value;
260
+ }
261
+ throw new Error("Replay requires a delivery contract snapshot.");
262
+ }
@@ -0,0 +1,87 @@
1
+ CREATE TYPE "public"."app_distribution" AS ENUM('custom', 'marketplace');--> statement-breakpoint
2
+ CREATE TYPE "public"."app_lifecycle_state" AS ENUM('active', 'suspended', 'deleted');--> statement-breakpoint
3
+ CREATE TYPE "public"."app_credential_kind" AS ENUM('client_secret', 'signing_key');--> statement-breakpoint
4
+ CREATE TYPE "public"."app_release_state" AS ENUM('available', 'suspended', 'yanked');--> statement-breakpoint
5
+ CREATE TYPE "public"."app_release_artifact_state" AS ENUM('available', 'unavailable');--> statement-breakpoint
6
+ CREATE TABLE "apps" (
7
+ "id" text PRIMARY KEY NOT NULL,
8
+ "platform_namespace" text NOT NULL,
9
+ "distribution" "app_distribution" NOT NULL,
10
+ "owner_id" text NOT NULL,
11
+ "display_name" text NOT NULL,
12
+ "slug" text NOT NULL,
13
+ "lifecycle_state" "app_lifecycle_state" DEFAULT 'active' NOT NULL,
14
+ "created_by" text NOT NULL,
15
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
16
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
17
+ CONSTRAINT "apps_platform_namespace_reserved" CHECK ("apps"."platform_namespace" LIKE 'app--%')
18
+ );--> statement-breakpoint
19
+ CREATE TABLE "app_redirect_uris" (
20
+ "id" text PRIMARY KEY NOT NULL,
21
+ "app_id" text NOT NULL,
22
+ "redirect_uri" text NOT NULL,
23
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
24
+ );--> statement-breakpoint
25
+ CREATE TABLE "app_credentials" (
26
+ "id" text PRIMARY KEY NOT NULL,
27
+ "app_id" text NOT NULL,
28
+ "kind" "app_credential_kind" NOT NULL,
29
+ "generation" integer NOT NULL,
30
+ "kms_key_ref" text NOT NULL,
31
+ "public_key_ref" text,
32
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
33
+ "retired_at" timestamp with time zone
34
+ );--> statement-breakpoint
35
+ CREATE TABLE "app_releases" (
36
+ "id" text PRIMARY KEY NOT NULL,
37
+ "app_id" text NOT NULL,
38
+ "release_version" text NOT NULL,
39
+ "manifest_schema_version" text NOT NULL,
40
+ "manifest_digest" text NOT NULL,
41
+ "manifest_snapshot" jsonb NOT NULL,
42
+ "normalized_record" jsonb NOT NULL,
43
+ "api_compatibility" jsonb NOT NULL,
44
+ "default_locale" text NOT NULL,
45
+ "supported_locales" jsonb NOT NULL,
46
+ "state" "app_release_state" DEFAULT 'available' NOT NULL,
47
+ "created_by" text NOT NULL,
48
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
49
+ );--> statement-breakpoint
50
+ CREATE TABLE "app_release_artifacts" (
51
+ "id" text PRIMARY KEY NOT NULL,
52
+ "release_id" text NOT NULL,
53
+ "digest" text NOT NULL,
54
+ "signature" text,
55
+ "provenance" jsonb NOT NULL,
56
+ "registry_coordinates" jsonb,
57
+ "asset_inventory" jsonb NOT NULL,
58
+ "state" "app_release_artifact_state" DEFAULT 'available' NOT NULL,
59
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
60
+ );--> statement-breakpoint
61
+ CREATE TABLE "app_release_localizations" (
62
+ "id" text PRIMARY KEY NOT NULL,
63
+ "release_id" text NOT NULL,
64
+ "locale" text NOT NULL,
65
+ "surface" text NOT NULL,
66
+ "message_key" text NOT NULL,
67
+ "text" text NOT NULL,
68
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
69
+ );--> statement-breakpoint
70
+ ALTER TABLE "app_redirect_uris" ADD CONSTRAINT "app_redirect_uris_app_id_apps_id_fk" FOREIGN KEY ("app_id") REFERENCES "public"."apps"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
71
+ ALTER TABLE "app_credentials" ADD CONSTRAINT "app_credentials_app_id_apps_id_fk" FOREIGN KEY ("app_id") REFERENCES "public"."apps"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
72
+ ALTER TABLE "app_releases" ADD CONSTRAINT "app_releases_app_id_apps_id_fk" FOREIGN KEY ("app_id") REFERENCES "public"."apps"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
73
+ ALTER TABLE "app_release_artifacts" ADD CONSTRAINT "app_release_artifacts_release_id_app_releases_id_fk" FOREIGN KEY ("release_id") REFERENCES "public"."app_releases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
74
+ ALTER TABLE "app_release_localizations" ADD CONSTRAINT "app_release_localizations_release_id_app_releases_id_fk" FOREIGN KEY ("release_id") REFERENCES "public"."app_releases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
75
+ CREATE UNIQUE INDEX "uidx_apps_platform_namespace" ON "apps" USING btree ("platform_namespace");--> statement-breakpoint
76
+ CREATE INDEX "idx_apps_owner" ON "apps" USING btree ("owner_id","distribution","lifecycle_state");--> statement-breakpoint
77
+ CREATE INDEX "idx_app_redirect_uris_app" ON "app_redirect_uris" USING btree ("app_id");--> statement-breakpoint
78
+ CREATE UNIQUE INDEX "uidx_app_redirect_uris_exact" ON "app_redirect_uris" USING btree ("app_id","redirect_uri");--> statement-breakpoint
79
+ CREATE INDEX "idx_app_credentials_app" ON "app_credentials" USING btree ("app_id","kind");--> statement-breakpoint
80
+ CREATE UNIQUE INDEX "uidx_app_credentials_generation" ON "app_credentials" USING btree ("app_id","kind","generation");--> statement-breakpoint
81
+ CREATE INDEX "idx_app_releases_app" ON "app_releases" USING btree ("app_id","created_at");--> statement-breakpoint
82
+ CREATE UNIQUE INDEX "uidx_app_releases_digest" ON "app_releases" USING btree ("app_id","manifest_digest");--> statement-breakpoint
83
+ CREATE UNIQUE INDEX "uidx_app_releases_version" ON "app_releases" USING btree ("app_id","release_version");--> statement-breakpoint
84
+ CREATE INDEX "idx_app_release_artifacts_release" ON "app_release_artifacts" USING btree ("release_id");--> statement-breakpoint
85
+ CREATE UNIQUE INDEX "uidx_app_release_artifacts_digest" ON "app_release_artifacts" USING btree ("release_id","digest");--> statement-breakpoint
86
+ CREATE INDEX "idx_app_release_localizations_release" ON "app_release_localizations" USING btree ("release_id","locale");--> statement-breakpoint
87
+ CREATE UNIQUE INDEX "uidx_app_release_localizations_key" ON "app_release_localizations" USING btree ("release_id","locale","surface","message_key");
@@ -0,0 +1,136 @@
1
+ CREATE TYPE "public"."app_access_credential_status" AS ENUM('active', 'inactive', 'revoked');--> statement-breakpoint
2
+ CREATE TYPE "public"."app_audit_event_kind" AS ENUM('lifecycle', 'grant', 'credential', 'reconciliation', 'purge');--> statement-breakpoint
3
+ CREATE TYPE "public"."app_grant_status" AS ENUM('requested', 'granted', 'optional', 'revoked');--> statement-breakpoint
4
+ CREATE TYPE "public"."app_installation_registration_status" AS ENUM('active', 'inactive');--> statement-breakpoint
5
+ CREATE TYPE "public"."app_installation_status" AS ENUM('pending', 'authorizing', 'active', 'paused', 'degraded', 'revoked', 'uninstalled');--> statement-breakpoint
6
+ CREATE TYPE "public"."app_installation_update_policy" AS ENUM('manual', 'compatible', 'patch', 'pinned');--> statement-breakpoint
7
+ CREATE TYPE "public"."app_webhook_subscription_status" AS ENUM('active', 'inactive', 'failed');--> statement-breakpoint
8
+ CREATE TABLE "app_installations" (
9
+ "id" text PRIMARY KEY NOT NULL,
10
+ "app_id" text NOT NULL,
11
+ "deployment_id" text NOT NULL,
12
+ "release_id" text NOT NULL,
13
+ "status" "app_installation_status" DEFAULT 'pending' NOT NULL,
14
+ "namespace" text NOT NULL,
15
+ "installed_by" text NOT NULL,
16
+ "update_policy" "app_installation_update_policy" DEFAULT 'compatible' NOT NULL,
17
+ "last_compatible_release_check_at" timestamp with time zone,
18
+ "pending_release_id" text,
19
+ "pending_reason" text,
20
+ "installed_at" timestamp with time zone DEFAULT now() NOT NULL,
21
+ "authorized_at" timestamp with time zone,
22
+ "activated_at" timestamp with time zone,
23
+ "paused_at" timestamp with time zone,
24
+ "degraded_at" timestamp with time zone,
25
+ "revoked_at" timestamp with time zone,
26
+ "uninstalled_at" timestamp with time zone,
27
+ "purged_at" timestamp with time zone,
28
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
29
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL,
30
+ CONSTRAINT "app_installations_namespace_reserved" CHECK ("app_installations"."namespace" LIKE 'app--%')
31
+ );
32
+ --> statement-breakpoint
33
+ CREATE TABLE "app_grants" (
34
+ "id" text PRIMARY KEY NOT NULL,
35
+ "installation_id" text NOT NULL,
36
+ "scope" text NOT NULL,
37
+ "status" "app_grant_status" NOT NULL,
38
+ "optional" boolean DEFAULT false NOT NULL,
39
+ "requested_at" timestamp with time zone DEFAULT now() NOT NULL,
40
+ "granted_at" timestamp with time zone,
41
+ "revoked_at" timestamp with time zone
42
+ );
43
+ --> statement-breakpoint
44
+ CREATE TABLE "app_access_credentials" (
45
+ "id" text PRIMARY KEY NOT NULL,
46
+ "installation_id" text NOT NULL,
47
+ "generation" integer NOT NULL,
48
+ "credential_hash" text NOT NULL,
49
+ "encrypted_metadata" jsonb NOT NULL,
50
+ "status" "app_access_credential_status" DEFAULT 'active' NOT NULL,
51
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
52
+ "expires_at" timestamp with time zone,
53
+ "deactivated_at" timestamp with time zone
54
+ );
55
+ --> statement-breakpoint
56
+ CREATE TABLE "app_installation_settings" (
57
+ "id" text PRIMARY KEY NOT NULL,
58
+ "installation_id" text NOT NULL,
59
+ "settings" jsonb NOT NULL,
60
+ "schema_digest" text NOT NULL,
61
+ "updated_by" text NOT NULL,
62
+ "updated_at" timestamp with time zone DEFAULT now() NOT NULL
63
+ );
64
+ --> statement-breakpoint
65
+ CREATE TABLE "app_secret_references" (
66
+ "id" text PRIMARY KEY NOT NULL,
67
+ "installation_id" text NOT NULL,
68
+ "key" text NOT NULL,
69
+ "secret_ref" text NOT NULL,
70
+ "created_by" text NOT NULL,
71
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
72
+ "revoked_at" timestamp with time zone
73
+ );
74
+ --> statement-breakpoint
75
+ CREATE TABLE "app_extension_installations" (
76
+ "id" text PRIMARY KEY NOT NULL,
77
+ "installation_id" text NOT NULL,
78
+ "release_id" text NOT NULL,
79
+ "extension_key" text NOT NULL,
80
+ "descriptor" jsonb NOT NULL,
81
+ "status" "app_installation_registration_status" DEFAULT 'active' NOT NULL,
82
+ "installed_at" timestamp with time zone DEFAULT now() NOT NULL,
83
+ "deactivated_at" timestamp with time zone
84
+ );
85
+ --> statement-breakpoint
86
+ CREATE TABLE "app_webhook_subscriptions" (
87
+ "id" text PRIMARY KEY NOT NULL,
88
+ "installation_id" text NOT NULL,
89
+ "release_id" text NOT NULL,
90
+ "event_type" text NOT NULL,
91
+ "event_version" text NOT NULL,
92
+ "endpoint_url" text NOT NULL,
93
+ "status" "app_webhook_subscription_status" DEFAULT 'active' NOT NULL,
94
+ "external_subscription_id" text,
95
+ "installed_at" timestamp with time zone DEFAULT now() NOT NULL,
96
+ "deactivated_at" timestamp with time zone
97
+ );
98
+ --> statement-breakpoint
99
+ CREATE TABLE "app_audit_events" (
100
+ "id" text PRIMARY KEY NOT NULL,
101
+ "installation_id" text,
102
+ "app_id" text NOT NULL,
103
+ "deployment_id" text NOT NULL,
104
+ "actor_id" text NOT NULL,
105
+ "kind" "app_audit_event_kind" NOT NULL,
106
+ "action" text NOT NULL,
107
+ "details" jsonb NOT NULL,
108
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
109
+ );
110
+ --> statement-breakpoint
111
+ ALTER TABLE "app_installations" ADD CONSTRAINT "app_installations_app_id_apps_id_fk" FOREIGN KEY ("app_id") REFERENCES "public"."apps"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
112
+ ALTER TABLE "app_installations" ADD CONSTRAINT "app_installations_release_id_app_releases_id_fk" FOREIGN KEY ("release_id") REFERENCES "public"."app_releases"("id") ON DELETE restrict ON UPDATE no action;--> statement-breakpoint
113
+ ALTER TABLE "app_grants" ADD CONSTRAINT "app_grants_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
114
+ ALTER TABLE "app_access_credentials" ADD CONSTRAINT "app_access_credentials_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
115
+ ALTER TABLE "app_installation_settings" ADD CONSTRAINT "app_installation_settings_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
116
+ ALTER TABLE "app_secret_references" ADD CONSTRAINT "app_secret_references_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
117
+ ALTER TABLE "app_extension_installations" ADD CONSTRAINT "app_extension_installations_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
118
+ ALTER TABLE "app_extension_installations" ADD CONSTRAINT "app_extension_installations_release_id_app_releases_id_fk" FOREIGN KEY ("release_id") REFERENCES "public"."app_releases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
119
+ ALTER TABLE "app_webhook_subscriptions" ADD CONSTRAINT "app_webhook_subscriptions_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
120
+ ALTER TABLE "app_webhook_subscriptions" ADD CONSTRAINT "app_webhook_subscriptions_release_id_app_releases_id_fk" FOREIGN KEY ("release_id") REFERENCES "public"."app_releases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
121
+ ALTER TABLE "app_audit_events" ADD CONSTRAINT "app_audit_events_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE set null ON UPDATE no action;--> statement-breakpoint
122
+ CREATE INDEX "idx_app_installations_app" ON "app_installations" USING btree ("app_id","status");--> statement-breakpoint
123
+ CREATE INDEX "idx_app_installations_deployment" ON "app_installations" USING btree ("deployment_id","status");--> statement-breakpoint
124
+ CREATE UNIQUE INDEX "uidx_app_installations_deployment_app" ON "app_installations" USING btree ("deployment_id","app_id");--> statement-breakpoint
125
+ CREATE INDEX "idx_app_grants_installation" ON "app_grants" USING btree ("installation_id","status");--> statement-breakpoint
126
+ CREATE UNIQUE INDEX "uidx_app_grants_scope" ON "app_grants" USING btree ("installation_id","scope");--> statement-breakpoint
127
+ CREATE INDEX "idx_app_access_credentials_installation" ON "app_access_credentials" USING btree ("installation_id","status");--> statement-breakpoint
128
+ CREATE UNIQUE INDEX "uidx_app_access_credentials_generation" ON "app_access_credentials" USING btree ("installation_id","generation");--> statement-breakpoint
129
+ CREATE UNIQUE INDEX "uidx_app_installation_settings_installation" ON "app_installation_settings" USING btree ("installation_id");--> statement-breakpoint
130
+ CREATE UNIQUE INDEX "uidx_app_secret_references_key" ON "app_secret_references" USING btree ("installation_id","key");--> statement-breakpoint
131
+ CREATE INDEX "idx_app_extension_installations_installation" ON "app_extension_installations" USING btree ("installation_id","status");--> statement-breakpoint
132
+ CREATE UNIQUE INDEX "uidx_app_extension_installations_key" ON "app_extension_installations" USING btree ("installation_id","extension_key");--> statement-breakpoint
133
+ CREATE INDEX "idx_app_webhook_subscriptions_installation" ON "app_webhook_subscriptions" USING btree ("installation_id","status");--> statement-breakpoint
134
+ CREATE UNIQUE INDEX "uidx_app_webhook_subscriptions_event" ON "app_webhook_subscriptions" USING btree ("installation_id","event_type","event_version","endpoint_url");--> statement-breakpoint
135
+ CREATE INDEX "idx_app_audit_events_installation" ON "app_audit_events" USING btree ("installation_id","created_at");--> statement-breakpoint
136
+ CREATE INDEX "idx_app_audit_events_app" ON "app_audit_events" USING btree ("app_id","deployment_id","created_at");
@@ -0,0 +1,4 @@
1
+ ALTER TABLE "app_webhook_subscriptions" ADD COLUMN "signing_key_id" text;--> statement-breakpoint
2
+ ALTER TABLE "app_webhook_subscriptions" ADD COLUMN "last_delivery_at" timestamp with time zone;--> statement-breakpoint
3
+ ALTER TABLE "app_webhook_subscriptions" ADD COLUMN "failure_count" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
4
+ ALTER TABLE "app_webhook_subscriptions" ADD COLUMN "paused_at" timestamp with time zone;
@@ -0,0 +1,47 @@
1
+ ALTER TYPE "public"."app_audit_event_kind" ADD VALUE IF NOT EXISTS 'consent';--> statement-breakpoint
2
+ ALTER TYPE "public"."app_audit_event_kind" ADD VALUE IF NOT EXISTS 'token';--> statement-breakpoint
3
+ CREATE TYPE "public"."app_access_token_mode" AS ENUM('offline', 'online');--> statement-breakpoint
4
+ ALTER TABLE "app_installations" ADD COLUMN "credential_generation" integer DEFAULT 0 NOT NULL;--> statement-breakpoint
5
+ ALTER TABLE "app_access_credentials" ADD COLUMN "token_mode" "app_access_token_mode" DEFAULT 'offline' NOT NULL;--> statement-breakpoint
6
+ ALTER TABLE "app_access_credentials" ADD COLUMN "actor_id" text;--> statement-breakpoint
7
+ ALTER TABLE "app_access_credentials" ADD COLUMN "viewer_id" text;--> statement-breakpoint
8
+ DROP INDEX "public"."uidx_app_access_credentials_generation";--> statement-breakpoint
9
+ CREATE INDEX "idx_app_access_credentials_generation" ON "app_access_credentials" USING btree ("installation_id","token_mode","generation");--> statement-breakpoint
10
+ CREATE TABLE "app_oauth_authorization_codes" (
11
+ "id" text PRIMARY KEY NOT NULL,
12
+ "app_id" text NOT NULL,
13
+ "installation_id" text NOT NULL,
14
+ "release_id" text NOT NULL,
15
+ "deployment_id" text NOT NULL,
16
+ "code_hash" text NOT NULL,
17
+ "state_hash" text NOT NULL,
18
+ "redirect_uri" text NOT NULL,
19
+ "code_challenge" text NOT NULL,
20
+ "code_challenge_method" text NOT NULL,
21
+ "requested_scopes" jsonb NOT NULL,
22
+ "granted_scopes" jsonb NOT NULL,
23
+ "denied_optional_scopes" jsonb NOT NULL,
24
+ "actor_id" text NOT NULL,
25
+ "expires_at" timestamp with time zone NOT NULL,
26
+ "consumed_at" timestamp with time zone,
27
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL
28
+ );--> statement-breakpoint
29
+ CREATE TABLE "app_oauth_refresh_tokens" (
30
+ "id" text PRIMARY KEY NOT NULL,
31
+ "installation_id" text NOT NULL,
32
+ "token_hash" text NOT NULL,
33
+ "generation" integer NOT NULL,
34
+ "status" "app_access_credential_status" DEFAULT 'active' NOT NULL,
35
+ "rotated_from_id" text,
36
+ "created_at" timestamp with time zone DEFAULT now() NOT NULL,
37
+ "expires_at" timestamp with time zone,
38
+ "revoked_at" timestamp with time zone
39
+ );--> statement-breakpoint
40
+ ALTER TABLE "app_oauth_authorization_codes" ADD CONSTRAINT "app_oauth_authorization_codes_app_id_apps_id_fk" FOREIGN KEY ("app_id") REFERENCES "public"."apps"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
41
+ ALTER TABLE "app_oauth_authorization_codes" ADD CONSTRAINT "app_oauth_authorization_codes_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
42
+ ALTER TABLE "app_oauth_authorization_codes" ADD CONSTRAINT "app_oauth_authorization_codes_release_id_app_releases_id_fk" FOREIGN KEY ("release_id") REFERENCES "public"."app_releases"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
43
+ ALTER TABLE "app_oauth_refresh_tokens" ADD CONSTRAINT "app_oauth_refresh_tokens_installation_id_app_installations_id_fk" FOREIGN KEY ("installation_id") REFERENCES "public"."app_installations"("id") ON DELETE cascade ON UPDATE no action;--> statement-breakpoint
44
+ CREATE UNIQUE INDEX "uidx_app_oauth_codes_hash" ON "app_oauth_authorization_codes" USING btree ("code_hash");--> statement-breakpoint
45
+ CREATE INDEX "idx_app_oauth_codes_installation" ON "app_oauth_authorization_codes" USING btree ("installation_id","expires_at");--> statement-breakpoint
46
+ CREATE UNIQUE INDEX "uidx_app_oauth_refresh_tokens_hash" ON "app_oauth_refresh_tokens" USING btree ("token_hash");--> statement-breakpoint
47
+ CREATE INDEX "idx_app_oauth_refresh_tokens_installation" ON "app_oauth_refresh_tokens" USING btree ("installation_id","status");
@@ -0,0 +1,34 @@
1
+ {
2
+ "version": "7",
3
+ "dialect": "postgresql",
4
+ "entries": [
5
+ {
6
+ "idx": 0,
7
+ "version": "7",
8
+ "when": 1784250060000,
9
+ "tag": "20260717000100_app_registry_foundation",
10
+ "breakpoints": true
11
+ },
12
+ {
13
+ "idx": 1,
14
+ "version": "7",
15
+ "when": 1784287178252,
16
+ "tag": "20260717111938_breezy_karma",
17
+ "breakpoints": true
18
+ },
19
+ {
20
+ "idx": 2,
21
+ "version": "7",
22
+ "when": 1784291400000,
23
+ "tag": "20260717123000_app_webhook_delivery_health",
24
+ "breakpoints": true
25
+ },
26
+ {
27
+ "idx": 3,
28
+ "version": "7",
29
+ "when": 1784298600000,
30
+ "tag": "20260717143000_app_oauth_authorization",
31
+ "breakpoints": true
32
+ }
33
+ ]
34
+ }