|
| 1 | +import { MigrationInterface, QueryRunner } from "typeorm"; |
| 2 | + |
| 3 | +/** |
| 4 | + * Initial schema for Awareness as a Service: awareness packets, consumers and |
| 5 | + * their access applications, API keys, webhook subscriptions, the delivery |
| 6 | + * queue and the dead-letter table. |
| 7 | + */ |
| 8 | +export class Init1715200000000 implements MigrationInterface { |
| 9 | + name = "Init1715200000000"; |
| 10 | + |
| 11 | + public async up(queryRunner: QueryRunner): Promise<void> { |
| 12 | + await queryRunner.query(` |
| 13 | + CREATE TABLE "packets" ( |
| 14 | + "id" varchar NOT NULL, |
| 15 | + "ontology" varchar NOT NULL, |
| 16 | + "evaultPublicKey" varchar, |
| 17 | + "w3id" varchar, |
| 18 | + "data" jsonb, |
| 19 | + "operation" varchar NOT NULL DEFAULT 'create', |
| 20 | + "receivedAt" timestamptz NOT NULL DEFAULT now(), |
| 21 | + "createdAt" timestamptz NOT NULL DEFAULT now(), |
| 22 | + CONSTRAINT "PK_packets" PRIMARY KEY ("id") |
| 23 | + ) |
| 24 | + `); |
| 25 | + await queryRunner.query( |
| 26 | + `CREATE INDEX "idx_packets_ontology" ON "packets" ("ontology")`, |
| 27 | + ); |
| 28 | + await queryRunner.query( |
| 29 | + `CREATE INDEX "idx_packets_evault_pubkey" ON "packets" ("evaultPublicKey")`, |
| 30 | + ); |
| 31 | + await queryRunner.query( |
| 32 | + `CREATE INDEX "idx_packets_w3id" ON "packets" ("w3id")`, |
| 33 | + ); |
| 34 | + await queryRunner.query( |
| 35 | + `CREATE INDEX "idx_packets_received" ON "packets" ("receivedAt")`, |
| 36 | + ); |
| 37 | + await queryRunner.query( |
| 38 | + `CREATE INDEX "idx_packets_ontology_received" ON "packets" ("ontology", "receivedAt")`, |
| 39 | + ); |
| 40 | + await queryRunner.query( |
| 41 | + `CREATE INDEX "idx_packets_received_id" ON "packets" ("receivedAt", "id")`, |
| 42 | + ); |
| 43 | + |
| 44 | + await queryRunner.query(` |
| 45 | + CREATE TABLE "consumers" ( |
| 46 | + "id" uuid NOT NULL DEFAULT gen_random_uuid(), |
| 47 | + "ename" varchar NOT NULL, |
| 48 | + "name" varchar, |
| 49 | + "contactEmail" varchar, |
| 50 | + "status" varchar NOT NULL DEFAULT 'pending', |
| 51 | + "webhookBaseUrl" varchar, |
| 52 | + "createdAt" timestamptz NOT NULL DEFAULT now(), |
| 53 | + "approvedAt" timestamptz, |
| 54 | + CONSTRAINT "PK_consumers" PRIMARY KEY ("id") |
| 55 | + ) |
| 56 | + `); |
| 57 | + await queryRunner.query( |
| 58 | + `CREATE UNIQUE INDEX "idx_consumers_ename" ON "consumers" ("ename")`, |
| 59 | + ); |
| 60 | + |
| 61 | + await queryRunner.query(` |
| 62 | + CREATE TABLE "access_applications" ( |
| 63 | + "id" uuid NOT NULL DEFAULT gen_random_uuid(), |
| 64 | + "consumerId" uuid NOT NULL, |
| 65 | + "justification" text, |
| 66 | + "requestedOntologies" text array NOT NULL DEFAULT '{}', |
| 67 | + "status" varchar NOT NULL DEFAULT 'pending', |
| 68 | + "reviewedByEname" varchar, |
| 69 | + "reviewNote" text, |
| 70 | + "createdAt" timestamptz NOT NULL DEFAULT now(), |
| 71 | + "reviewedAt" timestamptz, |
| 72 | + CONSTRAINT "PK_access_applications" PRIMARY KEY ("id") |
| 73 | + ) |
| 74 | + `); |
| 75 | + await queryRunner.query( |
| 76 | + `CREATE INDEX "idx_applications_consumer" ON "access_applications" ("consumerId")`, |
| 77 | + ); |
| 78 | + |
| 79 | + await queryRunner.query(` |
| 80 | + CREATE TABLE "api_keys" ( |
| 81 | + "id" uuid NOT NULL DEFAULT gen_random_uuid(), |
| 82 | + "consumerId" uuid NOT NULL, |
| 83 | + "keyHash" varchar NOT NULL, |
| 84 | + "keyPrefix" varchar NOT NULL, |
| 85 | + "revoked" boolean NOT NULL DEFAULT false, |
| 86 | + "createdAt" timestamptz NOT NULL DEFAULT now(), |
| 87 | + "lastUsedAt" timestamptz, |
| 88 | + CONSTRAINT "PK_api_keys" PRIMARY KEY ("id") |
| 89 | + ) |
| 90 | + `); |
| 91 | + await queryRunner.query( |
| 92 | + `CREATE INDEX "idx_api_keys_consumer" ON "api_keys" ("consumerId")`, |
| 93 | + ); |
| 94 | + await queryRunner.query( |
| 95 | + `CREATE UNIQUE INDEX "idx_api_keys_hash" ON "api_keys" ("keyHash")`, |
| 96 | + ); |
| 97 | + |
| 98 | + await queryRunner.query(` |
| 99 | + CREATE TABLE "subscriptions" ( |
| 100 | + "id" uuid NOT NULL DEFAULT gen_random_uuid(), |
| 101 | + "consumerId" uuid NOT NULL, |
| 102 | + "targetUrl" varchar NOT NULL, |
| 103 | + "ontologyFilter" text array NOT NULL DEFAULT '{}', |
| 104 | + "evaultFilter" text array NOT NULL DEFAULT '{}', |
| 105 | + "isCatchAll" boolean NOT NULL DEFAULT false, |
| 106 | + "active" boolean NOT NULL DEFAULT true, |
| 107 | + "secret" varchar, |
| 108 | + "createdAt" timestamptz NOT NULL DEFAULT now(), |
| 109 | + CONSTRAINT "PK_subscriptions" PRIMARY KEY ("id") |
| 110 | + ) |
| 111 | + `); |
| 112 | + await queryRunner.query( |
| 113 | + `CREATE INDEX "idx_subscriptions_consumer" ON "subscriptions" ("consumerId")`, |
| 114 | + ); |
| 115 | + |
| 116 | + await queryRunner.query(` |
| 117 | + CREATE TABLE "deliveries" ( |
| 118 | + "id" uuid NOT NULL DEFAULT gen_random_uuid(), |
| 119 | + "subscriptionId" uuid NOT NULL, |
| 120 | + "packetId" varchar NOT NULL, |
| 121 | + "status" varchar NOT NULL DEFAULT 'pending', |
| 122 | + "attempts" integer NOT NULL DEFAULT 0, |
| 123 | + "nextAttemptAt" timestamptz NOT NULL DEFAULT now(), |
| 124 | + "lastError" text, |
| 125 | + "lastResponseStatus" integer, |
| 126 | + "createdAt" timestamptz NOT NULL DEFAULT now(), |
| 127 | + "deliveredAt" timestamptz, |
| 128 | + CONSTRAINT "PK_deliveries" PRIMARY KEY ("id"), |
| 129 | + CONSTRAINT "uq_delivery_subscription_packet" UNIQUE ("subscriptionId", "packetId") |
| 130 | + ) |
| 131 | + `); |
| 132 | + await queryRunner.query( |
| 133 | + `CREATE INDEX "idx_deliveries_subscription" ON "deliveries" ("subscriptionId")`, |
| 134 | + ); |
| 135 | + await queryRunner.query( |
| 136 | + `CREATE INDEX "idx_deliveries_next_attempt" ON "deliveries" ("nextAttemptAt")`, |
| 137 | + ); |
| 138 | + |
| 139 | + await queryRunner.query(` |
| 140 | + CREATE TABLE "dead_letters" ( |
| 141 | + "id" uuid NOT NULL DEFAULT gen_random_uuid(), |
| 142 | + "deliveryId" uuid NOT NULL, |
| 143 | + "subscriptionId" uuid NOT NULL, |
| 144 | + "packetId" varchar NOT NULL, |
| 145 | + "consumerId" uuid NOT NULL, |
| 146 | + "payload" jsonb NOT NULL, |
| 147 | + "targetUrl" varchar NOT NULL, |
| 148 | + "totalAttempts" integer NOT NULL, |
| 149 | + "lastError" text, |
| 150 | + "lastResponseStatus" integer, |
| 151 | + "resolved" boolean NOT NULL DEFAULT false, |
| 152 | + "createdAt" timestamptz NOT NULL DEFAULT now(), |
| 153 | + CONSTRAINT "PK_dead_letters" PRIMARY KEY ("id") |
| 154 | + ) |
| 155 | + `); |
| 156 | + await queryRunner.query( |
| 157 | + `CREATE INDEX "idx_dead_letters_resolved" ON "dead_letters" ("resolved")`, |
| 158 | + ); |
| 159 | + } |
| 160 | + |
| 161 | + public async down(queryRunner: QueryRunner): Promise<void> { |
| 162 | + await queryRunner.query(`DROP TABLE "dead_letters"`); |
| 163 | + await queryRunner.query(`DROP TABLE "deliveries"`); |
| 164 | + await queryRunner.query(`DROP TABLE "subscriptions"`); |
| 165 | + await queryRunner.query(`DROP TABLE "api_keys"`); |
| 166 | + await queryRunner.query(`DROP TABLE "access_applications"`); |
| 167 | + await queryRunner.query(`DROP TABLE "consumers"`); |
| 168 | + await queryRunner.query(`DROP TABLE "packets"`); |
| 169 | + } |
| 170 | +} |
0 commit comments