@owlmeans/postgres 0.1.18-rc.8 → 0.1.18

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 (48) hide show
  1. package/README.md +7 -6
  2. package/agent-meta/manifest.json +3 -3
  3. package/agent-meta/skills/postgres/SKILL.md +67 -16
  4. package/build/bootstrap.d.ts.map +1 -1
  5. package/build/bootstrap.js +12 -7
  6. package/build/bootstrap.js.map +1 -1
  7. package/build/index.d.ts +2 -0
  8. package/build/index.d.ts.map +1 -1
  9. package/build/index.js.map +1 -1
  10. package/build/service.d.ts +1 -5
  11. package/build/service.d.ts.map +1 -1
  12. package/build/service.js +23 -41
  13. package/build/service.js.map +1 -1
  14. package/build/types.local.d.ts +5 -0
  15. package/build/types.local.d.ts.map +1 -0
  16. package/build/types.local.js +2 -0
  17. package/build/types.local.js.map +1 -0
  18. package/build/utils/config/types.d.ts +30 -0
  19. package/build/utils/config/types.d.ts.map +1 -0
  20. package/build/utils/config/types.js +2 -0
  21. package/build/utils/config/types.js.map +1 -0
  22. package/build/utils/config.d.ts +4 -24
  23. package/build/utils/config.d.ts.map +1 -1
  24. package/build/utils/config.js +74 -87
  25. package/build/utils/config.js.map +1 -1
  26. package/build/utils/connection/types.d.ts +17 -0
  27. package/build/utils/connection/types.d.ts.map +1 -0
  28. package/build/utils/connection/types.js +2 -0
  29. package/build/utils/connection/types.js.map +1 -0
  30. package/build/utils/connection.d.ts +2 -13
  31. package/build/utils/connection.d.ts.map +1 -1
  32. package/build/utils/connection.js +33 -37
  33. package/build/utils/connection.js.map +1 -1
  34. package/package.json +14 -13
  35. package/src/bootstrap.ts +13 -11
  36. package/src/index.ts +2 -0
  37. package/src/service.ts +28 -58
  38. package/src/types.local.ts +5 -0
  39. package/src/utils/config/types.ts +28 -0
  40. package/src/utils/config.ts +80 -89
  41. package/src/utils/connection/types.ts +17 -0
  42. package/src/utils/connection.ts +36 -36
  43. package/tests/context.ts +3 -3
  44. package/tests/criteria.spec.ts +107 -0
  45. package/tests/crud.spec.ts +68 -18
  46. package/tests/custom-sql.spec.ts +3 -2
  47. package/tests/migration.spec.ts +34 -3
  48. package/tests/sync.spec.ts +91 -1
@@ -1,5 +1,5 @@
1
1
  import { afterAll, describe, expect, test } from 'bun:test'
2
- import { makePostgresResource, resetDeclarations } from '@owlmeans/postgres-resource'
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'
@@ -61,6 +61,13 @@ const failingBody = async (): Promise<void> => {
61
61
  throw new Error('deliberate')
62
62
  }
63
63
 
64
+ const failingSql = 'SELECT o."enquiryId" FROM {{}} o WHERE o."title" = $1'
65
+ const sqlFailures: Record<string, (tx: PostgresTx) => Promise<unknown>> = {
66
+ query: async tx => await tx.query(failingSql, ['private-bound-value']),
67
+ queryOne: async tx => await tx.queryOne(failingSql, ['private-bound-value']),
68
+ execute: async tx => await tx.execute(failingSql, ['private-bound-value']),
69
+ }
70
+
64
71
  const driftBody = async (tx: PostgresTx): Promise<void> => { await tx.execute('SELECT 1') }
65
72
  const driftEdited = async (tx: PostgresTx): Promise<void> => { await tx.execute('SELECT 2') }
66
73
 
@@ -214,6 +221,30 @@ describe('@owlmeans/postgres — code registered migrations', () => {
214
221
  expect(rows).toHaveLength(0)
215
222
  })
216
223
 
224
+ it('failed migration SQL survives all transaction methods, with placeholders resolved and values kept separate', async () => {
225
+ for (const [method, apply] of Object.entries(sqlFailures)) {
226
+ const alias = `mig-sql-${method.toLowerCase()}`
227
+ await boot({ alias })
228
+ let failure: unknown
229
+ try {
230
+ await boot({ alias, declare: resource => {
231
+ resource.migration('0001-bad-sql', async tx => { await apply(tx) }, MigrationStage.Post)
232
+ } })
233
+ } catch (error) {
234
+ failure = error
235
+ }
236
+ expect(failure).toBeInstanceOf(MigrationError)
237
+ const cause = (failure as Error).cause as Error
238
+ expect(cause.message).toContain('42703')
239
+ expect(cause.message).toContain('SQL: SELECT o."enquiryId" FROM')
240
+ expect(cause.message).toContain(suite.schema)
241
+ expect(cause.message).toContain('$1')
242
+ expect(cause.message).not.toContain('{{}}')
243
+ expect(cause.message).not.toContain('private-bound-value')
244
+ expect(cause.cause).toBeInstanceOf(Error)
245
+ }
246
+ })
247
+
217
248
  it('refuses to start when an applied migration body has been edited', async () => {
218
249
  await boot({ alias: 'mig-e' })
219
250
  await boot({
@@ -222,7 +253,7 @@ describe('@owlmeans/postgres — code registered migrations', () => {
222
253
  })
223
254
 
224
255
  /** A restarted process with an edited source file — the registry has to be rebuilt. */
225
- resetDeclarations('mig-e')
256
+ pgDeclarationHelper.resetDeclarations('mig-e')
226
257
 
227
258
  await expect(boot({
228
259
  alias: 'mig-e',
@@ -231,7 +262,7 @@ describe('@owlmeans/postgres — code registered migrations', () => {
231
262
  })
232
263
 
233
264
  it('rejects a changed body under a name already registered in this process', () => {
234
- resetDeclarations('mig-conflict')
265
+ pgDeclarationHelper.resetDeclarations('mig-conflict')
235
266
  const resource = makePostgresResource<Note, PostgresResource<Note>>('mig-conflict')
236
267
  resource.migration('0001-drift', driftBody)
237
268
 
@@ -1,5 +1,7 @@
1
1
  import { afterAll, describe, expect, test } from 'bun:test'
2
- import { makePostgresResource, PgAutoSync, PostgresCastRequired } from '@owlmeans/postgres-resource'
2
+ import {
3
+ makePgIntrospectHelper, makePostgresResource, PgAutoSync, pgDiffHelper, PostgresCastRequired,
4
+ } from '@owlmeans/postgres-resource'
3
5
  import type { PostgresResource } from '@owlmeans/postgres-resource'
4
6
  import type { ResourceRecord } from '@owlmeans/resource'
5
7
 
@@ -154,3 +156,91 @@ describe('@owlmeans/postgres — structure reconciliation', () => {
154
156
  })).rejects.toThrow(PostgresCastRequired)
155
157
  })
156
158
  })
159
+
160
+ interface Plot extends ResourceRecord {
161
+ id?: string
162
+ garden: string
163
+ bed?: string
164
+ crop?: string
165
+ season: string
166
+ soil?: Record<string, unknown>
167
+ }
168
+
169
+ /**
170
+ * A community garden's plots: a plain compound index, an expression index and two partial ones —
171
+ * the shapes `pg_indexes.indexdef` renders differently from the statement that created them
172
+ * (identifiers unquoted, literals cast, a partial predicate parenthesized), plus an enum CHECK.
173
+ */
174
+ const plots = {
175
+ type: 'object',
176
+ properties: {
177
+ id: { type: 'string', format: 'uuid' },
178
+ garden: { type: 'string' },
179
+ bed: { type: 'string', nullable: true },
180
+ crop: { type: 'string', nullable: true },
181
+ season: { type: 'string', enum: ['spring', 'summer', 'autumn'] },
182
+ soil: { type: 'object', nullable: true }
183
+ },
184
+ required: ['id', 'garden', 'season'],
185
+ pg: {
186
+ indexes: [
187
+ { name: 'sync_plots_garden_season', columns: ['garden', 'season'] },
188
+ { name: 'sync_plots_scope', unique: true, expression: `"garden", COALESCE("bed", ''), "season"` },
189
+ { name: 'sync_plots_crop', columns: ['garden', 'crop'], where: '"crop" IS NOT NULL' },
190
+ { name: 'sync_plots_wet', columns: ['garden'], where: `("soil"->>'state') = 'wet'` }
191
+ ]
192
+ }
193
+ }
194
+
195
+ const plotSuite = makeSuite('sync_idx')
196
+
197
+ describe('@owlmeans/postgres — reconciling a table already at its shape', () => {
198
+ if (gate.skip) {
199
+ test.skip(gate.reason ?? 'postgres gate closed', () => {})
200
+
201
+ return
202
+ }
203
+
204
+ afterAll(async () => {
205
+ await plotSuite.teardown()
206
+ })
207
+
208
+ const bootPlots = async (): Promise<{ pg: PostgresService, resource: PostgresResource<Plot> }> => {
209
+ const resource = makePostgresResource<Plot, PostgresResource<Plot>>('sync-plots')
210
+ resource.schema = plots as never
211
+ const booted = await plotSuite.boot({ resources: [resource] })
212
+
213
+ return { pg: booted.pg, resource: booted.context.resource<PostgresResource<Plot>>('sync-plots') }
214
+ }
215
+
216
+ const objectOids = async (pg: PostgresService): Promise<Record<string, number>> => Object.fromEntries(
217
+ (await pg.query<{ name: string, oid: number }>(
218
+ `SELECT c.relname AS name, c.oid::int AS oid FROM pg_class c JOIN pg_namespace n ON n.oid = c.relnamespace
219
+ WHERE n.nspname = $1 AND c.relkind = 'i' AND c.relname LIKE 'sync_plots_%'
220
+ UNION ALL
221
+ SELECT k.conname AS name, k.oid::int AS oid FROM pg_constraint k JOIN pg_namespace n ON n.oid = k.connamespace
222
+ WHERE n.nspname = $1 AND k.contype = 'c' AND k.conname LIKE 'sync_plots_%'`, [plotSuite.schema]
223
+ )).map(row => [row.name, row.oid])
224
+ )
225
+
226
+ it('plans nothing on a second boot — no index or check is dropped and recreated', async () => {
227
+ const first = await bootPlots()
228
+ const before = await objectOids(first.pg)
229
+ expect(Object.keys(before).sort()).toEqual([
230
+ 'sync_plots_crop', 'sync_plots_garden_season', 'sync_plots_pkey', 'sync_plots_scope', 'sync_plots_season_enum',
231
+ 'sync_plots_wet'
232
+ ])
233
+
234
+ const second = await bootPlots()
235
+ const { pool } = await second.resource.db()
236
+ const client = await pool.connect()
237
+ try {
238
+ const table = second.resource.table
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
+ } finally {
242
+ client.release()
243
+ }
244
+ expect(await objectOids(second.pg)).toEqual(before)
245
+ })
246
+ })