shutro 0.0.0-stage → 0.2.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/LICENSE +21 -0
- package/README.md +99 -2
- package/dist/audit/index.d.ts +59 -0
- package/dist/audit/index.js +96 -0
- package/dist/cli/bin.d.ts +2 -0
- package/dist/cli/bin.js +11 -0
- package/dist/cli/cli.d.ts +10 -0
- package/dist/cli/cli.js +164 -0
- package/dist/cli/config.d.ts +18 -0
- package/dist/cli/config.js +49 -0
- package/dist/cli/errors.d.ts +16 -0
- package/dist/cli/errors.js +23 -0
- package/dist/cli/generate/generate.d.ts +33 -0
- package/dist/cli/generate/generate.js +84 -0
- package/dist/cli/generate/render.d.ts +23 -0
- package/dist/cli/generate/render.js +249 -0
- package/dist/cli/index.d.ts +12 -0
- package/dist/cli/index.js +12 -0
- package/dist/cli/init.d.ts +2 -0
- package/dist/cli/init.js +65 -0
- package/dist/cli/inspect.d.ts +10 -0
- package/dist/cli/inspect.js +82 -0
- package/dist/cli/packages.d.ts +6 -0
- package/dist/cli/packages.js +6 -0
- package/dist/cli/project.d.ts +18 -0
- package/dist/cli/project.js +39 -0
- package/dist/cli/resolve.d.ts +2 -0
- package/dist/cli/resolve.js +28 -0
- package/dist/core/compute/graph.d.ts +52 -0
- package/dist/core/compute/graph.js +162 -0
- package/dist/core/data/data.d.ts +59 -0
- package/dist/core/data/data.js +119 -0
- package/dist/core/data/load.d.ts +37 -0
- package/dist/core/data/load.js +236 -0
- package/dist/core/env/env.d.ts +468 -0
- package/dist/core/env/env.js +916 -0
- package/dist/core/env/identity.d.ts +43 -0
- package/dist/core/env/identity.js +65 -0
- package/dist/core/env/inspector.d.ts +55 -0
- package/dist/core/env/inspector.js +85 -0
- package/dist/core/env/time.d.ts +12 -0
- package/dist/core/env/time.js +38 -0
- package/dist/core/errors.d.ts +256 -0
- package/dist/core/errors.js +360 -0
- package/dist/core/events/events.d.ts +44 -0
- package/dist/core/events/events.js +58 -0
- package/dist/core/events/workers.d.ts +56 -0
- package/dist/core/events/workers.js +155 -0
- package/dist/core/index.d.ts +51 -0
- package/dist/core/index.js +42 -0
- package/dist/core/model/company.d.ts +6 -0
- package/dist/core/model/company.js +31 -0
- package/dist/core/model/faces.d.ts +42 -0
- package/dist/core/model/faces.js +1 -0
- package/dist/core/model/field.d.ts +479 -0
- package/dist/core/model/field.js +461 -0
- package/dist/core/model/generated.d.ts +77 -0
- package/dist/core/model/generated.js +1 -0
- package/dist/core/model/id.d.ts +33 -0
- package/dist/core/model/id.js +4 -0
- package/dist/core/model/model.d.ts +387 -0
- package/dist/core/model/model.js +566 -0
- package/dist/core/model/partition.d.ts +31 -0
- package/dist/core/model/partition.js +71 -0
- package/dist/core/model/related.d.ts +17 -0
- package/dist/core/model/related.js +60 -0
- package/dist/core/model/typed.d.ts +107 -0
- package/dist/core/model/typed.js +46 -0
- package/dist/core/money/currency.d.ts +39 -0
- package/dist/core/money/currency.js +37 -0
- package/dist/core/money/decimal.d.ts +26 -0
- package/dist/core/money/decimal.js +51 -0
- package/dist/core/money/fx.d.ts +68 -0
- package/dist/core/money/fx.js +124 -0
- package/dist/core/money/money.d.ts +84 -0
- package/dist/core/money/money.js +173 -0
- package/dist/core/money/quantity.d.ts +67 -0
- package/dist/core/money/quantity.js +108 -0
- package/dist/core/money/rounding.d.ts +11 -0
- package/dist/core/money/rounding.js +12 -0
- package/dist/core/recordset/aggregate.d.ts +22 -0
- package/dist/core/recordset/aggregate.js +258 -0
- package/dist/core/recordset/audit.d.ts +33 -0
- package/dist/core/recordset/audit.js +262 -0
- package/dist/core/recordset/compute.d.ts +36 -0
- package/dist/core/recordset/compute.js +414 -0
- package/dist/core/recordset/constraints.d.ts +9 -0
- package/dist/core/recordset/constraints.js +61 -0
- package/dist/core/recordset/domain.d.ts +31 -0
- package/dist/core/recordset/domain.js +300 -0
- package/dist/core/recordset/onchange.d.ts +43 -0
- package/dist/core/recordset/onchange.js +240 -0
- package/dist/core/recordset/recordset.d.ts +110 -0
- package/dist/core/recordset/recordset.js +789 -0
- package/dist/core/recordset/relations.d.ts +15 -0
- package/dist/core/recordset/relations.js +13 -0
- package/dist/core/recordset/rls.d.ts +15 -0
- package/dist/core/recordset/rls.js +108 -0
- package/dist/core/recordset/security.d.ts +56 -0
- package/dist/core/recordset/security.js +269 -0
- package/dist/core/recordset/sequences.d.ts +31 -0
- package/dist/core/recordset/sequences.js +154 -0
- package/dist/core/recordset/source.d.ts +9 -0
- package/dist/core/recordset/source.js +19 -0
- package/dist/core/recordset/sqlErrors.d.ts +6 -0
- package/dist/core/recordset/sqlErrors.js +39 -0
- package/dist/core/recordset/transitions.d.ts +10 -0
- package/dist/core/recordset/transitions.js +80 -0
- package/dist/core/recordset/types.d.ts +415 -0
- package/dist/core/recordset/types.js +8 -0
- package/dist/core/recordset/values.d.ts +33 -0
- package/dist/core/recordset/values.js +177 -0
- package/dist/core/recordset/writes.d.ts +42 -0
- package/dist/core/recordset/writes.js +786 -0
- package/dist/core/registry/ddl.d.ts +31 -0
- package/dist/core/registry/ddl.js +174 -0
- package/dist/core/registry/extension.d.ts +94 -0
- package/dist/core/registry/extension.js +90 -0
- package/dist/core/registry/module.d.ts +89 -0
- package/dist/core/registry/module.js +98 -0
- package/dist/core/registry/registry.d.ts +130 -0
- package/dist/core/registry/registry.js +591 -0
- package/dist/core/security/runtime.d.ts +102 -0
- package/dist/core/security/runtime.js +232 -0
- package/dist/driver-pg/driver.d.ts +88 -0
- package/dist/driver-pg/driver.js +177 -0
- package/dist/driver-pg/errors.d.ts +72 -0
- package/dist/driver-pg/errors.js +182 -0
- package/dist/driver-pg/index.d.ts +7 -0
- package/dist/driver-pg/index.js +7 -0
- package/dist/i18n/index.d.ts +25 -0
- package/dist/i18n/index.js +51 -0
- package/dist/migrate/apply.d.ts +78 -0
- package/dist/migrate/apply.js +167 -0
- package/dist/migrate/catalog.d.ts +10 -0
- package/dist/migrate/catalog.js +122 -0
- package/dist/migrate/diff.d.ts +98 -0
- package/dist/migrate/diff.js +160 -0
- package/dist/migrate/errors.d.ts +21 -0
- package/dist/migrate/errors.js +31 -0
- package/dist/migrate/index.d.ts +18 -0
- package/dist/migrate/index.js +18 -0
- package/dist/migrate/render.d.ts +5 -0
- package/dist/migrate/render.js +101 -0
- package/dist/migrate/schema.d.ts +61 -0
- package/dist/migrate/schema.js +233 -0
- package/dist/security/index.d.ts +147 -0
- package/dist/security/index.js +278 -0
- package/dist/sequence/index.d.ts +63 -0
- package/dist/sequence/index.js +106 -0
- package/dist/sql/ast.d.ts +49 -0
- package/dist/sql/ast.js +47 -0
- package/dist/sql/builder/builder.d.ts +215 -0
- package/dist/sql/builder/builder.js +308 -0
- package/dist/sql/builder/expression.d.ts +80 -0
- package/dist/sql/builder/expression.js +126 -0
- package/dist/sql/compile.d.ts +18 -0
- package/dist/sql/compile.js +41 -0
- package/dist/sql/errors.d.ts +13 -0
- package/dist/sql/errors.js +16 -0
- package/dist/sql/index.d.ts +19 -0
- package/dist/sql/index.js +19 -0
- package/dist/sql/tag.d.ts +47 -0
- package/dist/sql/tag.js +123 -0
- package/package.json +82 -4
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Diffs the desired schema against the live one into an ordered list of operations. Destructive operations
|
|
3
|
+
* (drops, type changes) are marked so that applying them needs explicit consent.
|
|
4
|
+
*/
|
|
5
|
+
import type { ColumnSpec, ForeignKeySpec, IndexSpec, LiveSchema, Schema, TableSpec } from './schema.js';
|
|
6
|
+
/** One schema change. */
|
|
7
|
+
export type Operation = {
|
|
8
|
+
readonly kind: 'createExtension';
|
|
9
|
+
readonly name: string;
|
|
10
|
+
readonly destructive: false;
|
|
11
|
+
} | {
|
|
12
|
+
readonly kind: 'createTable';
|
|
13
|
+
readonly table: TableSpec;
|
|
14
|
+
readonly destructive: false;
|
|
15
|
+
} | {
|
|
16
|
+
readonly kind: 'dropTable';
|
|
17
|
+
readonly table: string;
|
|
18
|
+
readonly destructive: true;
|
|
19
|
+
} | {
|
|
20
|
+
readonly kind: 'addColumn';
|
|
21
|
+
readonly table: string;
|
|
22
|
+
readonly column: ColumnSpec;
|
|
23
|
+
readonly destructive: false;
|
|
24
|
+
} | {
|
|
25
|
+
readonly kind: 'dropColumn';
|
|
26
|
+
readonly table: string;
|
|
27
|
+
readonly column: string;
|
|
28
|
+
readonly destructive: true;
|
|
29
|
+
} | {
|
|
30
|
+
readonly kind: 'alterType';
|
|
31
|
+
readonly table: string;
|
|
32
|
+
readonly column: string;
|
|
33
|
+
readonly from: string;
|
|
34
|
+
readonly to: string;
|
|
35
|
+
readonly destructive: true;
|
|
36
|
+
} | {
|
|
37
|
+
readonly kind: 'setDefault';
|
|
38
|
+
readonly table: string;
|
|
39
|
+
readonly column: string;
|
|
40
|
+
readonly default: string;
|
|
41
|
+
readonly destructive: false;
|
|
42
|
+
} | {
|
|
43
|
+
readonly kind: 'dropDefault';
|
|
44
|
+
readonly table: string;
|
|
45
|
+
readonly column: string;
|
|
46
|
+
readonly destructive: false;
|
|
47
|
+
} | {
|
|
48
|
+
readonly kind: 'backfill';
|
|
49
|
+
readonly table: string;
|
|
50
|
+
readonly column: string;
|
|
51
|
+
readonly default: string;
|
|
52
|
+
readonly destructive: false;
|
|
53
|
+
} | {
|
|
54
|
+
readonly kind: 'setNotNull';
|
|
55
|
+
readonly table: string;
|
|
56
|
+
readonly column: string;
|
|
57
|
+
readonly destructive: false;
|
|
58
|
+
} | {
|
|
59
|
+
readonly kind: 'dropNotNull';
|
|
60
|
+
readonly table: string;
|
|
61
|
+
readonly column: string;
|
|
62
|
+
readonly destructive: false;
|
|
63
|
+
} | {
|
|
64
|
+
readonly kind: 'addForeignKey';
|
|
65
|
+
readonly table: string;
|
|
66
|
+
readonly name: string;
|
|
67
|
+
readonly foreignKey: ForeignKeySpec;
|
|
68
|
+
readonly destructive: false;
|
|
69
|
+
} | {
|
|
70
|
+
readonly kind: 'addUnique';
|
|
71
|
+
readonly table: string;
|
|
72
|
+
readonly name: string;
|
|
73
|
+
readonly columns: readonly string[];
|
|
74
|
+
readonly destructive: false;
|
|
75
|
+
} | {
|
|
76
|
+
readonly kind: 'addCheck';
|
|
77
|
+
readonly table: string;
|
|
78
|
+
readonly name: string;
|
|
79
|
+
readonly expression: string;
|
|
80
|
+
readonly destructive: false;
|
|
81
|
+
} | {
|
|
82
|
+
readonly kind: 'dropConstraint';
|
|
83
|
+
readonly table: string;
|
|
84
|
+
readonly name: string;
|
|
85
|
+
readonly destructive: false;
|
|
86
|
+
} | {
|
|
87
|
+
readonly kind: 'createIndex';
|
|
88
|
+
readonly table: string;
|
|
89
|
+
readonly name: string;
|
|
90
|
+
readonly index: IndexSpec;
|
|
91
|
+
readonly destructive: false;
|
|
92
|
+
} | {
|
|
93
|
+
readonly kind: 'dropIndex';
|
|
94
|
+
readonly name: string;
|
|
95
|
+
readonly destructive: false;
|
|
96
|
+
};
|
|
97
|
+
/** Computes the operations that turn `live` into `desired`, in a safe order. */
|
|
98
|
+
export declare function diffSchema(desired: Schema, live: LiveSchema): Operation[];
|
|
@@ -0,0 +1,160 @@
|
|
|
1
|
+
function sameIndex(a, b) {
|
|
2
|
+
return (a.unique === b.unique &&
|
|
3
|
+
a.method === b.method &&
|
|
4
|
+
a.columns.join(',') === b.columns.join(',') &&
|
|
5
|
+
(a.opclass ?? '') === (b.opclass ?? '') &&
|
|
6
|
+
(a.where ?? '') === (b.where ?? ''));
|
|
7
|
+
}
|
|
8
|
+
function sameForeignKey(a, b) {
|
|
9
|
+
return a.column === b.column && a.references === b.references && a.onDelete === b.onDelete;
|
|
10
|
+
}
|
|
11
|
+
/** Computes the operations that turn `live` into `desired`, in a safe order. */
|
|
12
|
+
export function diffSchema(desired, live) {
|
|
13
|
+
const early = []; // extensions, tables, columns
|
|
14
|
+
const constraints = []; // drops of changed constraints/indexes, then adds (FKs after all tables exist)
|
|
15
|
+
const indexes = [];
|
|
16
|
+
const late = []; // destructive drops last
|
|
17
|
+
for (const ext of desired.extensions)
|
|
18
|
+
if (!live.extensions.includes(ext))
|
|
19
|
+
early.push({ kind: 'createExtension', name: ext, destructive: false });
|
|
20
|
+
for (const [name, table] of Object.entries(desired.tables)) {
|
|
21
|
+
const current = live.tables[name];
|
|
22
|
+
if (current === undefined) {
|
|
23
|
+
early.push({ kind: 'createTable', table, destructive: false });
|
|
24
|
+
for (const [fkName, fk] of Object.entries(table.foreignKeys))
|
|
25
|
+
constraints.push({
|
|
26
|
+
kind: 'addForeignKey',
|
|
27
|
+
table: name,
|
|
28
|
+
name: fkName,
|
|
29
|
+
foreignKey: fk,
|
|
30
|
+
destructive: false,
|
|
31
|
+
});
|
|
32
|
+
for (const [uName, columns] of Object.entries(table.uniques))
|
|
33
|
+
constraints.push({
|
|
34
|
+
kind: 'addUnique',
|
|
35
|
+
table: name,
|
|
36
|
+
name: uName,
|
|
37
|
+
columns,
|
|
38
|
+
destructive: false,
|
|
39
|
+
});
|
|
40
|
+
for (const [cName, expression] of Object.entries(table.checks))
|
|
41
|
+
constraints.push({
|
|
42
|
+
kind: 'addCheck',
|
|
43
|
+
table: name,
|
|
44
|
+
name: cName,
|
|
45
|
+
expression,
|
|
46
|
+
destructive: false,
|
|
47
|
+
});
|
|
48
|
+
for (const [iName, index] of Object.entries(table.indexes))
|
|
49
|
+
indexes.push({ kind: 'createIndex', table: name, name: iName, index, destructive: false });
|
|
50
|
+
continue;
|
|
51
|
+
}
|
|
52
|
+
for (const [colName, column] of Object.entries(table.columns)) {
|
|
53
|
+
const existing = current.columns[colName];
|
|
54
|
+
if (existing === undefined) {
|
|
55
|
+
early.push({ kind: 'addColumn', table: name, column, destructive: false });
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
if (existing.type !== column.type)
|
|
59
|
+
early.push({
|
|
60
|
+
kind: 'alterType',
|
|
61
|
+
table: name,
|
|
62
|
+
column: colName,
|
|
63
|
+
from: existing.type,
|
|
64
|
+
to: column.type,
|
|
65
|
+
destructive: true,
|
|
66
|
+
});
|
|
67
|
+
if ((existing.default ?? null) !== (column.default ?? null)) {
|
|
68
|
+
if (column.default === null)
|
|
69
|
+
early.push({ kind: 'dropDefault', table: name, column: colName, destructive: false });
|
|
70
|
+
else
|
|
71
|
+
early.push({
|
|
72
|
+
kind: 'setDefault',
|
|
73
|
+
table: name,
|
|
74
|
+
column: colName,
|
|
75
|
+
default: column.default,
|
|
76
|
+
destructive: false,
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
if (existing.nullable && !column.nullable) {
|
|
80
|
+
if (column.backfill !== null)
|
|
81
|
+
early.push({
|
|
82
|
+
kind: 'backfill',
|
|
83
|
+
table: name,
|
|
84
|
+
column: colName,
|
|
85
|
+
default: column.backfill,
|
|
86
|
+
destructive: false,
|
|
87
|
+
});
|
|
88
|
+
early.push({ kind: 'setNotNull', table: name, column: colName, destructive: false });
|
|
89
|
+
}
|
|
90
|
+
else if (!existing.nullable && column.nullable) {
|
|
91
|
+
early.push({ kind: 'dropNotNull', table: name, column: colName, destructive: false });
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
for (const colName of Object.keys(current.columns)) {
|
|
95
|
+
if (table.columns[colName] === undefined)
|
|
96
|
+
late.push({ kind: 'dropColumn', table: name, column: colName, destructive: true });
|
|
97
|
+
}
|
|
98
|
+
for (const [fkName, fk] of Object.entries(current.foreignKeys)) {
|
|
99
|
+
const wanted = table.foreignKeys[fkName];
|
|
100
|
+
if (wanted === undefined || !sameForeignKey(wanted, fk))
|
|
101
|
+
constraints.push({ kind: 'dropConstraint', table: name, name: fkName, destructive: false });
|
|
102
|
+
}
|
|
103
|
+
for (const [fkName, fk] of Object.entries(table.foreignKeys)) {
|
|
104
|
+
const existing = current.foreignKeys[fkName];
|
|
105
|
+
if (existing === undefined || !sameForeignKey(existing, fk))
|
|
106
|
+
constraints.push({
|
|
107
|
+
kind: 'addForeignKey',
|
|
108
|
+
table: name,
|
|
109
|
+
name: fkName,
|
|
110
|
+
foreignKey: fk,
|
|
111
|
+
destructive: false,
|
|
112
|
+
});
|
|
113
|
+
}
|
|
114
|
+
for (const [uName, columns] of Object.entries(current.uniques)) {
|
|
115
|
+
const wanted = table.uniques[uName];
|
|
116
|
+
if (wanted === undefined || wanted.join(',') !== columns.join(','))
|
|
117
|
+
constraints.push({ kind: 'dropConstraint', table: name, name: uName, destructive: false });
|
|
118
|
+
}
|
|
119
|
+
for (const [uName, columns] of Object.entries(table.uniques)) {
|
|
120
|
+
const existing = current.uniques[uName];
|
|
121
|
+
if (existing === undefined || existing.join(',') !== columns.join(','))
|
|
122
|
+
constraints.push({
|
|
123
|
+
kind: 'addUnique',
|
|
124
|
+
table: name,
|
|
125
|
+
name: uName,
|
|
126
|
+
columns,
|
|
127
|
+
destructive: false,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
for (const cName of Object.keys(current.checks)) {
|
|
131
|
+
if (table.checks[cName] === undefined)
|
|
132
|
+
constraints.push({ kind: 'dropConstraint', table: name, name: cName, destructive: false });
|
|
133
|
+
}
|
|
134
|
+
for (const [cName, expression] of Object.entries(table.checks)) {
|
|
135
|
+
if (current.checks[cName] === undefined)
|
|
136
|
+
constraints.push({
|
|
137
|
+
kind: 'addCheck',
|
|
138
|
+
table: name,
|
|
139
|
+
name: cName,
|
|
140
|
+
expression,
|
|
141
|
+
destructive: false,
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
for (const [iName, index] of Object.entries(current.indexes)) {
|
|
145
|
+
const wanted = table.indexes[iName];
|
|
146
|
+
if (wanted === undefined || !sameIndex(wanted, index))
|
|
147
|
+
constraints.push({ kind: 'dropIndex', name: iName, destructive: false });
|
|
148
|
+
}
|
|
149
|
+
for (const [iName, index] of Object.entries(table.indexes)) {
|
|
150
|
+
const existing = current.indexes[iName];
|
|
151
|
+
if (existing === undefined || !sameIndex(existing, index))
|
|
152
|
+
indexes.push({ kind: 'createIndex', table: name, name: iName, index, destructive: false });
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
for (const name of Object.keys(live.tables)) {
|
|
156
|
+
if (desired.tables[name] === undefined)
|
|
157
|
+
late.push({ kind: 'dropTable', table: name, destructive: true });
|
|
158
|
+
}
|
|
159
|
+
return [...early, ...constraints, ...indexes, ...late];
|
|
160
|
+
}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/** Machine-readable reason for a {@link MigrationError}. */
|
|
2
|
+
export type MigrationErrorCode = 'MIGRATION' | 'MIGRATION_DESTRUCTIVE' | 'MIGRATION_CHECKSUM' | 'MIGRATION_FILE';
|
|
3
|
+
/** Base class of `@shutro/migrate` errors: the message says what went wrong and ends with `hint`. */
|
|
4
|
+
export declare class MigrationError extends Error {
|
|
5
|
+
readonly code: MigrationErrorCode;
|
|
6
|
+
readonly hint: string;
|
|
7
|
+
constructor(problem: string, hint: string, code?: MigrationErrorCode);
|
|
8
|
+
}
|
|
9
|
+
/** A migration file contains destructive statements and `allowDestructive` is not set. */
|
|
10
|
+
export declare class DestructiveMigrationError extends MigrationError {
|
|
11
|
+
readonly statements: readonly string[];
|
|
12
|
+
constructor(file: string, statements: readonly string[]);
|
|
13
|
+
}
|
|
14
|
+
/** An applied migration file was edited afterwards. */
|
|
15
|
+
export declare class MigrationChecksumError extends MigrationError {
|
|
16
|
+
constructor(file: string);
|
|
17
|
+
}
|
|
18
|
+
/** A migration file name or content that can't be used. */
|
|
19
|
+
export declare class MigrationFileError extends MigrationError {
|
|
20
|
+
constructor(problem: string, hint: string);
|
|
21
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
/** Base class of `@shutro/migrate` errors: the message says what went wrong and ends with `hint`. */
|
|
2
|
+
export class MigrationError extends Error {
|
|
3
|
+
code;
|
|
4
|
+
hint;
|
|
5
|
+
constructor(problem, hint, code = 'MIGRATION') {
|
|
6
|
+
super(`${problem} ${hint}`);
|
|
7
|
+
this.name = new.target.name;
|
|
8
|
+
this.code = code;
|
|
9
|
+
this.hint = hint;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
/** A migration file contains destructive statements and `allowDestructive` is not set. */
|
|
13
|
+
export class DestructiveMigrationError extends MigrationError {
|
|
14
|
+
statements;
|
|
15
|
+
constructor(file, statements) {
|
|
16
|
+
super(`Migration ${file} contains ${statements.length} destructive statement(s): ${statements.join('; ')}.`, 'Review the data loss, then apply with { allowDestructive: true }, or edit the migration file.', 'MIGRATION_DESTRUCTIVE');
|
|
17
|
+
this.statements = statements;
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
/** An applied migration file was edited afterwards. */
|
|
21
|
+
export class MigrationChecksumError extends MigrationError {
|
|
22
|
+
constructor(file) {
|
|
23
|
+
super(`Migration ${file} was changed after it was applied.`, 'Applied migrations are immutable: restore the file, or write a new migration for the change.', 'MIGRATION_CHECKSUM');
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
/** A migration file name or content that can't be used. */
|
|
27
|
+
export class MigrationFileError extends MigrationError {
|
|
28
|
+
constructor(problem, hint) {
|
|
29
|
+
super(problem, hint, 'MIGRATION_FILE');
|
|
30
|
+
}
|
|
31
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* `@shutro/migrate`: schema diff against the live database, versioned SQL migrations, and per-module data-migration
|
|
4
|
+
* hooks (`CLAUDE.md` layer 12).
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const plan = await planMigration({ driver, registry });
|
|
9
|
+
* await writeMigrationFile({ dir: 'migrations', name: 'add_margin', plan });
|
|
10
|
+
* await applyMigrations({ driver, dir: 'migrations', registry });
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export { applyMigrations, type MigrationFile, type MigrationPlan, type MigrationResult, type MigrationStatus, migrationStatus, parseMigrationFile, planMigration, writeMigrationFile, } from './apply.js';
|
|
14
|
+
export { normalizeDefault, readLiveSchema } from './catalog.js';
|
|
15
|
+
export { diffSchema, type Operation } from './diff.js';
|
|
16
|
+
export { DestructiveMigrationError, MigrationChecksumError, MigrationError, type MigrationErrorCode, MigrationFileError, } from './errors.js';
|
|
17
|
+
export { renderOperation, renderOperations } from './render.js';
|
|
18
|
+
export { type ColumnSpec, canonicalType, columnName, desiredSchema, type ForeignKeySpec, type IndexSpec, type LiveSchema, literalOf, type Schema, type TableSpec, } from './schema.js';
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @packageDocumentation
|
|
3
|
+
* `@shutro/migrate`: schema diff against the live database, versioned SQL migrations, and per-module data-migration
|
|
4
|
+
* hooks (`CLAUDE.md` layer 12).
|
|
5
|
+
*
|
|
6
|
+
* @example
|
|
7
|
+
* ```ts
|
|
8
|
+
* const plan = await planMigration({ driver, registry });
|
|
9
|
+
* await writeMigrationFile({ dir: 'migrations', name: 'add_margin', plan });
|
|
10
|
+
* await applyMigrations({ driver, dir: 'migrations', registry });
|
|
11
|
+
* ```
|
|
12
|
+
*/
|
|
13
|
+
export { applyMigrations, migrationStatus, parseMigrationFile, planMigration, writeMigrationFile, } from './apply.js';
|
|
14
|
+
export { normalizeDefault, readLiveSchema } from './catalog.js';
|
|
15
|
+
export { diffSchema } from './diff.js';
|
|
16
|
+
export { DestructiveMigrationError, MigrationChecksumError, MigrationError, MigrationFileError, } from './errors.js';
|
|
17
|
+
export { renderOperation, renderOperations } from './render.js';
|
|
18
|
+
export { canonicalType, columnName, desiredSchema, literalOf, } from './schema.js';
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { Operation } from './diff.js';
|
|
2
|
+
/** Renders one operation as a statement. */
|
|
3
|
+
export declare function renderOperation(op: Operation): string;
|
|
4
|
+
/** Renders every operation, in order. */
|
|
5
|
+
export declare function renderOperations(operations: readonly Operation[]): string[];
|
|
@@ -0,0 +1,101 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders operations as DDL text. DDL can't take bind parameters, so every piece is validated before it becomes
|
|
3
|
+
* text: identifiers through `sql.id` (quoted), types and defaults through strict patterns, expressions from `sql`
|
|
4
|
+
* fragments that carried no parameters.
|
|
5
|
+
*/
|
|
6
|
+
import { compile, sql } from '../sql/index.js';
|
|
7
|
+
import { MigrationError } from './errors.js';
|
|
8
|
+
const TYPE = /^[a-z]+( [a-z]+)*(\(\d+(,\d+)?\))?(\[\])?$/;
|
|
9
|
+
const DEFAULT = /^(true|false|now\(\)|-?\d+(\.\d+)?|'(?:[^']|'')*')$/;
|
|
10
|
+
const METHOD = /^[a-z_]+$/;
|
|
11
|
+
function id(...parts) {
|
|
12
|
+
return compile(sql.id(...parts)).text;
|
|
13
|
+
}
|
|
14
|
+
function type(t) {
|
|
15
|
+
if (!TYPE.test(t))
|
|
16
|
+
throw new MigrationError(`${JSON.stringify(t)} is not a column type the migrator renders.`, 'Column types come from the field builders; this should not happen.');
|
|
17
|
+
return t;
|
|
18
|
+
}
|
|
19
|
+
function defaultExpr(d) {
|
|
20
|
+
if (!DEFAULT.test(d))
|
|
21
|
+
throw new MigrationError(`${JSON.stringify(d)} is not a default the migrator renders.`, 'Defaults are booleans, numbers, quoted strings or now().');
|
|
22
|
+
return d;
|
|
23
|
+
}
|
|
24
|
+
function columnDdl(column, primaryKey) {
|
|
25
|
+
const parts = [id(column.name)];
|
|
26
|
+
if (column.identity) {
|
|
27
|
+
parts.push('bigint GENERATED ALWAYS AS IDENTITY');
|
|
28
|
+
if (primaryKey.length === 1 && primaryKey[0] === column.name)
|
|
29
|
+
parts.push('PRIMARY KEY');
|
|
30
|
+
return parts.join(' ');
|
|
31
|
+
}
|
|
32
|
+
parts.push(type(column.type));
|
|
33
|
+
if (!column.nullable)
|
|
34
|
+
parts.push('NOT NULL');
|
|
35
|
+
if (column.default !== null)
|
|
36
|
+
parts.push(`DEFAULT ${defaultExpr(column.default)}`);
|
|
37
|
+
return parts.join(' ');
|
|
38
|
+
}
|
|
39
|
+
function createTable(table) {
|
|
40
|
+
const columns = Object.values(table.columns).map((c) => columnDdl(c, table.primaryKey));
|
|
41
|
+
const needsPk = !(table.primaryKey.length === 1 && table.columns[table.primaryKey[0] ?? '']?.identity);
|
|
42
|
+
if (needsPk && table.primaryKey.length > 0)
|
|
43
|
+
columns.push(`PRIMARY KEY (${table.primaryKey.map((c) => id(c)).join(', ')})`);
|
|
44
|
+
const partition = table.partition === undefined
|
|
45
|
+
? ''
|
|
46
|
+
: ` PARTITION BY ${table.partition.by.toUpperCase()} (${id(table.partition.column)})`;
|
|
47
|
+
return `CREATE TABLE ${id(table.name)} (${columns.join(', ')})${partition}`;
|
|
48
|
+
}
|
|
49
|
+
/** Renders one operation as a statement. */
|
|
50
|
+
export function renderOperation(op) {
|
|
51
|
+
switch (op.kind) {
|
|
52
|
+
case 'createExtension':
|
|
53
|
+
if (!METHOD.test(op.name))
|
|
54
|
+
throw new MigrationError(`${JSON.stringify(op.name)} is not an extension name.`, 'Extension names are lower-case identifiers.');
|
|
55
|
+
return `CREATE EXTENSION IF NOT EXISTS ${id(op.name)}`;
|
|
56
|
+
case 'createTable':
|
|
57
|
+
return createTable(op.table);
|
|
58
|
+
case 'dropTable':
|
|
59
|
+
return `DROP TABLE ${id(op.table)}`;
|
|
60
|
+
case 'addColumn':
|
|
61
|
+
return `ALTER TABLE ${id(op.table)} ADD COLUMN ${columnDdl(op.column, [])}`;
|
|
62
|
+
case 'dropColumn':
|
|
63
|
+
return `ALTER TABLE ${id(op.table)} DROP COLUMN ${id(op.column)}`;
|
|
64
|
+
case 'alterType':
|
|
65
|
+
return `ALTER TABLE ${id(op.table)} ALTER COLUMN ${id(op.column)} TYPE ${type(op.to)}`;
|
|
66
|
+
case 'setDefault':
|
|
67
|
+
return `ALTER TABLE ${id(op.table)} ALTER COLUMN ${id(op.column)} SET DEFAULT ${defaultExpr(op.default)}`;
|
|
68
|
+
case 'dropDefault':
|
|
69
|
+
return `ALTER TABLE ${id(op.table)} ALTER COLUMN ${id(op.column)} DROP DEFAULT`;
|
|
70
|
+
case 'backfill':
|
|
71
|
+
return `UPDATE ${id(op.table)} SET ${id(op.column)} = ${defaultExpr(op.default)} WHERE ${id(op.column)} IS NULL`;
|
|
72
|
+
case 'setNotNull':
|
|
73
|
+
return `ALTER TABLE ${id(op.table)} ALTER COLUMN ${id(op.column)} SET NOT NULL`;
|
|
74
|
+
case 'dropNotNull':
|
|
75
|
+
return `ALTER TABLE ${id(op.table)} ALTER COLUMN ${id(op.column)} DROP NOT NULL`;
|
|
76
|
+
case 'addForeignKey':
|
|
77
|
+
return `ALTER TABLE ${id(op.table)} ADD CONSTRAINT ${id(op.name)} FOREIGN KEY (${id(op.foreignKey.column)}) REFERENCES ${id(op.foreignKey.references)} (${id('id')}) ON DELETE ${op.foreignKey.onDelete}`;
|
|
78
|
+
case 'addUnique':
|
|
79
|
+
return `ALTER TABLE ${id(op.table)} ADD CONSTRAINT ${id(op.name)} UNIQUE (${op.columns.map((c) => id(c)).join(', ')})`;
|
|
80
|
+
case 'addCheck':
|
|
81
|
+
return `ALTER TABLE ${id(op.table)} ADD CONSTRAINT ${id(op.name)} CHECK (${op.expression})`;
|
|
82
|
+
case 'dropConstraint':
|
|
83
|
+
return `ALTER TABLE ${id(op.table)} DROP CONSTRAINT ${id(op.name)}`;
|
|
84
|
+
case 'createIndex': {
|
|
85
|
+
const { index } = op;
|
|
86
|
+
if (!METHOD.test(index.method))
|
|
87
|
+
throw new MigrationError(`${JSON.stringify(index.method)} is not an index method.`, 'Use btree, gin, gist or brin.');
|
|
88
|
+
const columns = index.columns
|
|
89
|
+
.map((c) => (index.opclass === undefined ? id(c) : `${id(c)} ${id(index.opclass)}`))
|
|
90
|
+
.join(', ');
|
|
91
|
+
const where = index.where === undefined ? '' : ` WHERE ${index.where}`;
|
|
92
|
+
return `CREATE ${index.unique ? 'UNIQUE ' : ''}INDEX ${id(op.name)} ON ${id(op.table)} USING ${index.method} (${columns})${where}`;
|
|
93
|
+
}
|
|
94
|
+
case 'dropIndex':
|
|
95
|
+
return `DROP INDEX ${id(op.name)}`;
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
/** Renders every operation, in order. */
|
|
99
|
+
export function renderOperations(operations) {
|
|
100
|
+
return operations.map(renderOperation);
|
|
101
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The intended database schema, derived from a registry (RFC 0001 §7.5 naming, RFC 0002 origins). The same shape
|
|
3
|
+
* describes the live schema read from `pg_catalog`, so the two can be diffed directly.
|
|
4
|
+
*/
|
|
5
|
+
import { type Registry } from '../core/index.js';
|
|
6
|
+
/** A column as PostgreSQL reports it (`format_type`), e.g. `numeric(32,2)`, `timestamp with time zone`. */
|
|
7
|
+
export interface ColumnSpec {
|
|
8
|
+
readonly name: string;
|
|
9
|
+
readonly type: string;
|
|
10
|
+
readonly nullable: boolean;
|
|
11
|
+
/** Database-level default expression (`true`, `1`, `now()`), or `null`. */
|
|
12
|
+
readonly default: string | null;
|
|
13
|
+
readonly identity: boolean;
|
|
14
|
+
/** Literal used to backfill existing rows when the column becomes NOT NULL (desired schema only). */
|
|
15
|
+
readonly backfill: string | null;
|
|
16
|
+
}
|
|
17
|
+
/** A foreign key. */
|
|
18
|
+
export interface ForeignKeySpec {
|
|
19
|
+
readonly column: string;
|
|
20
|
+
readonly references: string;
|
|
21
|
+
readonly onDelete: 'RESTRICT' | 'CASCADE' | 'SET NULL' | 'NO ACTION';
|
|
22
|
+
}
|
|
23
|
+
/** An index. */
|
|
24
|
+
export interface IndexSpec {
|
|
25
|
+
readonly columns: readonly string[];
|
|
26
|
+
readonly unique: boolean;
|
|
27
|
+
readonly method: string;
|
|
28
|
+
readonly opclass?: string;
|
|
29
|
+
readonly where?: string;
|
|
30
|
+
}
|
|
31
|
+
/** A table. Constraint and index names are the keys. */
|
|
32
|
+
export interface TableSpec {
|
|
33
|
+
readonly name: string;
|
|
34
|
+
readonly columns: Readonly<Record<string, ColumnSpec>>;
|
|
35
|
+
readonly primaryKey: readonly string[];
|
|
36
|
+
readonly foreignKeys: Readonly<Record<string, ForeignKeySpec>>;
|
|
37
|
+
readonly uniques: Readonly<Record<string, readonly string[]>>;
|
|
38
|
+
/** Check constraints by name. Expressions are compared by name only (PostgreSQL normalizes them). */
|
|
39
|
+
readonly checks: Readonly<Record<string, string>>;
|
|
40
|
+
readonly indexes: Readonly<Record<string, IndexSpec>>;
|
|
41
|
+
/** `PARTITION BY` of a partitioned table (RFC 0015 §7.2). Partitions themselves are not diffed. */
|
|
42
|
+
readonly partition?: {
|
|
43
|
+
readonly by: 'range' | 'list' | 'hash';
|
|
44
|
+
readonly column: string;
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
/** A whole schema: tables by name, plus required extensions. */
|
|
48
|
+
export interface Schema {
|
|
49
|
+
readonly tables: Readonly<Record<string, TableSpec>>;
|
|
50
|
+
readonly extensions: readonly string[];
|
|
51
|
+
}
|
|
52
|
+
/** The schema as read from the database (same shape). */
|
|
53
|
+
export type LiveSchema = Schema;
|
|
54
|
+
/** Maps the builders' type spellings to `format_type` output. */
|
|
55
|
+
export declare function canonicalType(pgType: string): string;
|
|
56
|
+
/** A SQL literal for a backfill (`'x'`, `1`, `true`). */
|
|
57
|
+
export declare function literalOf(value: unknown): string | null;
|
|
58
|
+
/** Builds the intended schema for every model in the registry. */
|
|
59
|
+
export declare function desiredSchema(registry: Registry): Schema;
|
|
60
|
+
/** Column name of a field key, as `defineModel` derives it (exported for callers building names). */
|
|
61
|
+
export declare function columnName(key: string, many2one: boolean): string;
|