ddd-ts-kit 0.111.0 → 0.112.0

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 (444) hide show
  1. package/CHANGELOG.md +112 -0
  2. package/README.fr.md +35 -6
  3. package/README.md +32 -5
  4. package/dist/alias/index.d.ts +2 -0
  5. package/dist/alias/index.d.ts.map +1 -1
  6. package/dist/alias/index.js +2 -0
  7. package/dist/alias/index.js.map +1 -1
  8. package/dist/alias/neighbour-sources.d.ts +17 -0
  9. package/dist/alias/neighbour-sources.d.ts.map +1 -0
  10. package/dist/alias/neighbour-sources.js +11 -0
  11. package/dist/alias/neighbour-sources.js.map +1 -0
  12. package/dist/alias/neighbour-suffix.d.ts +8 -0
  13. package/dist/alias/neighbour-suffix.d.ts.map +1 -0
  14. package/dist/alias/neighbour-suffix.js +10 -0
  15. package/dist/alias/neighbour-suffix.js.map +1 -0
  16. package/dist/build/rewrite-aliases.d.ts.map +1 -1
  17. package/dist/build/rewrite-aliases.js +11 -0
  18. package/dist/build/rewrite-aliases.js.map +1 -1
  19. package/dist/check/alias/build-scripts.d.ts.map +1 -1
  20. package/dist/check/alias/build-scripts.js +4 -8
  21. package/dist/check/alias/build-scripts.js.map +1 -1
  22. package/dist/check/alias/compiler-options-of.d.ts +3 -3
  23. package/dist/check/alias/compiler-options-of.js +17 -6
  24. package/dist/check/alias/compiler-options-of.js.map +1 -1
  25. package/dist/check/alias/neighbour-sources.d.ts +12 -0
  26. package/dist/check/alias/neighbour-sources.d.ts.map +1 -0
  27. package/dist/check/alias/neighbour-sources.js +80 -0
  28. package/dist/check/alias/neighbour-sources.js.map +1 -0
  29. package/dist/check/alias/script-steps.d.ts +11 -0
  30. package/dist/check/alias/script-steps.d.ts.map +1 -0
  31. package/dist/check/alias/script-steps.js +22 -0
  32. package/dist/check/alias/script-steps.js.map +1 -0
  33. package/dist/check/aspects/adapter-policies.d.ts +10 -0
  34. package/dist/check/aspects/adapter-policies.d.ts.map +1 -0
  35. package/dist/check/aspects/adapter-policies.js +104 -0
  36. package/dist/check/aspects/adapter-policies.js.map +1 -0
  37. package/dist/check/aspects/adapter-policy.d.ts +18 -0
  38. package/dist/check/aspects/adapter-policy.d.ts.map +1 -0
  39. package/dist/check/aspects/adapter-policy.js +2 -0
  40. package/dist/check/aspects/adapter-policy.js.map +1 -0
  41. package/dist/check/aspects/is-under-condition.d.ts +7 -0
  42. package/dist/check/aspects/is-under-condition.d.ts.map +1 -0
  43. package/dist/check/aspects/is-under-condition.js +50 -0
  44. package/dist/check/aspects/is-under-condition.js.map +1 -0
  45. package/dist/check/aspects/stated-policy.d.ts +18 -0
  46. package/dist/check/aspects/stated-policy.d.ts.map +1 -0
  47. package/dist/check/aspects/stated-policy.js +39 -0
  48. package/dist/check/aspects/stated-policy.js.map +1 -0
  49. package/dist/check/catalogue/architecture-rules.d.ts.map +1 -1
  50. package/dist/check/catalogue/architecture-rules.js +4 -0
  51. package/dist/check/catalogue/architecture-rules.js.map +1 -1
  52. package/dist/check/explain/imports-docs.d.ts.map +1 -1
  53. package/dist/check/explain/imports-docs.js +5 -3
  54. package/dist/check/explain/imports-docs.js.map +1 -1
  55. package/dist/check/explain/persistence-docs.d.ts.map +1 -1
  56. package/dist/check/explain/persistence-docs.js +21 -0
  57. package/dist/check/explain/persistence-docs.js.map +1 -1
  58. package/dist/check/explain/standard-docs.d.ts.map +1 -1
  59. package/dist/check/explain/standard-docs.js +16 -0
  60. package/dist/check/explain/standard-docs.js.map +1 -1
  61. package/dist/check/files/kit-content-types.d.ts +12 -0
  62. package/dist/check/files/kit-content-types.d.ts.map +1 -0
  63. package/dist/check/files/kit-content-types.js +38 -0
  64. package/dist/check/files/kit-content-types.js.map +1 -0
  65. package/dist/check/files/upload-content-defect.d.ts +7 -0
  66. package/dist/check/files/upload-content-defect.d.ts.map +1 -0
  67. package/dist/check/files/upload-content-defect.js +2 -0
  68. package/dist/check/files/upload-content-defect.js.map +1 -0
  69. package/dist/check/files/upload-content-defects.d.ts +9 -0
  70. package/dist/check/files/upload-content-defects.d.ts.map +1 -0
  71. package/dist/check/files/upload-content-defects.js +54 -0
  72. package/dist/check/files/upload-content-defects.js.map +1 -0
  73. package/dist/check/files/upload-declaration.d.ts +7 -0
  74. package/dist/check/files/upload-declaration.d.ts.map +1 -0
  75. package/dist/check/files/upload-declaration.js +2 -0
  76. package/dist/check/files/upload-declaration.js.map +1 -0
  77. package/dist/check/files/upload-declarations.d.ts +9 -0
  78. package/dist/check/files/upload-declarations.d.ts.map +1 -0
  79. package/dist/check/files/upload-declarations.js +41 -0
  80. package/dist/check/files/upload-declarations.js.map +1 -0
  81. package/dist/check/fixtures/alias-declared-fixtures.d.ts.map +1 -1
  82. package/dist/check/fixtures/alias-declared-fixtures.js +95 -1
  83. package/dist/check/fixtures/alias-declared-fixtures.js.map +1 -1
  84. package/dist/check/fixtures/fixtures.d.ts.map +1 -1
  85. package/dist/check/fixtures/fixtures.js +2 -0
  86. package/dist/check/fixtures/fixtures.js.map +1 -1
  87. package/dist/check/fixtures/imports-fixtures.d.ts.map +1 -1
  88. package/dist/check/fixtures/imports-fixtures.js +97 -0
  89. package/dist/check/fixtures/imports-fixtures.js.map +1 -1
  90. package/dist/check/fixtures/standard-fixtures.d.ts.map +1 -1
  91. package/dist/check/fixtures/standard-fixtures.js +42 -0
  92. package/dist/check/fixtures/standard-fixtures.js.map +1 -1
  93. package/dist/check/fixtures/upload-fixtures.d.ts +3 -0
  94. package/dist/check/fixtures/upload-fixtures.d.ts.map +1 -0
  95. package/dist/check/fixtures/upload-fixtures.js +94 -0
  96. package/dist/check/fixtures/upload-fixtures.js.map +1 -0
  97. package/dist/check/migrations/is-working-migration.d.ts +6 -0
  98. package/dist/check/migrations/is-working-migration.d.ts.map +1 -0
  99. package/dist/check/migrations/is-working-migration.js +8 -0
  100. package/dist/check/migrations/is-working-migration.js.map +1 -0
  101. package/dist/check/migrations/journal-entries.d.ts +4 -0
  102. package/dist/check/migrations/journal-entries.d.ts.map +1 -0
  103. package/dist/check/migrations/journal-entries.js +21 -0
  104. package/dist/check/migrations/journal-entries.js.map +1 -0
  105. package/dist/check/migrations/read-drizzle-migrations.d.ts.map +1 -1
  106. package/dist/check/migrations/read-drizzle-migrations.js +4 -9
  107. package/dist/check/migrations/read-drizzle-migrations.js.map +1 -1
  108. package/dist/check/modules/imported-modules.d.ts +4 -0
  109. package/dist/check/modules/imported-modules.d.ts.map +1 -0
  110. package/dist/check/modules/imported-modules.js +5 -0
  111. package/dist/check/modules/imported-modules.js.map +1 -0
  112. package/dist/check/modules/module-imports.d.ts +17 -0
  113. package/dist/check/modules/module-imports.d.ts.map +1 -0
  114. package/dist/check/modules/module-imports.js +23 -0
  115. package/dist/check/modules/module-imports.js.map +1 -0
  116. package/dist/check/project/assemble-project.d.ts.map +1 -1
  117. package/dist/check/project/assemble-project.js +1 -8
  118. package/dist/check/project/assemble-project.js.map +1 -1
  119. package/dist/check/project/file-severity.d.ts +4 -0
  120. package/dist/check/project/file-severity.d.ts.map +1 -0
  121. package/dist/check/project/file-severity.js +11 -0
  122. package/dist/check/project/file-severity.js.map +1 -0
  123. package/dist/check/project/load-project.js +1 -10
  124. package/dist/check/project/load-project.js.map +1 -1
  125. package/dist/check/project/not-in-the-tree.d.ts +10 -0
  126. package/dist/check/project/not-in-the-tree.d.ts.map +1 -0
  127. package/dist/check/project/not-in-the-tree.js +17 -0
  128. package/dist/check/project/not-in-the-tree.js.map +1 -0
  129. package/dist/check/project/skipped-directories.d.ts +3 -0
  130. package/dist/check/project/skipped-directories.d.ts.map +1 -0
  131. package/dist/check/project/skipped-directories.js +3 -0
  132. package/dist/check/project/skipped-directories.js.map +1 -0
  133. package/dist/check/project/walk-directory.d.ts.map +1 -1
  134. package/dist/check/project/walk-directory.js +1 -1
  135. package/dist/check/project/walk-directory.js.map +1 -1
  136. package/dist/check/rules/aspects-timeout-covers-delegate.d.ts +3 -0
  137. package/dist/check/rules/aspects-timeout-covers-delegate.d.ts.map +1 -0
  138. package/dist/check/rules/aspects-timeout-covers-delegate.js +179 -0
  139. package/dist/check/rules/aspects-timeout-covers-delegate.js.map +1 -0
  140. package/dist/check/rules/files-upload-content-rule.d.ts +3 -0
  141. package/dist/check/rules/files-upload-content-rule.d.ts.map +1 -0
  142. package/dist/check/rules/files-upload-content-rule.js +25 -0
  143. package/dist/check/rules/files-upload-content-rule.js.map +1 -0
  144. package/dist/check/rules/imports-alias-declared.d.ts.map +1 -1
  145. package/dist/check/rules/imports-alias-declared.js +3 -1
  146. package/dist/check/rules/imports-alias-declared.js.map +1 -1
  147. package/dist/check/rules/imports-alias-only.d.ts.map +1 -1
  148. package/dist/check/rules/imports-alias-only.js +86 -7
  149. package/dist/check/rules/imports-alias-only.js.map +1 -1
  150. package/dist/check/rules/module-no-cycle.d.ts.map +1 -1
  151. package/dist/check/rules/module-no-cycle.js +12 -34
  152. package/dist/check/rules/module-no-cycle.js.map +1 -1
  153. package/dist/cli-error.d.ts +7 -2
  154. package/dist/cli-error.d.ts.map +1 -1
  155. package/dist/cli-error.js +8 -2
  156. package/dist/cli-error.js.map +1 -1
  157. package/dist/commands/generate.js +1 -0
  158. package/dist/commands/generate.js.map +1 -1
  159. package/dist/commands/knex-generate.d.ts.map +1 -1
  160. package/dist/commands/knex-generate.js +2 -1
  161. package/dist/commands/knex-generate.js.map +1 -1
  162. package/dist/commands/move-to-module.d.ts +6 -1
  163. package/dist/commands/move-to-module.d.ts.map +1 -1
  164. package/dist/commands/move-to-module.js +28 -3
  165. package/dist/commands/move-to-module.js.map +1 -1
  166. package/dist/commands/prisma-generate.d.ts.map +1 -1
  167. package/dist/commands/prisma-generate.js +2 -1
  168. package/dist/commands/prisma-generate.js.map +1 -1
  169. package/dist/commands/typeorm-generate.d.ts.map +1 -1
  170. package/dist/commands/typeorm-generate.js +2 -1
  171. package/dist/commands/typeorm-generate.js.map +1 -1
  172. package/dist/commands/verify-merge.d.ts +4 -2
  173. package/dist/commands/verify-merge.d.ts.map +1 -1
  174. package/dist/commands/verify-merge.js +8 -5
  175. package/dist/commands/verify-merge.js.map +1 -1
  176. package/dist/generate/generate-options.d.ts +3 -0
  177. package/dist/generate/generate-options.d.ts.map +1 -1
  178. package/dist/generate/generate-options.js +1 -0
  179. package/dist/generate/generate-options.js.map +1 -1
  180. package/dist/generate/kind.d.ts +2 -0
  181. package/dist/generate/kind.d.ts.map +1 -1
  182. package/dist/generate/kinds/file-upload/upload-file-vars.d.ts +9 -0
  183. package/dist/generate/kinds/file-upload/upload-file-vars.d.ts.map +1 -0
  184. package/dist/generate/kinds/file-upload/upload-file-vars.js +39 -0
  185. package/dist/generate/kinds/file-upload/upload-file-vars.js.map +1 -0
  186. package/dist/generate/kinds/file-upload/write-nest-upload-controller.d.ts.map +1 -1
  187. package/dist/generate/kinds/file-upload/write-nest-upload-controller.js +2 -1
  188. package/dist/generate/kinds/file-upload/write-nest-upload-controller.js.map +1 -1
  189. package/dist/generate/kinds/file-upload/write-upload-controller.d.ts.map +1 -1
  190. package/dist/generate/kinds/file-upload/write-upload-controller.js +2 -1
  191. package/dist/generate/kinds/file-upload/write-upload-controller.js.map +1 -1
  192. package/dist/generate/kinds/file-upload-kind.d.ts.map +1 -1
  193. package/dist/generate/kinds/file-upload-kind.js +5 -3
  194. package/dist/generate/kinds/file-upload-kind.js.map +1 -1
  195. package/dist/generate/kinds/read-model/list-read-model.d.ts +17 -0
  196. package/dist/generate/kinds/read-model/list-read-model.d.ts.map +1 -0
  197. package/dist/generate/kinds/read-model/list-read-model.js +22 -0
  198. package/dist/generate/kinds/read-model/list-read-model.js.map +1 -0
  199. package/dist/generate/kinds/read-model-kind.d.ts.map +1 -1
  200. package/dist/generate/kinds/read-model-kind.js +19 -0
  201. package/dist/generate/kinds/read-model-kind.js.map +1 -1
  202. package/dist/generate/kinds/service-kind.d.ts.map +1 -1
  203. package/dist/generate/kinds/service-kind.js +8 -0
  204. package/dist/generate/kinds/service-kind.js.map +1 -1
  205. package/dist/generate/kinds/wire-context.d.ts.map +1 -1
  206. package/dist/generate/kinds/wire-context.js +4 -0
  207. package/dist/generate/kinds/wire-context.js.map +1 -1
  208. package/dist/generate/shared-writes.d.ts.map +1 -1
  209. package/dist/generate/shared-writes.js +3 -2
  210. package/dist/generate/shared-writes.js.map +1 -1
  211. package/dist/help/tool-notes.d.ts.map +1 -1
  212. package/dist/help/tool-notes.js +5 -1
  213. package/dist/help/tool-notes.js.map +1 -1
  214. package/dist/help/usage.js +1 -1
  215. package/dist/help/work-notes.d.ts.map +1 -1
  216. package/dist/help/work-notes.js +9 -2
  217. package/dist/help/work-notes.js.map +1 -1
  218. package/dist/init/files/end-to-end-files.d.ts.map +1 -1
  219. package/dist/init/files/end-to-end-files.js +3 -0
  220. package/dist/init/files/end-to-end-files.js.map +1 -1
  221. package/dist/init/files/persistence-files.d.ts.map +1 -1
  222. package/dist/init/files/persistence-files.js +8 -2
  223. package/dist/init/files/persistence-files.js.map +1 -1
  224. package/dist/init/files/script-files.d.ts +2 -1
  225. package/dist/init/files/script-files.d.ts.map +1 -1
  226. package/dist/init/files/script-files.js +6 -1
  227. package/dist/init/files/script-files.js.map +1 -1
  228. package/dist/init/is-team-file.d.ts +2 -2
  229. package/dist/init/is-team-file.d.ts.map +1 -1
  230. package/dist/init/is-team-file.js +5 -4
  231. package/dist/init/is-team-file.js.map +1 -1
  232. package/dist/init/list-read-models.d.ts +17 -0
  233. package/dist/init/list-read-models.d.ts.map +1 -0
  234. package/dist/init/list-read-models.js +74 -0
  235. package/dist/init/list-read-models.js.map +1 -0
  236. package/dist/init/manifest/database-scripts.d.ts.map +1 -1
  237. package/dist/init/manifest/database-scripts.js +8 -0
  238. package/dist/init/manifest/database-scripts.js.map +1 -1
  239. package/dist/init/manifest/versions.js +25 -25
  240. package/dist/init/rewire-contexts.d.ts +4 -2
  241. package/dist/init/rewire-contexts.d.ts.map +1 -1
  242. package/dist/init/rewire-contexts.js +10 -2
  243. package/dist/init/rewire-contexts.js.map +1 -1
  244. package/dist/init/run-init.js +1 -1
  245. package/dist/init/run-init.js.map +1 -1
  246. package/dist/init/wiring-notes.js +1 -1
  247. package/dist/init/wiring-notes.js.map +1 -1
  248. package/dist/init/write-type-script-configs.d.ts +3 -1
  249. package/dist/init/write-type-script-configs.d.ts.map +1 -1
  250. package/dist/init/write-type-script-configs.js +14 -2
  251. package/dist/init/write-type-script-configs.js.map +1 -1
  252. package/dist/merge/format-merge-report.js +5 -2
  253. package/dist/merge/format-merge-report.js.map +1 -1
  254. package/dist/merge/land-drizzle.d.ts +4 -0
  255. package/dist/merge/land-drizzle.d.ts.map +1 -1
  256. package/dist/merge/land-drizzle.js +6 -1
  257. package/dist/merge/land-drizzle.js.map +1 -1
  258. package/dist/merge/land-up-to-date.d.ts +15 -0
  259. package/dist/merge/land-up-to-date.d.ts.map +1 -0
  260. package/dist/merge/land-up-to-date.js +76 -0
  261. package/dist/merge/land-up-to-date.js.map +1 -0
  262. package/dist/merge/lost/format-merge-verification.d.ts +2 -1
  263. package/dist/merge/lost/format-merge-verification.d.ts.map +1 -1
  264. package/dist/merge/lost/format-merge-verification.js +13 -2
  265. package/dist/merge/lost/format-merge-verification.js.map +1 -1
  266. package/dist/merge/lost/merge-verification.d.ts +4 -0
  267. package/dist/merge/lost/merge-verification.d.ts.map +1 -1
  268. package/dist/merge/lost/verify-merges.d.ts +2 -1
  269. package/dist/merge/lost/verify-merges.d.ts.map +1 -1
  270. package/dist/merge/lost/verify-merges.js +10 -4
  271. package/dist/merge/lost/verify-merges.js.map +1 -1
  272. package/dist/merge/merge-trunk.d.ts +2 -1
  273. package/dist/merge/merge-trunk.d.ts.map +1 -1
  274. package/dist/merge/merge-trunk.js +5 -2
  275. package/dist/merge/merge-trunk.js.map +1 -1
  276. package/dist/merge/next-steps.d.ts +3 -0
  277. package/dist/merge/next-steps.d.ts.map +1 -1
  278. package/dist/merge/next-steps.js +3 -0
  279. package/dist/merge/next-steps.js.map +1 -1
  280. package/dist/merge/order/applied-under-old-name.d.ts +13 -0
  281. package/dist/merge/order/applied-under-old-name.d.ts.map +1 -0
  282. package/dist/merge/order/applied-under-old-name.js +14 -0
  283. package/dist/merge/order/applied-under-old-name.js.map +1 -0
  284. package/dist/merge/order/comes-after.d.ts +8 -0
  285. package/dist/merge/order/comes-after.d.ts.map +1 -0
  286. package/dist/merge/order/comes-after.js +9 -0
  287. package/dist/merge/order/comes-after.js.map +1 -0
  288. package/dist/merge/order/drizzle-order.d.ts +11 -0
  289. package/dist/merge/order/drizzle-order.d.ts.map +1 -0
  290. package/dist/merge/order/drizzle-order.js +64 -0
  291. package/dist/merge/order/drizzle-order.js.map +1 -0
  292. package/dist/merge/order/in-order.d.ts +4 -0
  293. package/dist/merge/order/in-order.d.ts.map +1 -0
  294. package/dist/merge/order/in-order.js +6 -0
  295. package/dist/merge/order/in-order.js.map +1 -0
  296. package/dist/merge/order/migration-order-finding.d.ts +18 -0
  297. package/dist/merge/order/migration-order-finding.d.ts.map +1 -0
  298. package/dist/merge/order/migration-order-finding.js +2 -0
  299. package/dist/merge/order/migration-order-finding.js.map +1 -0
  300. package/dist/merge/order/migration-order.d.ts +12 -0
  301. package/dist/merge/order/migration-order.d.ts.map +1 -0
  302. package/dist/merge/order/migration-order.js +21 -0
  303. package/dist/merge/order/migration-order.js.map +1 -0
  304. package/dist/merge/order/migrations-of-commit.d.ts +7 -0
  305. package/dist/merge/order/migrations-of-commit.d.ts.map +1 -0
  306. package/dist/merge/order/migrations-of-commit.js +43 -0
  307. package/dist/merge/order/migrations-of-commit.js.map +1 -0
  308. package/dist/merge/order/migrations-of-tree.d.ts +9 -0
  309. package/dist/merge/order/migrations-of-tree.d.ts.map +1 -0
  310. package/dist/merge/order/migrations-of-tree.js +25 -0
  311. package/dist/merge/order/migrations-of-tree.js.map +1 -0
  312. package/dist/merge/order/named-order.d.ts +11 -0
  313. package/dist/merge/order/named-order.d.ts.map +1 -0
  314. package/dist/merge/order/named-order.js +36 -0
  315. package/dist/merge/order/named-order.js.map +1 -0
  316. package/dist/merge/order/ordered-migration.d.ts +12 -0
  317. package/dist/merge/order/ordered-migration.d.ts.map +1 -0
  318. package/dist/merge/order/ordered-migration.js +2 -0
  319. package/dist/merge/order/ordered-migration.js.map +1 -0
  320. package/dist/merge/order/trunk-to-follow.d.ts +9 -0
  321. package/dist/merge/order/trunk-to-follow.d.ts.map +1 -0
  322. package/dist/merge/order/trunk-to-follow.js +2 -0
  323. package/dist/merge/order/trunk-to-follow.js.map +1 -0
  324. package/dist/merge/renumber-migrations.d.ts +14 -5
  325. package/dist/merge/renumber-migrations.d.ts.map +1 -1
  326. package/dist/merge/renumber-migrations.js +141 -60
  327. package/dist/merge/renumber-migrations.js.map +1 -1
  328. package/dist/merge/restamp-class.d.ts +9 -0
  329. package/dist/merge/restamp-class.d.ts.map +1 -0
  330. package/dist/merge/restamp-class.js +30 -0
  331. package/dist/merge/restamp-class.js.map +1 -0
  332. package/dist/migration/index.d.ts +1 -0
  333. package/dist/migration/index.d.ts.map +1 -1
  334. package/dist/migration/index.js +1 -0
  335. package/dist/migration/index.js.map +1 -1
  336. package/dist/migration/plan-replay.d.ts.map +1 -1
  337. package/dist/migration/plan-replay.js +2 -1
  338. package/dist/migration/plan-replay.js.map +1 -1
  339. package/dist/migration/refuse-taken-migration-name.d.ts +8 -0
  340. package/dist/migration/refuse-taken-migration-name.d.ts.map +1 -0
  341. package/dist/migration/refuse-taken-migration-name.js +22 -0
  342. package/dist/migration/refuse-taken-migration-name.js.map +1 -0
  343. package/dist/migration/replay-drizzle-migrations.d.ts +5 -0
  344. package/dist/migration/replay-drizzle-migrations.d.ts.map +1 -1
  345. package/dist/migration/replay-drizzle-migrations.js +10 -2
  346. package/dist/migration/replay-drizzle-migrations.js.map +1 -1
  347. package/dist/migration/replay-plan.d.ts +2 -2
  348. package/dist/migration/replay-plan.d.ts.map +1 -1
  349. package/dist/migration/restore-migration-folder.d.ts +6 -2
  350. package/dist/migration/restore-migration-folder.d.ts.map +1 -1
  351. package/dist/migration/restore-migration-folder.js +14 -7
  352. package/dist/migration/restore-migration-folder.js.map +1 -1
  353. package/dist/migration/snapshot-migration-folder.d.ts +7 -0
  354. package/dist/migration/snapshot-migration-folder.d.ts.map +1 -0
  355. package/dist/migration/snapshot-migration-folder.js +12 -0
  356. package/dist/migration/snapshot-migration-folder.js.map +1 -0
  357. package/dist/move-to-module/before-the-move.d.ts +5 -0
  358. package/dist/move-to-module/before-the-move.d.ts.map +1 -0
  359. package/dist/move-to-module/before-the-move.js +13 -0
  360. package/dist/move-to-module/before-the-move.js.map +1 -0
  361. package/dist/move-to-module/closed-cycles.d.ts +21 -0
  362. package/dist/move-to-module/closed-cycles.d.ts.map +1 -0
  363. package/dist/move-to-module/closed-cycles.js +66 -0
  364. package/dist/move-to-module/closed-cycles.js.map +1 -0
  365. package/dist/move-to-module/cycle-lines.d.ts +7 -0
  366. package/dist/move-to-module/cycle-lines.d.ts.map +1 -0
  367. package/dist/move-to-module/cycle-lines.js +22 -0
  368. package/dist/move-to-module/cycle-lines.js.map +1 -0
  369. package/dist/move-to-module/cycle-refusal.d.ts +17 -0
  370. package/dist/move-to-module/cycle-refusal.d.ts.map +1 -0
  371. package/dist/move-to-module/cycle-refusal.js +25 -0
  372. package/dist/move-to-module/cycle-refusal.js.map +1 -0
  373. package/dist/move-to-module/index.d.ts +5 -0
  374. package/dist/move-to-module/index.d.ts.map +1 -1
  375. package/dist/move-to-module/index.js +4 -0
  376. package/dist/move-to-module/index.js.map +1 -1
  377. package/dist/move-to-module/module-imports-in.d.ts +11 -0
  378. package/dist/move-to-module/module-imports-in.d.ts.map +1 -0
  379. package/dist/move-to-module/module-imports-in.js +32 -0
  380. package/dist/move-to-module/module-imports-in.js.map +1 -0
  381. package/dist/move-to-module/moves-into-module.d.ts.map +1 -1
  382. package/dist/move-to-module/moves-into-module.js +6 -18
  383. package/dist/move-to-module/moves-into-module.js.map +1 -1
  384. package/dist/move-to-module/place-in-module.d.ts +13 -0
  385. package/dist/move-to-module/place-in-module.d.ts.map +1 -0
  386. package/dist/move-to-module/place-in-module.js +24 -0
  387. package/dist/move-to-module/place-in-module.js.map +1 -0
  388. package/dist/move-to-module/watch-cycles.d.ts +19 -0
  389. package/dist/move-to-module/watch-cycles.d.ts.map +1 -0
  390. package/dist/move-to-module/watch-cycles.js +51 -0
  391. package/dist/move-to-module/watch-cycles.js.map +1 -0
  392. package/dist/run.js +1 -1
  393. package/dist/run.js.map +1 -1
  394. package/dist/slice/format-slice-verification.d.ts.map +1 -1
  395. package/dist/slice/format-slice-verification.js +11 -5
  396. package/dist/slice/format-slice-verification.js.map +1 -1
  397. package/dist/stack/alias-vars.d.ts +3 -1
  398. package/dist/stack/alias-vars.d.ts.map +1 -1
  399. package/dist/stack/alias-vars.js +5 -2
  400. package/dist/stack/alias-vars.js.map +1 -1
  401. package/dist/version.d.ts +1 -1
  402. package/dist/version.js +1 -1
  403. package/package.json +6 -6
  404. package/templates/file-upload/controller.spec.ts.tpl +66 -2
  405. package/templates/file-upload/controller.ts.tpl +12 -0
  406. package/templates/file-upload/handler.spec.ts.tpl +3 -3
  407. package/templates/init/README.md.tpl +20 -1
  408. package/templates/init/e2e/jest.e2e.config.js.tpl +5 -0
  409. package/templates/init/e2e/test-database-setup.ts.tpl +120 -0
  410. package/templates/init/e2e/vitest.e2e.config.ts.tpl +2 -1
  411. package/templates/init/platform/composition/compose-application.container.ts.tpl +5 -0
  412. package/templates/init/platform/composition/compose-application.manual.ts.tpl +20 -3
  413. package/templates/init/platform/composition/create-platform.manual.ts.tpl +6 -0
  414. package/templates/init/platform/composition/platform-options.manual.ts.tpl +11 -0
  415. package/templates/init/platform/composition/platform.manual.ts.tpl +7 -0
  416. package/templates/init/platform/composition/platform.module.container.ts.tpl +34 -0
  417. package/templates/init/platform/database/connect-to-database.drizzle.ts.tpl +7 -2
  418. package/templates/init/platform/database/open-test-database.drizzle.ts.tpl +8 -3
  419. package/templates/init/platform/database/open-test-database.knex.ts.tpl +8 -3
  420. package/templates/init/platform/database/open-test-database.prisma.ts.tpl +8 -3
  421. package/templates/init/platform/database/open-test-database.typeorm.ts.tpl +8 -3
  422. package/templates/init/platform/database/read-model-tables.ts.tpl +67 -0
  423. package/templates/init/platform/database/test-database-budget.ts.tpl +108 -0
  424. package/templates/init/platform/database/test-database-clone.ts.tpl +52 -161
  425. package/templates/init/platform/database/test-database-copy.ts.tpl +76 -0
  426. package/templates/init/platform/database/test-database-emptying.ts.tpl +209 -0
  427. package/templates/init/platform/database/test-database-kept.ts.tpl +32 -0
  428. package/templates/init/platform/database/test-database-leftovers.ts.tpl +52 -0
  429. package/templates/init/platform/database/test-database-run.ts.tpl +99 -0
  430. package/templates/init/platform/database/test-database-server.ts.tpl +140 -0
  431. package/templates/init/platform/database/test-database-session.ts.tpl +141 -0
  432. package/templates/init/platform/database/test-database-teardown.ts.tpl +11 -1
  433. package/templates/init/platform/resilience/default-policy.ts.tpl +8 -1
  434. package/templates/init/rules/infrastructure.md.tpl +2 -2
  435. package/templates/init/scripts/read-model-tables.ts.tpl +83 -0
  436. package/templates/nest-controller/upload.spec.ts.tpl +67 -2
  437. package/templates/nest-controller/upload.ts.tpl +12 -0
  438. package/templates/pipeline/package-scripts.json +1 -1
  439. package/templates/presets/identity/context/infrastructure/persistence/drizzle-account.repository.drizzle.ts.tpl +5 -9
  440. package/templates/presets/identity/context/infrastructure/persistence/drizzle-delegation.repository.drizzle.ts.tpl +5 -9
  441. package/templates/presets/identity/context/infrastructure/persistence/drizzle-invitation.repository.drizzle.ts.tpl +5 -9
  442. package/templates/presets/identity/context/infrastructure/persistence/drizzle-member.repository.drizzle.ts.tpl +5 -9
  443. package/templates/presets/identity/context/infrastructure/persistence/drizzle-organisation.repository.drizzle.ts.tpl +5 -9
  444. package/templates/repository/drizzle/repository.ts.tpl +6 -9
@@ -0,0 +1,76 @@
1
+ import { setTimeout as later } from 'node:timers/promises';
2
+ import type pg from 'pg';
3
+ import { codeOf, copying, databaseOf, dropping, oneAtATime, onServer } from './test-database-server{{alias.sibling}}';
4
+
5
+ /** The SQLSTATE of a server that refuses to copy a database other sessions are connected to. */
6
+ const OTHER_SESSIONS = '55006';
7
+ /** The SQLSTATE of a statement somebody cancelled: its session lives. */
8
+ const CANCELLED = '57014';
9
+ /** The SQLSTATE of a session that ended: the server stopped it (57P01 and its neighbours), the connection failed (class 08). */
10
+ const SESSION_ENDED = /^(?:57P|08)/;
11
+
12
+ const REFUSALS: Readonly<Record<string, (database: string) => string>> = {
13
+ [OTHER_SESSIONS]: (database) =>
14
+ `The database "${database}" has other sessions (an application that runs, a console, a migration): PostgreSQL copies a database only while nobody else is connected to it. Close them, or give the tests a database of their own.`,
15
+ '42501': (database) =>
16
+ `The role of DATABASE_URL may not copy the database "${database}", and each spec file runs on a copy of it: the role creates databases (alter role <role> createdb) and owns that one (alter database "${database}" owner to <role>).`,
17
+ '3D000': (database) =>
18
+ `The server of DATABASE_URL has no database "${database}": create it, then migrate it (npm run db:migrate).`,
19
+ // The sessions of a run set no timeout (test-database-server.ts): the statement was cancelled from outside, and is not taken again.
20
+ [CANCELLED]: (database) =>
21
+ `The copy of the database "${database}" was cancelled: the run sets no statement timeout on its own sessions, so somebody or a tool of the server cancelled the statement (pg_cancel_backend, a job that cancels the statements that last). Run again, or find what cancels it in the log of the server.`,
22
+ };
23
+
24
+ /**
25
+ * Copies the migrated database `url` names as `template`, one run at a time on the server: two
26
+ * copies at once slow each other. The copy has a session of its own, and a session can end while
27
+ * it works (the server restarts or fails over, an administrator ends it): what is left of the copy
28
+ * is then dropped and the copy made once more, on another session.
29
+ */
30
+ export async function copied(url: string, template: string): Promise<void> {
31
+ const once = (): Promise<void> =>
32
+ onServer(url, (server) => oneAtATime(server, '0', () => copiedOn(server, databaseOf(url), template)));
33
+
34
+ try {
35
+ await once().catch(async (error: unknown) => {
36
+ if (!sessionEnded(error)) {
37
+ throw error;
38
+ }
39
+
40
+ await later(1000);
41
+ await onServer(url, (server) => server.query(dropping(template)));
42
+ await once();
43
+ });
44
+ } catch (error) {
45
+ throw notCopied(databaseOf(url), error);
46
+ }
47
+ }
48
+
49
+ /** The client says it without a SQLSTATE, the server with one of class 08 or 57P: a cancelled statement (57014) is not an ended session. */
50
+ function sessionEnded(error: unknown): boolean {
51
+ return codeOf(error) === ''
52
+ ? /connection|terminat|queryable/i.test(String(error))
53
+ : SESSION_ENDED.test(codeOf(error));
54
+ }
55
+
56
+ /**
57
+ * Another session on the migrated database makes the server wait 5 seconds for it to leave, then
58
+ * refuse: asked once more, since a migration or a console that was ending has ended by then.
59
+ */
60
+ async function copiedOn(server: pg.Client, database: string, template: string): Promise<void> {
61
+ await server.query(copying(database, template)).catch((error: unknown) => {
62
+ if (codeOf(error) !== OTHER_SESSIONS) {
63
+ throw error;
64
+ }
65
+
66
+ return server.query(copying(database, template));
67
+ });
68
+ }
69
+
70
+ function notCopied(database: string, error: unknown): unknown {
71
+ const refusal = REFUSALS[codeOf(error)]?.(database);
72
+ const twice = `The session that copied the database "${database}" ended twice before the copy did (${String(error)}): the server restarts, fails over, or ends the sessions of the run. Run again once it is steady.`;
73
+ const said = refusal ?? (sessionEnded(error) ? twice : undefined);
74
+
75
+ return said === undefined ? error : new Error(said, { cause: error });
76
+ }
@@ -0,0 +1,209 @@
1
+ import type pg from 'pg';
2
+ import { KEPT_TABLES } from './test-database-kept{{alias.sibling}}';
3
+ import { codeOf, connectedTo, databaseOf } from './test-database-server{{alias.sibling}}';
4
+
5
+ /** What a run did to its copy of the migrated database. */
6
+ export interface Emptied {
7
+ /** The tables it emptied that held rows. */
8
+ readonly held: readonly string[];
9
+ /** The tables it left with their rows because the role of the run may not empty them. */
10
+ readonly ofAnotherRole: readonly string[];
11
+ /** The names of KEPT_TABLES that are no table of the copy. */
12
+ readonly unmatched: readonly string[];
13
+ /** The bytes the tables it emptied took. */
14
+ readonly bytes: number;
15
+ }
16
+
17
+ interface Table {
18
+ /** `<schema>.<table>`, as KEPT_TABLES names it and as the server stores it. */
19
+ readonly name: string;
20
+ readonly schema: string;
21
+ /** The same, as a statement names it. */
22
+ readonly quoted: string;
23
+ /** The role of the run may empty it: it owns it, a truncate that restarts the sequences being the right of the owner, and may use its schema. */
24
+ readonly owned: boolean;
25
+ /** Its rows are in its partitions, which are emptied with it. */
26
+ readonly partitioned: boolean;
27
+ readonly bytes: string;
28
+ }
29
+
30
+ interface Link {
31
+ readonly from: string;
32
+ readonly to: string;
33
+ readonly schema: string;
34
+ }
35
+
36
+ /** What a name of KEPT_TABLES looks like: a schema, then a table or `*`, as the server stores them, without quotes; a dot ends the schema. */
37
+ const NAME = /^[^.*"]+\.(?:[^.*"]+|\*)$/;
38
+ /** What the generated list names that a project may not hold yet: nothing is said when one of these is no table. */
39
+ {{#if orm.drizzle}}
40
+ const MAY_BE_ABSENT = ['drizzle.__drizzle_migrations', 'audit.*'];
41
+ {{/if}}
42
+ {{#if orm.knex}}
43
+ const MAY_BE_ABSENT = ['public.knex_migrations', 'public.knex_migrations_lock', 'audit.*'];
44
+ {{/if}}
45
+ {{#if orm.prisma}}
46
+ const MAY_BE_ABSENT = ['public._prisma_migrations', 'audit.*'];
47
+ {{/if}}
48
+ {{#if orm.typeorm}}
49
+ const MAY_BE_ABSENT = ['public.migrations', 'public.typeorm_metadata', 'audit.*'];
50
+ {{/if}}
51
+
52
+ /** The tables of the project: not those of the server, nor those of an extension, which keeps its own, nor a partition, which goes with its table. */
53
+ const TABLES = `select n.nspname || '.' || c.relname as name, n.nspname as schema, format('%I.%I', n.nspname, c.relname) as quoted,
54
+ pg_has_role(c.relowner, 'USAGE') and has_schema_privilege(n.oid, 'USAGE') as owned,
55
+ c.relkind = 'p' as partitioned, pg_total_relation_size(c.oid)::text as bytes
56
+ from pg_class c join pg_namespace n on n.oid = c.relnamespace
57
+ where c.relkind in ('r', 'p') and not c.relispartition
58
+ and n.nspname not in ('pg_catalog', 'information_schema') and n.nspname not like 'pg\\_%'
59
+ and not exists (select 1 from pg_depend d where d.classid = 'pg_class'::regclass and d.objid = c.oid and d.deptype = 'e')
60
+ order by 1`;
61
+
62
+ const NAMED = (alias: string, name: string): string =>
63
+ `join pg_class ${alias} on ${alias}.oid = ${name} join pg_namespace ${alias}n on ${alias}n.oid = ${alias}.relnamespace`;
64
+
65
+ /** A table, the one it references or is a partition of, and its own schema. */
66
+ const LINK = `rn.nspname || '.' || r.relname as "from", fn.nspname || '.' || f.relname as "to", rn.nspname as schema`;
67
+
68
+ const REFERENCES = `select ${LINK}
69
+ from pg_constraint k ${NAMED('r', 'k.conrelid')} ${NAMED('f', 'k.confrelid')} where k.contype = 'f'`;
70
+
71
+ /** Each partition and the table it is a partition of. */
72
+ const PARTITIONS = `select ${LINK}
73
+ from pg_inherits i ${NAMED('r', 'i.inhrelid')} ${NAMED('f', 'i.inhparent')} where r.relispartition`;
74
+
75
+ /** The materialized views that hold a result and that the role may refresh, the oldest first: one may read another. */
76
+ const VIEWS = `select format('%I.%I', n.nspname, c.relname) as quoted
77
+ from pg_class c join pg_namespace n on n.oid = c.relnamespace
78
+ where c.relkind = 'm' and c.relispopulated and pg_has_role(c.relowner, 'USAGE') order by c.oid`;
79
+
80
+ /**
81
+ * Empties the copy `name` of the migrated database `url` names: every table of the project but
82
+ * those KEPT_TABLES lists (test-database-kept.ts) and those the role may not empty, in one
83
+ * statement, without `cascade` and each table `only`, so that a kept table is never emptied
84
+ * through a foreign key nor through the table it inherits from. A spec file then starts from the
85
+ * tables of the migrated database and no row, as on PostgreSQL in process, whatever a migration,
86
+ * an application or a spec stored there. The materialized views are refreshed, so that none shows
87
+ * the rows that are gone. A sequence no column owns keeps its value.
88
+ */
89
+ export async function emptied(url: string, name: string): Promise<Emptied> {
90
+ const copy = await connectedTo(url, name);
91
+
92
+ try {
93
+ refuseAShapelessName();
94
+
95
+ const tables = (await copy.query<Table>(TABLES)).rows;
96
+ const emptying = tables.filter((table) => table.owned && !kept(table));
97
+
98
+ if (tables.length === 0) {
99
+ throw new Error(`The database "${databaseOf(url)}" holds no table: migrate it first (npm run db:migrate).`);
100
+ }
101
+
102
+ await refuseWhatAKeptTableLoses(copy, tables, emptying);
103
+
104
+ const held = await holdingRows(copy, emptying);
105
+
106
+ await empty(copy, emptying);
107
+
108
+ return {
109
+ held,
110
+ ofAnotherRole: tables.filter((table) => !table.owned && !kept(table)).map((table) => table.name),
111
+ unmatched: unmatched(tables),
112
+ bytes: emptying.reduce((sum, table) => sum + Number(table.bytes), 0),
113
+ };
114
+ } catch (error) {
115
+ throw codeOf(error) === '' ? error : notEmptied(databaseOf(url), error);
116
+ } finally {
117
+ await copy.end();
118
+ }
119
+ }
120
+
121
+ function kept(table: Pick<Table, 'name' | 'schema'>): boolean {
122
+ return KEPT_TABLES.some((listed) => names(listed, table));
123
+ }
124
+
125
+ /** A listed name is the table, letter for letter, or its schema followed by `.*`, whatever the name of the table holds. */
126
+ function names(listed: string, { name, schema }: Pick<Table, 'name' | 'schema'>): boolean {
127
+ return listed === name || listed === `${schema}.*`;
128
+ }
129
+
130
+ function unmatched(tables: readonly Table[]): string[] {
131
+ return KEPT_TABLES.filter((one) => !MAY_BE_ABSENT.includes(one) && !tables.some((table) => names(one, table)));
132
+ }
133
+
134
+ function refuseAShapelessName(): void {
135
+ const shapeless = KEPT_TABLES.find((listed) => !NAME.test(listed));
136
+
137
+ if (shapeless !== undefined) {
138
+ throw new Error(
139
+ `KEPT_TABLES of test/support/test-database-kept.ts holds "${shapeless}": a name is <schema>.<table>, or <schema>.* for every table of a schema, written as the server stores it and without quotes (a table created as "Countries" is public.Countries, one created as countries or Countries without quotes is public.countries); a table whose name holds a dot or a quote is kept through <schema>.* only.`,
140
+ );
141
+ }
142
+ }
143
+
144
+ /**
145
+ * Said before the server refuses it without a name, or does it without a word: a table that keeps
146
+ * its rows and references an emptied one, which no `cascade` empties for it, and a kept partition
147
+ * of an emptied table, which goes with its table.
148
+ */
149
+ async function refuseWhatAKeptTableLoses(
150
+ copy: pg.Client,
151
+ tables: readonly Table[],
152
+ emptying: readonly Table[],
153
+ ): Promise<void> {
154
+ const partitions = (await copy.query<Link>(PARTITIONS)).rows;
155
+ const tableOf = (name: string): string => partitions.find((partition) => partition.from === name)?.to ?? name;
156
+ const rootOf = (name: string): string => (tableOf(name) === name ? name : rootOf(tableOf(name)));
157
+ const gone = new Set(emptying.map((table) => table.name));
158
+
159
+ const stays = (name: string): boolean =>
160
+ tables.some((table) => table.name === rootOf(name)) && !gone.has(rootOf(name));
161
+
162
+ const partition = partitions.find(({ from, schema }) => kept({ name: from, schema }) && gone.has(rootOf(from)))?.from;
163
+ const broken = (await copy.query<Link>(REFERENCES)).rows.find(({ from, to }) => stays(from) && gone.has(rootOf(to)));
164
+ const where = 'in KEPT_TABLES of test/support/test-database-kept.ts';
165
+
166
+ if (partition !== undefined) {
167
+ throw new Error(
168
+ `The table ${partition} is a partition of ${rootOf(partition)}, which the run empties, and a partition is emptied with its table: keep ${rootOf(partition)} ${where}.`,
169
+ );
170
+ }
171
+
172
+ if (broken !== undefined) {
173
+ throw new Error(
174
+ `The table ${rootOf(broken.from)} keeps its rows and references ${rootOf(broken.to)}, which the run empties: keep ${rootOf(broken.to)} too, ${where}.`,
175
+ );
176
+ }
177
+ }
178
+
179
+ async function holdingRows(copy: pg.Client, emptying: readonly Table[]): Promise<string[]> {
180
+ const each = emptying.map(
181
+ ({ name, quoted }) => `select '${name.replaceAll("'", "''")}' as name where exists (select 1 from ${quoted})`,
182
+ );
183
+
184
+ if (emptying.length === 0) {
185
+ return [];
186
+ }
187
+
188
+ return (await copy.query<{ name: string }>(each.join(' union all '))).rows.map((row) => row.name);
189
+ }
190
+
191
+ async function empty(copy: pg.Client, emptying: readonly Table[]): Promise<void> {
192
+ // `only`: a table that inherits from an emptied one and keeps its rows is not reached through it. A partitioned table has no rows of its own.
193
+ const each = emptying.map((table) => (table.partitioned ? table.quoted : `only ${table.quoted}`));
194
+
195
+ if (emptying.length > 0) {
196
+ await copy.query(`truncate table ${each.join(', ')} restart identity`);
197
+ }
198
+
199
+ for (const view of (await copy.query<{ quoted: string }>(VIEWS)).rows) {
200
+ await copy.query(`refresh materialized view ${view.quoted}`);
201
+ }
202
+ }
203
+
204
+ function notEmptied(database: string, error: unknown): Error {
205
+ return new Error(
206
+ `The copy of the database "${database}" could not be emptied (${String(error)}): list the table it names in KEPT_TABLES of test/support/test-database-kept.ts, or give the tests a database of their own, migrated and empty.`,
207
+ { cause: error },
208
+ );
209
+ }
@@ -0,0 +1,32 @@
1
+ /**
2
+ * The tables an end-to-end run on a server leaves their rows (test-database-emptying.ts): every
3
+ * other table of its copy of the migrated database is emptied before a spec file starts. A name is
4
+ * `<schema>.<table>`, or `<schema>.*` for every table of a schema, written as the server stores it
5
+ * and without quotes: in lower case unless the table was created between quotes (`"Countries"` is
6
+ * `public.Countries`); a table whose name holds a dot or a quote is kept through `<schema>.*`
7
+ * only. The run says of a name that it is no table of the database. Add a table
8
+ * whose rows a migration inserts and the application reads (a list of countries, a plan). A kept
9
+ * table that references an emptied one, or is a partition of one, needs that one kept too: the run
10
+ * says which.
11
+ */
12
+ export const KEPT_TABLES: readonly string[] = [
13
+ // What the migration tool writes of what it applied: a table it is told to name otherwise goes here under that name.
14
+ {{#if orm.drizzle}}
15
+ 'drizzle.__drizzle_migrations',
16
+ {{/if}}
17
+ {{#if orm.knex}}
18
+ 'public.knex_migrations',
19
+ 'public.knex_migrations_lock',
20
+ {{/if}}
21
+ {{#if orm.prisma}}
22
+ 'public._prisma_migrations',
23
+ {{/if}}
24
+ {{#if orm.typeorm}}
25
+ 'public.migrations',
26
+ 'public.typeorm_metadata',
27
+ {{/if}}
28
+ {{#if audit.on}}
29
+ // The audit trail, which its guard keeps append-only whoever asks.
30
+ 'audit.*',
31
+ {{/if}}
32
+ ];
@@ -0,0 +1,52 @@
1
+ import type pg from 'pg';
2
+ import { dropping, keyOf, LIVE, locked, runOf } from './test-database-server{{alias.sibling}}';
3
+ import { BEAT, DEAD_AFTER_SECONDS } from './test-database-session{{alias.sibling}}';
4
+
5
+ interface Leftover {
6
+ readonly name: string;
7
+ /** The comment of the database: on its template, a run says when it last lived. */
8
+ readonly said: string | null;
9
+ /** The clock of the server, in seconds, as a text. */
10
+ readonly now: string;
11
+ readonly connected: boolean;
12
+ }
13
+
14
+ /**
15
+ * Drops what the runs that died left on the server, in the session `server` of the run `own`. A
16
+ * database is dropped when it is named after a run on the database `url` names, the lock of that
17
+ * run is free, the run has not said for a minute that it lives, and nobody is connected to the
18
+ * database. The lock alone is not enough: a run whose session the server ended takes it again a
19
+ * moment later, and a file whose pool is idle has no session. A run that lives, in another
20
+ * checkout or on another machine, therefore keeps its databases; a database with any other name is
21
+ * never dropped; and what a run killed a moment ago left goes with a run that starts a minute later.
22
+ */
23
+ export async function dropLeftovers(server: pg.Client, url: string, own: string): Promise<void> {
24
+ const { rows } = await server.query<Leftover>(
25
+ `select d.datname as name, shobj_description(d.oid, 'pg_database') as said, extract(epoch from now())::bigint::text as now,
26
+ exists (select 1 from pg_stat_activity a where a.datid = d.oid) as connected
27
+ from pg_database d`,
28
+ );
29
+
30
+ const left = rows.filter(({ name }) => ![undefined, own].includes(runOf(name, url)));
31
+
32
+ for (const run of new Set(left.map(({ name }) => runOf(name, url) ?? ''))) {
33
+ const ofTheRun = left.filter(({ name }) => runOf(name, url) === run);
34
+
35
+ if (!lives(ofTheRun) && (await locked(server, `pg_try_advisory_lock(${LIVE}, ${keyOf(run)})`))) {
36
+ // A database somebody is connected to is a file that still runs: asked to drop it, the server would wait 5 seconds, then refuse.
37
+ for (const { name } of ofTheRun.filter((database) => !database.connected)) {
38
+ await server.query(dropping(name, '')).catch(() => undefined);
39
+ }
40
+
41
+ await locked(server, `pg_advisory_unlock(${LIVE}, ${keyOf(run)})`);
42
+ }
43
+ }
44
+ }
45
+
46
+ function lives(databases: readonly Leftover[]): boolean {
47
+ return databases.some(({ said, now }) => {
48
+ const last = said?.startsWith(BEAT) === true ? Number(said.slice(BEAT.length)) : 0;
49
+
50
+ return Number(now) - last < DEAD_AFTER_SECONDS;
51
+ });
52
+ }
@@ -0,0 +1,99 @@
1
+ import type pg from 'pg';
2
+ import { poolSizeFor } from './test-database-budget{{alias.sibling}}';
3
+ import { copied } from './test-database-copy{{alias.sibling}}';
4
+ import { emptied, type Emptied } from './test-database-emptying{{alias.sibling}}';
5
+ import { dropLeftovers } from './test-database-leftovers{{alias.sibling}}';
6
+ import { databaseOf, dropping, nameOf, onServer, runOf, TEMPLATE } from './test-database-server{{alias.sibling}}';
7
+ import { openSession, type RunSession } from './test-database-session{{alias.sibling}}';
8
+
9
+ /** A run on a server: the template its files copy, the pool of each, and its end. */
10
+ export interface Run {
11
+ readonly template: string;
12
+ readonly poolSize: number;
13
+
14
+ /** Drops the template and what the files of the run did not drop, then lets the lock go. Never throws. */
15
+ end(): Promise<void>;
16
+ }
17
+
18
+ /** A migrated database holds a few megabytes: beyond this it holds rows, and the run says what copying them costs. */
19
+ const LARGE = 64 * 1024 * 1024;
20
+ /** How many of the tables a line names before it counts the others. */
21
+ const NAMED = 20;
22
+
23
+ /**
24
+ * Starts a run on the server `url` names, in which `atOnce` spec files run at the same time: takes
25
+ * the lock that says it lives, drops what the runs that died left, refuses a run the connections
26
+ * of the server cannot take, then copies the migrated database (test-database-copy.ts) and empties
27
+ * the copy. The global setup of the end-to-end run calls it once, where no timeout of
28
+ * a hook applies (test-database-setup.ts), and each file copies the template in a fraction of a
29
+ * second (test-database-clone.ts). What the run has to say goes to `tell`, the error output.
30
+ */
31
+ export async function openRun(url: string, atOnce: number, tell = toErrorOutput): Promise<Run> {
32
+ const session = await openSession(url, tell);
33
+ const template = nameOf(url, session.id, TEMPLATE);
34
+ const end = (): Promise<void> => ended(session, url);
35
+
36
+ try {
37
+ await dropLeftovers(session.server(), url, session.id);
38
+
39
+ const poolSize = await poolSizeFor(session.server(), atOnce);
40
+ const from = Date.now();
41
+
42
+ await copied(url, template);
43
+ await session.beatOn(template);
44
+
45
+ for (const line of said(await emptied(url, template), databaseOf(url), Math.ceil((Date.now() - from) / 1000))) {
46
+ tell(line + '\n');
47
+ }
48
+
49
+ return { template, poolSize, end };
50
+ } catch (error) {
51
+ await end();
52
+
53
+ throw error;
54
+ }
55
+ }
56
+
57
+ function toErrorOutput(line: string): void {
58
+ process.stderr.write(line);
59
+ }
60
+
61
+ /** On the session of the run, or on another one when the server ended that one; what cannot be dropped now goes with a later run. */
62
+ async function ended(session: RunSession, url: string): Promise<void> {
63
+ const dropOwn = async (server: pg.Client): Promise<void> => {
64
+ const { rows } = await server.query<{ datname: string }>('select datname from pg_database');
65
+
66
+ for (const { datname } of rows.filter((row) => runOf(row.datname, url) === session.id)) {
67
+ await server.query(dropping(datname));
68
+ }
69
+ };
70
+
71
+ await dropOwn(session.server())
72
+ .catch(() => onServer(url, dropOwn))
73
+ .catch(() => undefined);
74
+
75
+ await session.end();
76
+ }
77
+
78
+ /** One line for each thing a reader may have to act on: rows that are gone, tables left as they were, names that keep nothing, a copy that took time. */
79
+ function said({ held, ofAnotherRole, unmatched, bytes }: Emptied, database: string, seconds: number): string[] {
80
+ const gone = `The run emptied ${held.length} table(s) of its copy of "${database}" that held rows (${named(held)}): a spec stores the rows it needs, and a table whose rows the application reads is listed in KEPT_TABLES of test/support/test-database-kept.ts.`;
81
+ const left = `It left ${ofAnotherRole.length} table(s) with their rows, which another role owns or whose schema the role of the run may not use (${named(ofAnotherRole)}).`;
82
+ const large = `Those rows take ${Math.round(bytes / (1024 * 1024))} MB: copying them took ${seconds} s of this run. Give the tests a database of their own, migrated and empty: create it, name it in DATABASE_URL, then npm run db:migrate.`;
83
+
84
+ return [
85
+ ...(held.length > 0 ? [gone] : []),
86
+ ...(ofAnotherRole.length > 0 ? [left] : []),
87
+ ...unmatched.map(
88
+ (name) =>
89
+ `KEPT_TABLES of test/support/test-database-kept.ts lists ${name}, which is no table of "${database}" and keeps nothing: a name is written as the server stores it, in lower case unless the table was created between quotes, and a view holds no rows of its own.`,
90
+ ),
91
+ ...(bytes >= LARGE ? [large] : []),
92
+ ];
93
+ }
94
+
95
+ function named(tables: readonly string[]): string {
96
+ const others = tables.length - NAMED;
97
+
98
+ return tables.slice(0, NAMED).join(', ') + (others > 0 ? ` and ${others} more` : '');
99
+ }
@@ -0,0 +1,140 @@
1
+ import pg from 'pg';
2
+
3
+ /** Set by the global setup of an end-to-end run, read by its files: the template of the run and the pool of each file. */
4
+ export const RUN_TEMPLATE = 'TEST_DATABASE_RUN';
5
+ export const POOL_SIZE = 'TEST_DATABASE_POOL_SIZE';
6
+
7
+ /** A database of a run is named `<database>_test_<run>_<template or random>`, the migrated one cut so that the name fits in 63 bytes. */
8
+ const MARKER = '_test_';
9
+ const KEPT_OF_THE_NAME = 38;
10
+
11
+ export const TEMPLATE = 'template';
12
+ /** The advisory lock a run holds as long as it lives, on the `postgres` database of the server: this number and the run. */
13
+ export const LIVE = 0x64646b74;
14
+
15
+ /** The advisory lock held while a database is copied, with the run, or 0 while a run copies the migrated one. */
16
+ const COPYING = LIVE + 1;
17
+
18
+ /**
19
+ * What a role, a database or a server may set that ends a statement or a wait after some time
20
+ * (`alter role ... set statement_timeout`), for the requests of an application: the sessions of a
21
+ * run only copy, empty and drop its own databases and wait their turn to do so, which lasts as
22
+ * long as the database is large, so none applies to them. Read from the settings the server has:
23
+ * `transaction_timeout` came with PostgreSQL 17. These sessions open no transaction of their own:
24
+ * `idle_in_transaction_session_timeout` never applies. The pool of an application under test is
25
+ * another session, with the timeouts of its role.
26
+ */
27
+ const NO_TIMEOUT = `select set_config(name, '0', false) from pg_settings where name in ('statement_timeout', 'lock_timeout', 'transaction_timeout')`;
28
+
29
+ /**
30
+ * A session on a database of the server, `postgres` by default: the runs hold their locks there,
31
+ * and copy and drop from there, since a session on a database makes its copy refused. A run
32
+ * therefore cannot have `postgres` itself as its migrated database. No timeout of the role ends
33
+ * what the session does.
34
+ */
35
+ export async function connectedTo(url: string, database = 'postgres'): Promise<pg.Client> {
36
+ if (databaseOf(url) === 'postgres') {
37
+ throw new Error(
38
+ 'DATABASE_URL names the database "postgres", where the runs hold their locks and which they copy from: give the tests a database of their own (create database <name>, name it in DATABASE_URL, then npm run db:migrate).',
39
+ );
40
+ }
41
+
42
+ const client = new pg.Client({ connectionString: urlOf(url, database), keepAlive: true });
43
+
44
+ // A client whose session the server ended emits an error: with nobody listening, it would end the runner. Its queries reject.
45
+ client.on('error', () => undefined);
46
+ await client.connect();
47
+ await client.query(NO_TIMEOUT);
48
+
49
+ return client;
50
+ }
51
+
52
+ /** Runs `work` on a session of its own, ended whatever happens. */
53
+ export async function onServer(url: string, work: (server: pg.Client) => Promise<unknown>): Promise<void> {
54
+ const server = await connectedTo(url);
55
+
56
+ try {
57
+ await work(server);
58
+ } finally {
59
+ await server.end();
60
+ }
61
+ }
62
+
63
+ /** Whether the session took, or let go, the lock `call` names. */
64
+ export async function locked(server: pg.Client, call: string): Promise<boolean> {
65
+ const { rows } = await server.query<{ held: boolean }>(`select ${call} as held`);
66
+
67
+ return rows[0]?.held === true;
68
+ }
69
+
70
+ /** The run as the second key of a lock, a signed 32-bit integer. */
71
+ export function keyOf(run: string): number {
72
+ return Number.parseInt(run, 16) | 0;
73
+ }
74
+
75
+ /** Runs `work` while this session holds the copying lock of `run`: two copies at once slow each other, the next one waits. */
76
+ export async function oneAtATime(server: pg.Client, run: string, work: () => Promise<unknown>): Promise<void> {
77
+ await server.query(`select pg_advisory_lock(${COPYING}, ${keyOf(run)})`);
78
+
79
+ try {
80
+ await work();
81
+ } finally {
82
+ await server.query(`select pg_advisory_unlock(${COPYING}, ${keyOf(run)})`);
83
+ }
84
+ }
85
+
86
+ /** The run a database belongs to, by its name; undefined for a database that is not one of a run on the database `url` names. */
87
+ export function runOf(name: string, url: string): string | undefined {
88
+ const rest = name.startsWith(prefixOf(url)) ? name.slice(prefixOf(url).length) : '';
89
+
90
+ return /^([0-9a-f]{8})_(?:template|[0-9a-f]{8})$/.exec(rest)?.[1];
91
+ }
92
+
93
+ export function nameOf(url: string, run: string, last: string): string {
94
+ return prefixOf(url) + run + '_' + last;
95
+ }
96
+
97
+ function prefixOf(url: string): string {
98
+ return databaseOf(url).slice(0, KEPT_OF_THE_NAME) + MARKER;
99
+ }
100
+
101
+ export function databaseOf(url: string): string {
102
+ return decodeURIComponent(new URL(url).pathname.slice(1));
103
+ }
104
+
105
+ /** The same server, role and parameters, another database. */
106
+ export function urlOf(url: string, database: string): string {
107
+ const address = new URL(url);
108
+
109
+ address.pathname = '/' + encodeURIComponent(database);
110
+
111
+ return address.toString();
112
+ }
113
+
114
+ export function copying(from: string, to: string): string {
115
+ return 'create database ' + quoted(to) + ' template ' + quoted(from);
116
+ }
117
+
118
+ /** With `force`, whoever is still connected to it; without, only a database nobody is connected to. */
119
+ export function dropping(name: string, force = ' with (force)'): string {
120
+ return 'drop database if exists ' + quoted(name) + force;
121
+ }
122
+
123
+ function quoted(name: string): string {
124
+ return '"' + name.replaceAll('"', '""') + '"';
125
+ }
126
+
127
+ /** The SQLSTATE of a server that is asked for a database it does not have. */
128
+ export const NO_SUCH_DATABASE = '3D000';
129
+
130
+ /** Said by the run when it finds its template gone, and by a file that cannot copy it. */
131
+ export function lostDatabases(template: string): string {
132
+ return `The run lost its databases: its template "${template}" is no longer on the server. A run whose session ended for more than a minute is held dead, and a later run drops what it left; an administrator may have dropped it too. Run again.`;
133
+ }
134
+
135
+ /** The SQLSTATE of an error of the server, empty for any other error. */
136
+ export function codeOf(error: unknown): string {
137
+ const code: unknown = error instanceof Error ? Reflect.get(error, 'code') : undefined;
138
+
139
+ return typeof code === 'string' ? code : '';
140
+ }