@marinoscar/platform-db 0.1.0-next.3 → 0.1.0-next.30

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 (132) hide show
  1. package/CHANGELOG.md +107 -0
  2. package/README.md +158 -13
  3. package/dist/baseline/deps.d.ts.map +1 -1
  4. package/dist/baseline/deps.js +2 -0
  5. package/dist/baseline/deps.js.map +1 -1
  6. package/dist/baseline/errors.d.ts +3 -1
  7. package/dist/baseline/errors.d.ts.map +1 -1
  8. package/dist/baseline/errors.js.map +1 -1
  9. package/dist/baseline/report.d.ts +3 -1
  10. package/dist/baseline/report.d.ts.map +1 -1
  11. package/dist/baseline/report.js +4 -0
  12. package/dist/baseline/report.js.map +1 -1
  13. package/dist/baseline/run.d.ts +12 -1
  14. package/dist/baseline/run.d.ts.map +1 -1
  15. package/dist/baseline/run.js +19 -0
  16. package/dist/baseline/run.js.map +1 -1
  17. package/dist/bin/db-commands.d.ts +1 -1
  18. package/dist/bin/db-commands.d.ts.map +1 -1
  19. package/dist/bin/db-commands.js +40 -5
  20. package/dist/bin/db-commands.js.map +1 -1
  21. package/dist/drift/app-objects.d.ts +107 -0
  22. package/dist/drift/app-objects.d.ts.map +1 -0
  23. package/dist/drift/app-objects.js +193 -0
  24. package/dist/drift/app-objects.js.map +1 -0
  25. package/dist/drift/db-conformance.d.ts +3 -0
  26. package/dist/drift/db-conformance.d.ts.map +1 -1
  27. package/dist/drift/db-conformance.js +24 -0
  28. package/dist/drift/db-conformance.js.map +1 -1
  29. package/dist/drift/index.d.ts +5 -2
  30. package/dist/drift/index.d.ts.map +1 -1
  31. package/dist/drift/index.js +18 -1
  32. package/dist/drift/index.js.map +1 -1
  33. package/dist/drift/raw-sql-tripwire.d.ts +44 -3
  34. package/dist/drift/raw-sql-tripwire.d.ts.map +1 -1
  35. package/dist/drift/raw-sql-tripwire.js +39 -11
  36. package/dist/drift/raw-sql-tripwire.js.map +1 -1
  37. package/dist/drift/rls-policies.d.ts +226 -0
  38. package/dist/drift/rls-policies.d.ts.map +1 -0
  39. package/dist/drift/rls-policies.js +317 -0
  40. package/dist/drift/rls-policies.js.map +1 -0
  41. package/dist/drift/run.d.ts +24 -0
  42. package/dist/drift/run.d.ts.map +1 -1
  43. package/dist/drift/run.js +52 -1
  44. package/dist/drift/run.js.map +1 -1
  45. package/dist/drift/sql-expression.d.ts +27 -0
  46. package/dist/drift/sql-expression.d.ts.map +1 -0
  47. package/dist/drift/sql-expression.js +64 -0
  48. package/dist/drift/sql-expression.js.map +1 -0
  49. package/dist/lock/index.d.ts +1 -1
  50. package/dist/lock/index.d.ts.map +1 -1
  51. package/dist/lock/index.js.map +1 -1
  52. package/dist/lock/lock.d.ts +37 -0
  53. package/dist/lock/lock.d.ts.map +1 -1
  54. package/dist/lock/lock.js +11 -0
  55. package/dist/lock/lock.js.map +1 -1
  56. package/dist/lock/manifest.d.ts +8 -0
  57. package/dist/lock/manifest.d.ts.map +1 -1
  58. package/dist/lock/manifest.js +1 -0
  59. package/dist/lock/manifest.js.map +1 -1
  60. package/dist/seed/index.d.ts +1 -0
  61. package/dist/seed/index.d.ts.map +1 -1
  62. package/dist/seed/index.js +7 -1
  63. package/dist/seed/index.js.map +1 -1
  64. package/dist/seed/seed-platform.d.ts +16 -3
  65. package/dist/seed/seed-platform.d.ts.map +1 -1
  66. package/dist/seed/seed-platform.js +37 -5
  67. package/dist/seed/seed-platform.js.map +1 -1
  68. package/dist/seed/steps.d.ts +172 -0
  69. package/dist/seed/steps.d.ts.map +1 -0
  70. package/dist/seed/steps.js +136 -0
  71. package/dist/seed/steps.js.map +1 -0
  72. package/dist/seed/types.d.ts +12 -2
  73. package/dist/seed/types.d.ts.map +1 -1
  74. package/dist/sync/app-migrations.d.ts +52 -0
  75. package/dist/sync/app-migrations.d.ts.map +1 -0
  76. package/dist/sync/app-migrations.js +62 -0
  77. package/dist/sync/app-migrations.js.map +1 -0
  78. package/dist/sync/apply.d.ts.map +1 -1
  79. package/dist/sync/apply.js +1 -0
  80. package/dist/sync/apply.js.map +1 -1
  81. package/dist/sync/check.d.ts +5 -2
  82. package/dist/sync/check.d.ts.map +1 -1
  83. package/dist/sync/check.js +8 -1
  84. package/dist/sync/check.js.map +1 -1
  85. package/dist/sync/errors.d.ts +2 -1
  86. package/dist/sync/errors.d.ts.map +1 -1
  87. package/dist/sync/errors.js.map +1 -1
  88. package/dist/sync/index.d.ts +1 -0
  89. package/dist/sync/index.d.ts.map +1 -1
  90. package/dist/sync/index.js +3 -1
  91. package/dist/sync/index.js.map +1 -1
  92. package/dist/sync/plan.d.ts +3 -1
  93. package/dist/sync/plan.d.ts.map +1 -1
  94. package/dist/sync/plan.js +9 -2
  95. package/dist/sync/plan.js.map +1 -1
  96. package/dist/sync/promote.d.ts +4 -0
  97. package/dist/sync/promote.d.ts.map +1 -1
  98. package/dist/sync/promote.js +2 -0
  99. package/dist/sync/promote.js.map +1 -1
  100. package/migrations/0025_org_scoped_rls/migration.sql +215 -0
  101. package/migrations/0026_add_groups/migration.sql +165 -0
  102. package/migrations/0027_add_grants/migration.sql +117 -0
  103. package/migrations/0028_add_org_settings/migration.sql +49 -0
  104. package/migrations/0029_add_org_credentials/migration.sql +54 -0
  105. package/migrations/0030_add_jobs_org_id/migration.sql +18 -0
  106. package/migrations/0031_add_jobs_org_id_status_index/migration.sql +6 -0
  107. package/migrations/0032_add_broadcast_target_org/migration.sql +21 -0
  108. package/migrations/0033_add_android_app/migration.sql +60 -0
  109. package/migrations/0034_open_job_reason/migration.sql +16 -0
  110. package/migrations/0035_ai_usage_feature/migration.sql +37 -0
  111. package/migrations/0036_add_app_metadata/migration.sql +49 -0
  112. package/migrations/0037_open_sharing_vocabularies/migration.sql +97 -0
  113. package/migrations/0038_add_worker_node_events/migration.sql +21 -0
  114. package/migrations/0039_add_webhooks/migration.sql +125 -0
  115. package/migrations/0040_add_alerts/migration.sql +66 -0
  116. package/migrations/0041_notifications_state_counted/migration.sql +19 -0
  117. package/migrations/0042_add_storage_migrations/migration.sql +130 -0
  118. package/migrations/manifest.json +185 -0
  119. package/package.json +5 -2
  120. package/raw-sql-indexes.json +72 -0
  121. package/rls-policies.json +102 -0
  122. package/schema/ai.prisma +59 -2
  123. package/schema/alerts.prisma +75 -0
  124. package/schema/android-app.prisma +47 -0
  125. package/schema/credentials.prisma +62 -0
  126. package/schema/identity.prisma +34 -1
  127. package/schema/jobs.prisma +69 -7
  128. package/schema/notifications.prisma +84 -5
  129. package/schema/settings.prisma +25 -0
  130. package/schema/sharing.prisma +223 -0
  131. package/schema/storage.prisma +143 -4
  132. package/schema/webhooks.prisma +108 -0
@@ -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";
@@ -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,21 @@
1
+ -- CreateTable
2
+ CREATE TABLE "worker_node_events" (
3
+ "id" UUID NOT NULL,
4
+ "node_id" UUID NOT NULL,
5
+ "type" VARCHAR(64) NOT NULL,
6
+ "severity" VARCHAR(16) NOT NULL,
7
+ "attrs" JSONB,
8
+ "occurred_at" TIMESTAMPTZ NOT NULL,
9
+ "received_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
10
+
11
+ CONSTRAINT "worker_node_events_pkey" PRIMARY KEY ("id")
12
+ );
13
+
14
+ -- CreateIndex
15
+ CREATE INDEX "worker_node_events_node_id_received_at_idx" ON "worker_node_events"("node_id", "received_at" DESC);
16
+
17
+ -- CreateIndex
18
+ CREATE INDEX "worker_node_events_received_at_idx" ON "worker_node_events"("received_at");
19
+
20
+ -- AddForeignKey
21
+ ALTER TABLE "worker_node_events" ADD CONSTRAINT "worker_node_events_node_id_fkey" FOREIGN KEY ("node_id") REFERENCES "worker_nodes"("id") ON DELETE CASCADE ON UPDATE CASCADE;