@ultimat3/db 31.30.0 → 31.32.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/db",
3
- "version": "31.30.0",
3
+ "version": "31.32.0",
4
4
  "description": "Postgres access, transactions, migrations and drift detection",
5
5
  "keywords": [
6
6
  "ultimate",
@@ -45,7 +45,7 @@
45
45
  "test": "bun test"
46
46
  },
47
47
  "dependencies": {
48
- "@ultimat3/core": "31.30.0"
48
+ "@ultimat3/core": "31.32.0"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@electric-sql/pglite": ">=0.5.0"
@@ -1,13 +1,14 @@
1
1
  // Single responsibility: the two refusals `x db branch` raises — a branch that exists, and a name
2
2
  // that is not one. Split from `errors.ts` at its line ceiling; `index.ts` re-exports both.
3
3
 
4
+ import { renderFixShellArg } from '@ultimat3/core';
4
5
  import { DbError } from './errors';
5
6
 
6
7
  export const branchExists = (branch: string): DbError =>
7
8
  new DbError({
8
9
  code: 'X_BRANCH_EXISTS',
9
10
  cause: `database "${branch}" already exists`,
10
- fix: `x db branch delete ${branch} # then re-create, or pick another name`,
11
+ fix: `x db branch delete ${renderFixShellArg(branch, '<branch>')} # then re-create, or pick another name`,
11
12
  meta: { branch },
12
13
  });
13
14
 
@@ -6,6 +6,7 @@
6
6
  // whose evaluation registers every code. The code is still declared, titled and registered in
7
7
  // `errors.ts`, exactly as `migration-errors.ts` and `invariant-errors.ts` are.
8
8
 
9
+ import { renderFixShellArg } from '@ultimat3/core';
9
10
  import { DbError } from './errors';
10
11
  import { shellInertIdentifier } from './sql';
11
12
 
@@ -36,6 +37,6 @@ export const dbDrift = (tableName: string, columnName: string): DbError =>
36
37
  fix:
37
38
  shellInertIdentifier(columnName) === null
38
39
  ? `x db gen "add the column named in this error" # its name carries shell syntax, so it is in the cause and not in this command; ${undeclaredColumnNext('it')}`
39
- : `x db gen "add ${columnName}" # ${undeclaredColumnNext(columnName)}`,
40
+ : `x db gen "add ${renderFixShellArg(columnName, 'the undeclared column')}" # ${undeclaredColumnNext(columnName)}`,
40
41
  meta: { table: tableName, column: columnName },
41
42
  });
@@ -3,6 +3,7 @@
3
3
  // compares anything (`drift.ts` decides whether two schemas disagree). A `fix:` is ONE command a shell runs, and a difference names the declared
4
4
  // side's spelling, never the catalog's — which is Postgres' rewriting.
5
5
 
6
+ import { isFixShellSafe, renderFixShellArg } from '@ultimat3/core';
6
7
  import { psqlCommand } from './dependent-view';
7
8
  import { undeclaredColumnNext } from './drift-errors';
8
9
  import { addForeignKey, dropForeignKey, onDeleteRule, validateConstraint } from './foreign-key';
@@ -134,7 +135,7 @@ export function unexpectedColumn(table: string, column: string): DriftDifference
134
135
  fix:
135
136
  shellInertIdentifier(column) === null
136
137
  ? `x db gen "add the undeclared column" # the live column name carries shell syntax, so it is in the cause and not in this command; ${undeclaredColumnNext('it')}`
137
- : `x db gen "add ${column}" # ${undeclaredColumnNext(column)}`,
138
+ : `x db gen "add ${renderFixShellArg(column, 'the undeclared column')}" # ${undeclaredColumnNext(column)}`,
138
139
  };
139
140
  }
140
141
 
@@ -240,8 +241,11 @@ export function unknownSchema(migrations: readonly Migration[]): DriftDifference
240
241
  // identifier and a migration description is not one either, so neither has a quoted form that
241
242
  // makes a hostile name safe. An EMPTY id is inert by construction and keeps its glob — that is
242
243
  // "no migrations at all", not a name this function refused to spell.
244
+ // `isFixShellSafe` too, for the id: it is screened where it is spliced into the glob, and a
245
+ // screen answering its placeholder there would widen the glob to every snapshot in the tree.
243
246
  const spellable =
244
- (id === '' || shellInertIdentifier(id) !== null) && shellInertIdentifier(name) !== null;
247
+ (id === '' || (shellInertIdentifier(id) !== null && isFixShellSafe(id))) &&
248
+ shellInertIdentifier(name) !== null;
245
249
  return {
246
250
  kind: 'unknown-schema',
247
251
  table: '',
@@ -254,7 +258,7 @@ export function unknownSchema(migrations: readonly Migration[]): DriftDifference
254
258
  // fix pointed back here — a cycle a scaffolded app hit on its first `x db migrate`. The
255
259
  // pathspec is a glob because this package is tier 1: only `@ultimat3/cli` knows the directory.
256
260
  fix: spellable
257
- ? `git checkout -- "*${id}.snapshot.json" # or, if it was never written: ` +
261
+ ? `git checkout -- "*${renderFixShellArg(id, '')}.snapshot.json" # or, if it was never written: ` +
258
262
  `delete migration "${id}" and rerun x db gen "${name}"`
259
263
  : 'git status --short -- "*.snapshot.json" # shows the sidecar that is gone: git ' +
260
264
  'checkout it, or delete that migration and rerun x db gen with its description — the ' +
package/src/errors.ts CHANGED
@@ -338,7 +338,7 @@ export const driverError = (statement: string, sourceError: unknown): DbError =>
338
338
  fix:
339
339
  state === SQLSTATE.idleInTransactionTimeout
340
340
  ? IDLE_IN_TRANSACTION_FIX
341
- : code === 'X_DB_STATEMENT_FAILED'
341
+ : code === 'X_DB_STATEMENT_FAILED' || !Object.hasOwn(SQLSTATE_FIXES, code)
342
342
  ? `psql "$DATABASE_URL" -c "<the statement in cause>" # SQLSTATE ${state} is the server's verdict on it: fix the SQL or the data it names, not the connection`
343
343
  : SQLSTATE_FIXES[code].replace('{constraint}', () => constraint ?? UNNAMED_CONSTRAINT),
344
344
  meta: {
@@ -76,12 +76,12 @@ export const rollbackStepsInvalid = (received: number): DbError =>
76
76
  });
77
77
 
78
78
  /**
79
- * `packages/db/migrations/0000_initial.snapshot.json` → `packages/db/migrations/0000_initial.*` —
80
- * every file that one migration owns, as one `rm` argument. Derived from the path the caller passed
81
- * rather than rebuilt from a directory this package does not know: `db` is tier 1 and where an app
82
- * keeps its migrations is `@ultimat3/cli`'s answer, not this one's.
79
+ * `packages/db/migrations/0000_initial.snapshot.json` → `packages/db/migrations/0000_initial` —
80
+ * the stem every file that one migration owns shares, so `<stem>.*` is one `rm` argument. Derived
81
+ * from the path the caller passed rather than rebuilt from a directory this package does not know:
82
+ * `db` is tier 1 and where an app keeps its migrations is `@ultimat3/cli`'s answer, not this one's.
83
83
  */
84
- const snapshotSiblings = (file: string): string => file.replace(/\.snapshot\.json$/, '.*');
84
+ const snapshotStem = (file: string): string => file.replace(/\.snapshot\.json$/, '');
85
85
 
86
86
  /**
87
87
  * `20260817120000_add_posts` → `add_posts`, the argument `x db gen` takes. The name is free text
@@ -114,7 +114,12 @@ export const migrationSnapshotMissing = (id: string, file: string): DbError => {
114
114
  // rather than half of it being escaped — the same screen and the same degradation `unknownSchema`
115
115
  // (`drift-findings.ts`) runs on the same condition. A `rm` whose argument was substituted away
116
116
  // still reads as a command, and now removes something else.
117
- const spellable = shellSpellable(file) && shellSpellable(migrationNameOf(id));
117
+ // Each value below is screened where it is spliced as well, and `spellable` is what makes the
118
+ // screen's placeholder unreachable: a line is spelled whole or not at all.
119
+ const spellable =
120
+ shellSpellable(file) &&
121
+ shellSpellable(snapshotStem(file)) &&
122
+ shellSpellable(migrationNameOf(id));
118
123
  return new DbError({
119
124
  code: 'X_MIGRATION_SNAPSHOT_MISSING',
120
125
  cause: `migration "${id}" records no schema snapshot (${file}), so there is nothing to diff against`,
@@ -124,8 +129,8 @@ export const migrationSnapshotMissing = (id: string, file: string): DbError => {
124
129
  // the two errors pointed at each other and an app's first migration had no way out.
125
130
  // `x db gen` is named only *after* the files it would trip over are gone.
126
131
  fix: spellable
127
- ? `git checkout -- ${file} # or, if it was never written: ` +
128
- `rm ${snapshotSiblings(file)} && x db gen "${migrationNameOf(id)}"`
132
+ ? `git checkout -- ${renderFixShellArg(file, '')} # or, if it was never written: ` +
133
+ `rm ${renderFixShellArg(snapshotStem(file), '')}.* && x db gen "${renderFixShellArg(migrationNameOf(id), '')}"`
129
134
  : 'restore the .snapshot.json committed beside this migration, or delete every file that ' +
130
135
  'migration owns and rerun x db gen with its description — the file named in this ' +
131
136
  "error's cause carries a backtick, a dollar sign, a quote, a backslash or whitespace, " +
@@ -116,7 +116,10 @@ export const POOL_PROFILES = Object.freeze<Record<Role, PoolProfile>>({
116
116
  });
117
117
 
118
118
  export function poolProfileFor(role: Role = resolveRole()): PoolProfile {
119
- return POOL_PROFILES[role];
119
+ if (Object.hasOwn(POOL_PROFILES, role)) return POOL_PROFILES[role];
120
+ // Typed, and untyped at a JS caller: `POOL_PROFILES['constructor']` was the `Object` function,
121
+ // a pool with no `max`. Read as the env would be, a non-role is refused with `X_ROLE_INVALID`.
122
+ return poolProfileFor(resolveRole({ env: { ROLE: role } }));
120
123
  }
121
124
 
122
125
  /** The one pool knob an operator can turn without a rebuild. Layered over the role default. */
package/src/sql-noise.ts CHANGED
@@ -33,7 +33,7 @@ export function stripSqlNoise(text: string): string {
33
33
  index += 1;
34
34
  continue;
35
35
  }
36
- out += BLANK[noise.kind] ?? ' ';
36
+ out += Object.hasOwn(BLANK, noise.kind) ? (BLANK[noise.kind] ?? ' ') : ' ';
37
37
  index = noise.end;
38
38
  }
39
39
  return out;
package/src/sqlstate.ts CHANGED
@@ -136,7 +136,10 @@ export const DB_SQLSTATE_CODES: Readonly<Record<string, DbSqlStateCode>> = Objec
136
136
  */
137
137
  export function sqlStateCode(error: unknown): DbSqlStateCode | undefined {
138
138
  const state = sqlState(error);
139
- return state === undefined ? undefined : DB_SQLSTATE_CODES[state];
139
+ // A state the table does not classify — and never a prototype member — is unclassified.
140
+ return state === undefined || !Object.hasOwn(DB_SQLSTATE_CODES, state)
141
+ ? undefined
142
+ : DB_SQLSTATE_CODES[state];
140
143
  }
141
144
 
142
145
  /** Whether re-running the whole transaction is the documented answer. `withTransaction`'s retry. */