@marinoscar/platform-db 0.1.0-next.25 → 0.1.0-next.27

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.
package/CHANGELOG.md CHANGED
@@ -1,5 +1,17 @@
1
1
  # @marinoscar/platform-db
2
2
 
3
+ ## 0.1.0-next.27
4
+
5
+ ### Minor Changes
6
+
7
+ - 3c48c78: Add copy-only storage provider migration (#1093). An administrator starts a run from one storage driver to another (`POST /api/admin/storage/migrations`, `storage_config:write`; list, get and cancel need `storage_config:read` / `storage_config:write`); every `ready` object recorded on the source is copied by streaming, verified (byte count and sha256 of the bytes read back from the target), and only then repointed, while the source object is left in place as a fallback. Platform migration `0042_add_storage_migrations` adds `storage_migration_runs` and `storage_migration_items` (both under forced row-level security on `org_id`; an item links to its object by the composite `(object_id, org_id)` key) and the raw-SQL partial unique index `storage_migration_runs_active_uniq_idx`, so at most one run is pending or running (a second start is a 409 naming `details.activeRunId`). Two server-only job types do the work: `storage.migrate_plan` (one per run, lists the objects and queues them, resumable) and `storage.migrate_object` (one per object, `skipDedup`, background priority). A cancelled run stops before its next unclaimed object, and a re-run skips objects already on the target. The contract gains the run wire shapes, and `StorageConfigPage` gains a migration panel with live progress and run history.
8
+
9
+ ## 0.1.0-next.26
10
+
11
+ ### Minor Changes
12
+
13
+ - 5c815ac: Identity: `PersonalAccessToken` is now marked `// @extensible`, so an app fragment can declare the back-relation of a foreign key to `personal_access_tokens` (for example a device row that records the token it paired with) with `extend model PersonalAccessToken { ... }`. Marker only: no column, key or index changes, no migration.
14
+
3
15
  ## 0.1.0-next.25
4
16
 
5
17
  ## 0.1.0-next.24
package/README.md CHANGED
@@ -148,6 +148,7 @@ Rules:
148
148
  |---|---|---|---|---|
149
149
  | `User` | `users` | system | its own per-user rows (a plain key on `id`); app fields that fit a document go in [`metadata`](#app-metadata-on-users-and-organizations) with no table at all | a column; the credential and token tables |
150
150
  | `Organization` | `organizations` | system | its own per-organization rows; org-owned app tables point here with a `Cascade` key on `org_id` | |
151
+ | `PersonalAccessToken` | `personal_access_tokens` | system | a link from an app row to the token it was paired with (a nullable key on `id`, `onDelete: SetNull`, so revoking or purging a token never deletes the app row) | the `token_hash` column: an app reads token state through `PatService`, never copies the hash |
151
152
  | `Role` | `roles` | system | per-role app data (a plain key on `id`) | `role_permissions`: `seedPlatform` owns the grants |
152
153
  | `Group` | `groups` | org (row-level security) | app rows of a group: a composite key `(group_id, org_id)` -> `groups (id, org_id)` | a plain key on `id` alone |
153
154
  | `Grant` | `grants` | org (row-level security) | an annotation of a grant: a composite key `(grant_id, org_id)` -> `grants (id, org_id)` | a plain key on `id` alone; an `@@unique` over the two partial unique indexes' columns |
@@ -0,0 +1,130 @@
1
+ -- =============================================================================
2
+ -- Copy-only storage provider migration: storage_migration_runs and
3
+ -- storage_migration_items (#1093)
4
+ -- =============================================================================
5
+ --
6
+ -- Two new org-owned tables. A run is deployment-wide today, so its org_id is
7
+ -- NULLABLE (NULL: visible to the bypass client only, as for a system-scope
8
+ -- webhook_endpoints row in 0039_add_webhooks). An item carries its object's
9
+ -- org_id (NOT NULL) because a policy cannot join through the parent, and links
10
+ -- to the object by the composite (object_id, org_id) foreign key (ADR 0002 D5).
11
+ --
12
+ -- storage_migration_runs_active_uniq_idx is a raw-SQL partial unique index
13
+ -- (listed in raw-sql-indexes.json): at most one pending or running run. No
14
+ -- backfill: the tables are new, so RLS is enabled at once.
15
+ -- =============================================================================
16
+
17
+ BEGIN;
18
+ SELECT set_config('app.rls_bypass', 'on', true);
19
+
20
+ -- CreateEnum
21
+ CREATE TYPE "StorageMigrationRunStatus" AS ENUM ('pending', 'running', 'completed', 'failed', 'cancelled');
22
+
23
+ -- CreateEnum
24
+ CREATE TYPE "StorageMigrationItemStatus" AS ENUM ('pending', 'copying', 'migrated', 'failed', 'skipped');
25
+
26
+ -- CreateTable
27
+ CREATE TABLE "storage_migration_runs" (
28
+ "id" UUID NOT NULL,
29
+ "org_id" UUID,
30
+ "source_provider" VARCHAR(48) NOT NULL,
31
+ "target_provider" VARCHAR(48) NOT NULL,
32
+ "status" "StorageMigrationRunStatus" NOT NULL DEFAULT 'pending',
33
+ "total_count" INTEGER NOT NULL DEFAULT 0,
34
+ "migrated_count" INTEGER NOT NULL DEFAULT 0,
35
+ "failed_count" INTEGER NOT NULL DEFAULT 0,
36
+ "skipped_count" INTEGER NOT NULL DEFAULT 0,
37
+ "planned_at" TIMESTAMPTZ,
38
+ "started_by_id" UUID,
39
+ "cancelled_by_id" UUID,
40
+ "started_at" TIMESTAMPTZ,
41
+ "finished_at" TIMESTAMPTZ,
42
+ "cancelled_at" TIMESTAMPTZ,
43
+ "last_error" VARCHAR(1000),
44
+ "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
45
+ "updated_at" TIMESTAMPTZ NOT NULL,
46
+
47
+ CONSTRAINT "storage_migration_runs_pkey" PRIMARY KEY ("id")
48
+ );
49
+
50
+ -- CreateTable
51
+ CREATE TABLE "storage_migration_items" (
52
+ "id" UUID NOT NULL,
53
+ "run_id" UUID NOT NULL,
54
+ "object_id" UUID NOT NULL,
55
+ "org_id" UUID NOT NULL,
56
+ "status" "StorageMigrationItemStatus" NOT NULL DEFAULT 'pending',
57
+ "job_id" UUID,
58
+ "size_bytes" BIGINT,
59
+ "checksum_sha256" CHAR(64),
60
+ "last_error" VARCHAR(1000),
61
+ "created_at" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
62
+ "updated_at" TIMESTAMPTZ NOT NULL,
63
+
64
+ CONSTRAINT "storage_migration_items_pkey" PRIMARY KEY ("id")
65
+ );
66
+
67
+ -- CreateIndex
68
+ CREATE INDEX "storage_migration_runs_created_at_idx" ON "storage_migration_runs"("created_at" DESC);
69
+
70
+ -- CreateIndex
71
+ CREATE INDEX "storage_migration_runs_status_idx" ON "storage_migration_runs"("status");
72
+
73
+ -- CreateIndex
74
+ CREATE INDEX "storage_migration_runs_org_id_idx" ON "storage_migration_runs"("org_id");
75
+
76
+ -- CreateIndex
77
+ CREATE INDEX "storage_migration_items_run_id_status_idx" ON "storage_migration_items"("run_id", "status");
78
+
79
+ -- CreateIndex
80
+ CREATE INDEX "storage_migration_items_org_id_object_id_idx" ON "storage_migration_items"("org_id", "object_id");
81
+
82
+ -- CreateIndex
83
+ CREATE UNIQUE INDEX "storage_migration_items_run_id_object_id_key" ON "storage_migration_items"("run_id", "object_id");
84
+
85
+ -- AddForeignKey
86
+ ALTER TABLE "storage_migration_runs" ADD CONSTRAINT "storage_migration_runs_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE CASCADE ON UPDATE CASCADE;
87
+
88
+ -- AddForeignKey
89
+ ALTER TABLE "storage_migration_runs" ADD CONSTRAINT "storage_migration_runs_started_by_id_fkey" FOREIGN KEY ("started_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
90
+
91
+ -- AddForeignKey
92
+ ALTER TABLE "storage_migration_runs" ADD CONSTRAINT "storage_migration_runs_cancelled_by_id_fkey" FOREIGN KEY ("cancelled_by_id") REFERENCES "users"("id") ON DELETE SET NULL ON UPDATE CASCADE;
93
+
94
+ -- AddForeignKey
95
+ ALTER TABLE "storage_migration_items" ADD CONSTRAINT "storage_migration_items_run_id_fkey" FOREIGN KEY ("run_id") REFERENCES "storage_migration_runs"("id") ON DELETE CASCADE ON UPDATE CASCADE;
96
+
97
+ -- AddForeignKey
98
+ ALTER TABLE "storage_migration_items" ADD CONSTRAINT "storage_migration_items_object_id_org_id_fkey" FOREIGN KEY ("object_id", "org_id") REFERENCES "storage_objects"("id", "org_id") ON DELETE CASCADE ON UPDATE CASCADE;
99
+
100
+ -- AddForeignKey
101
+ ALTER TABLE "storage_migration_items" ADD CONSTRAINT "storage_migration_items_org_id_fkey" FOREIGN KEY ("org_id") REFERENCES "organizations"("id") ON DELETE RESTRICT ON UPDATE CASCADE;
102
+
103
+ -- At most one pending or running migration run, deployment-wide: a
104
+ -- constant-expression partial unique index Prisma cannot express. A concurrent
105
+ -- second start loses on this index (P2002 -> 409), never a findFirst pre-check.
106
+ CREATE UNIQUE INDEX "storage_migration_runs_active_uniq_idx" ON "storage_migration_runs" ((true)) WHERE "status" IN ('pending', 'running');
107
+
108
+ -- =============================================================================
109
+ -- Row-level security, as in 0039_add_webhooks. The policy names are listed in
110
+ -- RLS_POLICIES (rls-policies.json). A NULL org_id never equals app.org_id, so a
111
+ -- deployment-wide run needs the bypass.
112
+ -- =============================================================================
113
+
114
+ ALTER TABLE "storage_migration_runs" ENABLE ROW LEVEL SECURITY;
115
+ ALTER TABLE "storage_migration_runs" FORCE ROW LEVEL SECURITY;
116
+ CREATE POLICY "storage_migration_runs_org_isolation" ON "storage_migration_runs"
117
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
118
+ OR current_setting('app.rls_bypass', true) = 'on')
119
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
120
+ OR current_setting('app.rls_bypass', true) = 'on');
121
+
122
+ ALTER TABLE "storage_migration_items" ENABLE ROW LEVEL SECURITY;
123
+ ALTER TABLE "storage_migration_items" FORCE ROW LEVEL SECURITY;
124
+ CREATE POLICY "storage_migration_items_org_isolation" ON "storage_migration_items"
125
+ USING ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
126
+ OR current_setting('app.rls_bypass', true) = 'on')
127
+ WITH CHECK ("org_id" = NULLIF(current_setting('app.org_id', true), '')::uuid
128
+ OR current_setting('app.rls_bypass', true) = 'on');
129
+
130
+ COMMIT;
@@ -410,5 +410,16 @@
410
410
  "sha256": "c9285347971920da5310a279351d8de8acac3bba4b281e227cb025745dafc73c",
411
411
  "since": "1.0.0",
412
412
  "slice": "notifications"
413
+ },
414
+ {
415
+ "dir": "0042_add_storage_migrations",
416
+ "id": "0042_add_storage_migrations",
417
+ "requires": [
418
+ "identity"
419
+ ],
420
+ "rls": true,
421
+ "sha256": "8d37841523a5b4737379df567d5ad3feb37abb0f29dcef416cd34f20167ba181",
422
+ "since": "1.0.0",
423
+ "slice": "storage"
413
424
  }
414
425
  ]
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@marinoscar/platform-db",
3
- "version": "0.1.0-next.25",
3
+ "version": "0.1.0-next.27",
4
4
  "description": "Prisma schema fragments, SQL migrations and seed functions of the platform (CommonJS).",
5
5
  "license": "MIT",
6
6
  "type": "commonjs",
@@ -107,6 +107,15 @@
107
107
  "reason": "At most one LIVE foldable (STATE or COUNTED) notification per user and dedup key; a partial unique index Prisma cannot express. The writer serializes on an advisory lock and treats a unique violation as 'already live', so concurrent producers leave one row.",
108
108
  "doc": "docs/specs/platform-packages.md",
109
109
  "createdIn": "0041_notifications_state_counted"
110
+ },
111
+ {
112
+ "name": "storage_migration_runs_active_uniq_idx",
113
+ "table": "storage_migration_runs",
114
+ "unique": true,
115
+ "definition": "CREATE UNIQUE INDEX storage_migration_runs_active_uniq_idx ON public.storage_migration_runs USING btree ((true)) WHERE (status = ANY (ARRAY['pending'::\"StorageMigrationRunStatus\", 'running'::\"StorageMigrationRunStatus\"]))",
116
+ "reason": "At most one pending or running storage provider migration run deployment-wide: a constant-expression partial unique index Prisma cannot express. A concurrent second start loses on the index (409 with details.activeRunId), never a findFirst pre-check.",
117
+ "doc": "docs/specs/storage-providers.md",
118
+ "createdIn": "0042_add_storage_migrations"
110
119
  }
111
120
  ]
112
121
  }
package/rls-policies.json CHANGED
@@ -83,6 +83,20 @@
83
83
  "reason": "Organization isolation of webhook delivery attempts, whose payload is the event body sent; the row carries the endpoint's org_id because a policy cannot join through its parent.",
84
84
  "doc": "docs/SECURITY-ARCHITECTURE.md",
85
85
  "createdIn": "0039_add_webhooks"
86
+ },
87
+ {
88
+ "name": "storage_migration_runs_org_isolation",
89
+ "table": "storage_migration_runs",
90
+ "reason": "Organization isolation of copy-only storage provider migration runs (#1093); a deployment-wide run has a NULL org_id and is visible to the bypass (system) client only.",
91
+ "doc": "docs/specs/storage-providers.md",
92
+ "createdIn": "0042_add_storage_migrations"
93
+ },
94
+ {
95
+ "name": "storage_migration_items_org_isolation",
96
+ "table": "storage_migration_items",
97
+ "reason": "Organization isolation of the per-object rows of a storage migration run; the row carries its object's org_id because a policy cannot join through its parent.",
98
+ "doc": "docs/specs/storage-providers.md",
99
+ "createdIn": "0042_add_storage_migrations"
86
100
  }
87
101
  ]
88
102
  }
@@ -203,6 +203,7 @@ enum PatDurationUnit {
203
203
  months
204
204
  }
205
205
 
206
+ // @extensible — back-relations only, added with "extend model PersonalAccessToken { ... }" in a fragment
206
207
  model PersonalAccessToken {
207
208
  id String @id @default(uuid()) @db.Uuid
208
209
  userId String @map("user_id") @db.Uuid
@@ -32,9 +32,10 @@ model StorageObject {
32
32
  updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
33
33
 
34
34
  // Relations
35
- uploadedBy User? @relation("UserStorageObjects", fields: [uploadedById], references: [id], onDelete: SetNull)
36
- org Organization @relation(fields: [orgId], references: [id], onDelete: Restrict)
37
- chunks StorageObjectChunk[]
35
+ uploadedBy User? @relation("UserStorageObjects", fields: [uploadedById], references: [id], onDelete: SetNull)
36
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Restrict)
37
+ chunks StorageObjectChunk[]
38
+ migrationItems StorageMigrationItem[]
38
39
 
39
40
  // The target of the chunks' composite foreign key (object_id, org_id): a
40
41
  // reference between two org-owned tables is composite, because a foreign-key
@@ -74,11 +75,122 @@ model StorageObjectChunk {
74
75
  @@map("storage_object_chunks")
75
76
  }
76
77
 
78
+ // =============================================================================
79
+ // Copy-only provider migration (issue #1093)
80
+ // =============================================================================
81
+ //
82
+ // An administrator copies every `ready` object recorded on one storage driver
83
+ // to another: copy (streamed), verify (size and sha256 of the bytes read back
84
+ // from the target), repoint the `storage_objects` row, and leave the source
85
+ // object in place as a fallback. One job (`storage.migrate_object`) per item.
86
+ //
87
+ // A RUN is deployment-wide (storage configuration is system scope), so its
88
+ // `org_id` is NULL today: visible to the bypass (system) client only, like a
89
+ // system-scope `webhook_endpoints` row. The column exists so a future
90
+ // organization-owned run needs no migration. An ITEM carries its object's
91
+ // organization (NOT NULL), and its link to the object is the composite
92
+ // (object_id, org_id) foreign key, so an item can never name another
93
+ // organization's object (ADR 0002 D5).
94
+ //
95
+ // At most ONE run is pending or running: the raw-SQL partial unique index
96
+ // `storage_migration_runs_active_uniq_idx` (raw-sql-indexes.json), never a
97
+ // findFirst pre-check.
98
+ // =============================================================================
99
+
100
+ enum StorageMigrationRunStatus {
101
+ pending // Created; the planner job has not started yet
102
+ running // Planning or copying
103
+ completed // Every item migrated or skipped
104
+ failed // Finished with at least one failed item, or the planner gave up
105
+ cancelled // Stopped by an administrator
106
+ }
107
+
108
+ enum StorageMigrationItemStatus {
109
+ pending // Queued, not claimed
110
+ copying // A job is copying it now
111
+ migrated // Copied, verified and repointed
112
+ failed // Gave up (attempt budget spent, or verification failed every time)
113
+ skipped // Nothing to do: deleted, moved, already on the target, or run cancelled
114
+ }
115
+
116
+ model StorageMigrationRun {
117
+ id String @id @default(uuid()) @db.Uuid
118
+ // NULL: a deployment-wide run (today, every run). Row-level security forced
119
+ // (policy `storage_migration_runs_org_isolation`).
120
+ orgId String? @map("org_id") @db.Uuid
121
+ sourceProvider String @map("source_provider") @db.VarChar(48)
122
+ targetProvider String @map("target_provider") @db.VarChar(48)
123
+ status StorageMigrationRunStatus @default(pending)
124
+ // Snapshots written by the planner and the per-item jobs; GET recomputes
125
+ // them from the items, so a crash between an item write and a counter write
126
+ // never shows a wrong figure.
127
+ totalCount Int @default(0) @map("total_count")
128
+ migratedCount Int @default(0) @map("migrated_count")
129
+ failedCount Int @default(0) @map("failed_count")
130
+ skippedCount Int @default(0) @map("skipped_count")
131
+ // Set when the planner has created every item and enqueued every job; the
132
+ // run cannot finalize before.
133
+ plannedAt DateTime? @map("planned_at") @db.Timestamptz
134
+ startedById String? @map("started_by_id") @db.Uuid
135
+ cancelledById String? @map("cancelled_by_id") @db.Uuid
136
+ startedAt DateTime? @map("started_at") @db.Timestamptz
137
+ finishedAt DateTime? @map("finished_at") @db.Timestamptz
138
+ cancelledAt DateTime? @map("cancelled_at") @db.Timestamptz
139
+ lastError String? @map("last_error") @db.VarChar(1000)
140
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
141
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
142
+
143
+ org Organization? @relation(fields: [orgId], references: [id], onDelete: Cascade)
144
+ startedBy User? @relation("StorageMigrationRunsStarted", fields: [startedById], references: [id], onDelete: SetNull)
145
+ cancelledBy User? @relation("StorageMigrationRunsCancelled", fields: [cancelledById], references: [id], onDelete: SetNull)
146
+ items StorageMigrationItem[]
147
+
148
+ @@index([createdAt(sort: Desc)])
149
+ @@index([status])
150
+ @@index([orgId])
151
+ @@map("storage_migration_runs")
152
+ }
153
+
154
+ model StorageMigrationItem {
155
+ id String @id @default(uuid()) @db.Uuid
156
+ runId String @map("run_id") @db.Uuid
157
+ objectId String @map("object_id") @db.Uuid
158
+ // The object's organization, carried here because a policy cannot join
159
+ // through the parent. NOT NULL, row-level security forced.
160
+ orgId String @map("org_id") @db.Uuid
161
+ status StorageMigrationItemStatus @default(pending)
162
+ // The job that copies it, once the planner enqueued one (a retried planner
163
+ // enqueues only the items that have none).
164
+ jobId String? @map("job_id") @db.Uuid
165
+ // The object's byte count and the sha256 the copy was verified against.
166
+ sizeBytes BigInt? @map("size_bytes")
167
+ checksumSha256 String? @map("checksum_sha256") @db.Char(64)
168
+ lastError String? @map("last_error") @db.VarChar(1000)
169
+ createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz
170
+ updatedAt DateTime @updatedAt @map("updated_at") @db.Timestamptz
171
+
172
+ run StorageMigrationRun @relation(fields: [runId], references: [id], onDelete: Cascade)
173
+ // Composite: (object_id, org_id) -> storage_objects (id, org_id). Cascade: an
174
+ // object deleted mid-run takes its item with it.
175
+ object StorageObject @relation(fields: [objectId, orgId], references: [id, orgId], onDelete: Cascade)
176
+ org Organization @relation(fields: [orgId], references: [id], onDelete: Restrict)
177
+
178
+ // The idempotency anchor: one item per object per run.
179
+ @@unique([runId, objectId])
180
+ @@index([runId, status])
181
+ @@index([orgId, objectId])
182
+ @@map("storage_migration_items")
183
+ }
184
+
77
185
  extend model User {
78
- storageObjects StorageObject[] @relation("UserStorageObjects")
186
+ storageObjects StorageObject[] @relation("UserStorageObjects")
187
+ storageMigrationRunsStarted StorageMigrationRun[] @relation("StorageMigrationRunsStarted")
188
+ storageMigrationRunsCancelled StorageMigrationRun[] @relation("StorageMigrationRunsCancelled")
79
189
  }
80
190
 
81
191
  extend model Organization {
82
- storageObjects StorageObject[]
83
- storageObjectChunks StorageObjectChunk[]
192
+ storageObjects StorageObject[]
193
+ storageObjectChunks StorageObjectChunk[]
194
+ storageMigrationRuns StorageMigrationRun[]
195
+ storageMigrationItems StorageMigrationItem[]
84
196
  }