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

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 +88 -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,215 @@
1
+ -- =============================================================================
2
+ -- org_id on tenant-scoped tables and row-level security (PP-6.5, issue #725)
3
+ -- =============================================================================
4
+ --
5
+ -- The database enforces organization isolation (ADR 0002 D5). Four tables are
6
+ -- org-owned and get a NOT NULL (or, for usage events, nullable) org_id plus a
7
+ -- FORCEd policy keyed on transaction-local settings; audit_events gets a
8
+ -- nullable org_id and no policy in this release.
9
+ --
10
+ -- storage_objects org_id NOT NULL, FK RESTRICT, RLS forced
11
+ -- storage_object_chunks org_id NOT NULL, FK RESTRICT, RLS forced; its link to
12
+ -- the parent is now COMPOSITE (object_id, org_id) ->
13
+ -- storage_objects (id, org_id): a foreign-key check
14
+ -- bypasses row-level security, so a plain key would let
15
+ -- one organization store a reference to another's row
16
+ -- ai_runs org_id NOT NULL, FK RESTRICT, RLS forced. Restrict, not
17
+ -- SetNull: the column is NOT NULL, so history cannot
18
+ -- survive an organization's deletion by nulling it
19
+ -- ai_usage_events org_id nullable, FK SET NULL, RLS forced. Nullable so
20
+ -- a deployment-wide event (a catalogue sync) and the
21
+ -- history of a deleted organization survive; such a row
22
+ -- is visible to the system (bypass) client only
23
+ -- audit_events org_id nullable, FK SET NULL, NO row-level security
24
+ --
25
+ -- BACKFILL. Every existing row of the three NOT NULL tables, and of
26
+ -- ai_usage_events, goes to the default organization (created by
27
+ -- 0023_add_organizations on every database). Two exceptions keep NULL:
28
+ --
29
+ -- - ai_usage_events with operation = 'catalog': a catalogue sync is run by the
30
+ -- deployment, not by an organization, and new ones are written without one.
31
+ -- - audit_events whose action is a SYSTEM action. Everything else (including
32
+ -- an action this migration does not know) belongs to the default
33
+ -- organization. System action prefixes:
34
+ -- system_settings: email_settings: push_config: storage_config:
35
+ -- ai_config: ai_model: ai_catalog: telemetry: support_bundle:
36
+ -- maintenance: db_restore: db_backup: allowlist: users: nodes:
37
+ -- jobs. notification_broadcast. settings. retention.
38
+ --
39
+ -- The backfill runs in an explicit transaction that raises the bypass flag
40
+ -- (the pattern ADR 0002 prescribes for any migration that touches an RLS
41
+ -- table: under FORCE a bare UPDATE would silently change zero rows). The
42
+ -- policies are enabled AFTER it, so on a first apply the flag is belt and
43
+ -- braces; it keeps the pattern in front of the next reader.
44
+ --
45
+ -- THE APPLICATION ROLE MUST BE NOSUPERUSER NOBYPASSRLS, or every policy below is
46
+ -- inert (a superuser sees every row of a FORCEd table). The block at the end
47
+ -- warns when the role applying this migration is not an ordinary role. The
48
+ -- Doctor check `db.rls_role` fails while any FORCEd table exists and the
49
+ -- application role is a superuser or has BYPASSRLS.
50
+ --
51
+ -- DEPLOYMENT NOTE. The statements below take short locks and build the new
52
+ -- indexes inside the migration transaction; on a very large storage_objects or
53
+ -- ai_usage_events table pre-create the indexes with CREATE INDEX CONCURRENTLY
54
+ -- IF NOT EXISTS under the same names before applying.
55
+ -- =============================================================================
56
+
57
+ -- DropForeignKey
58
+ ALTER TABLE "storage_object_chunks" DROP CONSTRAINT "storage_object_chunks_object_id_fkey";
59
+
60
+ -- AlterTable (expand: nullable first, so existing rows can be backfilled)
61
+ ALTER TABLE "storage_objects" ADD COLUMN "org_id" UUID;
62
+ ALTER TABLE "storage_object_chunks" ADD COLUMN "org_id" UUID;
63
+ ALTER TABLE "ai_runs" ADD COLUMN "org_id" UUID;
64
+ ALTER TABLE "ai_usage_events" ADD COLUMN "org_id" UUID;
65
+ ALTER TABLE "audit_events" ADD COLUMN "org_id" UUID;
66
+
67
+ -- Backfill, as system work.
68
+ BEGIN;
69
+ SELECT set_config('app.rls_bypass', 'on', true);
70
+
71
+ UPDATE "storage_objects"
72
+ SET "org_id" = (SELECT "id" FROM "organizations" WHERE "is_default")
73
+ WHERE "org_id" IS NULL;
74
+
75
+ UPDATE "storage_object_chunks" AS c
76
+ SET "org_id" = o."org_id"
77
+ FROM "storage_objects" AS o
78
+ WHERE c."object_id" = o."id" AND c."org_id" IS NULL;
79
+
80
+ UPDATE "ai_runs"
81
+ SET "org_id" = (SELECT "id" FROM "organizations" WHERE "is_default")
82
+ WHERE "org_id" IS NULL;
83
+
84
+ UPDATE "ai_usage_events"
85
+ SET "org_id" = (SELECT "id" FROM "organizations" WHERE "is_default")
86
+ WHERE "org_id" IS NULL AND "operation" <> 'catalog';
87
+
88
+ UPDATE "audit_events"
89
+ SET "org_id" = (SELECT "id" FROM "organizations" WHERE "is_default")
90
+ WHERE "org_id" IS NULL
91
+ AND "action" NOT LIKE 'system\_settings:%'
92
+ AND "action" NOT LIKE 'email\_settings:%'
93
+ AND "action" NOT LIKE 'push\_config:%'
94
+ AND "action" NOT LIKE 'storage\_config:%'
95
+ AND "action" NOT LIKE 'ai\_config:%'
96
+ AND "action" NOT LIKE 'ai\_model:%'
97
+ AND "action" NOT LIKE 'ai\_catalog:%'
98
+ AND "action" NOT LIKE 'telemetry:%'
99
+ AND "action" NOT LIKE 'support\_bundle:%'
100
+ AND "action" NOT LIKE 'maintenance:%'
101
+ AND "action" NOT LIKE 'db\_restore:%'
102
+ AND "action" NOT LIKE 'db\_backup:%'
103
+ AND "action" NOT LIKE 'allowlist:%'
104
+ AND "action" NOT LIKE 'users:%'
105
+ AND "action" NOT LIKE 'nodes:%'
106
+ AND "action" NOT LIKE 'jobs.%'
107
+ AND "action" NOT LIKE 'notification\_broadcast.%'
108
+ AND "action" NOT LIKE 'settings.%'
109
+ AND "action" NOT LIKE 'retention.%';
110
+ COMMIT;
111
+
112
+ -- AlterTable (contract: the three tables whose column is NOT NULL)
113
+ ALTER TABLE "storage_objects" ALTER COLUMN "org_id" SET NOT NULL;
114
+ ALTER TABLE "storage_object_chunks" ALTER COLUMN "org_id" SET NOT NULL;
115
+ ALTER TABLE "ai_runs" ALTER COLUMN "org_id" SET NOT NULL;
116
+
117
+ -- CreateIndex
118
+ CREATE UNIQUE INDEX "storage_objects_id_org_id_key" ON "storage_objects"("id", "org_id");
119
+
120
+ -- CreateIndex
121
+ CREATE INDEX "storage_objects_org_id_uploaded_by_id_created_at_idx" ON "storage_objects"("org_id", "uploaded_by_id", "created_at");
122
+
123
+ -- CreateIndex
124
+ CREATE INDEX "storage_objects_org_id_status_idx" ON "storage_objects"("org_id", "status");
125
+
126
+ -- CreateIndex
127
+ CREATE INDEX "storage_object_chunks_org_id_object_id_idx" ON "storage_object_chunks"("org_id", "object_id");
128
+
129
+ -- CreateIndex
130
+ CREATE INDEX "ai_runs_org_id_user_id_created_at_idx" ON "ai_runs"("org_id", "user_id", "created_at");
131
+
132
+ -- CreateIndex
133
+ CREATE INDEX "ai_usage_events_org_id_user_id_created_at_idx" ON "ai_usage_events"("org_id", "user_id", "created_at");
134
+
135
+ -- CreateIndex
136
+ CREATE INDEX "ai_usage_events_org_id_created_at_idx" ON "ai_usage_events"("org_id", "created_at");
137
+
138
+ -- CreateIndex
139
+ CREATE INDEX "audit_events_org_id_created_at_idx" ON "audit_events"("org_id", "created_at");
140
+
141
+ -- AddForeignKey
142
+ ALTER TABLE "storage_objects" ADD CONSTRAINT "storage_objects_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
143
+
144
+ -- AddForeignKey (composite: object_id and org_id together)
145
+ ALTER TABLE "storage_object_chunks" ADD CONSTRAINT "storage_object_chunks_object_id_org_id_fkey" FOREIGN KEY ("object_id", "org_id") REFERENCES "storage_objects"("id", "org_id") ON DELETE CASCADE ON UPDATE CASCADE;
146
+
147
+ -- AddForeignKey
148
+ ALTER TABLE "storage_object_chunks" ADD CONSTRAINT "storage_object_chunks_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
149
+
150
+ -- AddForeignKey
151
+ ALTER TABLE "ai_runs" ADD CONSTRAINT "ai_runs_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
152
+
153
+ -- AddForeignKey
154
+ ALTER TABLE "ai_usage_events" ADD CONSTRAINT "ai_usage_events_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;
155
+
156
+ -- AddForeignKey
157
+ ALTER TABLE "audit_events" ADD CONSTRAINT "audit_events_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE;
158
+
159
+ -- =============================================================================
160
+ -- Row-level security. Prisma's schema language cannot express any of this and
161
+ -- its diff ignores it; the policy names are listed in RLS_POLICIES
162
+ -- (packages/platform-db/src/drift/rls-policies.ts) and asserted against
163
+ -- pg_policies by the drift test.
164
+ --
165
+ -- NULLIF(..., '') is required: current_setting(name, true) is NULL on a fresh
166
+ -- connection but the EMPTY STRING on a connection that has ever held a
167
+ -- transaction-local value, and a bare ''::uuid cast errors.
168
+ -- FORCE applies the policy to the table owner, which is the application role:
169
+ -- unscoped code sees no rows and cannot insert (it fails closed).
170
+ -- =============================================================================
171
+
172
+ ALTER TABLE "storage_objects" ENABLE ROW LEVEL SECURITY;
173
+ ALTER TABLE "storage_objects" FORCE ROW LEVEL SECURITY;
174
+ CREATE POLICY "storage_objects_org_isolation" ON "storage_objects"
175
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
176
+ OR current_setting('app.rls_bypass', true) = 'on')
177
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
178
+ OR current_setting('app.rls_bypass', true) = 'on');
179
+
180
+ ALTER TABLE "storage_object_chunks" ENABLE ROW LEVEL SECURITY;
181
+ ALTER TABLE "storage_object_chunks" FORCE ROW LEVEL SECURITY;
182
+ CREATE POLICY "storage_object_chunks_org_isolation" ON "storage_object_chunks"
183
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
184
+ OR current_setting('app.rls_bypass', true) = 'on')
185
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
186
+ OR current_setting('app.rls_bypass', true) = 'on');
187
+
188
+ ALTER TABLE "ai_runs" ENABLE ROW LEVEL SECURITY;
189
+ ALTER TABLE "ai_runs" FORCE ROW LEVEL SECURITY;
190
+ CREATE POLICY "ai_runs_org_isolation" ON "ai_runs"
191
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
192
+ OR current_setting('app.rls_bypass', true) = 'on')
193
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
194
+ OR current_setting('app.rls_bypass', true) = 'on');
195
+
196
+ ALTER TABLE "ai_usage_events" ENABLE ROW LEVEL SECURITY;
197
+ ALTER TABLE "ai_usage_events" FORCE ROW LEVEL SECURITY;
198
+ CREATE POLICY "ai_usage_events_org_isolation" ON "ai_usage_events"
199
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
200
+ OR current_setting('app.rls_bypass', true) = 'on')
201
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
202
+ OR current_setting('app.rls_bypass', true) = 'on');
203
+
204
+ -- A policy is inert for a superuser or a BYPASSRLS role. Warn (do not fail:
205
+ -- the migration role may legitimately differ from the application role).
206
+ DO $$
207
+ DECLARE
208
+ inert boolean;
209
+ BEGIN
210
+ SELECT rolsuper OR rolbypassrls INTO inert FROM pg_roles WHERE rolname = current_user;
211
+ IF inert THEN
212
+ RAISE WARNING 'row-level security is INERT for role "%": it is a superuser or has BYPASSRLS. Run the application as an ordinary role (NOSUPERUSER NOBYPASSRLS) or tenant isolation is not enforced.', current_user;
213
+ END IF;
214
+ END
215
+ $$;
@@ -0,0 +1,165 @@
1
+ -- =============================================================================
2
+ -- Groups: sharing groups inside an organization (PP-7.1, issue #728)
3
+ -- =============================================================================
4
+ --
5
+ -- Three org-owned tables, each with a NOT NULL org_id and a FORCEd
6
+ -- <table>_org_isolation policy copied from 0025_org_scoped_rls:
7
+ --
8
+ -- groups a set of users inside ONE organization that can own content
9
+ -- (spec decision D6: a group is never a tenant)
10
+ -- group_members one row per (group, user), with a GroupRole
11
+ -- group_invites an invitation of an e-mail address to a group
12
+ --
13
+ -- ORG CONSISTENCY. A member and an invite reach their group through the
14
+ -- COMPOSITE key (group_id, org_id) -> groups (id, org_id), with the supporting
15
+ -- UNIQUE (id, org_id) on groups: a foreign-key check bypasses row-level
16
+ -- security, so a plain key would let one organization store a reference to
17
+ -- another's group, and the composite key also guarantees that
18
+ -- group_members.org_id and group_invites.org_id equal groups.org_id.
19
+ --
20
+ -- RAW-SQL INDEX (intentional schema drift, listed in raw-sql-indexes.json):
21
+ -- group_invites_pending_uniq_idx at most one PENDING invite per (group,
22
+ -- email); a declined, revoked or accepted
23
+ -- invite does not block a new one. Prisma
24
+ -- cannot express a partial unique index.
25
+ --
26
+ -- No backfill: the tables are new, so the policies are enabled at once.
27
+ -- =============================================================================
28
+
29
+ -- CreateEnum
30
+ CREATE TYPE "GroupRole" AS ENUM ('admin', 'editor', 'viewer');
31
+
32
+ -- CreateTable
33
+ CREATE TABLE "groups" (
34
+ "id" UUID NOT NULL,
35
+ "org_id" UUID NOT NULL,
36
+ "name" TEXT NOT NULL,
37
+ "description" TEXT,
38
+ "metadata" JSONB,
39
+ "created_by_id" UUID,
40
+ "version" INTEGER NOT NULL DEFAULT 1,
41
+ "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
42
+ "updated_at" TIMESTAMPTZ NOT NULL,
43
+
44
+ CONSTRAINT "groups_pkey" PRIMARY KEY ("id")
45
+ );
46
+
47
+ -- CreateTable
48
+ CREATE TABLE "group_members" (
49
+ "id" UUID NOT NULL,
50
+ "group_id" UUID NOT NULL,
51
+ "user_id" UUID NOT NULL,
52
+ "org_id" UUID NOT NULL,
53
+ "role" "GroupRole" NOT NULL DEFAULT 'viewer',
54
+ "added_by_id" UUID,
55
+ "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
56
+ "updated_at" TIMESTAMPTZ NOT NULL,
57
+
58
+ CONSTRAINT "group_members_pkey" PRIMARY KEY ("id")
59
+ );
60
+
61
+ -- CreateTable
62
+ CREATE TABLE "group_invites" (
63
+ "id" UUID NOT NULL,
64
+ "group_id" UUID NOT NULL,
65
+ "org_id" UUID NOT NULL,
66
+ "email" TEXT NOT NULL,
67
+ "role" "GroupRole" NOT NULL DEFAULT 'viewer',
68
+ "invited_by_id" UUID,
69
+ "expires_at" TIMESTAMPTZ,
70
+ "accepted_at" TIMESTAMPTZ,
71
+ "accepted_by_id" UUID,
72
+ "declined_at" TIMESTAMPTZ,
73
+ "revoked_at" TIMESTAMPTZ,
74
+ "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
75
+
76
+ CONSTRAINT "group_invites_pkey" PRIMARY KEY ("id")
77
+ );
78
+
79
+ -- CreateIndex
80
+ CREATE INDEX "groups_org_id_idx" ON "groups"("org_id");
81
+
82
+ -- CreateIndex
83
+ CREATE UNIQUE INDEX "groups_id_org_id_key" ON "groups"("id", "org_id");
84
+
85
+ -- CreateIndex
86
+ CREATE INDEX "group_members_user_id_idx" ON "group_members"("user_id");
87
+
88
+ -- CreateIndex
89
+ CREATE INDEX "group_members_org_id_idx" ON "group_members"("org_id");
90
+
91
+ -- CreateIndex
92
+ CREATE UNIQUE INDEX "group_members_group_id_user_id_key" ON "group_members"("group_id", "user_id");
93
+
94
+ -- CreateIndex
95
+ CREATE INDEX "group_invites_email_idx" ON "group_invites"("email");
96
+
97
+ -- CreateIndex
98
+ CREATE INDEX "group_invites_group_id_idx" ON "group_invites"("group_id");
99
+
100
+ -- CreateIndex
101
+ CREATE INDEX "group_invites_org_id_idx" ON "group_invites"("org_id");
102
+
103
+ -- AddForeignKey
104
+ ALTER TABLE "groups" ADD CONSTRAINT "groups_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
105
+
106
+ -- AddForeignKey
107
+ ALTER TABLE "groups" ADD CONSTRAINT "groups_created_by_id_fkey" FOREIGN KEY ("created_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
108
+
109
+ -- AddForeignKey
110
+ ALTER TABLE "group_members" ADD CONSTRAINT "group_members_group_id_org_id_fkey" FOREIGN KEY ("group_id", "org_id") REFERENCES "groups"("id", "org_id") ON DELETE CASCADE ON UPDATE CASCADE;
111
+
112
+ -- AddForeignKey
113
+ ALTER TABLE "group_members" ADD CONSTRAINT "group_members_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
114
+
115
+ -- AddForeignKey
116
+ ALTER TABLE "group_members" ADD CONSTRAINT "group_members_user_id_fkey" FOREIGN KEY ("user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
117
+
118
+ -- AddForeignKey
119
+ ALTER TABLE "group_members" ADD CONSTRAINT "group_members_added_by_id_fkey" FOREIGN KEY ("added_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
120
+
121
+ -- AddForeignKey
122
+ ALTER TABLE "group_invites" ADD CONSTRAINT "group_invites_group_id_org_id_fkey" FOREIGN KEY ("group_id", "org_id") REFERENCES "groups"("id", "org_id") ON DELETE CASCADE ON UPDATE CASCADE;
123
+
124
+ -- AddForeignKey
125
+ ALTER TABLE "group_invites" ADD CONSTRAINT "group_invites_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
126
+
127
+ -- AddForeignKey
128
+ ALTER TABLE "group_invites" ADD CONSTRAINT "group_invites_invited_by_id_fkey" FOREIGN KEY ("invited_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
129
+
130
+ -- AddForeignKey
131
+ ALTER TABLE "group_invites" ADD CONSTRAINT "group_invites_accepted_by_id_fkey" FOREIGN KEY ("accepted_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
132
+
133
+ -- CreateIndex (partial: one pending invite per group and address)
134
+ CREATE UNIQUE INDEX "group_invites_pending_uniq_idx" ON "group_invites"("group_id", "email") WHERE "accepted_at" IS NULL AND "declined_at" IS NULL AND "revoked_at" IS NULL;
135
+
136
+ -- =============================================================================
137
+ -- Row-level security, as in 0025_org_scoped_rls. The policy names are listed in
138
+ -- RLS_POLICIES (rls-policies.json) and asserted against pg_policies by the
139
+ -- drift test. NULLIF(..., '') because current_setting(name, true) is the empty
140
+ -- string on a connection that ever held a transaction-local value.
141
+ -- =============================================================================
142
+
143
+ ALTER TABLE "groups" ENABLE ROW LEVEL SECURITY;
144
+ ALTER TABLE "groups" FORCE ROW LEVEL SECURITY;
145
+ CREATE POLICY "groups_org_isolation" ON "groups"
146
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
147
+ OR current_setting('app.rls_bypass', true) = 'on')
148
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
149
+ OR current_setting('app.rls_bypass', true) = 'on');
150
+
151
+ ALTER TABLE "group_members" ENABLE ROW LEVEL SECURITY;
152
+ ALTER TABLE "group_members" FORCE ROW LEVEL SECURITY;
153
+ CREATE POLICY "group_members_org_isolation" ON "group_members"
154
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
155
+ OR current_setting('app.rls_bypass', true) = 'on')
156
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
157
+ OR current_setting('app.rls_bypass', true) = 'on');
158
+
159
+ ALTER TABLE "group_invites" ENABLE ROW LEVEL SECURITY;
160
+ ALTER TABLE "group_invites" FORCE ROW LEVEL SECURITY;
161
+ CREATE POLICY "group_invites_org_isolation" ON "group_invites"
162
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
163
+ OR current_setting('app.rls_bypass', true) = 'on')
164
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
165
+ OR current_setting('app.rls_bypass', true) = 'on');
@@ -0,0 +1,117 @@
1
+ -- =============================================================================
2
+ -- Grants: share one record with a user, a group or a link (PP-7.2, issue #729)
3
+ -- =============================================================================
4
+ --
5
+ -- One org-owned table, `grants`, with a NOT NULL org_id and a FORCEd
6
+ -- grants_org_isolation policy copied from 0025_org_scoped_rls.
7
+ --
8
+ -- POLYMORPHIC RESOURCE. (resource_type, resource_id) names a row of an app
9
+ -- table the platform cannot know, so there is no foreign key to it. Integrity
10
+ -- comes from GrantsService.deleteForResources() (inside the app's delete
11
+ -- transaction) and the sharing.grants.prune job.
12
+ --
13
+ -- ORG CONSISTENCY. A group grantee reaches its group through the COMPOSITE key
14
+ -- (grantee_group_id, org_id) -> groups (id, org_id): a foreign-key check
15
+ -- bypasses row-level security, so a plain key would let a grant name another
16
+ -- organization's group. A user is not org-scoped; the API checks that the
17
+ -- grantee is an active member of the resource's organization.
18
+ --
19
+ -- The link columns (link_token_hash, link_token_ciphertext, link_label) are
20
+ -- created now so #730 needs no migration.
21
+ --
22
+ -- RAW SQL (intentional schema drift: Prisma can express neither):
23
+ -- grants_active_user_uniq_idx at most one ACTIVE (unrevoked) grant per
24
+ -- (resource, user): the API upserts on it
25
+ -- grants_active_group_uniq_idx the same per (resource, group)
26
+ -- grants_grantee_consistency_check the grantee columns match grantee_kind
27
+ -- The two indexes are listed in raw-sql-indexes.json.
28
+ --
29
+ -- No backfill: the table is new, so the policy is enabled at once.
30
+ -- =============================================================================
31
+
32
+ -- CreateEnum
33
+ CREATE TYPE "GrantGranteeKind" AS ENUM ('user', 'group', 'link');
34
+
35
+ -- CreateTable
36
+ CREATE TABLE "grants" (
37
+ "id" UUID NOT NULL,
38
+ "org_id" UUID NOT NULL,
39
+ "resource_type" TEXT NOT NULL,
40
+ "resource_id" UUID NOT NULL,
41
+ "grantee_kind" "GrantGranteeKind" NOT NULL,
42
+ "grantee_user_id" UUID,
43
+ "grantee_group_id" UUID,
44
+ "role" TEXT NOT NULL,
45
+ "link_token_hash" TEXT,
46
+ "link_token_ciphertext" TEXT,
47
+ "link_label" TEXT,
48
+ "expires_at" TIMESTAMPTZ,
49
+ "revoked_at" TIMESTAMPTZ,
50
+ "revoked_by_id" UUID,
51
+ "granted_by_id" UUID,
52
+ "metadata" JSONB,
53
+ "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
54
+ "updated_at" TIMESTAMPTZ NOT NULL,
55
+
56
+ CONSTRAINT "grants_pkey" PRIMARY KEY ("id")
57
+ );
58
+
59
+ -- CreateIndex
60
+ CREATE UNIQUE INDEX "grants_link_token_hash_key" ON "grants"("link_token_hash");
61
+
62
+ -- CreateIndex
63
+ CREATE INDEX "grants_resource_type_resource_id_idx" ON "grants"("resource_type", "resource_id");
64
+
65
+ -- CreateIndex
66
+ CREATE INDEX "grants_grantee_user_id_idx" ON "grants"("grantee_user_id");
67
+
68
+ -- CreateIndex
69
+ CREATE INDEX "grants_grantee_group_id_idx" ON "grants"("grantee_group_id");
70
+
71
+ -- CreateIndex
72
+ CREATE INDEX "grants_org_id_idx" ON "grants"("org_id");
73
+
74
+ -- AddForeignKey
75
+ ALTER TABLE "grants" ADD CONSTRAINT "grants_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
76
+
77
+ -- AddForeignKey
78
+ ALTER TABLE "grants" ADD CONSTRAINT "grants_grantee_user_id_fkey" FOREIGN KEY ("grantee_user_id") REFERENCES "users"("id") ON DELETE CASCADE ON UPDATE CASCADE;
79
+
80
+ -- AddForeignKey
81
+ ALTER TABLE "grants" ADD CONSTRAINT "grants_grantee_group_id_org_id_fkey" FOREIGN KEY ("grantee_group_id", "org_id") REFERENCES "groups"("id", "org_id") ON DELETE CASCADE ON UPDATE CASCADE;
82
+
83
+ -- AddForeignKey
84
+ ALTER TABLE "grants" ADD CONSTRAINT "grants_revoked_by_id_fkey" FOREIGN KEY ("revoked_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
85
+
86
+ -- AddForeignKey
87
+ ALTER TABLE "grants" ADD CONSTRAINT "grants_granted_by_id_fkey" FOREIGN KEY ("granted_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
88
+
89
+ -- CreateIndex (partial: one active grant per resource and user)
90
+ 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;
91
+
92
+ -- CreateIndex (partial: one active grant per resource and group)
93
+ 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;
94
+
95
+ -- AddCheckConstraint (the grantee columns match the grantee kind)
96
+ ALTER TABLE "grants" ADD CONSTRAINT "grants_grantee_consistency_check" CHECK (
97
+ ("grantee_kind" = 'user' AND "grantee_user_id" IS NOT NULL AND "grantee_group_id" IS NULL AND "link_token_hash" IS NULL)
98
+ OR
99
+ ("grantee_kind" = 'group' AND "grantee_user_id" IS NULL AND "grantee_group_id" IS NOT NULL AND "link_token_hash" IS NULL)
100
+ OR
101
+ ("grantee_kind" = 'link' AND "grantee_user_id" IS NULL AND "grantee_group_id" IS NULL AND "link_token_hash" IS NOT NULL)
102
+ );
103
+
104
+ -- =============================================================================
105
+ -- Row-level security, as in 0025_org_scoped_rls. The policy name is listed in
106
+ -- RLS_POLICIES (rls-policies.json) and asserted against pg_policies by the
107
+ -- drift test. NULLIF(..., '') because current_setting(name, true) is the empty
108
+ -- string on a connection that ever held a transaction-local value.
109
+ -- =============================================================================
110
+
111
+ ALTER TABLE "grants" ENABLE ROW LEVEL SECURITY;
112
+ ALTER TABLE "grants" FORCE ROW LEVEL SECURITY;
113
+ CREATE POLICY "grants_org_isolation" ON "grants"
114
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
115
+ OR current_setting('app.rls_bypass', true) = 'on')
116
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
117
+ OR current_setting('app.rls_bypass', true) = 'on');
@@ -0,0 +1,49 @@
1
+ -- =============================================================================
2
+ -- Organization settings: one organization's overrides (PP-8.1, issue #733)
3
+ -- =============================================================================
4
+ --
5
+ -- One org-owned table, `org_settings`: one row per organization, holding only
6
+ -- the org-overridable fields of each system settings namespace that declares
7
+ -- an `org` block ({ "<namespace>": { "<field>": value } }), with its own
8
+ -- version for If-Match. NOT NULL org_id, ENABLE and FORCE ROW LEVEL SECURITY
9
+ -- and an org_settings_org_isolation policy copied from 0025_org_scoped_rls:
10
+ -- organization A's transaction never reads or writes organization B's row.
11
+ -- system_settings (deployment-level) and user_settings (user-owned) are
12
+ -- unchanged.
13
+ -- =============================================================================
14
+
15
+ -- CreateTable
16
+ CREATE TABLE "org_settings" (
17
+ "id" UUID NOT NULL,
18
+ "org_id" UUID NOT NULL,
19
+ "value" JSONB NOT NULL,
20
+ "version" INTEGER NOT NULL DEFAULT 1,
21
+ "updated_by_user_id" UUID,
22
+ "updated_at" TIMESTAMPTZ NOT NULL,
23
+
24
+ CONSTRAINT "org_settings_pkey" PRIMARY KEY ("id")
25
+ );
26
+
27
+ -- CreateIndex
28
+ CREATE UNIQUE INDEX "org_settings_org_id_key" ON "org_settings"("org_id");
29
+
30
+ -- AddForeignKey
31
+ ALTER TABLE "org_settings" ADD CONSTRAINT "org_settings_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
32
+
33
+ -- AddForeignKey
34
+ ALTER TABLE "org_settings" ADD CONSTRAINT "org_settings_updated_by_user_id_fkey" FOREIGN KEY ("updated_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
35
+
36
+ -- =============================================================================
37
+ -- Row-level security, as in 0025_org_scoped_rls. The policy name is listed in
38
+ -- RLS_POLICIES (rls-policies.json) and asserted against pg_policies by the
39
+ -- drift test. NULLIF(..., '') because current_setting(name, true) is the empty
40
+ -- string on a connection that ever held a transaction-local value.
41
+ -- =============================================================================
42
+
43
+ ALTER TABLE "org_settings" ENABLE ROW LEVEL SECURITY;
44
+ ALTER TABLE "org_settings" FORCE ROW LEVEL SECURITY;
45
+ CREATE POLICY "org_settings_org_isolation" ON "org_settings"
46
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
47
+ OR current_setting('app.rls_bypass', true) = 'on')
48
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
49
+ OR current_setting('app.rls_bypass', true) = 'on');
@@ -0,0 +1,54 @@
1
+ -- =============================================================================
2
+ -- Organization credentials: the org tier of the credential store (PP-8.8, #735)
3
+ -- =============================================================================
4
+ --
5
+ -- One org-owned table, mirroring user_credentials with an organization as the
6
+ -- owner: org_credentials, addressed by (org_id, purpose, name), each secret
7
+ -- encrypted under the org-bound sub-key domain org:<orgId>:<purpose>.
8
+ --
9
+ -- A plain composite UNIQUE suffices (all three columns are NOT NULL), so no
10
+ -- raw-SQL index. The table is tenant data with a NOT NULL org_id and a FORCEd
11
+ -- org_credentials_org_isolation policy copied from 0025_org_scoped_rls.
12
+ --
13
+ -- No backfill: the table is new, so the policy is enabled at once.
14
+ -- =============================================================================
15
+
16
+ -- CreateTable
17
+ CREATE TABLE "org_credentials" (
18
+ "id" UUID NOT NULL,
19
+ "org_id" UUID NOT NULL,
20
+ "purpose" TEXT NOT NULL,
21
+ "name" TEXT NOT NULL,
22
+ "secret" TEXT NOT NULL,
23
+ "hint" TEXT,
24
+ "label" TEXT,
25
+ "updated_by_user_id" UUID,
26
+ "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
27
+ "updated_at" TIMESTAMPTZ NOT NULL,
28
+
29
+ CONSTRAINT "org_credentials_pkey" PRIMARY KEY ("id")
30
+ );
31
+
32
+ -- CreateIndex
33
+ CREATE UNIQUE INDEX "org_credentials_org_id_purpose_name_key" ON "org_credentials"("org_id", "purpose", "name");
34
+
35
+ -- AddForeignKey
36
+ ALTER TABLE "org_credentials" ADD CONSTRAINT "org_credentials_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
37
+
38
+ -- AddForeignKey
39
+ ALTER TABLE "org_credentials" ADD CONSTRAINT "org_credentials_updated_by_user_id_fkey" FOREIGN KEY ("updated_by_user_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
40
+
41
+ -- =============================================================================
42
+ -- Row-level security, as in 0025_org_scoped_rls. The policy name is listed in
43
+ -- RLS_POLICIES (rls-policies.json) and asserted against pg_policies by the
44
+ -- drift test. NULLIF(..., '') because current_setting(name, true) is the empty
45
+ -- string on a connection that ever held a transaction-local value.
46
+ -- =============================================================================
47
+
48
+ ALTER TABLE "org_credentials" ENABLE ROW LEVEL SECURITY;
49
+ ALTER TABLE "org_credentials" FORCE ROW LEVEL SECURITY;
50
+ CREATE POLICY "org_credentials_org_isolation" ON "org_credentials"
51
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
52
+ OR current_setting('app.rls_bypass', true) = 'on')
53
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
54
+ OR current_setting('app.rls_bypass', true) = 'on');
@@ -0,0 +1,18 @@
1
+ -- Jobs carry the organization they belong to (issue #734, PP-8.2).
2
+ --
3
+ -- Nullable with no default: a metadata-only ADD COLUMN, no table rewrite.
4
+ -- NULL is a deployment-wide (system) job, and every row written before this
5
+ -- migration is one. No row-level security on `jobs` (the claim is one
6
+ -- cross-organization statement); see the `Job.orgId` comment in the jobs
7
+ -- fragment. The `(org_id, status)` index is built CONCURRENTLY in the next
8
+ -- migration, on its own, because `jobs` is a big table.
9
+
10
+ -- AlterTable
11
+ ALTER TABLE "jobs" ADD COLUMN "org_id" UUID;
12
+
13
+ -- AddForeignKey
14
+ -- NOT VALID then VALIDATE: the add takes its lock without scanning `jobs`,
15
+ -- and the validation scan runs under a lock that does not block writes.
16
+ -- ON DELETE SET NULL keeps an organization's job history when it is deleted.
17
+ ALTER TABLE "jobs" ADD CONSTRAINT "jobs_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE SET NULL ON UPDATE CASCADE NOT VALID;
18
+ ALTER TABLE "jobs" VALIDATE CONSTRAINT "jobs_org_id_fkey";
@@ -0,0 +1,6 @@
1
+ -- "This organization's jobs by status" (issue #734, PP-8.2): the admin job
2
+ -- list's orgId filter and offboarding's cancel of an organization's pending
3
+ -- jobs. CONCURRENTLY, so building it never blocks the queue's writes, and
4
+ -- therefore the only statement in this migration (it cannot run inside a
5
+ -- transaction, and Prisma runs a single such statement outside one).
6
+ CREATE INDEX CONCURRENTLY IF NOT EXISTS "jobs_org_id_status_idx" ON "jobs"("org_id", "status");
@@ -0,0 +1,21 @@
1
+ -- Broadcasts can target one organization (issue #738, PP-8.5).
2
+ --
3
+ -- Nullable with no default: a metadata-only ADD COLUMN. NULL is a system
4
+ -- broadcast (every active user of the deployment), and every row written
5
+ -- before this migration is one. No row-level security on
6
+ -- `notification_broadcasts`: a system broadcast belongs to no organization and
7
+ -- the delivering job must read it; the API authorizes `target_org_id` instead.
8
+ -- ON DELETE CASCADE: an organization's broadcasts have no audience once it is
9
+ -- deleted.
10
+
11
+ -- AlterTable
12
+ ALTER TABLE "notification_broadcasts" ADD COLUMN "target_org_id" UUID;
13
+
14
+ -- CreateIndex
15
+ CREATE INDEX "notification_broadcasts_target_org_id_idx" ON "notification_broadcasts"("target_org_id");
16
+
17
+ -- AddForeignKey
18
+ -- NOT VALID then VALIDATE, as for `jobs.org_id`: the add takes its lock
19
+ -- without scanning the table, and the validation scan does not block writes.
20
+ ALTER TABLE "notification_broadcasts" ADD CONSTRAINT "notification_broadcasts_target_org_id_fkey" FOREIGN KEY ("target_org_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE NOT VALID;
21
+ ALTER TABLE "notification_broadcasts" VALIDATE CONSTRAINT "notification_broadcasts_target_org_id_fkey";