@owlmeans/postgres 0.1.18-rc.43 → 0.1.18-rc.45
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +5 -4
- package/agent-meta/manifest.json +2 -2
- package/agent-meta/skills/postgres/SKILL.md +1 -1
- package/build/bootstrap.d.ts.map +1 -1
- package/build/bootstrap.js +12 -7
- package/build/bootstrap.js.map +1 -1
- package/build/index.d.ts +2 -0
- package/build/index.d.ts.map +1 -1
- package/build/index.js.map +1 -1
- package/build/service.d.ts +1 -5
- package/build/service.d.ts.map +1 -1
- package/build/service.js +18 -9
- package/build/service.js.map +1 -1
- package/build/types.local.d.ts +5 -0
- package/build/types.local.d.ts.map +1 -0
- package/build/types.local.js +2 -0
- package/build/types.local.js.map +1 -0
- package/build/utils/config/types.d.ts +30 -0
- package/build/utils/config/types.d.ts.map +1 -0
- package/build/utils/config/types.js +2 -0
- package/build/utils/config/types.js.map +1 -0
- package/build/utils/config.d.ts +4 -24
- package/build/utils/config.d.ts.map +1 -1
- package/build/utils/config.js +74 -87
- package/build/utils/config.js.map +1 -1
- package/build/utils/connection/types.d.ts +17 -0
- package/build/utils/connection/types.d.ts.map +1 -0
- package/build/utils/connection/types.js +2 -0
- package/build/utils/connection/types.js.map +1 -0
- package/build/utils/connection.d.ts +2 -13
- package/build/utils/connection.d.ts.map +1 -1
- package/build/utils/connection.js +33 -37
- package/build/utils/connection.js.map +1 -1
- package/package.json +13 -12
- package/src/bootstrap.ts +13 -11
- package/src/index.ts +2 -0
- package/src/service.ts +23 -20
- package/src/types.local.ts +5 -0
- package/src/utils/config/types.ts +28 -0
- package/src/utils/config.ts +80 -89
- package/src/utils/connection/types.ts +17 -0
- package/src/utils/connection.ts +36 -36
- package/tests/context.ts +3 -3
- package/tests/criteria.spec.ts +2 -2
- package/tests/migration.spec.ts +3 -3
- package/tests/sync.spec.ts +3 -3
package/src/utils/connection.ts
CHANGED
|
@@ -1,8 +1,12 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { pgErrorHelper, pgNameHelper, PostgresConnectionError } from '@owlmeans/postgres-resource'
|
|
2
2
|
import type { PostgresMeta } from '@owlmeans/postgres-resource'
|
|
3
|
+
import { logger } from '@owlmeans/log'
|
|
3
4
|
import type { Pool } from 'pg'
|
|
4
5
|
|
|
5
6
|
import { DEF_RETRIES, DEF_RETRY_DELAY, TERMINAL_CONNECT_CODES } from '../consts.js'
|
|
7
|
+
import type { PgConnectionHelper } from './connection/types.js'
|
|
8
|
+
|
|
9
|
+
const log = logger('postgres')
|
|
6
10
|
|
|
7
11
|
/**
|
|
8
12
|
* Retryable means "the server isn't up yet". A driver level failure carries no `code` at
|
|
@@ -18,47 +22,43 @@ const isTransient = (error: unknown): boolean => {
|
|
|
18
22
|
return !TERMINAL_CONNECT_CODES.includes(code)
|
|
19
23
|
}
|
|
20
24
|
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
*
|
|
24
|
-
* Postgres binds its port before it finishes recovery, so a sidecar or an operator managed
|
|
25
|
-
* instance routinely accepts a connection and then refuses to run anything. Every OwlMeans
|
|
26
|
-
* deployment grew its own copy of this loop; this is the one.
|
|
27
|
-
*
|
|
28
|
-
* @throws {PostgresConnectionError}
|
|
29
|
-
*/
|
|
30
|
-
export const probe = async (pool: Pool, meta: PostgresMeta, location: string): Promise<void> => {
|
|
31
|
-
const retries = Math.max(1, meta.retries ?? DEF_RETRIES)
|
|
32
|
-
const delay = meta.retryDelayMillis ?? DEF_RETRY_DELAY
|
|
33
|
-
let last: unknown
|
|
25
|
+
export const makePgConnectionHelper = (pool: Pool): PgConnectionHelper => {
|
|
26
|
+
const { pgErrorToResourceError } = pgErrorHelper
|
|
34
27
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
28
|
+
const probe = async (meta: PostgresMeta, location: string): Promise<void> => {
|
|
29
|
+
const retries = Math.max(1, meta.retries ?? DEF_RETRIES)
|
|
30
|
+
const delay = meta.retryDelayMillis ?? DEF_RETRY_DELAY
|
|
31
|
+
let last: unknown
|
|
38
32
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
33
|
+
for (let attempt = 1; attempt <= retries; ++attempt) {
|
|
34
|
+
try {
|
|
35
|
+
await pool.query('SELECT 1')
|
|
36
|
+
|
|
37
|
+
return
|
|
38
|
+
} catch (error) {
|
|
39
|
+
last = error
|
|
40
|
+
if (attempt === retries || !isTransient(error)) {
|
|
41
|
+
break
|
|
42
|
+
}
|
|
43
|
+
log.debug('Postgres not ready, retrying', { location, attempt, retries, delay })
|
|
44
|
+
await new Promise(resolve => setTimeout(resolve, delay))
|
|
44
45
|
}
|
|
45
|
-
console.log(`${location}: not ready (${attempt}/${retries}), retrying in ${delay}ms…`)
|
|
46
|
-
await new Promise(resolve => setTimeout(resolve, delay))
|
|
47
46
|
}
|
|
48
|
-
}
|
|
49
47
|
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
48
|
+
const translated = pgErrorToResourceError(last)
|
|
49
|
+
const failure = new PostgresConnectionError(`unreachable:${location}:${translated.message}`)
|
|
50
|
+
failure.cause = last
|
|
53
51
|
|
|
54
|
-
|
|
55
|
-
}
|
|
52
|
+
throw failure
|
|
53
|
+
}
|
|
56
54
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
55
|
+
const ensureSchema = async (schema: string): Promise<void> => {
|
|
56
|
+
try {
|
|
57
|
+
await pool.query(`CREATE SCHEMA IF NOT EXISTS ${pgNameHelper.quoteIdent(schema)}`)
|
|
58
|
+
} catch (error) {
|
|
59
|
+
throw pgErrorToResourceError(error)
|
|
60
|
+
}
|
|
63
61
|
}
|
|
62
|
+
|
|
63
|
+
return { probe, ensureSchema }
|
|
64
64
|
}
|
package/tests/context.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { randomNamespace, gateHelper } from '@owlmeans/test-integration'
|
|
2
2
|
import type { IntegrationGate, PostgresEnv } from '@owlmeans/test-integration'
|
|
3
3
|
import { PgAutoSync, resetPlaceholderCache } from '@owlmeans/postgres-resource'
|
|
4
4
|
import type { PostgresResource } from '@owlmeans/postgres-resource'
|
|
@@ -19,7 +19,7 @@ import type { PostgresService } from '@owlmeans/postgres'
|
|
|
19
19
|
* that backs it, which is why the specs live in this package rather than the resource one
|
|
20
20
|
* (the reverse would make `@owlmeans/postgres-resource` dev-depend on its own dependent).
|
|
21
21
|
*/
|
|
22
|
-
export const gate: IntegrationGate<PostgresEnv> = postgresGate()
|
|
22
|
+
export const gate: IntegrationGate<PostgresEnv> = gateHelper.postgresGate()
|
|
23
23
|
|
|
24
24
|
const url = (): string => gate.env.POSTGRES_URL as string
|
|
25
25
|
|
|
@@ -103,7 +103,7 @@ export const makeSuite = (label: string): PgSuite => {
|
|
|
103
103
|
* Declarations are deliberately *not* reset here. They survive a context switch on
|
|
104
104
|
* purpose (that is the whole point of keying them by alias), and a `.migration()`
|
|
105
105
|
* registered before `boot()` would be silently dropped. A spec that wants a clean
|
|
106
|
-
* slate calls `resetDeclarations(alias)` itself, which is exactly what a spec
|
|
106
|
+
* slate calls `pgDeclarationHelper.resetDeclarations(alias)` itself, which is exactly what a spec
|
|
107
107
|
* simulating a restarted process should have to say out loud.
|
|
108
108
|
*/
|
|
109
109
|
resetPlaceholderCache()
|
package/tests/criteria.spec.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { afterAll, beforeAll, describe, expect, test } from 'bun:test'
|
|
2
2
|
import { makePostgresResource } from '@owlmeans/postgres-resource'
|
|
3
3
|
import type { PostgresResource } from '@owlmeans/postgres-resource'
|
|
4
|
-
import {
|
|
4
|
+
import { recordQueryHelper, UnsupportedArgumentError } from '@owlmeans/resource'
|
|
5
5
|
import type { Criteria, ResourceRecord } from '@owlmeans/resource'
|
|
6
6
|
|
|
7
7
|
import { gate, makeSuite } from './context.js'
|
|
@@ -62,7 +62,7 @@ describe('@owlmeans/postgres — criteria answer what the in-memory engine answe
|
|
|
62
62
|
|
|
63
63
|
const byBoth = async (criteria: Criteria<Plot>): Promise<void> => {
|
|
64
64
|
const listed = (await plots.list(criteria, { size: 0 })).items.map(plot => plot.id).sort()
|
|
65
|
-
const expected = seeded.filter(plot => matchCriteria(plot, criteria)).map(plot => plot.id).sort()
|
|
65
|
+
const expected = seeded.filter(plot => recordQueryHelper.matchCriteria(plot, criteria)).map(plot => plot.id).sort()
|
|
66
66
|
expect([JSON.stringify(criteria), listed]).toEqual([JSON.stringify(criteria), expected])
|
|
67
67
|
}
|
|
68
68
|
|
package/tests/migration.spec.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { afterAll, describe, expect, test } from 'bun:test'
|
|
2
|
-
import { makePostgresResource,
|
|
2
|
+
import { makePostgresResource, pgDeclarationHelper } from '@owlmeans/postgres-resource'
|
|
3
3
|
import type { PostgresResource, PostgresTx } from '@owlmeans/postgres-resource'
|
|
4
4
|
import { MigrationConflict, MigrationError, MigrationStage } from '@owlmeans/resource'
|
|
5
5
|
import type { ResourceRecord } from '@owlmeans/resource'
|
|
@@ -253,7 +253,7 @@ describe('@owlmeans/postgres — code registered migrations', () => {
|
|
|
253
253
|
})
|
|
254
254
|
|
|
255
255
|
/** A restarted process with an edited source file — the registry has to be rebuilt. */
|
|
256
|
-
resetDeclarations('mig-e')
|
|
256
|
+
pgDeclarationHelper.resetDeclarations('mig-e')
|
|
257
257
|
|
|
258
258
|
await expect(boot({
|
|
259
259
|
alias: 'mig-e',
|
|
@@ -262,7 +262,7 @@ describe('@owlmeans/postgres — code registered migrations', () => {
|
|
|
262
262
|
})
|
|
263
263
|
|
|
264
264
|
it('rejects a changed body under a name already registered in this process', () => {
|
|
265
|
-
resetDeclarations('mig-conflict')
|
|
265
|
+
pgDeclarationHelper.resetDeclarations('mig-conflict')
|
|
266
266
|
const resource = makePostgresResource<Note, PostgresResource<Note>>('mig-conflict')
|
|
267
267
|
resource.migration('0001-drift', driftBody)
|
|
268
268
|
|
package/tests/sync.spec.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { afterAll, describe, expect, test } from 'bun:test'
|
|
2
2
|
import {
|
|
3
|
-
|
|
3
|
+
makePgIntrospectHelper, makePostgresResource, PgAutoSync, pgDiffHelper, PostgresCastRequired,
|
|
4
4
|
} from '@owlmeans/postgres-resource'
|
|
5
5
|
import type { PostgresResource } from '@owlmeans/postgres-resource'
|
|
6
6
|
import type { ResourceRecord } from '@owlmeans/resource'
|
|
@@ -236,8 +236,8 @@ describe('@owlmeans/postgres — reconciling a table already at its shape', () =
|
|
|
236
236
|
const client = await pool.connect()
|
|
237
237
|
try {
|
|
238
238
|
const table = second.resource.table
|
|
239
|
-
const live = await introspectTable(
|
|
240
|
-
expect(planSync(table, live).statements.map(statement => statement.sql)).toEqual([])
|
|
239
|
+
const live = await makePgIntrospectHelper(client).introspectTable(table.schema, table.table, table.qualified)
|
|
240
|
+
expect(pgDiffHelper.planSync(table, live).statements.map(statement => statement.sql)).toEqual([])
|
|
241
241
|
} finally {
|
|
242
242
|
client.release()
|
|
243
243
|
}
|