@fabrika/cli 0.4.7 → 0.4.8

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 (2) hide show
  1. package/package.json +12 -12
  2. package/src/db.ts +28 -6
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fabrika/cli",
3
- "version": "0.4.7",
3
+ "version": "0.4.8",
4
4
  "type": "module",
5
5
  "repository": {
6
6
  "type": "git",
@@ -30,19 +30,19 @@
30
30
  },
31
31
  "dependencies": {
32
32
  "zod": "^3.25.76",
33
- "@fabrika/app": "0.4.7",
34
- "@fabrika/auth-core": "0.4.7",
35
- "@fabrika/control-contract": "0.4.7",
36
- "@fabrika/provider-contract": "0.4.7",
37
- "@fabrika/db": "0.4.7",
38
- "@fabrika/platform": "0.4.7",
39
- "@fabrika/platform-node": "0.4.7",
40
- "@fabrika/check": "0.4.7"
33
+ "@fabrika/app": "0.4.8",
34
+ "@fabrika/auth-core": "0.4.8",
35
+ "@fabrika/control-contract": "0.4.8",
36
+ "@fabrika/provider-contract": "0.4.8",
37
+ "@fabrika/db": "0.4.8",
38
+ "@fabrika/platform": "0.4.8",
39
+ "@fabrika/platform-node": "0.4.8",
40
+ "@fabrika/check": "0.4.8"
41
41
  },
42
42
  "peerDependencies": {
43
- "@fabrika/builder": "0.4.7",
44
- "@fabrika/provider-cloudflare": "0.4.7",
45
- "@fabrika/provider-zerops": "0.4.7"
43
+ "@fabrika/builder": "0.4.8",
44
+ "@fabrika/provider-cloudflare": "0.4.8",
45
+ "@fabrika/provider-zerops": "0.4.8"
46
46
  },
47
47
  "peerDependenciesMeta": {
48
48
  "@fabrika/builder": {
package/src/db.ts CHANGED
@@ -11,6 +11,8 @@ import {
11
11
  import { CodegenError, generateSchema, SCHEMA_FILE } from '@fabrika/db/codegen'
12
12
  import { FRAMEWORK_MIGRATION_BUNDLE, FRAMEWORK_MIGRATIONS } from '@fabrika/db/migrations'
13
13
  import {
14
+ CONNECTION_CAPACITY_MESSAGE,
15
+ ConnectionCapacityError,
14
16
  createTestProvisioner,
15
17
  DEFAULT_MIGRATION_LOCK_TIMEOUT_MS,
16
18
  DEFAULT_MIGRATION_STATEMENT_TIMEOUT_MS,
@@ -19,6 +21,8 @@ import {
19
21
  PostgresMigrationError,
20
22
  postgresMigrationResultMessage,
21
23
  type TestProvisioner,
24
+ TOO_MANY_CONNECTIONS,
25
+ withConnectionCapacity,
22
26
  } from '@fabrika/platform-node'
23
27
  import { SQL } from 'bun'
24
28
  import { mkdir, mkdtemp, readFile, rm, writeFile } from 'node:fs/promises'
@@ -52,7 +56,9 @@ one that is missing, edited or stale.
52
56
  \`test-isolation\` is the platform's run of them: with FABRIKA_APP_DATABASE_URL it creates a throwaway provisioner
53
57
  (CREATEROLE, CREATE on the database, nothing of any application) that the tests get instead, ignores the application's
54
58
  bunfig.toml, and checks the JUnit report against the case names the scaffold wrote. It first drops the provisioners
55
- that killed runs left behind: expired, and without a session of their own or of a role they created.
59
+ that killed runs left behind: expired, and without a session of their own or of a role they created. A database at its
60
+ connection limit (SQLSTATE ${TOO_MANY_CONNECTIONS}) is waited for; one that never frees a slot fails the run saying so,
61
+ never as failed cases.
56
62
  `
57
63
 
58
64
  export interface DbCommandDependencies {
@@ -99,7 +105,7 @@ const reportable = (error: unknown): Error => {
99
105
  const server = serverError(error.cause)
100
106
  return new Error(server === undefined ? error.message : `${error.message}: ${server}`)
101
107
  }
102
- if (error instanceof CodegenError) return error
108
+ if (error instanceof CodegenError || error instanceof ConnectionCapacityError) return error
103
109
  const server = serverError(error)
104
110
  if (server !== undefined) return new Error(server)
105
111
  return new Error('the database connection failed; check FABRIKA_APP_DATABASE_URL (the driver error is withheld: it may contain the URL)')
@@ -201,9 +207,20 @@ const testEnvironment = (source: DbCommandDependencies['source'], url: string):
201
207
  }
202
208
 
203
209
  /**
204
- * Runs exactly the generated tests, with an empty `bunfig.toml` so that no preload, root or ignore pattern of the
205
- * application applies, and fails unless every case the scaffold wrote is reported as passed.
210
+ * The server's own words for a refused connection (SQLSTATE 53300) and the test kit's report of one it gave up on. A
211
+ * run whose tests met one says so instead of counting cases: a case that could not connect proved nothing.
206
212
  */
213
+ const EXHAUSTED_CONNECTIONS = [
214
+ CONNECTION_CAPACITY_MESSAGE,
215
+ 'remaining connection slots are reserved',
216
+ 'too many clients already',
217
+ 'too many connections for role',
218
+ 'too many connections for database',
219
+ ]
220
+
221
+ /** Whether the tests' output reports a connection the database refused for lack of a free slot. */
222
+ export const reportsExhaustedConnections = (output: string): boolean => EXHAUSTED_CONNECTIONS.some((text) => output.includes(text))
223
+
207
224
  /**
208
225
  * Runs exactly the generated tests, with an empty `bunfig.toml` so that no preload, root or ignore pattern of the
209
226
  * application applies, and the platform's preload, which captures Bun's test functions before the application loads.
@@ -225,7 +242,7 @@ const testIsolation = async (argv: readonly string[], deps: DbCommandDependencie
225
242
  }
226
243
  let provisioner: TestProvisioner
227
244
  try {
228
- provisioner = await createTestProvisioner(url)
245
+ provisioner = await withConnectionCapacity(() => createTestProvisioner(url))
229
246
  } catch (error) {
230
247
  throw reportable(error)
231
248
  }
@@ -263,6 +280,11 @@ const testIsolation = async (argv: readonly string[], deps: DbCommandDependencie
263
280
  if (outcome !== 'passed') failed.push(`${path}: ${name} (${outcome})`)
264
281
  }
265
282
  }
283
+ if ((code !== 0 || failed.length > 0) && reportsExhaustedConnections(`${stdout}${stderr}`)) {
284
+ throw new Error(
285
+ `db test-isolation: ${CONNECTION_CAPACITY_MESSAGE} for the generated tests (SQLSTATE ${TOO_MANY_CONNECTIONS}), so they reached no verdict; other processes hold the database's connections: free them or give the database more, then run it again`,
286
+ )
287
+ }
266
288
  if (code !== 0 || failed.length > 0) {
267
289
  const rerun = verbose ? '' : '; rerun with --verbose locally to see the test output'
268
290
  throw new Error(
@@ -274,7 +296,7 @@ const testIsolation = async (argv: readonly string[], deps: DbCommandDependencie
274
296
  deps.write(`${total} generated cases passed\n`)
275
297
  } finally {
276
298
  await rm(scratch, { recursive: true, force: true })
277
- await provisioner.drop().catch((error: unknown) => {
299
+ await withConnectionCapacity(() => provisioner.drop()).catch((error: unknown) => {
278
300
  throw reportable(error)
279
301
  })
280
302
  }