@powerhousedao/switchboard 6.2.3-dev.9 → 6.2.3-dev.91

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 (310) hide show
  1. package/.tsbuild/test/ai-gateway.test.d.ts +2 -0
  2. package/.tsbuild/test/ai-gateway.test.d.ts.map +1 -0
  3. package/.tsbuild/test/attachments/byte-grants.test.d.ts +2 -0
  4. package/.tsbuild/test/attachments/byte-grants.test.d.ts.map +1 -0
  5. package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts +2 -0
  6. package/.tsbuild/test/attachments/byte-routes-read-gate.integration.test.d.ts.map +1 -0
  7. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts +2 -0
  8. package/.tsbuild/test/attachments/reserve-metadata-parity.test.d.ts.map +1 -0
  9. package/.tsbuild/test/attachments/url-signer.test.d.ts +2 -0
  10. package/.tsbuild/test/attachments/url-signer.test.d.ts.map +1 -0
  11. package/.tsbuild/test/boot-auth-config.test.d.ts +2 -0
  12. package/.tsbuild/test/boot-auth-config.test.d.ts.map +1 -0
  13. package/.tsbuild/test/boot-create-signature-policy.test.d.ts +2 -0
  14. package/.tsbuild/test/boot-create-signature-policy.test.d.ts.map +1 -0
  15. package/.tsbuild/test/boot-desktop-auth.test.d.ts +2 -0
  16. package/.tsbuild/test/boot-desktop-auth.test.d.ts.map +1 -0
  17. package/.tsbuild/test/boot-env-isolation.test.d.ts +2 -0
  18. package/.tsbuild/test/boot-env-isolation.test.d.ts.map +1 -0
  19. package/.tsbuild/test/boot-helpers.d.ts +5 -0
  20. package/.tsbuild/test/boot-helpers.d.ts.map +1 -0
  21. package/.tsbuild/test/boot-inspection-enabled.test.d.ts +2 -0
  22. package/.tsbuild/test/boot-inspection-enabled.test.d.ts.map +1 -0
  23. package/.tsbuild/test/boot-mcp-auth.test.d.ts +2 -0
  24. package/.tsbuild/test/boot-mcp-auth.test.d.ts.map +1 -0
  25. package/.tsbuild/test/boot-mcp-enabled.test.d.ts +2 -0
  26. package/.tsbuild/test/boot-mcp-enabled.test.d.ts.map +1 -0
  27. package/.tsbuild/test/boot-port-zero.test.d.ts +2 -0
  28. package/.tsbuild/test/boot-port-zero.test.d.ts.map +1 -0
  29. package/.tsbuild/test/boot-preconditions.test.d.ts +2 -0
  30. package/.tsbuild/test/boot-preconditions.test.d.ts.map +1 -0
  31. package/.tsbuild/test/boot-privacy.test.d.ts +2 -0
  32. package/.tsbuild/test/boot-privacy.test.d.ts.map +1 -0
  33. package/.tsbuild/test/boot-ready-drives.test.d.ts +2 -0
  34. package/.tsbuild/test/boot-ready-drives.test.d.ts.map +1 -0
  35. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts +2 -0
  36. package/.tsbuild/test/boot-undo-v3-flag.test.d.ts.map +1 -0
  37. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts +2 -0
  38. package/.tsbuild/test/boot-unsupported-stored-documents.test.d.ts.map +1 -0
  39. package/.tsbuild/test/boot-without-renown.test.d.ts +2 -0
  40. package/.tsbuild/test/boot-without-renown.test.d.ts.map +1 -0
  41. package/.tsbuild/test/boot-without-require-signatures.test.d.ts +2 -0
  42. package/.tsbuild/test/boot-without-require-signatures.test.d.ts.map +1 -0
  43. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts +2 -0
  44. package/.tsbuild/test/boot-worker-trust-policy.test.d.ts.map +1 -0
  45. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts +2 -0
  46. package/.tsbuild/test/boot-workflow-singleton-conflict.test.d.ts.map +1 -0
  47. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts +2 -0
  48. package/.tsbuild/test/boot-workflow-singleton-retry.test.d.ts.map +1 -0
  49. package/.tsbuild/test/builder-defaults.test.d.ts +2 -0
  50. package/.tsbuild/test/builder-defaults.test.d.ts.map +1 -0
  51. package/.tsbuild/test/config-from-env.test.d.ts +2 -0
  52. package/.tsbuild/test/config-from-env.test.d.ts.map +1 -0
  53. package/.tsbuild/test/config-merge.test.d.ts +2 -0
  54. package/.tsbuild/test/config-merge.test.d.ts.map +1 -0
  55. package/.tsbuild/test/config-schema.test.d.ts +2 -0
  56. package/.tsbuild/test/config-schema.test.d.ts.map +1 -0
  57. package/.tsbuild/test/create-signature-policy.test.d.ts +2 -0
  58. package/.tsbuild/test/create-signature-policy.test.d.ts.map +1 -0
  59. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts +2 -0
  60. package/.tsbuild/test/default-drive-protocol-versions.test.d.ts.map +1 -0
  61. package/.tsbuild/test/default-drive-slug.test.d.ts +2 -0
  62. package/.tsbuild/test/default-drive-slug.test.d.ts.map +1 -0
  63. package/.tsbuild/test/delegated-publish.test.d.ts +2 -0
  64. package/.tsbuild/test/delegated-publish.test.d.ts.map +1 -0
  65. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts +2 -0
  66. package/.tsbuild/test/durable-node-fs-pg16.test.d.ts.map +1 -0
  67. package/.tsbuild/test/fatal-shutdown.test.d.ts +2 -0
  68. package/.tsbuild/test/fatal-shutdown.test.d.ts.map +1 -0
  69. package/.tsbuild/test/inspection-throttle.test.d.ts +2 -0
  70. package/.tsbuild/test/inspection-throttle.test.d.ts.map +1 -0
  71. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts +2 -0
  72. package/.tsbuild/test/keyframe-reclaim-store.test.d.ts.map +1 -0
  73. package/.tsbuild/test/observability-signals.test.d.ts +2 -0
  74. package/.tsbuild/test/observability-signals.test.d.ts.map +1 -0
  75. package/.tsbuild/test/permissions-backfill.test.d.ts +2 -0
  76. package/.tsbuild/test/permissions-backfill.test.d.ts.map +1 -0
  77. package/.tsbuild/test/pglite-preflight.test.d.ts +2 -0
  78. package/.tsbuild/test/pglite-preflight.test.d.ts.map +1 -0
  79. package/.tsbuild/test/pglite-storage-dirs.test.d.ts +2 -0
  80. package/.tsbuild/test/pglite-storage-dirs.test.d.ts.map +1 -0
  81. package/.tsbuild/test/pglite-store.test.d.ts +2 -0
  82. package/.tsbuild/test/pglite-store.test.d.ts.map +1 -0
  83. package/.tsbuild/test/plugin-config-schemas.test.d.ts +2 -0
  84. package/.tsbuild/test/plugin-config-schemas.test.d.ts.map +1 -0
  85. package/.tsbuild/test/plugin-presence.test.d.ts +2 -0
  86. package/.tsbuild/test/plugin-presence.test.d.ts.map +1 -0
  87. package/.tsbuild/test/port-probe.test.d.ts +2 -0
  88. package/.tsbuild/test/port-probe.test.d.ts.map +1 -0
  89. package/.tsbuild/test/processor-timing-log.test.d.ts +2 -0
  90. package/.tsbuild/test/processor-timing-log.test.d.ts.map +1 -0
  91. package/.tsbuild/test/renown-storage-path.test.d.ts +2 -0
  92. package/.tsbuild/test/renown-storage-path.test.d.ts.map +1 -0
  93. package/.tsbuild/test/renown-trust-policy.test.d.ts +2 -0
  94. package/.tsbuild/test/renown-trust-policy.test.d.ts.map +1 -0
  95. package/.tsbuild/test/served-schemas.test.d.ts +2 -0
  96. package/.tsbuild/test/served-schemas.test.d.ts.map +1 -0
  97. package/.tsbuild/test/shutdown.test.d.ts +2 -0
  98. package/.tsbuild/test/shutdown.test.d.ts.map +1 -0
  99. package/.tsbuild/test/snapshot-writer.d.ts +18 -0
  100. package/.tsbuild/test/snapshot-writer.d.ts.map +1 -0
  101. package/.tsbuild/test/store-lock.test.d.ts +2 -0
  102. package/.tsbuild/test/store-lock.test.d.ts.map +1 -0
  103. package/.tsbuild/test/two-switchboards.test.d.ts +2 -0
  104. package/.tsbuild/test/two-switchboards.test.d.ts.map +1 -0
  105. package/.tsbuild/test/unsupported-stored-documents.test.d.ts +2 -0
  106. package/.tsbuild/test/unsupported-stored-documents.test.d.ts.map +1 -0
  107. package/.tsbuild/test/workflow/harness.d.ts +56 -0
  108. package/.tsbuild/test/workflow/harness.d.ts.map +1 -0
  109. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts +2 -0
  110. package/.tsbuild/test/workflow/oauth-apps-config.test.d.ts.map +1 -0
  111. package/.tsbuild/test/workflow/oauth.test.d.ts +2 -0
  112. package/.tsbuild/test/workflow/oauth.test.d.ts.map +1 -0
  113. package/.tsbuild/test/workflow/resolvers.test.d.ts +2 -0
  114. package/.tsbuild/test/workflow/resolvers.test.d.ts.map +1 -0
  115. package/.tsbuild/test/workflow/runtime-host.test.d.ts +2 -0
  116. package/.tsbuild/test/workflow/runtime-host.test.d.ts.map +1 -0
  117. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts +2 -0
  118. package/.tsbuild/test/workflow/waitpoint-routes.test.d.ts.map +1 -0
  119. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts +2 -0
  120. package/.tsbuild/test/workflow/waitpoint-runtime.test.d.ts.map +1 -0
  121. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts +2 -0
  122. package/.tsbuild/test/workflow/webhook-dedupe.test.d.ts.map +1 -0
  123. package/.tsbuild/test/workflow/webhook-signed.test.d.ts +2 -0
  124. package/.tsbuild/test/workflow/webhook-signed.test.d.ts.map +1 -0
  125. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts +2 -0
  126. package/.tsbuild/test/workflow/webhook-trigger-test.test.d.ts.map +1 -0
  127. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts +2 -0
  128. package/.tsbuild/test/workflow/webhook-unsigned.test.d.ts.map +1 -0
  129. package/.tsbuild/test/workflow-reactor-access.test.d.ts +2 -0
  130. package/.tsbuild/test/workflow-reactor-access.test.d.ts.map +1 -0
  131. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts +2 -0
  132. package/.tsbuild/test/workflow-reactor-enforcement.test.d.ts.map +1 -0
  133. package/.tsbuild/test/workflow-read-gate.test.d.ts +2 -0
  134. package/.tsbuild/test/workflow-read-gate.test.d.ts.map +1 -0
  135. package/.tsbuild/test/workflow-runtime.test.d.ts +2 -0
  136. package/.tsbuild/test/workflow-runtime.test.d.ts.map +1 -0
  137. package/.tsbuild/test/workflows-plugin-config.test.d.ts +2 -0
  138. package/.tsbuild/test/workflows-plugin-config.test.d.ts.map +1 -0
  139. package/.tsbuild/tsconfig.tsbuildinfo +1 -1
  140. package/.tsbuild/tsdown.config.d.ts.map +1 -1
  141. package/.tsbuild/vitest.config.d.ts.map +1 -1
  142. package/Auth.md +144 -2
  143. package/CHANGELOG.md +1226 -0
  144. package/README.md +101 -22
  145. package/dist/boot.d.mts +46 -0
  146. package/dist/boot.d.mts.map +1 -0
  147. package/dist/boot.mjs +67 -0
  148. package/dist/boot.mjs.map +1 -0
  149. package/dist/config/from-env.d.mts +15 -0
  150. package/dist/config/from-env.d.mts.map +1 -0
  151. package/dist/config/from-env.mjs +4 -0
  152. package/dist/config/schema.d.mts +3 -0
  153. package/dist/config/schema.mjs +4 -0
  154. package/dist/from-env-C6dHDvAu.mjs +474 -0
  155. package/dist/from-env-C6dHDvAu.mjs.map +1 -0
  156. package/dist/index.d.mts +2 -1
  157. package/dist/index.mjs +84 -84
  158. package/dist/index.mjs.map +1 -1
  159. package/dist/install-packages.d.mts +2 -1
  160. package/dist/install-packages.mjs +2 -2
  161. package/dist/install-packages.mjs.map +1 -1
  162. package/dist/mcp-SsBK14Jm.mjs +32 -0
  163. package/dist/mcp-SsBK14Jm.mjs.map +1 -0
  164. package/dist/migrate.d.mts +2 -1
  165. package/dist/migrate.mjs +7 -5
  166. package/dist/migrate.mjs.map +1 -1
  167. package/dist/plugin-host-H2uebOKI.d.mts +46 -0
  168. package/dist/plugin-host-H2uebOKI.d.mts.map +1 -0
  169. package/dist/plugins/ai.d.mts +37 -0
  170. package/dist/plugins/ai.d.mts.map +1 -0
  171. package/dist/plugins/ai.mjs +400 -0
  172. package/dist/plugins/ai.mjs.map +1 -0
  173. package/dist/plugins/attachments.d.mts +33 -0
  174. package/dist/plugins/attachments.d.mts.map +1 -0
  175. package/dist/plugins/attachments.mjs +4 -0
  176. package/dist/plugins/mcp.d.mts +31 -0
  177. package/dist/plugins/mcp.d.mts.map +1 -0
  178. package/dist/plugins/mcp.mjs +4 -0
  179. package/dist/plugins/privacy.d.mts +22 -0
  180. package/dist/plugins/privacy.d.mts.map +1 -0
  181. package/dist/plugins/privacy.mjs +4 -0
  182. package/dist/plugins/workflows.d.mts +79 -0
  183. package/dist/plugins/workflows.d.mts.map +1 -0
  184. package/dist/plugins/workflows.mjs +4 -0
  185. package/dist/postgres-url-DTDhBjXX.mjs +11 -0
  186. package/dist/postgres-url-DTDhBjXX.mjs.map +1 -0
  187. package/dist/privacy-BO8Q25xI.mjs +44 -0
  188. package/dist/privacy-BO8Q25xI.mjs.map +1 -0
  189. package/dist/privacy-DxIJGH4P.mjs +91 -0
  190. package/dist/privacy-DxIJGH4P.mjs.map +1 -0
  191. package/dist/renown-layers-DmbWLt46.mjs +42 -0
  192. package/dist/renown-layers-DmbWLt46.mjs.map +1 -0
  193. package/dist/schema-B11_2pKM.mjs +328 -0
  194. package/dist/schema-B11_2pKM.mjs.map +1 -0
  195. package/dist/schema-BRNH0PaA.d.mts +247 -0
  196. package/dist/schema-BRNH0PaA.d.mts.map +1 -0
  197. package/dist/server-DyLVCH38.mjs +2730 -0
  198. package/dist/server-DyLVCH38.mjs.map +1 -0
  199. package/dist/server.d.mts +129 -189
  200. package/dist/server.d.mts.map +1 -1
  201. package/dist/server.mjs +5 -5
  202. package/dist/types-DzO3_320.d.mts +308 -0
  203. package/dist/types-DzO3_320.d.mts.map +1 -0
  204. package/dist/utils-Cm0TE6dP.mjs +148 -0
  205. package/dist/utils-Cm0TE6dP.mjs.map +1 -0
  206. package/dist/utils.d.mts +30 -5
  207. package/dist/utils.d.mts.map +1 -1
  208. package/dist/utils.mjs +5 -4
  209. package/dist/worker-pool-gyxJnCxA.mjs +304 -0
  210. package/dist/worker-pool-gyxJnCxA.mjs.map +1 -0
  211. package/dist/workflow-runtime-CF5pc8mt.d.mts +33 -0
  212. package/dist/workflow-runtime-CF5pc8mt.d.mts.map +1 -0
  213. package/dist/workflows-C9BQlyT0.mjs +1943 -0
  214. package/dist/workflows-C9BQlyT0.mjs.map +1 -0
  215. package/package.json +85 -24
  216. package/scripts/install-packages.sh +0 -0
  217. package/test/ai-gateway.test.ts +612 -0
  218. package/test/attachment-reference-read-model.test.ts +28 -20
  219. package/test/attachments/auth.test.ts +85 -0
  220. package/test/attachments/byte-grants.test.ts +290 -0
  221. package/test/attachments/byte-routes-read-gate.integration.test.ts +314 -0
  222. package/test/attachments/download-target.test.ts +211 -32
  223. package/test/attachments/index.test.ts +47 -4
  224. package/test/attachments/reserve-metadata-parity.test.ts +96 -0
  225. package/test/attachments/routes-integration.test.ts +60 -4
  226. package/test/attachments/routes.test.ts +297 -35
  227. package/test/attachments/service-config.test.ts +41 -19
  228. package/test/attachments/url-signer.test.ts +105 -0
  229. package/test/boot-auth-config.test.ts +109 -0
  230. package/test/boot-create-signature-policy.test.ts +110 -0
  231. package/test/boot-desktop-auth.test.ts +278 -0
  232. package/test/boot-env-isolation.test.ts +26 -0
  233. package/test/boot-helpers.ts +19 -0
  234. package/test/boot-inspection-enabled.test.ts +140 -0
  235. package/test/boot-mcp-auth.test.ts +449 -0
  236. package/test/boot-mcp-enabled.test.ts +119 -0
  237. package/test/boot-port-zero.test.ts +101 -0
  238. package/test/boot-preconditions.test.ts +213 -0
  239. package/test/boot-privacy.test.ts +232 -0
  240. package/test/boot-ready-drives.test.ts +141 -0
  241. package/test/boot-undo-v3-flag.test.ts +120 -0
  242. package/test/boot-unsupported-stored-documents.test.ts +214 -0
  243. package/test/boot-without-renown.test.ts +83 -0
  244. package/test/boot-without-require-signatures.test.ts +76 -0
  245. package/test/boot-worker-trust-policy.test.ts +91 -0
  246. package/test/boot-workflow-singleton-conflict.test.ts +109 -0
  247. package/test/boot-workflow-singleton-retry.test.ts +122 -0
  248. package/test/builder-defaults.test.ts +80 -0
  249. package/test/config-from-env.test.ts +963 -0
  250. package/test/config-merge.test.ts +226 -0
  251. package/test/config-schema.test.ts +167 -0
  252. package/test/create-signature-policy.test.ts +69 -0
  253. package/test/default-drive-protocol-versions.test.ts +79 -0
  254. package/test/default-drive-slug.test.ts +265 -0
  255. package/test/delegated-publish.test.ts +435 -0
  256. package/test/durable-node-fs-pg16.test.ts +80 -0
  257. package/test/fatal-shutdown.test.ts +205 -0
  258. package/test/goldens/packages.graphql +797 -0
  259. package/test/goldens/privacy.graphql +853 -0
  260. package/test/goldens/reactor-drive-nodes.graphql +827 -0
  261. package/test/goldens/resolvers.json +327 -0
  262. package/test/goldens/supergraph.graphql +5918 -0
  263. package/test/goldens/workflow-runtime.graphql +1548 -0
  264. package/test/inspection-throttle.test.ts +23 -0
  265. package/test/keyframe-reclaim-store.test.ts +156 -0
  266. package/test/observability-signals.test.ts +131 -0
  267. package/test/permissions-backfill.test.ts +486 -0
  268. package/test/pglite-dialect.test.ts +237 -2
  269. package/test/pglite-preflight.test.ts +313 -0
  270. package/test/pglite-storage-dirs.test.ts +113 -0
  271. package/test/pglite-store.test.ts +441 -0
  272. package/test/pglite-version.test.ts +9 -1
  273. package/test/plugin-config-schemas.test.ts +83 -0
  274. package/test/plugin-presence.test.ts +262 -0
  275. package/test/port-probe.test.ts +45 -0
  276. package/test/processor-timing-log.test.ts +109 -0
  277. package/test/reactor-feature-flags.test.ts +20 -0
  278. package/test/redact-webhook-path.test.ts +9 -0
  279. package/test/renown-storage-path.test.ts +40 -0
  280. package/test/renown-trust-policy.test.ts +313 -0
  281. package/test/served-schemas.test.ts +134 -0
  282. package/test/shutdown.test.ts +278 -0
  283. package/test/snapshot-writer.ts +94 -0
  284. package/test/store-lock.test.ts +65 -0
  285. package/test/two-switchboards.test.ts +146 -0
  286. package/test/unsupported-stored-documents.test.ts +72 -0
  287. package/test/worker-pool.test.ts +27 -1
  288. package/test/workflow/harness.ts +289 -0
  289. package/test/workflow/oauth-apps-config.test.ts +66 -0
  290. package/test/workflow/oauth.test.ts +286 -0
  291. package/test/workflow/resolvers.test.ts +488 -0
  292. package/test/workflow/runtime-host.test.ts +19 -0
  293. package/test/workflow/waitpoint-routes.test.ts +306 -0
  294. package/test/workflow/waitpoint-runtime.test.ts +204 -0
  295. package/test/workflow/webhook-dedupe.test.ts +295 -0
  296. package/test/workflow/webhook-signed.test.ts +509 -0
  297. package/test/workflow/webhook-trigger-test.test.ts +82 -0
  298. package/test/workflow/webhook-unsigned.test.ts +345 -0
  299. package/test/workflow-reactor-access.test.ts +384 -0
  300. package/test/workflow-reactor-enforcement.test.ts +808 -0
  301. package/test/workflow-read-gate.test.ts +213 -0
  302. package/test/workflow-runtime.test.ts +1254 -0
  303. package/test/workflows-plugin-config.test.ts +77 -0
  304. package/tsconfig.json +11 -2
  305. package/tsdown.config.ts +13 -1
  306. package/vitest.config.ts +65 -0
  307. package/dist/server-CiKkVE6g.mjs +0 -1555
  308. package/dist/server-CiKkVE6g.mjs.map +0 -1
  309. package/dist/utils-Baw7rThP.mjs +0 -80
  310. package/dist/utils-Baw7rThP.mjs.map +0 -1
@@ -0,0 +1,486 @@
1
+ import { PGlite } from "@electric-sql/pglite";
2
+ import { getDbClient } from "@powerhousedao/reactor-api";
3
+ import type { ILogger } from "document-model";
4
+ import { Kysely, PostgresDialect, sql } from "kysely";
5
+ import { PGliteDialect } from "kysely-pglite-dialect";
6
+ import pg from "pg";
7
+ import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
8
+ import {
9
+ backfillPermissionTables,
10
+ MIGRATED_TABLE_SUFFIX,
11
+ } from "../src/permissions-backfill.mjs";
12
+
13
+ /** What reactor-api's migrations 001 to 003 leave on the read-model store. */
14
+ const LEGACY_DDL = `
15
+ CREATE TABLE "DocumentPermission" (
16
+ "id" SERIAL PRIMARY KEY,
17
+ "documentId" VARCHAR(255) NOT NULL,
18
+ "userAddress" VARCHAR(255) NOT NULL,
19
+ "permission" VARCHAR(20) NOT NULL CHECK ("permission" IN ('READ', 'WRITE', 'ADMIN')),
20
+ "grantedBy" VARCHAR(255) NOT NULL,
21
+ "createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
22
+ "updatedAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
23
+ UNIQUE ("documentId", "userAddress")
24
+ );
25
+ CREATE TABLE "OperationUserPermission" (
26
+ "id" SERIAL PRIMARY KEY,
27
+ "documentId" VARCHAR(255) NOT NULL,
28
+ "operationType" VARCHAR(255) NOT NULL,
29
+ "userAddress" VARCHAR(255) NOT NULL,
30
+ "grantedBy" VARCHAR(255) NOT NULL,
31
+ "createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
32
+ UNIQUE ("documentId", "operationType", "userAddress")
33
+ );
34
+ CREATE TABLE "DocumentProtection" (
35
+ "documentId" VARCHAR(255) PRIMARY KEY,
36
+ "protected" BOOLEAN NOT NULL DEFAULT false,
37
+ "ownerAddress" VARCHAR(255),
38
+ "createdAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
39
+ "updatedAt" TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP
40
+ );
41
+ `;
42
+
43
+ /** What reactor migration 029 creates in the reactor schema. */
44
+ const REACTOR_DDL = `
45
+ CREATE SCHEMA reactor;
46
+ CREATE TABLE reactor."DocumentPermission" (
47
+ "id" SERIAL PRIMARY KEY,
48
+ "documentId" VARCHAR(255) NOT NULL,
49
+ "userAddress" VARCHAR(255) NOT NULL,
50
+ "permission" VARCHAR(20) NOT NULL CHECK ("permission" IN ('READ', 'WRITE', 'ADMIN')),
51
+ "grantedBy" VARCHAR(255) NOT NULL,
52
+ "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
53
+ "updatedAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
54
+ UNIQUE ("documentId", "userAddress")
55
+ );
56
+ CREATE TABLE reactor."OperationUserPermission" (
57
+ "id" SERIAL PRIMARY KEY,
58
+ "documentId" VARCHAR(255) NOT NULL,
59
+ "operationType" VARCHAR(255) NOT NULL,
60
+ "userAddress" VARCHAR(255) NOT NULL,
61
+ "grantedBy" VARCHAR(255) NOT NULL,
62
+ "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
63
+ UNIQUE ("documentId", "operationType", "userAddress")
64
+ );
65
+ CREATE TABLE reactor."DocumentProtection" (
66
+ "documentId" VARCHAR(255) PRIMARY KEY,
67
+ "protected" BOOLEAN NOT NULL DEFAULT false,
68
+ "ownerAddress" VARCHAR(255),
69
+ "createdAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP,
70
+ "updatedAt" TIMESTAMPTZ NOT NULL DEFAULT CURRENT_TIMESTAMP
71
+ );
72
+ `;
73
+
74
+ const CREATED = new Date("2026-01-02T03:04:05.000Z");
75
+
76
+ function createLogger(): ILogger {
77
+ return {
78
+ info: vi.fn(),
79
+ warn: vi.fn(),
80
+ debug: vi.fn(),
81
+ error: vi.fn(),
82
+ } as unknown as ILogger;
83
+ }
84
+
85
+ /** The two stores a test runs against; `destroy` releases both. */
86
+ type BackfillStores = {
87
+ readModelDb: Kysely<any>;
88
+ reactorBase: Kysely<any>;
89
+ execRead: (statements: string) => Promise<void>;
90
+ execReactor: (statements: string) => Promise<void>;
91
+ destroy: () => Promise<void>;
92
+ };
93
+
94
+ function openPgliteStores(): Promise<BackfillStores> {
95
+ const readPg = new PGlite();
96
+ const reactorPg = new PGlite();
97
+ const readModelDb = new Kysely<any>({ dialect: new PGliteDialect(readPg) });
98
+ const reactorBase = new Kysely<any>({
99
+ dialect: new PGliteDialect(reactorPg),
100
+ });
101
+ return Promise.resolve({
102
+ readModelDb,
103
+ reactorBase,
104
+ execRead: async (statements) => {
105
+ await readPg.exec(statements);
106
+ },
107
+ execReactor: async (statements) => {
108
+ await reactorPg.exec(statements);
109
+ },
110
+ destroy: async () => {
111
+ await readModelDb.destroy();
112
+ await reactorBase.destroy();
113
+ },
114
+ });
115
+ }
116
+
117
+ /**
118
+ * The read-model store opened the way reactor-api opens it (knex over PGlite,
119
+ * UTC timestamp parsers), which is the handle the switchboard receives.
120
+ */
121
+ function openReactorApiReadStore(): Promise<BackfillStores> {
122
+ const read = getDbClient();
123
+ const reactorPg = new PGlite();
124
+ const reactorBase = new Kysely<any>({
125
+ dialect: new PGliteDialect(reactorPg),
126
+ });
127
+ return Promise.resolve({
128
+ readModelDb: read.db,
129
+ reactorBase,
130
+ execRead: async (statements) => {
131
+ await read.pglite!.exec(statements);
132
+ },
133
+ execReactor: async (statements) => {
134
+ await reactorPg.exec(statements);
135
+ },
136
+ destroy: async () => {
137
+ await read.knex.destroy();
138
+ if (!read.pglite!.closed) await read.pglite!.close();
139
+ await reactorBase.destroy();
140
+ },
141
+ });
142
+ }
143
+
144
+ /**
145
+ * Both stores in one Postgres database, the read model in `public` and the
146
+ * reactor in its schema: what a bare `dbPath` gives a Postgres deployment.
147
+ */
148
+ async function openPostgresStores(url: string): Promise<BackfillStores> {
149
+ const name = `sb_permissions_backfill_${process.pid}`;
150
+ const admin = new pg.Pool({ connectionString: url });
151
+ await admin.query(`DROP DATABASE IF EXISTS "${name}" WITH (FORCE)`);
152
+ await admin.query(`CREATE DATABASE "${name}"`);
153
+ const target = new URL(url);
154
+ target.pathname = `/${name}`;
155
+ const readPool = new pg.Pool({ connectionString: target.toString() });
156
+ const reactorPool = new pg.Pool({ connectionString: target.toString() });
157
+ for (const pool of [readPool, reactorPool]) {
158
+ // Dropping the database terminates whatever is still connected to it.
159
+ pool.on("error", (error: Error & { code?: string }) => {
160
+ if (error.code !== "57P01") throw error;
161
+ });
162
+ }
163
+ const readModelDb = new Kysely<any>({
164
+ dialect: new PostgresDialect({ pool: readPool }),
165
+ });
166
+ const reactorBase = new Kysely<any>({
167
+ dialect: new PostgresDialect({ pool: reactorPool }),
168
+ });
169
+ return {
170
+ readModelDb,
171
+ reactorBase,
172
+ execRead: async (statements) => {
173
+ await readPool.query(statements);
174
+ },
175
+ execReactor: async (statements) => {
176
+ await reactorPool.query(statements);
177
+ },
178
+ destroy: async () => {
179
+ try {
180
+ await readModelDb.destroy();
181
+ await reactorBase.destroy();
182
+ } finally {
183
+ await admin.query(`DROP DATABASE IF EXISTS "${name}" WITH (FORCE)`);
184
+ await admin.end();
185
+ }
186
+ },
187
+ };
188
+ }
189
+
190
+ const PG_URL = process.env.REACTOR_TEST_PG_URL;
191
+
192
+ defineBackfillCases("PGlite", openPgliteStores, false);
193
+ defineBackfillCases("reactor-api read store", openReactorApiReadStore, false);
194
+ defineBackfillCases("Postgres", () => openPostgresStores(PG_URL!), !PG_URL);
195
+
196
+ function defineBackfillCases(
197
+ backend: string,
198
+ open: () => Promise<BackfillStores>,
199
+ skip: boolean,
200
+ ): void {
201
+ describe.skipIf(skip)(`backfillPermissionTables [${backend}]`, () => {
202
+ let stores: BackfillStores;
203
+ let readModelDb: Kysely<any>;
204
+ let reactorDb: Kysely<any>;
205
+ let logger: ILogger;
206
+
207
+ const seedLegacy = async () => {
208
+ await stores.execRead(LEGACY_DDL);
209
+ await readModelDb
210
+ .insertInto("DocumentPermission")
211
+ .values([
212
+ {
213
+ documentId: "doc-1",
214
+ userAddress: "0xreader",
215
+ permission: "READ",
216
+ grantedBy: "0xowner",
217
+ createdAt: CREATED,
218
+ updatedAt: CREATED,
219
+ },
220
+ {
221
+ documentId: "doc-1",
222
+ userAddress: "0xowner",
223
+ permission: "ADMIN",
224
+ grantedBy: "0xowner",
225
+ createdAt: CREATED,
226
+ updatedAt: CREATED,
227
+ },
228
+ ])
229
+ .execute();
230
+ await readModelDb
231
+ .insertInto("OperationUserPermission")
232
+ .values({
233
+ documentId: "doc-1",
234
+ operationType: "SET_NAME",
235
+ userAddress: "0xreader",
236
+ grantedBy: "0xowner",
237
+ createdAt: CREATED,
238
+ })
239
+ .execute();
240
+ await readModelDb
241
+ .insertInto("DocumentProtection")
242
+ .values({
243
+ documentId: "doc-1",
244
+ protected: true,
245
+ ownerAddress: "0xowner",
246
+ createdAt: CREATED,
247
+ updatedAt: CREATED,
248
+ })
249
+ .execute();
250
+ };
251
+
252
+ const readModelTables = async (): Promise<string[]> => {
253
+ const result = await sql<{ table_name: string }>`
254
+ select table_name from information_schema.tables
255
+ where table_schema = 'public' order by table_name
256
+ `.execute(readModelDb);
257
+ return result.rows.map((row) => row.table_name);
258
+ };
259
+
260
+ beforeEach(async () => {
261
+ stores = await open();
262
+ readModelDb = stores.readModelDb;
263
+ await stores.execReactor(REACTOR_DDL);
264
+ reactorDb = stores.reactorBase.withSchema("reactor");
265
+ logger = createLogger();
266
+ });
267
+
268
+ afterEach(async () => {
269
+ await stores.destroy();
270
+ });
271
+
272
+ it("copies every row into the reactor store and renames the read-model tables", async () => {
273
+ await seedLegacy();
274
+
275
+ const outcome = await backfillPermissionTables(
276
+ readModelDb,
277
+ reactorDb,
278
+ logger,
279
+ );
280
+
281
+ expect(outcome).toEqual({
282
+ status: "copied",
283
+ counts: {
284
+ DocumentPermission: 2,
285
+ OperationUserPermission: 1,
286
+ DocumentProtection: 1,
287
+ },
288
+ });
289
+ expect(
290
+ await reactorDb
291
+ .selectFrom("DocumentPermission")
292
+ .select(["documentId", "userAddress", "permission", "grantedBy"])
293
+ .orderBy("id")
294
+ .execute(),
295
+ ).toEqual([
296
+ {
297
+ documentId: "doc-1",
298
+ userAddress: "0xreader",
299
+ permission: "READ",
300
+ grantedBy: "0xowner",
301
+ },
302
+ {
303
+ documentId: "doc-1",
304
+ userAddress: "0xowner",
305
+ permission: "ADMIN",
306
+ grantedBy: "0xowner",
307
+ },
308
+ ]);
309
+ expect(
310
+ await reactorDb
311
+ .selectFrom("OperationUserPermission")
312
+ .select(["documentId", "operationType", "userAddress", "grantedBy"])
313
+ .execute(),
314
+ ).toEqual([
315
+ {
316
+ documentId: "doc-1",
317
+ operationType: "SET_NAME",
318
+ userAddress: "0xreader",
319
+ grantedBy: "0xowner",
320
+ },
321
+ ]);
322
+ const protection = await reactorDb
323
+ .selectFrom("DocumentProtection")
324
+ .selectAll()
325
+ .executeTakeFirstOrThrow();
326
+ expect(protection).toMatchObject({
327
+ documentId: "doc-1",
328
+ protected: true,
329
+ ownerAddress: "0xowner",
330
+ });
331
+ const legacyCreated = await readModelDb
332
+ .selectFrom(`DocumentProtection${MIGRATED_TABLE_SUFFIX}`)
333
+ .select("createdAt")
334
+ .executeTakeFirstOrThrow();
335
+ expect((protection.createdAt as Date).getTime()).toBe(
336
+ (legacyCreated.createdAt as Date).getTime(),
337
+ );
338
+
339
+ expect(await readModelTables()).toEqual([
340
+ `DocumentPermission${MIGRATED_TABLE_SUFFIX}`,
341
+ `DocumentProtection${MIGRATED_TABLE_SUFFIX}`,
342
+ `OperationUserPermission${MIGRATED_TABLE_SUFFIX}`,
343
+ ]);
344
+ expect(logger.info).toHaveBeenCalledWith(
345
+ `Permission tables backfill: copied DocumentPermission=2, OperationUserPermission=1, DocumentProtection=1 from the read-model store to the reactor store; renamed the read-model tables to *${MIGRATED_TABLE_SUFFIX}`,
346
+ );
347
+ });
348
+
349
+ it("does nothing on a second run", async () => {
350
+ await seedLegacy();
351
+ await backfillPermissionTables(readModelDb, reactorDb, logger);
352
+
353
+ const outcome = await backfillPermissionTables(
354
+ readModelDb,
355
+ reactorDb,
356
+ logger,
357
+ );
358
+
359
+ expect(outcome).toEqual({ status: "already-backfilled" });
360
+ const count = await reactorDb
361
+ .selectFrom("DocumentPermission")
362
+ .select((eb) => eb.fn.countAll().as("count"))
363
+ .executeTakeFirstOrThrow();
364
+ expect(Number(count.count)).toBe(2);
365
+ });
366
+
367
+ it("does nothing on a fresh install", async () => {
368
+ const outcome = await backfillPermissionTables(
369
+ readModelDb,
370
+ reactorDb,
371
+ logger,
372
+ );
373
+
374
+ expect(outcome).toEqual({ status: "fresh-install" });
375
+ expect(await readModelTables()).toEqual([]);
376
+ expect(logger.info).not.toHaveBeenCalled();
377
+ expect(logger.warn).not.toHaveBeenCalled();
378
+ });
379
+
380
+ it("renames empty legacy tables without copying", async () => {
381
+ await stores.execRead(LEGACY_DDL);
382
+
383
+ const outcome = await backfillPermissionTables(
384
+ readModelDb,
385
+ reactorDb,
386
+ logger,
387
+ );
388
+
389
+ expect(outcome).toEqual({
390
+ status: "copied",
391
+ counts: {
392
+ DocumentPermission: 0,
393
+ OperationUserPermission: 0,
394
+ DocumentProtection: 0,
395
+ },
396
+ });
397
+ expect(await readModelTables()).toHaveLength(3);
398
+ });
399
+
400
+ it("only renames when an earlier boot copied the rows but stopped before renaming", async () => {
401
+ await seedLegacy();
402
+ await backfillPermissionTables(readModelDb, reactorDb, logger);
403
+ for (const table of [
404
+ "DocumentPermission",
405
+ "OperationUserPermission",
406
+ "DocumentProtection",
407
+ ]) {
408
+ await sql`ALTER TABLE ${sql.id(`${table}${MIGRATED_TABLE_SUFFIX}`)} RENAME TO ${sql.id(table)}`.execute(
409
+ readModelDb,
410
+ );
411
+ }
412
+
413
+ const outcome = await backfillPermissionTables(
414
+ readModelDb,
415
+ reactorDb,
416
+ logger,
417
+ );
418
+
419
+ expect(outcome.status).toBe("rename-completed");
420
+ const count = await reactorDb
421
+ .selectFrom("DocumentPermission")
422
+ .select((eb) => eb.fn.countAll().as("count"))
423
+ .executeTakeFirstOrThrow();
424
+ expect(Number(count.count)).toBe(2);
425
+ expect(await readModelTables()).toEqual([
426
+ `DocumentPermission${MIGRATED_TABLE_SUFFIX}`,
427
+ `DocumentProtection${MIGRATED_TABLE_SUFFIX}`,
428
+ `OperationUserPermission${MIGRATED_TABLE_SUFFIX}`,
429
+ ]);
430
+ });
431
+
432
+ it("refuses when the reactor store already holds different rows", async () => {
433
+ await seedLegacy();
434
+ await reactorDb
435
+ .insertInto("DocumentPermission")
436
+ .values({
437
+ documentId: "doc-9",
438
+ userAddress: "0xsomeone",
439
+ permission: "WRITE",
440
+ grantedBy: "0xowner",
441
+ })
442
+ .execute();
443
+
444
+ const outcome = await backfillPermissionTables(
445
+ readModelDb,
446
+ reactorDb,
447
+ logger,
448
+ );
449
+
450
+ expect(outcome.status).toBe("refused");
451
+ expect(logger.warn).toHaveBeenCalledTimes(1);
452
+ expect(await readModelTables()).toEqual([
453
+ "DocumentPermission",
454
+ "DocumentProtection",
455
+ "OperationUserPermission",
456
+ ]);
457
+ const count = await reactorDb
458
+ .selectFrom("DocumentPermission")
459
+ .select((eb) => eb.fn.countAll().as("count"))
460
+ .executeTakeFirstOrThrow();
461
+ expect(Number(count.count)).toBe(1);
462
+ });
463
+
464
+ it("rolls the copy back when a row does not fit the reactor table", async () => {
465
+ await seedLegacy();
466
+ await stores.execReactor(
467
+ `ALTER TABLE reactor."OperationUserPermission" ADD CONSTRAINT no_set_name CHECK ("operationType" <> 'SET_NAME')`,
468
+ );
469
+
470
+ await expect(
471
+ backfillPermissionTables(readModelDb, reactorDb, logger),
472
+ ).rejects.toThrow();
473
+
474
+ const count = await reactorDb
475
+ .selectFrom("DocumentPermission")
476
+ .select((eb) => eb.fn.countAll().as("count"))
477
+ .executeTakeFirstOrThrow();
478
+ expect(Number(count.count)).toBe(0);
479
+ expect(await readModelTables()).toEqual([
480
+ "DocumentPermission",
481
+ "DocumentProtection",
482
+ "OperationUserPermission",
483
+ ]);
484
+ });
485
+ });
486
+ }