@ultimat3/db 31.10.0 → 31.11.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/README.md CHANGED
@@ -681,6 +681,8 @@ await subscription.unlisten();
681
681
 
682
682
  `As of 2026-11-02`: **a foreign key on a live table** is written to the deferred step (`not valid`, then `validate constraint`, an old key held until the contract phase), **a CHECK** `not valid` in the transaction with its validate deferred, **a column drop** takes two releases (`retired`, `X_MIGRATION_ROLLING_UNSAFE`), and **`migrate()` / `rollback()` send it again** (a transactional step whole, a `-- transaction: false` step per statement) when `lock_timeout` fires (`lockRetries`, default 5). The user-facing page: `wiki/Migrations-And-Backfills.md`.
683
683
 
684
+ **A declared collation** (`As of 2026-11`, `text().collate('C')`): `collate "C"` follows the type in a `create table` or `add column`, the snapshot records `collation` only when declared, and declaring or dropping it on an existing column is `alter column … type <type> collate "C"` (or `"default"`) in place, its reverse in `down`. A retype states the collation it keeps — Postgres resets it otherwise (`generate-collation.test.ts`). Drift does not read the catalog's collation, as it reads no column type.
685
+
684
686
  `migrate()` takes the advisory lock by **polling** `pg_try_advisory_lock(4919202607)` every 500ms
685
687
  until `lockWaitMs` (default 60s) and then throwing `X_MIGRATE_CONCURRENT` (`As of 2026-08`).
686
688
  `pg_advisory_lock` has no timeout, and a predecessor OOM-killed on a partition keeps its backend —
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ultimat3/db",
3
- "version": "31.10.0",
3
+ "version": "31.11.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.10.0"
48
+ "@ultimat3/core": "31.11.0"
49
49
  },
50
50
  "peerDependencies": {
51
51
  "@electric-sql/pglite": ">=0.5.0"
@@ -1,5 +1,5 @@
1
- // Single responsibility: the attributes of an EXISTING column that move in place — its default and
2
- // its nullability. Split from `generate.ts` because `diffTable` emitted SQL for a type change only:
1
+ // Single responsibility: the attributes of an EXISTING column that move in place — its default, its
2
+ // collation and its nullability. Split from `generate.ts` because `diffTable` emitted SQL for a type change only:
3
3
  // a changed or removed `.default()`, or a nullability change, emitted nothing while the snapshot
4
4
  // beside it recorded the move, so `x verify` answered `X_DB_SCHEMA_UNMIGRATED` and its own fix,
5
5
  // `x db gen`, produced an empty diff. A gate red forever, with the repair being the thing that failed.
@@ -11,6 +11,7 @@ import type { Plan } from './foreign-key-plan';
11
11
  import { appendOnlyBackfillRefused } from './generate-append-only';
12
12
  import type { ColumnDescription } from './introspect';
13
13
  import { identifier } from './sql';
14
+ import { sqlType } from './sql-type';
14
15
  import { statementsOf } from './statement-split';
15
16
 
16
17
  /**
@@ -26,6 +27,20 @@ function screened(column: string, expression: string): string {
26
27
  return expression;
27
28
  }
28
29
 
30
+ /**
31
+ * ` collate "<name>"` after a type, or nothing. An ALTER … TYPE with no COLLATE resets the column to
32
+ * its type's default collation, so a retype states the collation it keeps — `"default"` included.
33
+ */
34
+ export function collateClause(collation: string | null | undefined, always = false): string {
35
+ if (collation == null) return always ? ' collate "default"' : '';
36
+ return ` collate ${identifier(collation).text}`;
37
+ }
38
+
39
+ /** The type a column clause declares: `text`, or `text collate "C"` when one was declared. */
40
+ export function typeClause(column: ColumnDescriptionLike): string {
41
+ return `${sqlType(column.kind)}${collateClause(column.collation)}`;
42
+ }
43
+
29
44
  /** What the column should hold as a default: one this generator cannot render stays as it is. */
30
45
  export function wantedDefault(
31
46
  column: ColumnDescriptionLike,
@@ -39,17 +54,20 @@ export function wantedDefault(
39
54
  * the column and refuses when no assignment cast exists (`42804`), whatever the new default is, so
40
55
  * it is dropped first and set after: a type change is the one move that has to span the default.
41
56
  * `down` is reversed at assembly, so what it pushes here runs last to first: drop the new default,
42
- * retype back, restore the old one. With no default on either side, this is the bare ALTER.
57
+ * retype back, restore the old one. With no default on either side, this is the bare ALTER. Each
58
+ * side states its own collation, which an ALTER … TYPE would otherwise reset to the type's default.
43
59
  */
44
60
  export function retypeOverDefault(
45
61
  table: string,
46
62
  column: ColumnDescriptionLike,
47
63
  recorded: ColumnDescription,
48
64
  plan: Plan,
49
- up: string,
50
- down: string,
51
65
  ): void {
52
66
  const alter = `alter table ${identifier(table).text} alter column ${identifier(column.column).text}`;
67
+ const retype = (type: string, collation: string | undefined): string =>
68
+ `${alter} type ${type}${collateClause(collation)} using ${identifier(column.column).text}::${type};`;
69
+ const up = retype(sqlType(column.kind), column.collation);
70
+ const down = retype(recorded.dataType, recorded.collation);
53
71
  const drop = `${alter} drop default;`;
54
72
  const set = (expression: string): string =>
55
73
  `${alter} set default ${screened(column.column, expression)};`;
@@ -96,6 +114,12 @@ export function alterColumnInPlace(
96
114
  plan.up.push(set(wanted));
97
115
  plan.down.push(set(held));
98
116
  }
117
+ // Same type, new collation: no `using`, no rewrite — Postgres rebuilds the column's indexes.
118
+ if ((column.collation ?? null) !== (recorded.collation ?? null)) {
119
+ const type = `${alter} type ${sqlType(column.kind)}`;
120
+ plan.up.push(`${type}${collateClause(column.collation, true)};`);
121
+ plan.down.push(`${type}${collateClause(recorded.collation, true)};`);
122
+ }
99
123
  const nullable = !column.notNull;
100
124
  if (nullable === recorded.nullable) return;
101
125
  if (nullable) {
@@ -24,10 +24,11 @@ import { identifier } from './sql';
24
24
  */
25
25
 
26
26
  /**
27
- * A snapshot column as a column definition. `dataType`, `default` and `generated` are the DECLARED
28
- * side's own text, out of the author's migration — the trust `missingCheck` gives its predicate —
29
- * and `psqlCommand` closes the shell layer around all of it. `notNull` is the caller's: an `add
30
- * column … not null` with no value to fill existing rows with is refused on a table holding any.
27
+ * A snapshot column as a column definition. `dataType`, `collation`, `default` and `generated` are
28
+ * the DECLARED side's own text, out of the author's migration — the trust `missingCheck` gives its
29
+ * predicate — and `psqlCommand` closes the shell layer around all of it. `notNull` is the caller's:
30
+ * an `add column … not null` with no value to fill existing rows with is refused on a table holding
31
+ * any.
31
32
  */
32
33
  const columnClause = (column: ColumnDescription, notNull: boolean): string => {
33
34
  const value =
@@ -36,7 +37,9 @@ const columnClause = (column: ColumnDescription, notNull: boolean): string => {
36
37
  : column.default === null
37
38
  ? ''
38
39
  : ` default ${column.default}`;
39
- return `${identifier(column.name).text} ${column.dataType}${value}${notNull ? ' not null' : ''}`;
40
+ const collate =
41
+ column.collation === undefined ? '' : ` collate ${identifier(column.collation).text}`;
42
+ return `${identifier(column.name).text} ${column.dataType}${collate}${value}${notNull ? ' not null' : ''}`;
40
43
  };
41
44
 
42
45
  /** Every row an `add column` lands on gets a value, so `not null` can ride on the same statement. */
@@ -11,6 +11,11 @@ export interface ColumnDescriptionLike {
11
11
  readonly property: string;
12
12
  readonly column: string;
13
13
  readonly kind: string;
14
+ /**
15
+ * `text().collate('C')`'s collation, absent when the column takes the database default. Crosses
16
+ * structurally like `onDelete`, and absent emits exactly what an older description emitted.
17
+ */
18
+ readonly collation?: string | undefined;
14
19
  readonly notNull: boolean;
15
20
  readonly primaryKey: boolean;
16
21
  readonly unique: boolean;
@@ -42,6 +42,7 @@ export function snapshotOf(
42
42
  // Only when one was declared — absent stays absent, so no snapshot written before this
43
43
  // field existed gains a key and no app's sidecar regenerates over a fact already true.
44
44
  ...(column.generated === undefined ? {} : { generated: column.generated }),
45
+ ...(column.collation === undefined ? {} : { collation: column.collation }),
45
46
  }));
46
47
  const checks = declaredChecks(entity);
47
48
  return {
package/src/generate.ts CHANGED
@@ -5,7 +5,7 @@
5
5
 
6
6
  import { systemClock } from '@ultimat3/core';
7
7
  import { checkClauses, checkPlan } from './check-ddl';
8
- import { alterColumnInPlace, retypeOverDefault, wantedDefault } from './column-alter';
8
+ import { alterColumnInPlace, retypeOverDefault, typeClause, wantedDefault } from './column-alter';
9
9
  import { defaultExpression } from './column-default';
10
10
  import { type ConcurrentPlan, emptyConcurrentPlan } from './concurrent-plan';
11
11
  import { dropOrder } from './drop-order';
@@ -54,10 +54,7 @@ function columnClause(column: ColumnDescriptionLike): string {
54
54
  // typecheck and a name that closes its own quote produced a real `drop table` through
55
55
  // `generateMigration` once already. It is also what makes an unrendered report safe to write
56
56
  // into a `--` comment, since generation refuses the dangerous name before the comment exists.
57
- const parts = [
58
- identifier(column.column).text,
59
- `${sqlType(column.kind)}${generatedClause(column)}`,
60
- ];
57
+ const parts = [identifier(column.column).text, `${typeClause(column)}${generatedClause(column)}`];
61
58
  const expression = defaultExpression(column);
62
59
  if (expression !== null) parts.push(`default ${expression}`);
63
60
  if (column.notNull) parts.push('not null');
@@ -124,10 +121,7 @@ function retypeColumn(
124
121
  // are recorded on tables this diff is not looking at (`retype-keys.ts`).
125
122
  if (!retyped.has(column.column)) return 'unchanged';
126
123
  moveDependentsAside(live, column.column, plan, moved);
127
- const alter = (type: string): string =>
128
- `alter table ${identifier(table).text} alter column ${identifier(column.column).text} ` +
129
- `type ${type} using ${identifier(column.column).text}::${type};`;
130
- retypeOverDefault(table, column, recorded, plan, alter(wanted), alter(recorded.dataType));
124
+ retypeOverDefault(table, column, recorded, plan);
131
125
  moved.defaults.add(column.column);
132
126
  return 'altered';
133
127
  }
@@ -162,6 +156,8 @@ function diffTable(
162
156
  ? {
163
157
  ...recorded,
164
158
  default: wantedDefault(column, recorded),
159
+ // The retype's ALTER stated the collation too (`retypeColumn`).
160
+ collation: column.collation,
165
161
  }
166
162
  : recorded,
167
163
  plan,
package/src/introspect.ts CHANGED
@@ -36,6 +36,11 @@ export interface ColumnDescription {
36
36
  * spellings, which is the same rule `IndexDescription.where` states one field down.
37
37
  */
38
38
  readonly generated?: string | undefined;
39
+ /**
40
+ * The declared collation, as the SNAPSHOT records it (`collate('C')`); absent for the database
41
+ * default and on every catalog read — `x db gen` diffs snapshot against snapshot, as `generated`.
42
+ */
43
+ readonly collation?: string | undefined;
39
44
  /**
40
45
  * `true` on a column an entity lists under `retired` and a migration has marked (`retired.ts`):
41
46
  * old pods may still read it, so it stays until a later migration drops it. Absent otherwise, and
@@ -39,7 +39,16 @@ const directions = (value: unknown): value is readonly ('asc' | 'desc' | null)[]
39
39
 
40
40
  function column(value: unknown): ColumnDescription | undefined {
41
41
  if (!isRow(value)) return undefined;
42
- const { name, dataType, nullable, default: fallback, position, generated, retired } = value;
42
+ const {
43
+ name,
44
+ dataType,
45
+ nullable,
46
+ default: fallback,
47
+ position,
48
+ generated,
49
+ retired,
50
+ collation,
51
+ } = value;
43
52
  if (!str(name) || !str(dataType) || !bool(nullable) || !nullableStr(fallback)) return undefined;
44
53
  if (typeof position !== 'number') return undefined;
45
54
  // `generated` was recorded by `snapshotOf` and dropped HERE, silently, for as long as the field
@@ -47,6 +56,8 @@ function column(value: unknown): ColumnDescription | undefined {
47
56
  // so `retypeColumn` read every generated column as newly generated and rebuilt it on every
48
57
  // `x db gen`. Absent stays absent — an ordinary column must gain no key.
49
58
  if (!(generated === undefined || str(generated))) return undefined;
59
+ // The same rule for a collation: dropped here, every `x db gen` would declare it again.
60
+ if (!(collation === undefined || str(collation))) return undefined;
50
61
  // Dropped here it would refuse the drop it exists to allow. `true` or nothing, `false` normalised
51
62
  // away: a hand-edit meaning what absence means, the rule `appendOnly` states below.
52
63
  if (!(retired === undefined || bool(retired))) return undefined;
@@ -57,6 +68,7 @@ function column(value: unknown): ColumnDescription | undefined {
57
68
  default: fallback,
58
69
  position,
59
70
  ...(generated === undefined ? {} : { generated }),
71
+ ...(collation === undefined ? {} : { collation }),
60
72
  ...(retired === true ? { retired: true as const } : {}),
61
73
  };
62
74
  }