@marinoscar/platform-db 0.0.0-stage → 0.1.0-next.10

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 (244) hide show
  1. package/CHANGELOG.md +86 -0
  2. package/LICENSE +21 -0
  3. package/README.md +525 -2
  4. package/bin/platform.js +28 -0
  5. package/dist/baseline/deps.d.ts +32 -0
  6. package/dist/baseline/deps.d.ts.map +1 -0
  7. package/dist/baseline/deps.js +66 -0
  8. package/dist/baseline/deps.js.map +1 -0
  9. package/dist/baseline/errors.d.ts +36 -0
  10. package/dist/baseline/errors.d.ts.map +1 -0
  11. package/dist/baseline/errors.js +23 -0
  12. package/dist/baseline/errors.js.map +1 -0
  13. package/dist/baseline/fs.d.ts +9 -0
  14. package/dist/baseline/fs.d.ts.map +1 -0
  15. package/dist/baseline/fs.js +39 -0
  16. package/dist/baseline/fs.js.map +1 -0
  17. package/dist/baseline/index.d.ts +8 -0
  18. package/dist/baseline/index.d.ts.map +1 -0
  19. package/dist/baseline/index.js +24 -0
  20. package/dist/baseline/index.js.map +1 -0
  21. package/dist/baseline/mapping.d.ts +43 -0
  22. package/dist/baseline/mapping.d.ts.map +1 -0
  23. package/dist/baseline/mapping.js +80 -0
  24. package/dist/baseline/mapping.js.map +1 -0
  25. package/dist/baseline/normalise.d.ts +42 -0
  26. package/dist/baseline/normalise.d.ts.map +1 -0
  27. package/dist/baseline/normalise.js +133 -0
  28. package/dist/baseline/normalise.js.map +1 -0
  29. package/dist/baseline/plan.d.ts +66 -0
  30. package/dist/baseline/plan.d.ts.map +1 -0
  31. package/dist/baseline/plan.js +105 -0
  32. package/dist/baseline/plan.js.map +1 -0
  33. package/dist/baseline/report.d.ts +127 -0
  34. package/dist/baseline/report.d.ts.map +1 -0
  35. package/dist/baseline/report.js +89 -0
  36. package/dist/baseline/report.js.map +1 -0
  37. package/dist/baseline/run.d.ts +117 -0
  38. package/dist/baseline/run.d.ts.map +1 -0
  39. package/dist/baseline/run.js +358 -0
  40. package/dist/baseline/run.js.map +1 -0
  41. package/dist/bin/db-commands.d.ts +31 -0
  42. package/dist/bin/db-commands.d.ts.map +1 -0
  43. package/dist/bin/db-commands.js +311 -0
  44. package/dist/bin/db-commands.js.map +1 -0
  45. package/dist/bin/fs-io.d.ts +7 -0
  46. package/dist/bin/fs-io.d.ts.map +1 -0
  47. package/dist/bin/fs-io.js +45 -0
  48. package/dist/bin/fs-io.js.map +1 -0
  49. package/dist/bin/platform.d.ts +21 -0
  50. package/dist/bin/platform.d.ts.map +1 -0
  51. package/dist/bin/platform.js +99 -0
  52. package/dist/bin/platform.js.map +1 -0
  53. package/dist/compose/api.d.ts +84 -0
  54. package/dist/compose/api.d.ts.map +1 -0
  55. package/dist/compose/api.js +101 -0
  56. package/dist/compose/api.js.map +1 -0
  57. package/dist/compose/compose.d.ts +51 -0
  58. package/dist/compose/compose.d.ts.map +1 -0
  59. package/dist/compose/compose.js +177 -0
  60. package/dist/compose/compose.js.map +1 -0
  61. package/dist/compose/diff.d.ts +7 -0
  62. package/dist/compose/diff.d.ts.map +1 -0
  63. package/dist/compose/diff.js +68 -0
  64. package/dist/compose/diff.js.map +1 -0
  65. package/dist/compose/errors.d.ts +31 -0
  66. package/dist/compose/errors.d.ts.map +1 -0
  67. package/dist/compose/errors.js +35 -0
  68. package/dist/compose/errors.js.map +1 -0
  69. package/dist/compose/index.d.ts +3 -0
  70. package/dist/compose/index.d.ts.map +1 -0
  71. package/dist/compose/index.js +11 -0
  72. package/dist/compose/index.js.map +1 -0
  73. package/dist/compose/parse.d.ts +65 -0
  74. package/dist/compose/parse.d.ts.map +1 -0
  75. package/dist/compose/parse.js +122 -0
  76. package/dist/compose/parse.js.map +1 -0
  77. package/dist/drift/app-objects.d.ts +107 -0
  78. package/dist/drift/app-objects.d.ts.map +1 -0
  79. package/dist/drift/app-objects.js +193 -0
  80. package/dist/drift/app-objects.js.map +1 -0
  81. package/dist/drift/db-conformance.d.ts +52 -0
  82. package/dist/drift/db-conformance.d.ts.map +1 -0
  83. package/dist/drift/db-conformance.js +89 -0
  84. package/dist/drift/db-conformance.js.map +1 -0
  85. package/dist/drift/index.d.ts +8 -0
  86. package/dist/drift/index.d.ts.map +1 -0
  87. package/dist/drift/index.js +43 -0
  88. package/dist/drift/index.js.map +1 -0
  89. package/dist/drift/raw-sql-indexes.d.ts +106 -0
  90. package/dist/drift/raw-sql-indexes.d.ts.map +1 -0
  91. package/dist/drift/raw-sql-indexes.js +113 -0
  92. package/dist/drift/raw-sql-indexes.js.map +1 -0
  93. package/dist/drift/raw-sql-tripwire.d.ts +191 -0
  94. package/dist/drift/raw-sql-tripwire.d.ts.map +1 -0
  95. package/dist/drift/raw-sql-tripwire.js +422 -0
  96. package/dist/drift/raw-sql-tripwire.js.map +1 -0
  97. package/dist/drift/rls-policies.d.ts +226 -0
  98. package/dist/drift/rls-policies.d.ts.map +1 -0
  99. package/dist/drift/rls-policies.js +317 -0
  100. package/dist/drift/rls-policies.js.map +1 -0
  101. package/dist/drift/run.d.ts +152 -0
  102. package/dist/drift/run.d.ts.map +1 -0
  103. package/dist/drift/run.js +252 -0
  104. package/dist/drift/run.js.map +1 -0
  105. package/dist/drift/sql-expression.d.ts +27 -0
  106. package/dist/drift/sql-expression.d.ts.map +1 -0
  107. package/dist/drift/sql-expression.js +64 -0
  108. package/dist/drift/sql-expression.js.map +1 -0
  109. package/dist/index.d.ts +16 -0
  110. package/dist/index.d.ts.map +1 -0
  111. package/dist/index.js +33 -0
  112. package/dist/index.js.map +1 -0
  113. package/dist/lock/hash.d.ts +13 -0
  114. package/dist/lock/hash.d.ts.map +1 -0
  115. package/dist/lock/hash.js +19 -0
  116. package/dist/lock/hash.js.map +1 -0
  117. package/dist/lock/index.d.ts +5 -0
  118. package/dist/lock/index.d.ts.map +1 -0
  119. package/dist/lock/index.js +20 -0
  120. package/dist/lock/index.js.map +1 -0
  121. package/dist/lock/lock.d.ts +154 -0
  122. package/dist/lock/lock.d.ts.map +1 -0
  123. package/dist/lock/lock.js +151 -0
  124. package/dist/lock/lock.js.map +1 -0
  125. package/dist/lock/manifest.d.ts +68 -0
  126. package/dist/lock/manifest.d.ts.map +1 -0
  127. package/dist/lock/manifest.js +96 -0
  128. package/dist/lock/manifest.js.map +1 -0
  129. package/dist/lock/semver.d.ts +10 -0
  130. package/dist/lock/semver.d.ts.map +1 -0
  131. package/dist/lock/semver.js +13 -0
  132. package/dist/lock/semver.js.map +1 -0
  133. package/dist/seed/index.d.ts +5 -0
  134. package/dist/seed/index.d.ts.map +1 -0
  135. package/dist/seed/index.js +16 -0
  136. package/dist/seed/index.js.map +1 -0
  137. package/dist/seed/input.d.ts +52 -0
  138. package/dist/seed/input.d.ts.map +1 -0
  139. package/dist/seed/input.js +77 -0
  140. package/dist/seed/input.js.map +1 -0
  141. package/dist/seed/seed-platform.d.ts +41 -0
  142. package/dist/seed/seed-platform.d.ts.map +1 -0
  143. package/dist/seed/seed-platform.js +165 -0
  144. package/dist/seed/seed-platform.js.map +1 -0
  145. package/dist/seed/steps.d.ts +172 -0
  146. package/dist/seed/steps.d.ts.map +1 -0
  147. package/dist/seed/steps.js +136 -0
  148. package/dist/seed/steps.js.map +1 -0
  149. package/dist/seed/types.d.ts +287 -0
  150. package/dist/seed/types.d.ts.map +1 -0
  151. package/dist/seed/types.js +5 -0
  152. package/dist/seed/types.js.map +1 -0
  153. package/dist/sync/app-migrations.d.ts +52 -0
  154. package/dist/sync/app-migrations.d.ts.map +1 -0
  155. package/dist/sync/app-migrations.js +62 -0
  156. package/dist/sync/app-migrations.js.map +1 -0
  157. package/dist/sync/apply.d.ts +29 -0
  158. package/dist/sync/apply.d.ts.map +1 -0
  159. package/dist/sync/apply.js +38 -0
  160. package/dist/sync/apply.js.map +1 -0
  161. package/dist/sync/check.d.ts +59 -0
  162. package/dist/sync/check.d.ts.map +1 -0
  163. package/dist/sync/check.js +115 -0
  164. package/dist/sync/check.js.map +1 -0
  165. package/dist/sync/errors.d.ts +32 -0
  166. package/dist/sync/errors.d.ts.map +1 -0
  167. package/dist/sync/errors.js +23 -0
  168. package/dist/sync/errors.js.map +1 -0
  169. package/dist/sync/index.d.ts +10 -0
  170. package/dist/sync/index.d.ts.map +1 -0
  171. package/dist/sync/index.js +27 -0
  172. package/dist/sync/index.js.map +1 -0
  173. package/dist/sync/ledger.d.ts +65 -0
  174. package/dist/sync/ledger.d.ts.map +1 -0
  175. package/dist/sync/ledger.js +49 -0
  176. package/dist/sync/ledger.js.map +1 -0
  177. package/dist/sync/plan.d.ts +63 -0
  178. package/dist/sync/plan.d.ts.map +1 -0
  179. package/dist/sync/plan.js +86 -0
  180. package/dist/sync/plan.js.map +1 -0
  181. package/dist/sync/promote.d.ts +62 -0
  182. package/dist/sync/promote.d.ts.map +1 -0
  183. package/dist/sync/promote.js +95 -0
  184. package/dist/sync/promote.js.map +1 -0
  185. package/dist/sync/timestamps.d.ts +41 -0
  186. package/dist/sync/timestamps.d.ts.map +1 -0
  187. package/dist/sync/timestamps.js +68 -0
  188. package/dist/sync/timestamps.js.map +1 -0
  189. package/dist/sync/version.d.ts +28 -0
  190. package/dist/sync/version.d.ts.map +1 -0
  191. package/dist/sync/version.js +66 -0
  192. package/dist/sync/version.js.map +1 -0
  193. package/migrations/0001_initial/migration.sql +297 -0
  194. package/migrations/0002_add_personal_access_tokens/migration.sql +31 -0
  195. package/migrations/0003_add_credentials/migration.sql +20 -0
  196. package/migrations/0004_add_notification_deliveries/migration.sql +27 -0
  197. package/migrations/0005_drop_stale_uuid_defaults/migration.sql +35 -0
  198. package/migrations/0006_add_notifications/migration.sql +22 -0
  199. package/migrations/0007_add_push_subscriptions/migration.sql +25 -0
  200. package/migrations/0008_add_jobs/migration.sql +110 -0
  201. package/migrations/0009_add_worker_nodes/migration.sql +90 -0
  202. package/migrations/0010_add_database_backup_runs/migration.sql +104 -0
  203. package/migrations/0011_add_notification_broadcasts/migration.sql +57 -0
  204. package/migrations/0012_add_backup_run_job_link/migration.sql +87 -0
  205. package/migrations/0013_add_job_node_secrets/migration.sql +61 -0
  206. package/migrations/0014_add_backup_run_pg_dump_version/migration.sql +26 -0
  207. package/migrations/0015_add_job_claim_token/migration.sql +92 -0
  208. package/migrations/0016_add_ai_platform/migration.sql +108 -0
  209. package/migrations/0017_revoke_viewer_ai_use/migration.sql +32 -0
  210. package/migrations/0018_add_user_credentials/migration.sql +29 -0
  211. package/migrations/0019_add_device_session_credential_link/migration.sql +52 -0
  212. package/migrations/0020_add_worker_node_vitals/migration.sql +3 -0
  213. package/migrations/0021_add_job_trace_context/migration.sql +6 -0
  214. package/migrations/0022_add_retention_created_at_indexes/migration.sql +13 -0
  215. package/migrations/0023_add_organizations/migration.sql +142 -0
  216. package/migrations/0024_split_system_org_roles/migration.sql +131 -0
  217. package/migrations/0025_org_scoped_rls/migration.sql +215 -0
  218. package/migrations/0026_add_groups/migration.sql +165 -0
  219. package/migrations/0027_add_grants/migration.sql +117 -0
  220. package/migrations/0028_add_org_settings/migration.sql +49 -0
  221. package/migrations/0029_add_org_credentials/migration.sql +54 -0
  222. package/migrations/0030_add_jobs_org_id/migration.sql +18 -0
  223. package/migrations/0031_add_jobs_org_id_status_index/migration.sql +6 -0
  224. package/migrations/0032_add_broadcast_target_org/migration.sql +21 -0
  225. package/migrations/0033_add_android_app/migration.sql +60 -0
  226. package/migrations/0034_open_job_reason/migration.sql +16 -0
  227. package/migrations/0035_ai_usage_feature/migration.sql +37 -0
  228. package/migrations/0036_add_app_metadata/migration.sql +49 -0
  229. package/migrations/0037_open_sharing_vocabularies/migration.sql +97 -0
  230. package/migrations/manifest.json +379 -0
  231. package/package.json +78 -4
  232. package/raw-sql-indexes.json +94 -0
  233. package/rls-policies.json +74 -0
  234. package/schema/ai.prisma +444 -0
  235. package/schema/android-app.prisma +47 -0
  236. package/schema/base.prisma +11 -0
  237. package/schema/credentials.prisma +170 -0
  238. package/schema/db-backup.prisma +376 -0
  239. package/schema/identity.prisma +439 -0
  240. package/schema/jobs.prisma +760 -0
  241. package/schema/notifications.prisma +649 -0
  242. package/schema/settings.prisma +60 -0
  243. package/schema/sharing.prisma +223 -0
  244. package/schema/storage.prisma +84 -0
@@ -0,0 +1,60 @@
1
+ -- The Android companion slice (#746, PP-9.4; merged from EvoPath and
2
+ -- MemoriaHub). Expand-only: one new table and one defaulted column.
3
+
4
+ -- CreateTable
5
+ CREATE TABLE "android_app_releases" (
6
+ "id" UUID NOT NULL,
7
+ "package_name" TEXT NOT NULL,
8
+ "version_name" VARCHAR(50) NOT NULL,
9
+ "version_code" INTEGER NOT NULL,
10
+ "signing_sha256" TEXT NOT NULL,
11
+ "file_sha256" CHAR(64) NOT NULL,
12
+ "size_bytes" BIGINT NOT NULL,
13
+ "storage_key" TEXT NOT NULL,
14
+ "storage_provider" TEXT,
15
+ "bucket" TEXT,
16
+ "notes" VARCHAR(2000),
17
+ "is_current" BOOLEAN NOT NULL DEFAULT false,
18
+ "uploaded_by_id" UUID,
19
+ "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
20
+
21
+ CONSTRAINT "android_app_releases_pkey" PRIMARY KEY ("id")
22
+ );
23
+
24
+ -- CreateIndex
25
+ CREATE INDEX "android_app_releases_created_at_idx" ON "android_app_releases"("created_at");
26
+
27
+ -- CreateIndex
28
+ CREATE INDEX "android_app_releases_uploaded_by_id_idx" ON "android_app_releases"("uploaded_by_id");
29
+
30
+ -- CreateIndex
31
+ CREATE UNIQUE INDEX "android_app_releases_package_name_version_code_key" ON "android_app_releases"("package_name", "version_code");
32
+
33
+ -- AddForeignKey
34
+ ALTER TABLE "android_app_releases" ADD CONSTRAINT "android_app_releases_uploaded_by_id_fkey" FOREIGN KEY ("uploaded_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
35
+
36
+ -- =============================================================================
37
+ -- Raw-SQL constraints (intentional schema drift: Prisma cannot express these).
38
+ -- Never declare android_app_releases_one_current_uniq_idx as @@unique and never
39
+ -- replace it with a findFirst pre-check; make-current clears the old flag and
40
+ -- sets the new one in one transaction, and the database rejects a second
41
+ -- current row (P2002, mapped to 409 RELEASE_CURRENT_CONFLICT).
42
+ -- =============================================================================
43
+
44
+ -- At most one current release deployment-wide.
45
+ CREATE UNIQUE INDEX "android_app_releases_one_current_uniq_idx" ON "android_app_releases" ((true)) WHERE "is_current";
46
+
47
+ -- Push subscription platform: tags each Web Push subscription with the surface
48
+ -- that registered it, so the `android_app` notification channel can target the
49
+ -- Android companion alone. Existing rows are browser subscriptions. A constant
50
+ -- default makes this a metadata-only ADD COLUMN (no table rewrite).
51
+
52
+ -- AlterTable
53
+ ALTER TABLE "push_subscriptions" ADD COLUMN "platform" TEXT NOT NULL DEFAULT 'browser';
54
+
55
+ -- Prisma cannot express a CHECK constraint, so it lives here only (intentional
56
+ -- schema drift, like the raw-SQL partial unique indexes). The contract's
57
+ -- PUSH_SUBSCRIPTION_PLATFORMS mirrors it.
58
+ ALTER TABLE "push_subscriptions"
59
+ ADD CONSTRAINT "push_subscriptions_platform_check"
60
+ CHECK ("platform" IN ('browser', 'android_app'));
@@ -0,0 +1,16 @@
1
+ -- Open the job-reason vocabulary: "jobs"."reason" goes from the closed
2
+ -- "JobReason" enum ('upload', 'rerun', 'backfill') to plain TEXT, so an app can
3
+ -- register its own reasons (`registerJobReason`) at zero migrations. Every
4
+ -- stored value is kept as it is: the cast is the enum's own label.
5
+ --
6
+ -- No CHECK constraint on purpose: a row must outlive the reason that wrote it,
7
+ -- and the allowed shape is the contract's `jobReasonSchema` pattern at the API
8
+ -- edge. "jobs" has no row-level security, so no bypass is needed here.
9
+ --
10
+ -- The column type changes in place (a table rewrite, taken under an ACCESS
11
+ -- EXCLUSIVE lock for its duration); no index covers "reason", so nothing is
12
+ -- rebuilt besides the heap. The enum type is dropped last: nothing else uses it.
13
+ ALTER TABLE "jobs" ALTER COLUMN "reason" TYPE TEXT USING "reason"::text;
14
+
15
+ -- DropEnum
16
+ DROP TYPE "JobReason";
@@ -0,0 +1,37 @@
1
+ -- =============================================================================
2
+ -- Per-feature AI usage (PP-14.17, #935)
3
+ -- =============================================================================
4
+ --
5
+ -- Persist the registered AI feature (`registerAiFeature`) a call was made for,
6
+ -- so usage can be filtered and grouped per feature from the database:
7
+ --
8
+ -- ai_runs.feature the feature a background run was started for
9
+ -- ai_usage_events.feature the feature a provider round trip was made for
10
+ --
11
+ -- Both columns are nullable plain TEXT: every row written before this
12
+ -- migration, a call made without a feature, and a catalogue sync have none.
13
+ -- No backfill, no default, no foreign key (a feature is code, not data, and a
14
+ -- row must outlive the feature that wrote it).
15
+ --
16
+ -- ai_runs and ai_usage_events are organization tables under forced row-level
17
+ -- security, so the migration runs with the bypass on for its transaction
18
+ -- (set_config with is_local = true: transaction-local, never session-level).
19
+ -- Adding a nullable column rewrites nothing; the index is built under the
20
+ -- table's SHARE lock, which blocks writes to ai_usage_events for the length of
21
+ -- the build (the table holds one row per provider round trip, an append-only
22
+ -- ledger a retention purge keeps bounded).
23
+ -- =============================================================================
24
+
25
+ BEGIN;
26
+ SELECT set_config('app.rls_bypass', 'on', true);
27
+
28
+ -- AlterTable
29
+ ALTER TABLE "ai_runs" ADD COLUMN "feature" TEXT;
30
+
31
+ -- AlterTable
32
+ ALTER TABLE "ai_usage_events" ADD COLUMN "feature" TEXT;
33
+
34
+ -- CreateIndex
35
+ CREATE INDEX "ai_usage_events_org_id_feature_created_at_idx" ON "ai_usage_events"("org_id", "feature", "created_at");
36
+
37
+ COMMIT;
@@ -0,0 +1,49 @@
1
+ -- =============================================================================
2
+ -- App metadata on users and organizations; composite-key targets (PP-14.27, #945)
3
+ -- =============================================================================
4
+ --
5
+ -- users.metadata and organizations.metadata are JSONB documents an app fills
6
+ -- with namespaced fields of its own (registerUserMetadataSchema,
7
+ -- registerOrganizationMetadataSchema): one top-level key per registered
8
+ -- namespace, validated against that namespace's zod schema on write. They are
9
+ -- not a secret store; a secret-named field is refused at registration.
10
+ --
11
+ -- NOT NULL DEFAULT '{}': every existing row gets an empty document; no backfill
12
+ -- and no rewrite (a constant default is stored in the catalog, PostgreSQL 11+).
13
+ --
14
+ -- The size CHECK is a backstop, not the contract. The writer enforces 16 KiB of
15
+ -- compact JSON per row; the constraint allows 32 KiB of jsonb text, because the
16
+ -- text form of a jsonb value carries a space after every colon and comma, so
17
+ -- the two measures differ by a few percent. A constraint that matched the
18
+ -- writer's limit exactly would reject a document the writer accepted.
19
+ --
20
+ -- ai_runs and grants get a unique index on (id, org_id): the target of a
21
+ -- composite foreign key (grant_id, org_id) from an organization-owned table an
22
+ -- app adds, exactly as groups and storage_objects already are. A plain key on
23
+ -- id alone would let a row of one organization point at another's, since a
24
+ -- foreign-key check bypasses row-level security. No existing key changes.
25
+ --
26
+ -- ai_runs and grants are organization tables under forced row-level security,
27
+ -- so the migration runs with the bypass on for its transaction (set_config with
28
+ -- is_local = true: transaction-local, never session-level). users and
29
+ -- organizations have no row-level security.
30
+ -- =============================================================================
31
+
32
+ BEGIN;
33
+ SELECT set_config('app.rls_bypass', 'on', true);
34
+
35
+ -- AlterTable
36
+ ALTER TABLE "users" ADD COLUMN "metadata" JSONB NOT NULL DEFAULT '{}';
37
+ ALTER TABLE "users" ADD CONSTRAINT "users_metadata_size_check" CHECK (jsonb_typeof("metadata") = 'object' AND octet_length("metadata"::text) <= 32768);
38
+
39
+ -- AlterTable
40
+ ALTER TABLE "organizations" ADD COLUMN "metadata" JSONB NOT NULL DEFAULT '{}';
41
+ ALTER TABLE "organizations" ADD CONSTRAINT "organizations_metadata_size_check" CHECK (jsonb_typeof("metadata") = 'object' AND octet_length("metadata"::text) <= 32768);
42
+
43
+ -- CreateIndex
44
+ CREATE UNIQUE INDEX "ai_runs_id_org_id_key" ON "ai_runs"("id", "org_id");
45
+
46
+ -- CreateIndex
47
+ CREATE UNIQUE INDEX "grants_id_org_id_key" ON "grants"("id", "org_id");
48
+
49
+ COMMIT;
@@ -0,0 +1,97 @@
1
+ -- =============================================================================
2
+ -- Open the sharing vocabularies (PP-14.15b, issue #967)
3
+ -- =============================================================================
4
+ --
5
+ -- Two closed Prisma enums become plain TEXT, so an app can add a group role
6
+ -- (`registerGroupRole`) and a persisted grantee kind (`registerGranteeKind`)
7
+ -- at zero further migrations:
8
+ --
9
+ -- group_members.role, group_invites.role "GroupRole" admin | editor | viewer
10
+ -- grants.grantee_kind "GrantGranteeKind" user | group | link
11
+ --
12
+ -- Every stored value is kept as it is (the cast is the enum's own label) and
13
+ -- both enums are dropped. No CHECK on the role columns on purpose: a row must
14
+ -- outlive the role that wrote it, and the allowed set is the API's registry.
15
+ --
16
+ -- grants gains the nullable `grantee_ref` TEXT: the reference of a grantee of an
17
+ -- app-registered kind (a domain, a team id), as the kind normalised it. The
18
+ -- built-in kinds keep their own columns (grantee_user_id, grantee_group_id,
19
+ -- link_token_hash) and leave it NULL.
20
+ --
21
+ -- RAW SQL (intentional schema drift, as in 0027_add_grants):
22
+ -- grants_grantee_consistency_check relaxed: the three built-in row shapes
23
+ -- (now also with grantee_ref NULL) OR a row
24
+ -- of any OTHER kind that carries a
25
+ -- grantee_ref and none of the built-in
26
+ -- grantee columns. This CHECK is the second
27
+ -- line of defence behind the API.
28
+ -- grants_active_user_uniq_idx,
29
+ -- grants_active_group_uniq_idx rebuilt unchanged apart from the predicate
30
+ -- literal, which was an enum cast and is
31
+ -- now text (an index predicate over a column
32
+ -- whose type changes cannot be re-parsed).
33
+ -- grants_active_ref_uniq_idx NEW: at most one ACTIVE grant per
34
+ -- (resource, app kind, reference); the grants
35
+ -- API upserts on it.
36
+ -- All three indexes are listed in raw-sql-indexes.json.
37
+ --
38
+ -- group_members, group_invites and grants are organization tables under forced
39
+ -- row-level security, so the migration runs with the bypass on for its
40
+ -- transaction (set_config with is_local = true: transaction-local, never
41
+ -- session-level).
42
+ --
43
+ -- LOCKING. Each ALTER TABLE takes an ACCESS EXCLUSIVE lock held until COMMIT, so
44
+ -- no concurrent write can slip a duplicate in while an index is dropped and
45
+ -- rebuilt. The three column changes rewrite their tables (the sharing tables
46
+ -- are small: one row per membership, invite and grant).
47
+ -- =============================================================================
48
+
49
+ BEGIN;
50
+ SELECT set_config('app.rls_bypass', 'on', true);
51
+
52
+ -- Dependents of grants.grantee_kind: the two partial unique indexes and the CHECK.
53
+ DROP INDEX "grants_active_user_uniq_idx";
54
+ DROP INDEX "grants_active_group_uniq_idx";
55
+ ALTER TABLE "grants" DROP CONSTRAINT "grants_grantee_consistency_check";
56
+
57
+ -- AlterTable (the default is an enum literal: drop it first, set the text one after)
58
+ ALTER TABLE "group_members" ALTER COLUMN "role" DROP DEFAULT;
59
+ ALTER TABLE "group_members" ALTER COLUMN "role" TYPE TEXT USING "role"::text;
60
+ ALTER TABLE "group_members" ALTER COLUMN "role" SET DEFAULT 'viewer';
61
+
62
+ -- AlterTable
63
+ ALTER TABLE "group_invites" ALTER COLUMN "role" DROP DEFAULT;
64
+ ALTER TABLE "group_invites" ALTER COLUMN "role" TYPE TEXT USING "role"::text;
65
+ ALTER TABLE "group_invites" ALTER COLUMN "role" SET DEFAULT 'viewer';
66
+
67
+ -- AlterTable
68
+ ALTER TABLE "grants" ALTER COLUMN "grantee_kind" TYPE TEXT USING "grantee_kind"::text;
69
+ ALTER TABLE "grants" ADD COLUMN "grantee_ref" TEXT;
70
+
71
+ -- DropEnum
72
+ DROP TYPE "GroupRole";
73
+
74
+ -- DropEnum
75
+ DROP TYPE "GrantGranteeKind";
76
+
77
+ -- CreateIndex (partial: one active grant per resource and user)
78
+ CREATE UNIQUE INDEX "grants_active_user_uniq_idx" ON "grants"("resource_type", "resource_id", "grantee_user_id") WHERE "grantee_kind" = 'user' AND "revoked_at" IS NULL;
79
+
80
+ -- CreateIndex (partial: one active grant per resource and group)
81
+ CREATE UNIQUE INDEX "grants_active_group_uniq_idx" ON "grants"("resource_type", "resource_id", "grantee_group_id") WHERE "grantee_kind" = 'group' AND "revoked_at" IS NULL;
82
+
83
+ -- CreateIndex (partial: one active grant per resource, app-registered kind and reference)
84
+ CREATE UNIQUE INDEX "grants_active_ref_uniq_idx" ON "grants"("resource_type", "resource_id", "grantee_kind", "grantee_ref") WHERE "grantee_kind" NOT IN ('user', 'group', 'link') AND "revoked_at" IS NULL;
85
+
86
+ -- AddCheckConstraint (the grantee columns match the grantee kind)
87
+ ALTER TABLE "grants" ADD CONSTRAINT "grants_grantee_consistency_check" CHECK (
88
+ ("grantee_kind" = 'user' AND "grantee_user_id" IS NOT NULL AND "grantee_group_id" IS NULL AND "link_token_hash" IS NULL AND "grantee_ref" IS NULL)
89
+ OR
90
+ ("grantee_kind" = 'group' AND "grantee_user_id" IS NULL AND "grantee_group_id" IS NOT NULL AND "link_token_hash" IS NULL AND "grantee_ref" IS NULL)
91
+ OR
92
+ ("grantee_kind" = 'link' AND "grantee_user_id" IS NULL AND "grantee_group_id" IS NULL AND "link_token_hash" IS NOT NULL AND "grantee_ref" IS NULL)
93
+ OR
94
+ ("grantee_kind" NOT IN ('user', 'group', 'link') AND "grantee_ref" IS NOT NULL AND "grantee_user_id" IS NULL AND "grantee_group_id" IS NULL AND "link_token_hash" IS NULL)
95
+ );
96
+
97
+ COMMIT;
@@ -0,0 +1,379 @@
1
+ [
2
+ {
3
+ "dir": "0001_initial",
4
+ "id": "0001_initial",
5
+ "requires": [],
6
+ "sha256": "755688a7d01e70c97f497efdbd5a72adecf9404ba20fc5fc15c748c2bb7d0d2e",
7
+ "since": "1.0.0",
8
+ "slice": "identity",
9
+ "touches": [
10
+ "settings",
11
+ "storage"
12
+ ]
13
+ },
14
+ {
15
+ "dir": "0002_add_personal_access_tokens",
16
+ "id": "0002_add_personal_access_tokens",
17
+ "requires": [],
18
+ "sha256": "696a7bc4a974a2b0142f7183fe795d98b21a353f402ad21b3d5a5e79c06800cd",
19
+ "since": "1.0.0",
20
+ "slice": "identity"
21
+ },
22
+ {
23
+ "dir": "0003_add_credentials",
24
+ "id": "0003_add_credentials",
25
+ "requires": [
26
+ "identity"
27
+ ],
28
+ "sha256": "825d08352a40f595a16e3086862c7d0eeb0333c8643119a618666627e87bcb32",
29
+ "since": "1.0.0",
30
+ "slice": "credentials"
31
+ },
32
+ {
33
+ "dir": "0004_add_notification_deliveries",
34
+ "id": "0004_add_notification_deliveries",
35
+ "requires": [
36
+ "identity"
37
+ ],
38
+ "sha256": "242c3fc6a3a8c68c3515b0a5f19c819e48dd0b8ee3719007a24ffcbf2d8eacdc",
39
+ "since": "1.0.0",
40
+ "slice": "notifications"
41
+ },
42
+ {
43
+ "dir": "0005_drop_stale_uuid_defaults",
44
+ "id": "0005_drop_stale_uuid_defaults",
45
+ "requires": [],
46
+ "sha256": "34e3ee9325646bd77d3123090826798c7c7fc7b57938ee7cee6584a03d24d6ad",
47
+ "since": "1.0.0",
48
+ "slice": "identity",
49
+ "touches": [
50
+ "settings",
51
+ "storage"
52
+ ]
53
+ },
54
+ {
55
+ "dir": "0006_add_notifications",
56
+ "id": "0006_add_notifications",
57
+ "requires": [
58
+ "identity"
59
+ ],
60
+ "sha256": "56a8156d13b09f0bdfdaa6033c5cf3f0adbd0191a53680eeb2376e13a637c383",
61
+ "since": "1.0.0",
62
+ "slice": "notifications"
63
+ },
64
+ {
65
+ "dir": "0007_add_push_subscriptions",
66
+ "id": "0007_add_push_subscriptions",
67
+ "requires": [
68
+ "identity"
69
+ ],
70
+ "sha256": "7bfd542911735beae1152c3584e4ae8e131e37413d6d59054a29e9d4ab967e85",
71
+ "since": "1.0.0",
72
+ "slice": "notifications"
73
+ },
74
+ {
75
+ "dir": "0008_add_jobs",
76
+ "id": "0008_add_jobs",
77
+ "requires": [
78
+ "identity"
79
+ ],
80
+ "sha256": "29ca499509b06bc8ee11c09ed86d8d5b2385bcc9aa48fe597c0efc1ccb40febf",
81
+ "since": "1.0.0",
82
+ "slice": "jobs"
83
+ },
84
+ {
85
+ "dir": "0009_add_worker_nodes",
86
+ "id": "0009_add_worker_nodes",
87
+ "requires": [
88
+ "identity"
89
+ ],
90
+ "sha256": "d26c1412c6e293f562422bbe292e9e833f1506cb1dc3466f78eacd7bc34ea398",
91
+ "since": "1.0.0",
92
+ "slice": "jobs"
93
+ },
94
+ {
95
+ "dir": "0010_add_database_backup_runs",
96
+ "id": "0010_add_database_backup_runs",
97
+ "requires": [
98
+ "identity"
99
+ ],
100
+ "sha256": "e6a2c130a155748768148a1993b7e47cabca09236a4eeb31f8793448b389077a",
101
+ "since": "1.0.0",
102
+ "slice": "db-backup"
103
+ },
104
+ {
105
+ "dir": "0011_add_notification_broadcasts",
106
+ "id": "0011_add_notification_broadcasts",
107
+ "requires": [
108
+ "identity"
109
+ ],
110
+ "sha256": "d218ee33f855f575c6633cf0d9a709cb3e0db69a83ff2d38b48c501e730d4370",
111
+ "since": "1.0.0",
112
+ "slice": "notifications"
113
+ },
114
+ {
115
+ "dir": "0012_add_backup_run_job_link",
116
+ "id": "0012_add_backup_run_job_link",
117
+ "requires": [
118
+ "identity",
119
+ "jobs"
120
+ ],
121
+ "sha256": "1a11a1bed116e303136bd1f7ee5065f3b470cb2cb04bb33d0c083b3dfec37144",
122
+ "since": "1.0.0",
123
+ "slice": "db-backup",
124
+ "touches": [
125
+ "jobs"
126
+ ]
127
+ },
128
+ {
129
+ "dir": "0013_add_job_node_secrets",
130
+ "id": "0013_add_job_node_secrets",
131
+ "requires": [
132
+ "identity"
133
+ ],
134
+ "sha256": "309f4a371c0d5ed62b140a10acb28a705fc92c8a1412551fc07ac20b9cb77f0a",
135
+ "since": "1.0.0",
136
+ "slice": "jobs"
137
+ },
138
+ {
139
+ "dir": "0014_add_backup_run_pg_dump_version",
140
+ "id": "0014_add_backup_run_pg_dump_version",
141
+ "requires": [],
142
+ "sha256": "966c2e2af657739d7e4e447761bf93635c7e8bd41dbc31494f7118ffb4e8a67c",
143
+ "since": "1.0.0",
144
+ "slice": "db-backup"
145
+ },
146
+ {
147
+ "dir": "0015_add_job_claim_token",
148
+ "id": "0015_add_job_claim_token",
149
+ "requires": [],
150
+ "sha256": "22815ddb2bd4fe39edb4ec6bb11709a15df887c46c261d8d567a3a2cae7f2047",
151
+ "since": "1.0.0",
152
+ "slice": "jobs"
153
+ },
154
+ {
155
+ "dir": "0016_add_ai_platform",
156
+ "id": "0016_add_ai_platform",
157
+ "requires": [
158
+ "identity"
159
+ ],
160
+ "sha256": "89bf4f29db08eb4261b10c5bd64faa61b65d73f524af70851e019fe264fe7617",
161
+ "since": "1.0.0",
162
+ "slice": "ai"
163
+ },
164
+ {
165
+ "dir": "0017_revoke_viewer_ai_use",
166
+ "id": "0017_revoke_viewer_ai_use",
167
+ "requires": [
168
+ "identity"
169
+ ],
170
+ "sha256": "85b27bc2d52666f8a3babc5d0abfa9f589f1844869d79fbbdaa7935e6d49b3df",
171
+ "since": "1.0.0",
172
+ "slice": "ai",
173
+ "touches": [
174
+ "identity"
175
+ ]
176
+ },
177
+ {
178
+ "dir": "0018_add_user_credentials",
179
+ "id": "0018_add_user_credentials",
180
+ "requires": [
181
+ "identity"
182
+ ],
183
+ "sha256": "360316e38bf4af727fc095c65e4cac246108b9e91fc3c184646161ce7a5859fb",
184
+ "since": "1.0.0",
185
+ "slice": "credentials"
186
+ },
187
+ {
188
+ "dir": "0019_add_device_session_credential_link",
189
+ "id": "0019_add_device_session_credential_link",
190
+ "requires": [],
191
+ "sha256": "e2f26e504872850a0fe799151efdf1df4f153e51db3d686f5ad7e603b1796326",
192
+ "since": "1.0.0",
193
+ "slice": "identity"
194
+ },
195
+ {
196
+ "dir": "0020_add_worker_node_vitals",
197
+ "id": "0020_add_worker_node_vitals",
198
+ "requires": [],
199
+ "sha256": "978649d6700635017d22d08a492845307659b8a2f6fdb46e230a887d966a2fe4",
200
+ "since": "1.0.0",
201
+ "slice": "jobs"
202
+ },
203
+ {
204
+ "dir": "0021_add_job_trace_context",
205
+ "id": "0021_add_job_trace_context",
206
+ "requires": [],
207
+ "sha256": "4d54077792406901812bb76d697e6708c9058245ec1003c49bc8983b53b01302",
208
+ "since": "1.0.0",
209
+ "slice": "jobs"
210
+ },
211
+ {
212
+ "dir": "0022_add_retention_created_at_indexes",
213
+ "id": "0022_add_retention_created_at_indexes",
214
+ "requires": [
215
+ "ai"
216
+ ],
217
+ "sha256": "f7bc4ab3ec81c55261752c61e48e750faf7affb7202635f8e051934a9410c32f",
218
+ "since": "1.0.0",
219
+ "slice": "notifications",
220
+ "touches": [
221
+ "ai"
222
+ ]
223
+ },
224
+ {
225
+ "dir": "0023_add_organizations",
226
+ "id": "0023_add_organizations",
227
+ "requires": [],
228
+ "sha256": "68bed4bfebd7c94600546943bc66dd6245fd912d0cf5583f3485267c89e0faed",
229
+ "since": "1.0.0",
230
+ "slice": "identity"
231
+ },
232
+ {
233
+ "dir": "0024_split_system_org_roles",
234
+ "id": "0024_split_system_org_roles",
235
+ "requires": [],
236
+ "sha256": "11484b30fdabf64abb14ec99e4d218c1897f5692130bd5a04720d08b905e1332",
237
+ "since": "1.0.0",
238
+ "slice": "identity"
239
+ },
240
+ {
241
+ "dir": "0025_org_scoped_rls",
242
+ "id": "0025_org_scoped_rls",
243
+ "requires": [
244
+ "identity",
245
+ "ai"
246
+ ],
247
+ "rls": true,
248
+ "sha256": "f6a0a61d07e5b91767b9bf18824ae1872e236f566877282c4cb2332f024dd636",
249
+ "since": "1.0.0",
250
+ "slice": "storage",
251
+ "touches": [
252
+ "ai",
253
+ "identity"
254
+ ]
255
+ },
256
+ {
257
+ "dir": "0026_add_groups",
258
+ "id": "0026_add_groups",
259
+ "requires": [
260
+ "identity"
261
+ ],
262
+ "rls": true,
263
+ "sha256": "93e97e6b72578a89bf5232a036ecfe6a067c883c7f3c9f091bfdc00475c6e714",
264
+ "since": "1.0.0",
265
+ "slice": "sharing"
266
+ },
267
+ {
268
+ "dir": "0027_add_grants",
269
+ "id": "0027_add_grants",
270
+ "requires": [
271
+ "identity",
272
+ "sharing"
273
+ ],
274
+ "rls": true,
275
+ "sha256": "2e2d5515f131c104ee7f4e5b2ee4d76fbf5409e500da0b412edd4bad0b71c6da",
276
+ "since": "1.0.0",
277
+ "slice": "sharing"
278
+ },
279
+ {
280
+ "dir": "0028_add_org_settings",
281
+ "id": "0028_add_org_settings",
282
+ "requires": [
283
+ "identity"
284
+ ],
285
+ "rls": true,
286
+ "sha256": "e4d811ab7c5446d905e5e4fbb3ce2b94e06d82488f916352a2a413cdc55f94a4",
287
+ "since": "1.0.0",
288
+ "slice": "settings"
289
+ },
290
+ {
291
+ "dir": "0029_add_org_credentials",
292
+ "id": "0029_add_org_credentials",
293
+ "requires": [
294
+ "identity"
295
+ ],
296
+ "rls": true,
297
+ "sha256": "fa5db76f0081149b5716f763166d2099b134857f27f15f36f9a4ea33e1a0806e",
298
+ "since": "1.0.0",
299
+ "slice": "credentials"
300
+ },
301
+ {
302
+ "dir": "0030_add_jobs_org_id",
303
+ "id": "0030_add_jobs_org_id",
304
+ "requires": [
305
+ "identity"
306
+ ],
307
+ "sha256": "26556306864283a8feb2233692d5fcb66b9f38d2671cfeb66e2e094d4c8f84bb",
308
+ "since": "1.0.0",
309
+ "slice": "jobs"
310
+ },
311
+ {
312
+ "dir": "0031_add_jobs_org_id_status_index",
313
+ "id": "0031_add_jobs_org_id_status_index",
314
+ "requires": [
315
+ "identity"
316
+ ],
317
+ "sha256": "434d69d4112bcb429de609942a851e08c49793212a538adcc5a2481d1f8b0bb0",
318
+ "since": "1.0.0",
319
+ "slice": "jobs"
320
+ },
321
+ {
322
+ "dir": "0032_add_broadcast_target_org",
323
+ "id": "0032_add_broadcast_target_org",
324
+ "requires": [],
325
+ "sha256": "9f266799b1e3769233ea72873ce55394340da9f8ef1312a7e0b7b2ae410152e2",
326
+ "since": "1.0.0",
327
+ "slice": "notifications"
328
+ },
329
+ {
330
+ "dir": "0033_add_android_app",
331
+ "id": "0033_add_android_app",
332
+ "requires": [
333
+ "identity",
334
+ "notifications"
335
+ ],
336
+ "sha256": "7f343a96417825c1372388c199ad2dbecfcf8632a4ebc5080afb29c82109b719",
337
+ "since": "1.0.0",
338
+ "slice": "android-app"
339
+ },
340
+ {
341
+ "dir": "0034_open_job_reason",
342
+ "id": "0034_open_job_reason",
343
+ "requires": [],
344
+ "sha256": "31b16d3aeddb81840ad34ec2df29aeaf065b1f5082c839bbc31c94c90a205d77",
345
+ "since": "1.0.0",
346
+ "slice": "jobs"
347
+ },
348
+ {
349
+ "dir": "0035_ai_usage_feature",
350
+ "id": "0035_ai_usage_feature",
351
+ "requires": [],
352
+ "sha256": "ad7322539e971a2248cc2f73a12e188453a3312cc1a04d168bc4e7ddd68e5752",
353
+ "since": "1.0.0",
354
+ "slice": "ai"
355
+ },
356
+ {
357
+ "dir": "0036_add_app_metadata",
358
+ "id": "0036_add_app_metadata",
359
+ "requires": [
360
+ "ai",
361
+ "sharing"
362
+ ],
363
+ "sha256": "17e727075b3d02d129f60cdebf156b9736113e3b689ff35513c474c520185f75",
364
+ "since": "1.0.0",
365
+ "slice": "identity",
366
+ "touches": [
367
+ "ai",
368
+ "sharing"
369
+ ]
370
+ },
371
+ {
372
+ "dir": "0037_open_sharing_vocabularies",
373
+ "id": "0037_open_sharing_vocabularies",
374
+ "requires": [],
375
+ "sha256": "4b7047703bacab08af629004ff569e250869355acd03795f17d1b1e5b277c5e6",
376
+ "since": "1.0.0",
377
+ "slice": "sharing"
378
+ }
379
+ ]