@ultimat3/db 31.48.0 → 31.50.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/CLAUDE.md +6 -5
- package/README.md +4 -3
- package/package.json +2 -2
- package/src/column-drop.ts +154 -0
- package/src/errors.ts +6 -0
- package/src/generate.ts +5 -38
- package/src/generated-column.ts +12 -4
- package/src/index-plan.ts +8 -0
- package/src/index.ts +2 -0
- package/src/invariant-ddl.ts +5 -2
- package/src/migrate-dev-fix.ts +30 -0
- package/src/migrate-rollback.ts +8 -1
- package/src/migrate.ts +33 -10
- package/src/migration-errors.ts +67 -0
- package/src/migration-fork-fold.ts +215 -0
- package/src/migration-fork-sql.ts +133 -0
- package/src/migration-fork.ts +143 -0
- package/src/migration-ledger.ts +37 -1
- package/src/pool-profile.ts +2 -0
- package/src/replica-client.ts +7 -2
- package/src/retype-dependents.ts +17 -0
- package/src/rolling-errors.ts +25 -0
- package/src/row-security.ts +11 -3
- package/src/schema-dump-entry.ts +10 -2
- package/src/snapshot-parse.ts +5 -0
package/src/retype-dependents.ts
CHANGED
|
@@ -64,6 +64,23 @@ export function referencesColumn(expression: string, column: string): boolean {
|
|
|
64
64
|
return false;
|
|
65
65
|
}
|
|
66
66
|
|
|
67
|
+
/**
|
|
68
|
+
* Whether `drop column` takes `index` with it: Postgres drops every index that DEPENDS on the
|
|
69
|
+
* column — keyed by it, carrying it as INCLUDE payload, or reading it in its predicate or its
|
|
70
|
+
* expression key (measured on PGlite: `posts_p on (title) where "legacy" is not null` goes with
|
|
71
|
+
* `legacy`). A catalog-read expression key sits in `columns` in parentheses, so it is read too.
|
|
72
|
+
*/
|
|
73
|
+
export function indexDependsOn(index: IndexDescription, column: string): boolean {
|
|
74
|
+
return (
|
|
75
|
+
index.columns.some((key) =>
|
|
76
|
+
key.startsWith('(') ? referencesColumn(key, column) : key === column,
|
|
77
|
+
) ||
|
|
78
|
+
(index.include ?? []).includes(column) ||
|
|
79
|
+
(index.where !== null && referencesColumn(index.where, column)) ||
|
|
80
|
+
(index.expression !== undefined && referencesColumn(index.expression, column))
|
|
81
|
+
);
|
|
82
|
+
}
|
|
83
|
+
|
|
67
84
|
/** The recorded objects a retype of `column` cannot leave in place. */
|
|
68
85
|
export interface RetypeDependents {
|
|
69
86
|
/**
|
package/src/rolling-errors.ts
CHANGED
|
@@ -4,6 +4,7 @@
|
|
|
4
4
|
|
|
5
5
|
import { renderFixShellArg } from '@ultimat3/core';
|
|
6
6
|
import { DbError } from './errors';
|
|
7
|
+
import { migrationNameArg } from './primary-key';
|
|
7
8
|
import type { RetireAndContract, RollingUnsafeStatement } from './rolling';
|
|
8
9
|
|
|
9
10
|
const CODE = 'X_MIGRATION_ROLLING_UNSAFE';
|
|
@@ -37,6 +38,30 @@ export const columnDropNotRetired = (
|
|
|
37
38
|
meta: { table, column },
|
|
38
39
|
});
|
|
39
40
|
|
|
41
|
+
/**
|
|
42
|
+
* `x db gen`: a plain column declared generated is dropped and added back computed
|
|
43
|
+
* (`regenerate`), with no `retired` in between. The same fact as `columnDropNotRetired` from the
|
|
44
|
+
* other side: the previous release still WRITES it. `migration` is the name as given: this one
|
|
45
|
+
* prints it back as the argument it was (`migrationNameArg`), so the command can be pasted.
|
|
46
|
+
*/
|
|
47
|
+
export const columnRebuildNotRetired = (
|
|
48
|
+
entity: string,
|
|
49
|
+
table: string,
|
|
50
|
+
column: string,
|
|
51
|
+
migration: string,
|
|
52
|
+
): DbError => {
|
|
53
|
+
const c = renderFixShellArg(column, '<column>');
|
|
54
|
+
const gen = `x db gen ${migrationNameArg(migration)} --allow-destructive`;
|
|
55
|
+
return new DbError({
|
|
56
|
+
code: CODE,
|
|
57
|
+
cause:
|
|
58
|
+
`making "${table}"."${column}" a generated column drops it and adds it back computed, and the ` +
|
|
59
|
+
'pods still running the previous release write it: Postgres refuses a value for a generated column (428C9)',
|
|
60
|
+
fix: `entity('${renderFixShellArg(entity, '<entity>')}', { retired: ['${c}'] }) # with ${c} out of columns; then x db gen "retire ${c}", deploy, then declare it generated and ${gen} — one instance that stops before the next starts: ${gen} --rolling-unsafe`,
|
|
61
|
+
meta: { table, column },
|
|
62
|
+
});
|
|
63
|
+
};
|
|
64
|
+
|
|
40
65
|
/** `retired` names what no migration recorded, or a column the entity still declares. */
|
|
41
66
|
export const retiredNameUnknown = (entity: string, table: string, column: string): DbError =>
|
|
42
67
|
new DbError({
|
package/src/row-security.ts
CHANGED
|
@@ -20,13 +20,21 @@ const TYPE = /^[a-z][a-z0-9 ]*$/;
|
|
|
20
20
|
|
|
21
21
|
/**
|
|
22
22
|
* Unset is `''` (a `set_config` local value reads back as `''` once its transaction ends) and
|
|
23
|
-
* `nullif` turns it into NULL, so an unset tenant matches NO row: fail closed, never open.
|
|
23
|
+
* `nullif` turns it into NULL, so an unset tenant matches NO row: fail closed, never open. An unset
|
|
24
|
+
* cross switch reads NULL on a connection that never set it, and `''` on a pooled one whose earlier
|
|
25
|
+
* transaction set it locally; neither is `'on'`, so in both the `else` — the tenant — decides.
|
|
26
|
+
*
|
|
27
|
+
* A `case`, never `tenant or cross`, and the two read the same rows. The difference is the PLAN:
|
|
28
|
+
* the planner multiplies an `or` of two guessed selectivities (~5% together) into every row
|
|
29
|
+
* estimate, so a tenant's 2,500-row keyset page read as ~140 rows and was answered by a bitmap scan
|
|
30
|
+
* and a sort instead of the ordered index scan its `(org_id, …)` index exists for. A `case` is
|
|
31
|
+
* estimated at one half, which keeps that scan (`row-security-plan-shape.test.ts`).
|
|
24
32
|
*/
|
|
25
33
|
function predicate(column: string, type: string): string {
|
|
26
34
|
if (!TYPE.test(type)) throw identifierUnsafe(type);
|
|
27
35
|
return (
|
|
28
|
-
`(
|
|
29
|
-
`
|
|
36
|
+
`(case when current_setting('${CROSS_TENANT_SETTING}', true) = 'on' then true` +
|
|
37
|
+
` else ${identifier(column).text} = nullif(current_setting('${TENANT_SETTING}', true), '')::${type} end)`
|
|
30
38
|
);
|
|
31
39
|
}
|
|
32
40
|
|
package/src/schema-dump-entry.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
// `@ultimat3/db/schema-dump`: the whole schema as files, and the drift checks over them. Its own
|
|
2
2
|
// entry because only three callers ever run it — `x db gen`, `x db migrate` and the gate's `drift`
|
|
3
|
-
// step — while `@ultimat3/db` is imported by every role of every app: on the barrel these
|
|
4
|
-
//
|
|
3
|
+
// step — while `@ultimat3/db` is imported by every role of every app: on the barrel these modules
|
|
4
|
+
// were evaluated by every web, worker and scheduler pod to serve nothing. The fork check too.
|
|
5
5
|
|
|
6
6
|
export type {
|
|
7
7
|
CatalogColumn,
|
|
@@ -32,6 +32,14 @@ export {
|
|
|
32
32
|
} from './dump-drift';
|
|
33
33
|
export type { IntrospectCatalogOptions } from './introspect-catalog';
|
|
34
34
|
export { introspectCatalog } from './introspect-catalog';
|
|
35
|
+
export type {
|
|
36
|
+
ForkCollision,
|
|
37
|
+
ForkLoss,
|
|
38
|
+
ForkObjectKind,
|
|
39
|
+
ForkStale,
|
|
40
|
+
MigrationFork,
|
|
41
|
+
} from './migration-fork';
|
|
42
|
+
export { migrationFork } from './migration-fork';
|
|
35
43
|
export { unexpectedObjects } from './object-drift';
|
|
36
44
|
export type { SchemaDumpFile } from './schema-dump';
|
|
37
45
|
export { renderSchemaDump } from './schema-dump';
|
package/src/snapshot-parse.ts
CHANGED
|
@@ -15,6 +15,7 @@ import type {
|
|
|
15
15
|
TriggerRecord,
|
|
16
16
|
ViewDescription,
|
|
17
17
|
} from './introspect';
|
|
18
|
+
import { statementsOf } from './statement-split';
|
|
18
19
|
|
|
19
20
|
const str = (value: unknown): value is string => typeof value === 'string';
|
|
20
21
|
const bool = (value: unknown): value is boolean => typeof value === 'boolean';
|
|
@@ -47,6 +48,10 @@ function column(value: unknown): ColumnDescription | undefined {
|
|
|
47
48
|
} = value;
|
|
48
49
|
if (!str(name) || !str(dataType) || !bool(nullable) || !nullableStr(fallback)) return undefined;
|
|
49
50
|
if (typeof position !== 'number') return undefined;
|
|
51
|
+
// Spliced into `down` by every arm that puts a column back (`add column "x" <dataType>`), from a
|
|
52
|
+
// file anything may edit: one statement, the screen a recorded default and a generation
|
|
53
|
+
// expression take where they are spliced. Here, not there, because there are five such arms.
|
|
54
|
+
if (statementsOf(dataType).length > 1) return undefined;
|
|
50
55
|
// `generated` was recorded by `snapshotOf` and dropped HERE, silently, for as long as the field
|
|
51
56
|
// has existed: the sidecar carried the expression and the parse handed back a column without it,
|
|
52
57
|
// so `retypeColumn` read every generated column as newly generated and rebuilt it on every
|