@uncutsql/core 0.2.1 → 0.4.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/dist/index.cjs +1326 -100
- package/dist/index.d.cts +472 -16
- package/dist/index.d.ts +472 -16
- package/dist/index.js +1305 -101
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -5,6 +5,178 @@ const tableKey = (table) => JSON.stringify([
|
|
|
5
5
|
table.schema ?? null,
|
|
6
6
|
table.name
|
|
7
7
|
]);
|
|
8
|
+
const triggerKey = (trigger) => trigger.scope === "target" ? JSON.stringify([tableKey(trigger.target), trigger.identity.name]) : tableKey(trigger.identity);
|
|
9
|
+
const functionKey = (fn) => JSON.stringify([
|
|
10
|
+
fn.identity.catalog ?? null,
|
|
11
|
+
fn.identity.schema ?? null,
|
|
12
|
+
fn.identity.name,
|
|
13
|
+
fn.signature
|
|
14
|
+
]);
|
|
15
|
+
/** `schema.name(type, …)` for diagnostics; signature entries that are not strings print as JSON. */
|
|
16
|
+
const functionLabel = (fn) => `${[
|
|
17
|
+
fn.identity.catalog,
|
|
18
|
+
fn.identity.schema,
|
|
19
|
+
fn.identity.name
|
|
20
|
+
].filter((part) => part !== void 0).join(".")}(${fn.signature.map((item) => typeof item === "string" ? item : JSON.stringify(item)).join(", ")})`;
|
|
21
|
+
/** The external objects of the models, tables before functions, each listed once by its key. */
|
|
22
|
+
const externalObjects = (...databases) => {
|
|
23
|
+
const found = /* @__PURE__ */ new Map();
|
|
24
|
+
for (const { externals } of databases) {
|
|
25
|
+
for (const { identity } of externals?.tables ?? []) {
|
|
26
|
+
const name = [
|
|
27
|
+
identity.catalog,
|
|
28
|
+
identity.schema,
|
|
29
|
+
identity.name
|
|
30
|
+
].filter((part) => part !== void 0).join(".");
|
|
31
|
+
found.set(`table ${tableKey(identity)}`, {
|
|
32
|
+
identity,
|
|
33
|
+
label: `External table ${name}`
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
for (const fn of externals?.functions ?? []) found.set(`function ${functionKey(fn)}`, {
|
|
37
|
+
function: fn,
|
|
38
|
+
identity: fn.identity,
|
|
39
|
+
label: `External function ${functionLabel(fn)}`
|
|
40
|
+
});
|
|
41
|
+
}
|
|
42
|
+
return [...found.values()];
|
|
43
|
+
};
|
|
44
|
+
/** The managed and external enums of the models, each listed once by its key. */
|
|
45
|
+
const enumObjects = (...databases) => {
|
|
46
|
+
const found = /* @__PURE__ */ new Map();
|
|
47
|
+
for (const database of databases) for (const [kind, enums] of [["Enum", database.enums], ["External enum", database.externals?.enums]]) for (const { identity } of enums ?? []) {
|
|
48
|
+
const name = [
|
|
49
|
+
identity.catalog,
|
|
50
|
+
identity.schema,
|
|
51
|
+
identity.name
|
|
52
|
+
].filter((part) => part !== void 0).join(".");
|
|
53
|
+
found.set(tableKey(identity), {
|
|
54
|
+
identity,
|
|
55
|
+
label: `${kind} ${name}`
|
|
56
|
+
});
|
|
57
|
+
}
|
|
58
|
+
return [...found.values()];
|
|
59
|
+
};
|
|
60
|
+
/** The managed and external extensions of the models, each listed once by its name. */
|
|
61
|
+
const extensionObjects = (...databases) => {
|
|
62
|
+
const found = /* @__PURE__ */ new Map();
|
|
63
|
+
for (const database of databases) for (const extension of [...database.extensions ?? [], ...database.externals?.extensions ?? []]) found.set(extension.name, extension);
|
|
64
|
+
return [...found.values()];
|
|
65
|
+
};
|
|
66
|
+
const dotted = ({ catalog, name, schema }) => [
|
|
67
|
+
catalog,
|
|
68
|
+
schema,
|
|
69
|
+
name
|
|
70
|
+
].filter((part) => part !== void 0).join(".");
|
|
71
|
+
const commentObject = (target) => {
|
|
72
|
+
const on = (identity, text) => ({
|
|
73
|
+
identity,
|
|
74
|
+
label: `Comment on ${text}`
|
|
75
|
+
});
|
|
76
|
+
switch (target.kind) {
|
|
77
|
+
case "enum":
|
|
78
|
+
case "sequence":
|
|
79
|
+
case "table":
|
|
80
|
+
case "view": return on(target.identity, `${target.kind} ${dotted(target.identity)}`);
|
|
81
|
+
case "column": return on(target.table, `column ${dotted(target.table)}.${target.column}`);
|
|
82
|
+
case "viewColumn": return on(target.view, `view column ${dotted(target.view)}.${target.column}`);
|
|
83
|
+
case "index": return on(target.table, `index ${target.index} of table ${dotted(target.table)}`);
|
|
84
|
+
case "constraint": return on(target.table, `${target.constraint.kind} constraint of ${dotted(target.table)}`);
|
|
85
|
+
case "function": return on(target.identity, `function ${functionLabel(target)}`);
|
|
86
|
+
case "trigger": return on(target.target, `trigger ${target.name} on ${dotted(target.target)}`);
|
|
87
|
+
case "namespace": return on(target.identity, `schema ${dotted(target.identity)}`);
|
|
88
|
+
case "extension": return on({ name: target.name }, `extension ${target.name}`);
|
|
89
|
+
}
|
|
90
|
+
};
|
|
91
|
+
/** The targets of a model's own comments; externals are not included. */
|
|
92
|
+
const commentTargets = (database) => {
|
|
93
|
+
const targets = [];
|
|
94
|
+
const add = (item, target) => {
|
|
95
|
+
if (item.comment !== void 0) targets.push(target());
|
|
96
|
+
};
|
|
97
|
+
for (const item of database.namespaces ?? []) add(item, () => ({
|
|
98
|
+
identity: item.identity,
|
|
99
|
+
kind: "namespace"
|
|
100
|
+
}));
|
|
101
|
+
for (const item of database.extensions ?? []) add(item, () => ({
|
|
102
|
+
kind: "extension",
|
|
103
|
+
name: item.name
|
|
104
|
+
}));
|
|
105
|
+
for (const item of database.enums ?? []) add(item, () => ({
|
|
106
|
+
identity: item.identity,
|
|
107
|
+
kind: "enum"
|
|
108
|
+
}));
|
|
109
|
+
for (const table of database.tables ?? []) {
|
|
110
|
+
const { identity } = table;
|
|
111
|
+
const constraints = (items) => {
|
|
112
|
+
for (const item of items ?? []) add(item, () => ({
|
|
113
|
+
constraint: {
|
|
114
|
+
kind: item.kind,
|
|
115
|
+
properties: item.properties
|
|
116
|
+
},
|
|
117
|
+
kind: "constraint",
|
|
118
|
+
table: identity
|
|
119
|
+
}));
|
|
120
|
+
};
|
|
121
|
+
add(table, () => ({
|
|
122
|
+
identity,
|
|
123
|
+
kind: "table"
|
|
124
|
+
}));
|
|
125
|
+
for (const column of table.columns ?? []) {
|
|
126
|
+
add(column, () => ({
|
|
127
|
+
column: column.key,
|
|
128
|
+
kind: "column",
|
|
129
|
+
table: identity
|
|
130
|
+
}));
|
|
131
|
+
constraints(column.constraints);
|
|
132
|
+
}
|
|
133
|
+
constraints(table.constraints);
|
|
134
|
+
for (const index of table.indexes ?? []) add(index, () => ({
|
|
135
|
+
index: index.key,
|
|
136
|
+
kind: "index",
|
|
137
|
+
table: identity
|
|
138
|
+
}));
|
|
139
|
+
}
|
|
140
|
+
for (const view of database.views ?? []) {
|
|
141
|
+
add(view, () => ({
|
|
142
|
+
identity: view.identity,
|
|
143
|
+
kind: "view"
|
|
144
|
+
}));
|
|
145
|
+
for (const column of Object.keys(view.columnComments ?? {})) targets.push({
|
|
146
|
+
column,
|
|
147
|
+
kind: "viewColumn",
|
|
148
|
+
view: view.identity
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
for (const item of database.triggers ?? []) add(item, () => ({
|
|
152
|
+
kind: "trigger",
|
|
153
|
+
name: item.identity.name,
|
|
154
|
+
target: item.target
|
|
155
|
+
}));
|
|
156
|
+
for (const item of database.sequences ?? []) add(item, () => ({
|
|
157
|
+
identity: item.identity,
|
|
158
|
+
kind: "sequence"
|
|
159
|
+
}));
|
|
160
|
+
for (const item of database.functions ?? []) add(item, () => ({
|
|
161
|
+
identity: item.identity,
|
|
162
|
+
kind: "function",
|
|
163
|
+
signature: item.signature
|
|
164
|
+
}));
|
|
165
|
+
return targets;
|
|
166
|
+
};
|
|
167
|
+
/**
|
|
168
|
+
* Every commented object of the models, externals included, and of `targets` (for example those of
|
|
169
|
+
* `SetComment` procedures), each listed once by its label. A dialect without comments refuses them.
|
|
170
|
+
*/
|
|
171
|
+
const commentObjects = (databases, targets = []) => {
|
|
172
|
+
const found = /* @__PURE__ */ new Map();
|
|
173
|
+
const all = [...databases.flatMap((database) => [...commentTargets(database), ...commentTargets(database.externals ?? {})]), ...targets];
|
|
174
|
+
for (const target of all) {
|
|
175
|
+
const object = commentObject(target);
|
|
176
|
+
found.set(object.label, object);
|
|
177
|
+
}
|
|
178
|
+
return [...found.values()];
|
|
179
|
+
};
|
|
8
180
|
const namespaceKey = (namespace) => JSON.stringify([namespace.catalog ?? null, namespace.name]);
|
|
9
181
|
/** The namespace that contains an object, or undefined when its identity has no schema level. */
|
|
10
182
|
const namespaceOf = (identity) => identity.schema === void 0 ? void 0 : identity.catalog === void 0 ? { name: identity.schema } : {
|
|
@@ -31,26 +203,29 @@ const validateDatabase = (database, side) => {
|
|
|
31
203
|
const diagnostics = [];
|
|
32
204
|
const tables = /* @__PURE__ */ new Set();
|
|
33
205
|
const indexes = /* @__PURE__ */ new Set();
|
|
34
|
-
|
|
35
|
-
const id = tableKey(table.identity);
|
|
36
|
-
if (tables.has(id)) diagnostics.push({
|
|
37
|
-
code: "DuplicateTable",
|
|
38
|
-
message: `Duplicate table ${id}`,
|
|
39
|
-
side,
|
|
40
|
-
table: table.identity
|
|
41
|
-
});
|
|
42
|
-
tables.add(id);
|
|
206
|
+
const duplicateColumns = (table) => {
|
|
43
207
|
const columns = /* @__PURE__ */ new Set();
|
|
44
208
|
for (const column of table.columns) {
|
|
45
209
|
if (columns.has(column.key)) diagnostics.push({
|
|
46
210
|
code: "DuplicateColumn",
|
|
47
211
|
column: column.key,
|
|
48
|
-
message: `Duplicate column ${JSON.stringify(column.key)} in table ${
|
|
212
|
+
message: `Duplicate column ${JSON.stringify(column.key)} in table ${tableKey(table.identity)}`,
|
|
49
213
|
side,
|
|
50
214
|
table: table.identity
|
|
51
215
|
});
|
|
52
216
|
columns.add(column.key);
|
|
53
217
|
}
|
|
218
|
+
};
|
|
219
|
+
for (const table of database.tables) {
|
|
220
|
+
const id = tableKey(table.identity);
|
|
221
|
+
if (tables.has(id)) diagnostics.push({
|
|
222
|
+
code: "DuplicateTable",
|
|
223
|
+
message: `Duplicate table ${id}`,
|
|
224
|
+
side,
|
|
225
|
+
table: table.identity
|
|
226
|
+
});
|
|
227
|
+
tables.add(id);
|
|
228
|
+
duplicateColumns(table);
|
|
54
229
|
}
|
|
55
230
|
for (const table of database.tables) for (const index of table.indexes ?? []) {
|
|
56
231
|
const id = JSON.stringify([tableKey(table.identity), index.key]);
|
|
@@ -80,8 +255,9 @@ const validateDatabase = (database, side) => {
|
|
|
80
255
|
views.add(id);
|
|
81
256
|
}
|
|
82
257
|
const triggers = /* @__PURE__ */ new Set();
|
|
258
|
+
const externalTables = new Set((database.externals?.tables ?? []).map((table) => tableKey(table.identity)));
|
|
83
259
|
for (const trigger of database.triggers ?? []) {
|
|
84
|
-
const id =
|
|
260
|
+
const id = triggerKey(trigger);
|
|
85
261
|
if (triggers.has(id)) diagnostics.push({
|
|
86
262
|
code: "DuplicateTrigger",
|
|
87
263
|
message: `Duplicate trigger ${id}`,
|
|
@@ -89,6 +265,54 @@ const validateDatabase = (database, side) => {
|
|
|
89
265
|
table: trigger.identity
|
|
90
266
|
});
|
|
91
267
|
triggers.add(id);
|
|
268
|
+
const target = tableKey(trigger.target);
|
|
269
|
+
if (!tables.has(target) && !views.has(target)) {
|
|
270
|
+
const { catalog, name, schema } = trigger.target;
|
|
271
|
+
const label = [
|
|
272
|
+
catalog,
|
|
273
|
+
schema,
|
|
274
|
+
name
|
|
275
|
+
].filter((part) => part !== void 0).join(".");
|
|
276
|
+
const isExternal = externalTables.has(target);
|
|
277
|
+
diagnostics.push({
|
|
278
|
+
code: isExternal ? "ExternalTriggerTarget" : "UnknownTriggerTarget",
|
|
279
|
+
message: isExternal ? `Trigger ${trigger.name} on ${label} cannot be created: ${label} is external, and a trigger on it would alter an object outside the managed scope` : `Trigger ${trigger.name} targets ${label}, which is not a table or view of the model`,
|
|
280
|
+
side,
|
|
281
|
+
table: trigger.identity
|
|
282
|
+
});
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
const sequences = /* @__PURE__ */ new Set();
|
|
286
|
+
for (const sequence of database.sequences ?? []) {
|
|
287
|
+
const id = tableKey(sequence.identity);
|
|
288
|
+
if (sequences.has(id)) diagnostics.push({
|
|
289
|
+
code: "DuplicateSequence",
|
|
290
|
+
message: `Duplicate sequence ${id}`,
|
|
291
|
+
side,
|
|
292
|
+
table: sequence.identity
|
|
293
|
+
});
|
|
294
|
+
sequences.add(id);
|
|
295
|
+
const owner = sequence.owner;
|
|
296
|
+
if (owner) {
|
|
297
|
+
if (!database.tables.find((table) => tableKey(table.identity) === tableKey(owner.table))?.columns.some((column) => column.key === owner.column)) diagnostics.push({
|
|
298
|
+
code: "UndeclaredSequenceOwner",
|
|
299
|
+
column: owner.column,
|
|
300
|
+
message: `Sequence ${id} is owned by ${tableKey(owner.table)} column ${JSON.stringify(owner.column)}, which is not in the model`,
|
|
301
|
+
side,
|
|
302
|
+
table: sequence.identity
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
const functions = /* @__PURE__ */ new Set();
|
|
307
|
+
for (const fn of database.functions ?? []) {
|
|
308
|
+
const id = functionKey(fn);
|
|
309
|
+
if (functions.has(id)) diagnostics.push({
|
|
310
|
+
code: "DuplicateFunction",
|
|
311
|
+
message: `Duplicate function ${id}`,
|
|
312
|
+
side,
|
|
313
|
+
table: fn.identity
|
|
314
|
+
});
|
|
315
|
+
functions.add(id);
|
|
92
316
|
}
|
|
93
317
|
const namespaces = /* @__PURE__ */ new Set();
|
|
94
318
|
for (const namespace of database.namespaces ?? []) {
|
|
@@ -101,6 +325,125 @@ const validateDatabase = (database, side) => {
|
|
|
101
325
|
});
|
|
102
326
|
namespaces.add(id);
|
|
103
327
|
}
|
|
328
|
+
const externals = /* @__PURE__ */ new Set();
|
|
329
|
+
const external = (key, identity, label, statement) => {
|
|
330
|
+
if (externals.has(key)) diagnostics.push({
|
|
331
|
+
code: "DuplicateExternal",
|
|
332
|
+
message: `Duplicate ${label}`,
|
|
333
|
+
side,
|
|
334
|
+
table: identity
|
|
335
|
+
});
|
|
336
|
+
externals.add(key);
|
|
337
|
+
const namespace = namespaceOf(identity);
|
|
338
|
+
if (namespace && namespaces.has(namespaceKey(namespace))) diagnostics.push({
|
|
339
|
+
code: "ExternalInManagedNamespace",
|
|
340
|
+
message: `External ${label} is in managed schema ${namespace.name}; declare it with ${statement}, or move it out of the managed scope`,
|
|
341
|
+
side,
|
|
342
|
+
table: identity
|
|
343
|
+
});
|
|
344
|
+
};
|
|
345
|
+
for (const table of database.externals?.tables ?? []) {
|
|
346
|
+
const { catalog, name, schema } = table.identity;
|
|
347
|
+
external(`table ${tableKey(table.identity)}`, table.identity, `table ${[
|
|
348
|
+
catalog,
|
|
349
|
+
schema,
|
|
350
|
+
name
|
|
351
|
+
].filter((part) => part !== void 0).join(".")}`, "CREATE TABLE");
|
|
352
|
+
duplicateColumns(table);
|
|
353
|
+
}
|
|
354
|
+
for (const fn of database.externals?.functions ?? []) external(`function ${functionKey(fn)}`, fn.identity, `function ${functionLabel(fn)}`, "CREATE FUNCTION");
|
|
355
|
+
for (const item of commentObjects([{
|
|
356
|
+
externals: database.externals ?? {},
|
|
357
|
+
tables: []
|
|
358
|
+
}])) diagnostics.push({
|
|
359
|
+
code: "ExternalComment",
|
|
360
|
+
message: `${item.label} is on an external object, which migrations never alter; remove the comment`,
|
|
361
|
+
side,
|
|
362
|
+
table: item.identity
|
|
363
|
+
});
|
|
364
|
+
const enums = /* @__PURE__ */ new Set();
|
|
365
|
+
const duplicateEnum = (identity, message) => diagnostics.push({
|
|
366
|
+
code: "DuplicateEnum",
|
|
367
|
+
message,
|
|
368
|
+
side,
|
|
369
|
+
table: identity
|
|
370
|
+
});
|
|
371
|
+
const duplicateValues = ({ identity, values }) => {
|
|
372
|
+
const labels = /* @__PURE__ */ new Set();
|
|
373
|
+
for (const label of values) {
|
|
374
|
+
if (labels.has(label)) diagnostics.push({
|
|
375
|
+
code: "DuplicateEnumValue",
|
|
376
|
+
message: `Duplicate value ${JSON.stringify(label)} in enum ${tableKey(identity)}`,
|
|
377
|
+
side,
|
|
378
|
+
table: identity
|
|
379
|
+
});
|
|
380
|
+
labels.add(label);
|
|
381
|
+
}
|
|
382
|
+
};
|
|
383
|
+
for (const item of database.enums ?? []) {
|
|
384
|
+
const id = tableKey(item.identity);
|
|
385
|
+
if (enums.has(id)) duplicateEnum(item.identity, `Duplicate enum ${id}`);
|
|
386
|
+
enums.add(id);
|
|
387
|
+
duplicateValues(item);
|
|
388
|
+
}
|
|
389
|
+
for (const item of database.externals?.enums ?? []) {
|
|
390
|
+
const { catalog, name, schema } = item.identity;
|
|
391
|
+
const id = tableKey(item.identity);
|
|
392
|
+
if (enums.has(id)) duplicateEnum(item.identity, `Enum ${id} is both managed and external`);
|
|
393
|
+
external(`enum ${id}`, item.identity, `enum ${[
|
|
394
|
+
catalog,
|
|
395
|
+
schema,
|
|
396
|
+
name
|
|
397
|
+
].filter((part) => part !== void 0).join(".")}`, "CREATE TYPE");
|
|
398
|
+
duplicateValues(item);
|
|
399
|
+
}
|
|
400
|
+
const extensions = /* @__PURE__ */ new Set();
|
|
401
|
+
const extensionIdentity = ({ name, schema }) => schema === void 0 ? { name } : {
|
|
402
|
+
name,
|
|
403
|
+
schema
|
|
404
|
+
};
|
|
405
|
+
const managedNamespace = (identity) => {
|
|
406
|
+
const namespace = namespaceOf(identity);
|
|
407
|
+
return namespace && namespaces.has(namespaceKey(namespace));
|
|
408
|
+
};
|
|
409
|
+
for (const extension of database.extensions ?? []) {
|
|
410
|
+
if (extensions.has(extension.name)) diagnostics.push({
|
|
411
|
+
code: "DuplicateExtension",
|
|
412
|
+
message: `Duplicate extension ${extension.name}`,
|
|
413
|
+
side,
|
|
414
|
+
table: extensionIdentity(extension)
|
|
415
|
+
});
|
|
416
|
+
extensions.add(extension.name);
|
|
417
|
+
if (database.namespaces && extension.schema !== void 0 && !managedNamespace(extensionIdentity(extension))) diagnostics.push({
|
|
418
|
+
code: "UndeclaredNamespace",
|
|
419
|
+
message: `Extension ${extension.name} belongs to undeclared namespace ${namespaceKey({ name: extension.schema })}; declare an extension that another tool installs with DECLARE EXTERNAL EXTENSION`,
|
|
420
|
+
side,
|
|
421
|
+
table: extensionIdentity(extension)
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
const externalExtensions = /* @__PURE__ */ new Set();
|
|
425
|
+
for (const extension of database.externals?.extensions ?? []) {
|
|
426
|
+
const table = extensionIdentity(extension);
|
|
427
|
+
if (externalExtensions.has(extension.name)) diagnostics.push({
|
|
428
|
+
code: "DuplicateExternal",
|
|
429
|
+
message: `Duplicate extension ${extension.name}`,
|
|
430
|
+
side,
|
|
431
|
+
table
|
|
432
|
+
});
|
|
433
|
+
else if (extensions.has(extension.name)) diagnostics.push({
|
|
434
|
+
code: "DuplicateExtension",
|
|
435
|
+
message: `Extension ${extension.name} is both managed and external`,
|
|
436
|
+
side,
|
|
437
|
+
table
|
|
438
|
+
});
|
|
439
|
+
externalExtensions.add(extension.name);
|
|
440
|
+
if (managedNamespace(table)) diagnostics.push({
|
|
441
|
+
code: "ExternalInManagedNamespace",
|
|
442
|
+
message: `External extension ${extension.name} is in managed schema ${extension.schema}; declare it with CREATE EXTENSION`,
|
|
443
|
+
side,
|
|
444
|
+
table
|
|
445
|
+
});
|
|
446
|
+
}
|
|
104
447
|
if (database.namespaces) {
|
|
105
448
|
const undeclared = (identity, owner, role) => {
|
|
106
449
|
const namespace = namespaceOf(identity);
|
|
@@ -117,9 +460,32 @@ const validateDatabase = (database, side) => {
|
|
|
117
460
|
undeclared(trigger.identity, trigger.identity, "Trigger");
|
|
118
461
|
undeclared(trigger.target, trigger.identity, "Target of trigger");
|
|
119
462
|
}
|
|
463
|
+
for (const sequence of database.sequences ?? []) {
|
|
464
|
+
undeclared(sequence.identity, sequence.identity, "Sequence");
|
|
465
|
+
if (sequence.owner) undeclared(sequence.owner.table, sequence.identity, "Owner table of sequence");
|
|
466
|
+
}
|
|
467
|
+
for (const fn of database.functions ?? []) undeclared(fn.identity, fn.identity, "Function");
|
|
468
|
+
for (const item of database.enums ?? []) undeclared(item.identity, item.identity, "Enum");
|
|
120
469
|
}
|
|
121
470
|
return diagnostics;
|
|
122
471
|
};
|
|
472
|
+
/**
|
|
473
|
+
* Checks an explicit list of managed schemas against the namespaces of a model built from schema
|
|
474
|
+
* files. Names are compared as catalog names, case-sensitively. The shape of the list (non-empty,
|
|
475
|
+
* unique, including `public`) is the caller's to validate.
|
|
476
|
+
*/
|
|
477
|
+
const checkManagedScope = (model, managedSchemas) => {
|
|
478
|
+
const managed = new Set(managedSchemas);
|
|
479
|
+
const created = new Set((model.namespaces ?? []).map((namespace) => namespace.identity.name));
|
|
480
|
+
const diagnostic = (code, name, message) => ({
|
|
481
|
+
code,
|
|
482
|
+
message,
|
|
483
|
+
side: "after",
|
|
484
|
+
table: { name }
|
|
485
|
+
});
|
|
486
|
+
return [...[...created].filter((name) => !managed.has(name)).map((name) => diagnostic("UnmanagedSchema", name, `Schema ${name} is created by the schema files but is not in managedSchemas; add it to managedSchemas or declare its objects external`)), ...[...managed].filter((name) => name !== "public" && !created.has(name)).map((name) => diagnostic("ManagedSchemaNotCreated", name, `Schema ${name} is in managedSchemas but no schema file creates it`))];
|
|
487
|
+
};
|
|
488
|
+
/** Same kinds and properties in the same order; comments are compared by `diffComments`. */
|
|
123
489
|
const constraintsEqual = (before, after) => before.length === after.length && before.every((item, index) => item.kind === after[index].kind && schemaValueEqual(item.properties, after[index].properties));
|
|
124
490
|
const compareTable = (before, after) => {
|
|
125
491
|
const changes = [];
|
|
@@ -179,7 +545,7 @@ const compareTable = (before, after) => {
|
|
|
179
545
|
_tag: "IndexAdded",
|
|
180
546
|
index
|
|
181
547
|
});
|
|
182
|
-
else if (old.unique !== index.unique || !schemaValueEqual(old.terms, index.terms) || !schemaValueEqual(old.where, index.where)) changes.push({
|
|
548
|
+
else if (old.unique !== index.unique || !schemaValueEqual(old.terms, index.terms) || !schemaValueEqual(old.where, index.where) || !schemaValueEqual(old.options ?? {}, index.options ?? {})) changes.push({
|
|
183
549
|
_tag: "IndexChanged",
|
|
184
550
|
after: index,
|
|
185
551
|
before: old
|
|
@@ -187,6 +553,400 @@ const compareTable = (before, after) => {
|
|
|
187
553
|
}
|
|
188
554
|
return changes;
|
|
189
555
|
};
|
|
556
|
+
/** Collect the table and column renames that a list of changes carries. */
|
|
557
|
+
const sequenceRenames = (changes) => {
|
|
558
|
+
const tables = /* @__PURE__ */ new Map();
|
|
559
|
+
const columns = /* @__PURE__ */ new Map();
|
|
560
|
+
for (const change of changes) {
|
|
561
|
+
if (change._tag !== "TableChanged" && change._tag !== "TableRenamed") continue;
|
|
562
|
+
const key = tableKey(change.before.identity);
|
|
563
|
+
if (change._tag === "TableRenamed") tables.set(key, change.after.identity);
|
|
564
|
+
for (const item of change.changes) if (item._tag === "ColumnRenamed") {
|
|
565
|
+
const renamed = columns.get(key) ?? /* @__PURE__ */ new Map();
|
|
566
|
+
renamed.set(item.before.key, item.after.key);
|
|
567
|
+
columns.set(key, renamed);
|
|
568
|
+
}
|
|
569
|
+
}
|
|
570
|
+
return {
|
|
571
|
+
columns,
|
|
572
|
+
tables
|
|
573
|
+
};
|
|
574
|
+
};
|
|
575
|
+
/** The owner of a `before` sequence expressed in the `after` model, or its key when unowned. */
|
|
576
|
+
const ownerAfterRenames = (sequence, renames) => {
|
|
577
|
+
const owner = sequence.owner;
|
|
578
|
+
if (!owner) return;
|
|
579
|
+
const source = tableKey(owner.table);
|
|
580
|
+
return {
|
|
581
|
+
column: renames.columns.get(source)?.get(owner.column) ?? owner.column,
|
|
582
|
+
table: renames.tables.get(source) ?? owner.table
|
|
583
|
+
};
|
|
584
|
+
};
|
|
585
|
+
const ownerKey = (owner) => owner ? JSON.stringify([tableKey(owner.table), owner.column]) : null;
|
|
586
|
+
/** Function changes. A changed signature is another key: a removal and an addition. */
|
|
587
|
+
const diffFunctions = (before, after) => {
|
|
588
|
+
const changes = [];
|
|
589
|
+
const oldFunctions = new Map((before.functions ?? []).map((item) => [functionKey(item), item]));
|
|
590
|
+
const newFunctions = new Map((after.functions ?? []).map((item) => [functionKey(item), item]));
|
|
591
|
+
for (const [key, fn] of oldFunctions) if (!newFunctions.has(key)) changes.push({
|
|
592
|
+
_tag: "FunctionRemoved",
|
|
593
|
+
function: fn
|
|
594
|
+
});
|
|
595
|
+
for (const [key, fn] of newFunctions) {
|
|
596
|
+
const old = oldFunctions.get(key);
|
|
597
|
+
if (!old) changes.push({
|
|
598
|
+
_tag: "FunctionAdded",
|
|
599
|
+
function: fn
|
|
600
|
+
});
|
|
601
|
+
else if (!schemaValueEqual(old.definition, fn.definition)) changes.push({
|
|
602
|
+
_tag: "FunctionChanged",
|
|
603
|
+
after: fn,
|
|
604
|
+
before: old
|
|
605
|
+
});
|
|
606
|
+
}
|
|
607
|
+
return changes;
|
|
608
|
+
};
|
|
609
|
+
/** Sequence changes. `renames` are the table and column renames the migration applies. */
|
|
610
|
+
const diffSequences = (before, after, renames = {
|
|
611
|
+
columns: /* @__PURE__ */ new Map(),
|
|
612
|
+
tables: /* @__PURE__ */ new Map()
|
|
613
|
+
}) => {
|
|
614
|
+
const changes = [];
|
|
615
|
+
const oldSequences = new Map((before.sequences ?? []).map((item) => [tableKey(item.identity), item]));
|
|
616
|
+
const newSequences = new Map((after.sequences ?? []).map((item) => [tableKey(item.identity), item]));
|
|
617
|
+
const removed = [];
|
|
618
|
+
const added = [];
|
|
619
|
+
for (const [key, sequence] of oldSequences) if (!newSequences.has(key)) removed.push(sequence);
|
|
620
|
+
for (const [key, sequence] of newSequences) {
|
|
621
|
+
const old = oldSequences.get(key);
|
|
622
|
+
if (!old) added.push(sequence);
|
|
623
|
+
else if (!schemaValueEqual(old.definition, sequence.definition) || ownerKey(ownerAfterRenames(old, renames)) !== ownerKey(sequence.owner)) changes.push({
|
|
624
|
+
_tag: "SequenceChanged",
|
|
625
|
+
after: sequence,
|
|
626
|
+
before: old
|
|
627
|
+
});
|
|
628
|
+
}
|
|
629
|
+
const groupKey = (sequence, owner) => JSON.stringify([namespaceOf(sequence.identity) ?? null, owner]);
|
|
630
|
+
const groups = /* @__PURE__ */ new Map();
|
|
631
|
+
const group = (key) => {
|
|
632
|
+
const found = groups.get(key) ?? {
|
|
633
|
+
added: [],
|
|
634
|
+
removed: []
|
|
635
|
+
};
|
|
636
|
+
groups.set(key, found);
|
|
637
|
+
return found;
|
|
638
|
+
};
|
|
639
|
+
for (const sequence of removed) {
|
|
640
|
+
const owner = ownerKey(ownerAfterRenames(sequence, renames));
|
|
641
|
+
if (owner !== null) group(groupKey(sequence, owner)).removed.push(sequence);
|
|
642
|
+
}
|
|
643
|
+
for (const sequence of added) {
|
|
644
|
+
const owner = ownerKey(sequence.owner);
|
|
645
|
+
if (owner !== null) group(groupKey(sequence, owner)).added.push(sequence);
|
|
646
|
+
}
|
|
647
|
+
const paired = /* @__PURE__ */ new Set();
|
|
648
|
+
for (const found of groups.values()) if (found.removed.length === 1 && found.added.length === 1) {
|
|
649
|
+
const [old] = found.removed;
|
|
650
|
+
const [next] = found.added;
|
|
651
|
+
paired.add(old);
|
|
652
|
+
paired.add(next);
|
|
653
|
+
changes.push({
|
|
654
|
+
_tag: "SequenceRenamed",
|
|
655
|
+
after: next,
|
|
656
|
+
before: old
|
|
657
|
+
});
|
|
658
|
+
}
|
|
659
|
+
for (const sequence of removed) if (!paired.has(sequence)) changes.push({
|
|
660
|
+
_tag: "SequenceRemoved",
|
|
661
|
+
sequence
|
|
662
|
+
});
|
|
663
|
+
for (const sequence of added) if (!paired.has(sequence)) changes.push({
|
|
664
|
+
_tag: "SequenceAdded",
|
|
665
|
+
sequence
|
|
666
|
+
});
|
|
667
|
+
return changes;
|
|
668
|
+
};
|
|
669
|
+
/** The `before` triggers keyed as in the after model, and those that lose their key to another. */
|
|
670
|
+
const triggersAfterRenames = (before, renames) => {
|
|
671
|
+
const keyed = /* @__PURE__ */ new Map();
|
|
672
|
+
const displaced = [];
|
|
673
|
+
const moved = (trigger) => renames.tables.has(tableKey(trigger.target));
|
|
674
|
+
const ordered = [...before.triggers ?? []].sort((a, b) => Number(moved(a)) - Number(moved(b)));
|
|
675
|
+
for (const trigger of ordered) {
|
|
676
|
+
const target = renames.tables.get(tableKey(trigger.target)) ?? trigger.target;
|
|
677
|
+
const key = triggerKey({
|
|
678
|
+
...trigger,
|
|
679
|
+
target
|
|
680
|
+
});
|
|
681
|
+
const previous = keyed.get(key);
|
|
682
|
+
if (previous) displaced.push(previous.trigger);
|
|
683
|
+
keyed.set(key, {
|
|
684
|
+
target,
|
|
685
|
+
trigger
|
|
686
|
+
});
|
|
687
|
+
}
|
|
688
|
+
return {
|
|
689
|
+
displaced,
|
|
690
|
+
keyed
|
|
691
|
+
};
|
|
692
|
+
};
|
|
693
|
+
/**
|
|
694
|
+
* Trigger changes, paired by `triggerKey`. `renames` are the table renames the migration applies: a
|
|
695
|
+
* trigger follows its renamed target. There are no trigger renames.
|
|
696
|
+
*/
|
|
697
|
+
const diffTriggers = (before, after, renames = {
|
|
698
|
+
columns: /* @__PURE__ */ new Map(),
|
|
699
|
+
tables: /* @__PURE__ */ new Map()
|
|
700
|
+
}) => {
|
|
701
|
+
const { displaced, keyed: oldTriggers } = triggersAfterRenames(before, renames);
|
|
702
|
+
const changes = displaced.map((trigger) => ({
|
|
703
|
+
_tag: "TriggerRemoved",
|
|
704
|
+
trigger
|
|
705
|
+
}));
|
|
706
|
+
const newTriggers = new Map((after.triggers ?? []).map((item) => [triggerKey(item), item]));
|
|
707
|
+
for (const [key, { trigger }] of oldTriggers) if (!newTriggers.has(key)) changes.push({
|
|
708
|
+
_tag: "TriggerRemoved",
|
|
709
|
+
trigger
|
|
710
|
+
});
|
|
711
|
+
for (const [key, trigger] of newTriggers) {
|
|
712
|
+
const old = oldTriggers.get(key);
|
|
713
|
+
if (!old) changes.push({
|
|
714
|
+
_tag: "TriggerAdded",
|
|
715
|
+
trigger
|
|
716
|
+
});
|
|
717
|
+
else if (tableKey(old.target) !== tableKey(trigger.target) || !schemaValueEqual(old.trigger.definition, trigger.definition)) changes.push({
|
|
718
|
+
_tag: "TriggerChanged",
|
|
719
|
+
after: trigger,
|
|
720
|
+
before: old.trigger
|
|
721
|
+
});
|
|
722
|
+
}
|
|
723
|
+
return changes;
|
|
724
|
+
};
|
|
725
|
+
/**
|
|
726
|
+
* Enum changes, paired by `tableKey`. A renamed enum has one `EnumRenamed`, whether or not its
|
|
727
|
+
* labels differ too. Removals and order are not judged here; external enums are ignored.
|
|
728
|
+
*/
|
|
729
|
+
const diffEnums = (before, after, renames = {
|
|
730
|
+
enums: /* @__PURE__ */ new Map(),
|
|
731
|
+
values: /* @__PURE__ */ new Map()
|
|
732
|
+
}) => {
|
|
733
|
+
const changes = [];
|
|
734
|
+
const oldEnums = new Map((before.enums ?? []).map((item) => [tableKey(item.identity), item]));
|
|
735
|
+
const newEnums = new Map((after.enums ?? []).map((item) => [tableKey(item.identity), item]));
|
|
736
|
+
const valueRenames = (item) => {
|
|
737
|
+
const renamed = renames.values.get(tableKey(item.identity));
|
|
738
|
+
return item.values.flatMap((label) => {
|
|
739
|
+
const target = renamed?.get(label);
|
|
740
|
+
return target === void 0 ? [] : [{
|
|
741
|
+
after: target,
|
|
742
|
+
before: label
|
|
743
|
+
}];
|
|
744
|
+
});
|
|
745
|
+
};
|
|
746
|
+
const renamed = /* @__PURE__ */ new Map();
|
|
747
|
+
for (const [key, item] of oldEnums) {
|
|
748
|
+
const target = renames.enums.get(key);
|
|
749
|
+
if (target && tableKey(target) !== key && newEnums.has(tableKey(target))) renamed.set(tableKey(target), item);
|
|
750
|
+
}
|
|
751
|
+
const sources = new Set(renamed.values());
|
|
752
|
+
for (const [key, item] of oldEnums) if (!sources.has(item) && (!newEnums.has(key) || renamed.has(key))) changes.push({
|
|
753
|
+
_tag: "EnumRemoved",
|
|
754
|
+
enum: item
|
|
755
|
+
});
|
|
756
|
+
for (const [key, item] of newEnums) {
|
|
757
|
+
const source = renamed.get(key);
|
|
758
|
+
if (source) {
|
|
759
|
+
changes.push({
|
|
760
|
+
_tag: "EnumRenamed",
|
|
761
|
+
after: item,
|
|
762
|
+
before: source,
|
|
763
|
+
values: valueRenames(source)
|
|
764
|
+
});
|
|
765
|
+
continue;
|
|
766
|
+
}
|
|
767
|
+
const old = oldEnums.get(key);
|
|
768
|
+
if (!old || sources.has(old)) changes.push({
|
|
769
|
+
_tag: "EnumAdded",
|
|
770
|
+
enum: item
|
|
771
|
+
});
|
|
772
|
+
else if (!schemaValueEqual(old.values, item.values)) changes.push({
|
|
773
|
+
_tag: "EnumChanged",
|
|
774
|
+
after: item,
|
|
775
|
+
before: old,
|
|
776
|
+
values: valueRenames(old)
|
|
777
|
+
});
|
|
778
|
+
}
|
|
779
|
+
return changes;
|
|
780
|
+
};
|
|
781
|
+
/** Extensions are compared by name. Unpinning a version is no change. */
|
|
782
|
+
const diffExtensions = (before, after) => {
|
|
783
|
+
const changes = [];
|
|
784
|
+
const oldExtensions = new Map((before.extensions ?? []).map((item) => [item.name, item]));
|
|
785
|
+
const newExtensions = new Map((after.extensions ?? []).map((item) => [item.name, item]));
|
|
786
|
+
for (const [name, extension] of oldExtensions) if (!newExtensions.has(name)) changes.push({
|
|
787
|
+
_tag: "ExtensionRemoved",
|
|
788
|
+
extension
|
|
789
|
+
});
|
|
790
|
+
for (const [name, extension] of newExtensions) {
|
|
791
|
+
const old = oldExtensions.get(name);
|
|
792
|
+
if (!old) {
|
|
793
|
+
changes.push({
|
|
794
|
+
_tag: "ExtensionAdded",
|
|
795
|
+
extension
|
|
796
|
+
});
|
|
797
|
+
continue;
|
|
798
|
+
}
|
|
799
|
+
if (old.schema !== extension.schema) changes.push({
|
|
800
|
+
_tag: "ExtensionSchemaChanged",
|
|
801
|
+
after: extension,
|
|
802
|
+
before: old
|
|
803
|
+
});
|
|
804
|
+
if (extension.version !== void 0 && extension.version !== old.version) changes.push({
|
|
805
|
+
_tag: "ExtensionVersionChanged",
|
|
806
|
+
after: extension,
|
|
807
|
+
before: old
|
|
808
|
+
});
|
|
809
|
+
}
|
|
810
|
+
return changes;
|
|
811
|
+
};
|
|
812
|
+
/**
|
|
813
|
+
* The objects that both lists hold, as `[before, after]` in after order. `renamed` maps the key of a
|
|
814
|
+
* renamed after object to its key before; the source of a rename pairs with nothing else.
|
|
815
|
+
*/
|
|
816
|
+
const pairs = (before, after, key, renamed = /* @__PURE__ */ new Map()) => {
|
|
817
|
+
const old = new Map((before ?? []).map((item) => [key(item), item]));
|
|
818
|
+
const sources = new Set(renamed.values());
|
|
819
|
+
return (after ?? []).flatMap((item) => {
|
|
820
|
+
const own = key(item);
|
|
821
|
+
const source = renamed.get(own) ?? (sources.has(own) ? void 0 : own);
|
|
822
|
+
const previous = source === void 0 ? void 0 : old.get(source);
|
|
823
|
+
return previous === void 0 ? [] : [[previous, item]];
|
|
824
|
+
});
|
|
825
|
+
};
|
|
826
|
+
/** Constraints that are equal apart from their comment, preferring those with the same comment. */
|
|
827
|
+
const constraintPairs = (before, after) => {
|
|
828
|
+
const unused = new Set(before.keys());
|
|
829
|
+
const found = /* @__PURE__ */ new Map();
|
|
830
|
+
for (const sameComment of [true, false]) for (const [position, item] of after.entries()) {
|
|
831
|
+
if (found.has(position)) continue;
|
|
832
|
+
const match = [...unused].find((index) => constraintsEqual([before[index]], [item]) && (!sameComment || before[index].comment === item.comment));
|
|
833
|
+
if (match !== void 0) {
|
|
834
|
+
unused.delete(match);
|
|
835
|
+
found.set(position, before[match]);
|
|
836
|
+
}
|
|
837
|
+
}
|
|
838
|
+
return after.flatMap((item, position) => {
|
|
839
|
+
const previous = found.get(position);
|
|
840
|
+
return previous ? [[previous, item]] : [];
|
|
841
|
+
});
|
|
842
|
+
};
|
|
843
|
+
/**
|
|
844
|
+
* Comment changes of the managed objects that both models hold. `changes` are the changes of the
|
|
845
|
+
* migration: their table, column, sequence and enum renames pair objects across a rename. Added and
|
|
846
|
+
* removed objects have no change here.
|
|
847
|
+
*/
|
|
848
|
+
const diffComments = (before, after, changes = []) => {
|
|
849
|
+
const found = [];
|
|
850
|
+
const compare = (previous, next, target) => {
|
|
851
|
+
if (previous !== next) found.push({
|
|
852
|
+
_tag: "CommentChanged",
|
|
853
|
+
...next === void 0 ? {} : { after: next },
|
|
854
|
+
...previous === void 0 ? {} : { before: previous },
|
|
855
|
+
target
|
|
856
|
+
});
|
|
857
|
+
};
|
|
858
|
+
const tables = /* @__PURE__ */ new Map();
|
|
859
|
+
const columns = /* @__PURE__ */ new Map();
|
|
860
|
+
const sequences = /* @__PURE__ */ new Map();
|
|
861
|
+
const enums = /* @__PURE__ */ new Map();
|
|
862
|
+
for (const change of changes) if (change._tag === "TableChanged" || change._tag === "TableRenamed") {
|
|
863
|
+
const key = tableKey(change.after.identity);
|
|
864
|
+
if (change._tag === "TableRenamed") tables.set(key, tableKey(change.before.identity));
|
|
865
|
+
for (const item of change.changes) if (item._tag === "ColumnRenamed") {
|
|
866
|
+
const renamed = columns.get(key) ?? /* @__PURE__ */ new Map();
|
|
867
|
+
renamed.set(item.after.key, item.before.key);
|
|
868
|
+
columns.set(key, renamed);
|
|
869
|
+
}
|
|
870
|
+
} else if (change._tag === "SequenceRenamed") sequences.set(tableKey(change.after.identity), tableKey(change.before.identity));
|
|
871
|
+
else if (change._tag === "EnumRenamed") enums.set(tableKey(change.after.identity), tableKey(change.before.identity));
|
|
872
|
+
for (const [old, item] of pairs(before.namespaces, after.namespaces, (item) => namespaceKey(item.identity))) compare(old.comment, item.comment, {
|
|
873
|
+
identity: item.identity,
|
|
874
|
+
kind: "namespace"
|
|
875
|
+
});
|
|
876
|
+
for (const [old, item] of pairs(before.extensions, after.extensions, (item) => item.name)) compare(old.comment, item.comment, {
|
|
877
|
+
kind: "extension",
|
|
878
|
+
name: item.name
|
|
879
|
+
});
|
|
880
|
+
const identityKey = (item) => tableKey(item.identity);
|
|
881
|
+
for (const [old, item] of pairs(before.enums, after.enums, identityKey, enums)) compare(old.comment, item.comment, {
|
|
882
|
+
identity: item.identity,
|
|
883
|
+
kind: "enum"
|
|
884
|
+
});
|
|
885
|
+
for (const [old, table] of pairs(before.tables, after.tables, identityKey, tables)) {
|
|
886
|
+
const identity = table.identity;
|
|
887
|
+
const constraints = (previous, next) => {
|
|
888
|
+
for (const [source, { comment, kind, properties }] of constraintPairs(previous, next)) compare(source.comment, comment, {
|
|
889
|
+
constraint: {
|
|
890
|
+
kind,
|
|
891
|
+
properties
|
|
892
|
+
},
|
|
893
|
+
kind: "constraint",
|
|
894
|
+
table: identity
|
|
895
|
+
});
|
|
896
|
+
};
|
|
897
|
+
compare(old.comment, table.comment, {
|
|
898
|
+
identity,
|
|
899
|
+
kind: "table"
|
|
900
|
+
});
|
|
901
|
+
for (const [source, column] of pairs(old.columns, table.columns, (item) => item.key, columns.get(tableKey(identity)))) {
|
|
902
|
+
compare(source.comment, column.comment, {
|
|
903
|
+
column: column.key,
|
|
904
|
+
kind: "column",
|
|
905
|
+
table: identity
|
|
906
|
+
});
|
|
907
|
+
constraints(source.constraints, column.constraints);
|
|
908
|
+
}
|
|
909
|
+
constraints(old.constraints, table.constraints);
|
|
910
|
+
for (const [source, index] of pairs(old.indexes, table.indexes, (item) => item.key)) compare(source.comment, index.comment, {
|
|
911
|
+
index: index.key,
|
|
912
|
+
kind: "index",
|
|
913
|
+
table: identity
|
|
914
|
+
});
|
|
915
|
+
}
|
|
916
|
+
for (const [old, view] of pairs(before.views, after.views, identityKey)) {
|
|
917
|
+
compare(old.comment, view.comment, {
|
|
918
|
+
identity: view.identity,
|
|
919
|
+
kind: "view"
|
|
920
|
+
});
|
|
921
|
+
const previous = new Map(Object.entries(old.columnComments ?? {}));
|
|
922
|
+
const next = new Map(Object.entries(view.columnComments ?? {}));
|
|
923
|
+
for (const column of /* @__PURE__ */ new Set([...next.keys(), ...previous.keys()])) compare(previous.get(column), next.get(column), {
|
|
924
|
+
column,
|
|
925
|
+
kind: "viewColumn",
|
|
926
|
+
view: view.identity
|
|
927
|
+
});
|
|
928
|
+
}
|
|
929
|
+
const renames = sequenceRenames(changes);
|
|
930
|
+
const oldTriggers = triggersAfterRenames(before, renames).keyed;
|
|
931
|
+
for (const trigger of after.triggers ?? []) {
|
|
932
|
+
const old = oldTriggers.get(triggerKey(trigger));
|
|
933
|
+
if (old) compare(old.trigger.comment, trigger.comment, {
|
|
934
|
+
kind: "trigger",
|
|
935
|
+
name: trigger.identity.name,
|
|
936
|
+
target: trigger.target
|
|
937
|
+
});
|
|
938
|
+
}
|
|
939
|
+
for (const [old, item] of pairs(before.sequences, after.sequences, identityKey, sequences)) compare(old.comment, item.comment, {
|
|
940
|
+
identity: item.identity,
|
|
941
|
+
kind: "sequence"
|
|
942
|
+
});
|
|
943
|
+
for (const [old, item] of pairs(before.functions, after.functions, functionKey)) compare(old.comment, item.comment, {
|
|
944
|
+
identity: item.identity,
|
|
945
|
+
kind: "function",
|
|
946
|
+
signature: item.signature
|
|
947
|
+
});
|
|
948
|
+
return found;
|
|
949
|
+
};
|
|
190
950
|
/** Compare normalized relational models without parsing SQL or consulting a dialect. */
|
|
191
951
|
const diffDatabases = (before, after) => Effect.gen(function* () {
|
|
192
952
|
const diagnostics = [...validateDatabase(before, "before"), ...validateDatabase(after, "after")];
|
|
@@ -210,6 +970,8 @@ const diffDatabases = (before, after) => Effect.gen(function* () {
|
|
|
210
970
|
before: old
|
|
211
971
|
});
|
|
212
972
|
}
|
|
973
|
+
changes.push(...diffExtensions(before, after));
|
|
974
|
+
changes.push(...diffEnums(before, after));
|
|
213
975
|
const previous = new Map(before.tables.map((table) => [tableKey(table.identity), table]));
|
|
214
976
|
const next = new Map(after.tables.map((table) => [tableKey(table.identity), table]));
|
|
215
977
|
for (const [key, table] of previous) if (!next.has(key)) changes.push({
|
|
@@ -247,30 +1009,16 @@ const diffDatabases = (before, after) => Effect.gen(function* () {
|
|
|
247
1009
|
_tag: "ViewAdded",
|
|
248
1010
|
view
|
|
249
1011
|
});
|
|
250
|
-
else if (!schemaValueEqual(old.columns ?? null, view.columns ?? null) || !schemaValueEqual(old.query, view.query)) changes.push({
|
|
1012
|
+
else if (!schemaValueEqual(old.columns ?? null, view.columns ?? null) || !schemaValueEqual(old.query, view.query) || !schemaValueEqual(old.options ?? {}, view.options ?? {})) changes.push({
|
|
251
1013
|
_tag: "ViewChanged",
|
|
252
1014
|
after: view,
|
|
253
1015
|
before: old
|
|
254
1016
|
});
|
|
255
1017
|
}
|
|
256
|
-
|
|
257
|
-
|
|
258
|
-
|
|
259
|
-
|
|
260
|
-
trigger
|
|
261
|
-
});
|
|
262
|
-
for (const [key, trigger] of newTriggers) {
|
|
263
|
-
const old = oldTriggers.get(key);
|
|
264
|
-
if (!old) changes.push({
|
|
265
|
-
_tag: "TriggerAdded",
|
|
266
|
-
trigger
|
|
267
|
-
});
|
|
268
|
-
else if (tableKey(old.target) !== tableKey(trigger.target) || !schemaValueEqual(old.definition, trigger.definition)) changes.push({
|
|
269
|
-
_tag: "TriggerChanged",
|
|
270
|
-
after: trigger,
|
|
271
|
-
before: old
|
|
272
|
-
});
|
|
273
|
-
}
|
|
1018
|
+
changes.push(...diffTriggers(before, after));
|
|
1019
|
+
changes.push(...diffSequences(before, after));
|
|
1020
|
+
changes.push(...diffFunctions(before, after));
|
|
1021
|
+
changes.push(...diffComments(before, after, changes));
|
|
274
1022
|
return { changes };
|
|
275
1023
|
});
|
|
276
1024
|
// src/migration.ts
|
|
@@ -289,6 +1037,58 @@ const planDatabaseMigration = (diff) => ({
|
|
|
289
1037
|
after: change.after,
|
|
290
1038
|
before: change.before
|
|
291
1039
|
};
|
|
1040
|
+
case "EnumAdded":
|
|
1041
|
+
case "EnumRemoved": return {
|
|
1042
|
+
_tag: change._tag === "EnumAdded" ? "CreateEnum" : "DropEnum",
|
|
1043
|
+
enum: change.enum
|
|
1044
|
+
};
|
|
1045
|
+
case "EnumRenamed":
|
|
1046
|
+
case "EnumChanged": return {
|
|
1047
|
+
_tag: change._tag === "EnumRenamed" ? "RenameEnum" : "AlterEnum",
|
|
1048
|
+
after: change.after,
|
|
1049
|
+
before: change.before,
|
|
1050
|
+
values: change.values
|
|
1051
|
+
};
|
|
1052
|
+
case "ExtensionAdded":
|
|
1053
|
+
case "ExtensionRemoved": return {
|
|
1054
|
+
_tag: change._tag === "ExtensionAdded" ? "CreateExtension" : "DropExtension",
|
|
1055
|
+
extension: change.extension
|
|
1056
|
+
};
|
|
1057
|
+
case "ExtensionVersionChanged":
|
|
1058
|
+
case "ExtensionSchemaChanged": return change._tag === "ExtensionVersionChanged" ? {
|
|
1059
|
+
_tag: "UpdateExtension",
|
|
1060
|
+
after: change.after,
|
|
1061
|
+
before: change.before
|
|
1062
|
+
} : {
|
|
1063
|
+
_tag: "MoveExtension",
|
|
1064
|
+
after: change.after,
|
|
1065
|
+
before: change.before
|
|
1066
|
+
};
|
|
1067
|
+
case "FunctionAdded":
|
|
1068
|
+
case "FunctionRemoved": return {
|
|
1069
|
+
_tag: change._tag === "FunctionAdded" ? "CreateFunction" : "DropFunction",
|
|
1070
|
+
function: change.function
|
|
1071
|
+
};
|
|
1072
|
+
case "FunctionChanged": return {
|
|
1073
|
+
_tag: "ReplaceFunction",
|
|
1074
|
+
after: change.after,
|
|
1075
|
+
before: change.before
|
|
1076
|
+
};
|
|
1077
|
+
case "SequenceAdded":
|
|
1078
|
+
case "SequenceRemoved": return {
|
|
1079
|
+
_tag: change._tag === "SequenceAdded" ? "CreateSequence" : "DropSequence",
|
|
1080
|
+
sequence: change.sequence
|
|
1081
|
+
};
|
|
1082
|
+
case "SequenceChanged": return {
|
|
1083
|
+
_tag: "AlterSequence",
|
|
1084
|
+
after: change.after,
|
|
1085
|
+
before: change.before
|
|
1086
|
+
};
|
|
1087
|
+
case "SequenceRenamed": return {
|
|
1088
|
+
_tag: "RenameSequence",
|
|
1089
|
+
after: change.after,
|
|
1090
|
+
before: change.before
|
|
1091
|
+
};
|
|
292
1092
|
case "TriggerAdded":
|
|
293
1093
|
case "TriggerRemoved": return {
|
|
294
1094
|
_tag: change._tag === "TriggerAdded" ? "CreateTrigger" : "DropTrigger",
|
|
@@ -324,14 +1124,79 @@ const planDatabaseMigration = (diff) => ({
|
|
|
324
1124
|
before: change.before,
|
|
325
1125
|
changes: change.changes
|
|
326
1126
|
};
|
|
1127
|
+
case "CommentChanged": {
|
|
1128
|
+
const { _tag, ...comment } = change;
|
|
1129
|
+
return {
|
|
1130
|
+
_tag: "SetComment",
|
|
1131
|
+
...comment
|
|
1132
|
+
};
|
|
1133
|
+
}
|
|
327
1134
|
}
|
|
328
1135
|
})
|
|
329
1136
|
});
|
|
1137
|
+
/** Names a comment target of the `up` migration's after model as its before model names it. */
|
|
1138
|
+
const targetBeforeRenames = (up) => {
|
|
1139
|
+
const tables = /* @__PURE__ */ new Map();
|
|
1140
|
+
const columns = /* @__PURE__ */ new Map();
|
|
1141
|
+
const sequences = /* @__PURE__ */ new Map();
|
|
1142
|
+
const enums = /* @__PURE__ */ new Map();
|
|
1143
|
+
for (const step of up.procedures) if (step._tag === "AlterTable" || step._tag === "RenameTable") {
|
|
1144
|
+
const key = tableKey(step.after.identity);
|
|
1145
|
+
if (step._tag === "RenameTable") tables.set(key, step.before.identity);
|
|
1146
|
+
const renamed = /* @__PURE__ */ new Map();
|
|
1147
|
+
for (const change of step.changes) if (change._tag === "ColumnRenamed") renamed.set(change.after.key, change.before.key);
|
|
1148
|
+
columns.set(key, renamed);
|
|
1149
|
+
} else if (step._tag === "RenameSequence") sequences.set(tableKey(step.after.identity), step.before.identity);
|
|
1150
|
+
else if (step._tag === "RenameEnum") enums.set(tableKey(step.after.identity), step.before.identity);
|
|
1151
|
+
const renamed = (names, identity) => names.get(tableKey(identity)) ?? identity;
|
|
1152
|
+
return (target) => {
|
|
1153
|
+
switch (target.kind) {
|
|
1154
|
+
case "table": return {
|
|
1155
|
+
...target,
|
|
1156
|
+
identity: renamed(tables, target.identity)
|
|
1157
|
+
};
|
|
1158
|
+
case "sequence": return {
|
|
1159
|
+
...target,
|
|
1160
|
+
identity: renamed(sequences, target.identity)
|
|
1161
|
+
};
|
|
1162
|
+
case "enum": return {
|
|
1163
|
+
...target,
|
|
1164
|
+
identity: renamed(enums, target.identity)
|
|
1165
|
+
};
|
|
1166
|
+
case "column": return {
|
|
1167
|
+
...target,
|
|
1168
|
+
column: columns.get(tableKey(target.table))?.get(target.column) ?? target.column,
|
|
1169
|
+
table: renamed(tables, target.table)
|
|
1170
|
+
};
|
|
1171
|
+
case "index":
|
|
1172
|
+
case "constraint": return {
|
|
1173
|
+
...target,
|
|
1174
|
+
table: renamed(tables, target.table)
|
|
1175
|
+
};
|
|
1176
|
+
case "trigger": return {
|
|
1177
|
+
...target,
|
|
1178
|
+
target: renamed(tables, target.target)
|
|
1179
|
+
};
|
|
1180
|
+
default: return target;
|
|
1181
|
+
}
|
|
1182
|
+
};
|
|
1183
|
+
};
|
|
330
1184
|
/** Reverse reviewed intent, retaining rename pairs and guarding newly added data. */
|
|
331
1185
|
const planDownMigration = (up) => {
|
|
332
1186
|
const procedures = [];
|
|
333
1187
|
const dropGuards = [];
|
|
1188
|
+
const downTarget = targetBeforeRenames(up);
|
|
334
1189
|
for (const step of up.procedures) switch (step._tag) {
|
|
1190
|
+
case "SetComment": {
|
|
1191
|
+
const { after, before, target } = step;
|
|
1192
|
+
procedures.push({
|
|
1193
|
+
_tag: "SetComment",
|
|
1194
|
+
...before === void 0 ? {} : { after: before },
|
|
1195
|
+
...after === void 0 ? {} : { before: after },
|
|
1196
|
+
target: downTarget(target)
|
|
1197
|
+
});
|
|
1198
|
+
break;
|
|
1199
|
+
}
|
|
335
1200
|
case "DropTable": return `The up migration removed table ${step.table.name}.`;
|
|
336
1201
|
case "CreateTable":
|
|
337
1202
|
dropGuards.push({ table: step.table.identity });
|
|
@@ -340,6 +1205,52 @@ const planDownMigration = (up) => {
|
|
|
340
1205
|
_tag: "DropTable"
|
|
341
1206
|
});
|
|
342
1207
|
break;
|
|
1208
|
+
case "CreateEnum":
|
|
1209
|
+
case "DropEnum":
|
|
1210
|
+
procedures.push({
|
|
1211
|
+
...step,
|
|
1212
|
+
_tag: step._tag === "CreateEnum" ? "DropEnum" : "CreateEnum"
|
|
1213
|
+
});
|
|
1214
|
+
break;
|
|
1215
|
+
case "RenameEnum":
|
|
1216
|
+
case "AlterEnum":
|
|
1217
|
+
procedures.push({
|
|
1218
|
+
...swap(step),
|
|
1219
|
+
values: step.values.map(swap)
|
|
1220
|
+
});
|
|
1221
|
+
break;
|
|
1222
|
+
case "CreateExtension":
|
|
1223
|
+
case "DropExtension":
|
|
1224
|
+
procedures.push({
|
|
1225
|
+
...step,
|
|
1226
|
+
_tag: step._tag === "CreateExtension" ? "DropExtension" : "CreateExtension"
|
|
1227
|
+
});
|
|
1228
|
+
break;
|
|
1229
|
+
case "UpdateExtension": return `extension ${step.after.name} cannot be downgraded from ${step.after.version} to ${step.before.version ?? "an unpinned version"}`;
|
|
1230
|
+
case "MoveExtension":
|
|
1231
|
+
procedures.push(swap(step));
|
|
1232
|
+
break;
|
|
1233
|
+
case "CreateFunction":
|
|
1234
|
+
case "DropFunction":
|
|
1235
|
+
procedures.push({
|
|
1236
|
+
...step,
|
|
1237
|
+
_tag: step._tag === "CreateFunction" ? "DropFunction" : "CreateFunction"
|
|
1238
|
+
});
|
|
1239
|
+
break;
|
|
1240
|
+
case "ReplaceFunction":
|
|
1241
|
+
procedures.push(swap(step));
|
|
1242
|
+
break;
|
|
1243
|
+
case "CreateSequence":
|
|
1244
|
+
case "DropSequence":
|
|
1245
|
+
procedures.push({
|
|
1246
|
+
...step,
|
|
1247
|
+
_tag: step._tag === "CreateSequence" ? "DropSequence" : "CreateSequence"
|
|
1248
|
+
});
|
|
1249
|
+
break;
|
|
1250
|
+
case "AlterSequence":
|
|
1251
|
+
case "RenameSequence":
|
|
1252
|
+
procedures.push(swap(step));
|
|
1253
|
+
break;
|
|
343
1254
|
case "CreateTrigger":
|
|
344
1255
|
case "DropTrigger":
|
|
345
1256
|
procedures.push({
|
|
@@ -448,9 +1359,20 @@ const properties = (value, path) => {
|
|
|
448
1359
|
record$1(value, path);
|
|
449
1360
|
semanticValue(value, path);
|
|
450
1361
|
};
|
|
1362
|
+
const commentText = (value, path) => {
|
|
1363
|
+
if (typeof value !== "string" || value === "") fail(path, "a non-empty string");
|
|
1364
|
+
};
|
|
1365
|
+
const comment = (item, path) => {
|
|
1366
|
+
if (Object.hasOwn(item, "comment")) commentText(item.comment, `${path}.comment`);
|
|
1367
|
+
};
|
|
451
1368
|
const constraint = (value, path) => {
|
|
452
1369
|
const item = record$1(value, path);
|
|
453
|
-
fields(item, [
|
|
1370
|
+
fields(item, [
|
|
1371
|
+
"kind",
|
|
1372
|
+
"properties",
|
|
1373
|
+
"comment"
|
|
1374
|
+
], path);
|
|
1375
|
+
comment(item, path);
|
|
454
1376
|
text(item.kind, `${path}.kind`);
|
|
455
1377
|
properties(item.properties, `${path}.properties`);
|
|
456
1378
|
};
|
|
@@ -460,8 +1382,10 @@ const column = (value, path) => {
|
|
|
460
1382
|
"key",
|
|
461
1383
|
"name",
|
|
462
1384
|
"dataType",
|
|
463
|
-
"constraints"
|
|
1385
|
+
"constraints",
|
|
1386
|
+
"comment"
|
|
464
1387
|
], path);
|
|
1388
|
+
comment(item, path);
|
|
465
1389
|
text(item.key, `${path}.key`);
|
|
466
1390
|
text(item.name, `${path}.name`);
|
|
467
1391
|
semanticValue(item.dataType, `${path}.dataType`);
|
|
@@ -474,10 +1398,14 @@ const index = (value, path) => {
|
|
|
474
1398
|
"name",
|
|
475
1399
|
"unique",
|
|
476
1400
|
"terms",
|
|
477
|
-
"where"
|
|
1401
|
+
"where",
|
|
1402
|
+
"options",
|
|
1403
|
+
"comment"
|
|
478
1404
|
], path);
|
|
1405
|
+
comment(item, path);
|
|
479
1406
|
text(item.key, `${path}.key`);
|
|
480
1407
|
text(item.name, `${path}.name`);
|
|
1408
|
+
if (Object.hasOwn(item, "options")) properties(item.options, `${path}.options`);
|
|
481
1409
|
if (typeof item.unique !== "boolean") fail(`${path}.unique`, "a boolean");
|
|
482
1410
|
array(item.terms, `${path}.terms`, semanticValue);
|
|
483
1411
|
semanticValue(item.where, `${path}.where`);
|
|
@@ -497,25 +1425,74 @@ const trigger = (value, path) => {
|
|
|
497
1425
|
fields(item, [
|
|
498
1426
|
"identity",
|
|
499
1427
|
"name",
|
|
1428
|
+
"scope",
|
|
500
1429
|
"target",
|
|
501
|
-
"definition"
|
|
1430
|
+
"definition",
|
|
1431
|
+
"comment"
|
|
502
1432
|
], path);
|
|
1433
|
+
comment(item, path);
|
|
503
1434
|
identity(item.identity, `${path}.identity`);
|
|
504
1435
|
text(item.name, `${path}.name`);
|
|
1436
|
+
if (Object.hasOwn(item, "scope") && item.scope !== "target") fail(`${path}.scope`, "\"target\"");
|
|
505
1437
|
identity(item.target, `${path}.target`);
|
|
506
1438
|
semanticValue(item.definition, `${path}.definition`);
|
|
507
1439
|
};
|
|
1440
|
+
const sequence = (value, path) => {
|
|
1441
|
+
const item = record$1(value, path);
|
|
1442
|
+
fields(item, [
|
|
1443
|
+
"identity",
|
|
1444
|
+
"name",
|
|
1445
|
+
"definition",
|
|
1446
|
+
"owner",
|
|
1447
|
+
"comment"
|
|
1448
|
+
], path);
|
|
1449
|
+
comment(item, path);
|
|
1450
|
+
identity(item.identity, `${path}.identity`);
|
|
1451
|
+
text(item.name, `${path}.name`);
|
|
1452
|
+
properties(item.definition, `${path}.definition`);
|
|
1453
|
+
if (Object.hasOwn(item, "owner")) {
|
|
1454
|
+
const owner = record$1(item.owner, `${path}.owner`);
|
|
1455
|
+
fields(owner, ["column", "table"], `${path}.owner`);
|
|
1456
|
+
text(owner.column, `${path}.owner.column`);
|
|
1457
|
+
identity(owner.table, `${path}.owner.table`);
|
|
1458
|
+
}
|
|
1459
|
+
};
|
|
1460
|
+
const databaseFunction = (value, path) => {
|
|
1461
|
+
const item = record$1(value, path);
|
|
1462
|
+
fields(item, [
|
|
1463
|
+
"identity",
|
|
1464
|
+
"name",
|
|
1465
|
+
"signature",
|
|
1466
|
+
"definition",
|
|
1467
|
+
"comment"
|
|
1468
|
+
], path);
|
|
1469
|
+
comment(item, path);
|
|
1470
|
+
identity(item.identity, `${path}.identity`);
|
|
1471
|
+
text(item.name, `${path}.name`);
|
|
1472
|
+
array(item.signature, `${path}.signature`, semanticValue);
|
|
1473
|
+
properties(item.definition, `${path}.definition`);
|
|
1474
|
+
};
|
|
508
1475
|
const view = (value, path) => {
|
|
509
1476
|
const item = record$1(value, path);
|
|
510
1477
|
fields(item, [
|
|
511
1478
|
"identity",
|
|
512
1479
|
"name",
|
|
513
1480
|
"columns",
|
|
514
|
-
"query"
|
|
1481
|
+
"query",
|
|
1482
|
+
"options",
|
|
1483
|
+
"comment",
|
|
1484
|
+
"columnComments"
|
|
515
1485
|
], path);
|
|
1486
|
+
comment(item, path);
|
|
1487
|
+
if (Object.hasOwn(item, "columnComments")) {
|
|
1488
|
+
const columns = record$1(item.columnComments, `${path}.columnComments`);
|
|
1489
|
+
for (const key of Reflect.ownKeys(columns)) if (typeof key !== "string") fail(`${path}.columnComments`, "string property keys");
|
|
1490
|
+
else commentText(columns[key], `${path}.columnComments.${key}`);
|
|
1491
|
+
}
|
|
516
1492
|
identity(item.identity, `${path}.identity`);
|
|
517
1493
|
text(item.name, `${path}.name`);
|
|
518
1494
|
if (Object.hasOwn(item, "columns")) array(item.columns, `${path}.columns`, text);
|
|
1495
|
+
if (Object.hasOwn(item, "options")) properties(item.options, `${path}.options`);
|
|
519
1496
|
semanticValue(item.query, `${path}.query`);
|
|
520
1497
|
};
|
|
521
1498
|
const namespace = (value, path) => {
|
|
@@ -523,8 +1500,10 @@ const namespace = (value, path) => {
|
|
|
523
1500
|
fields(item, [
|
|
524
1501
|
"identity",
|
|
525
1502
|
"name",
|
|
526
|
-
"options"
|
|
1503
|
+
"options",
|
|
1504
|
+
"comment"
|
|
527
1505
|
], path);
|
|
1506
|
+
comment(item, path);
|
|
528
1507
|
const identity = record$1(item.identity, `${path}.identity`);
|
|
529
1508
|
fields(identity, ["catalog", "name"], `${path}.identity`);
|
|
530
1509
|
text(identity.name, `${path}.identity.name`);
|
|
@@ -540,8 +1519,10 @@ const table = (value, path) => {
|
|
|
540
1519
|
"columns",
|
|
541
1520
|
"constraints",
|
|
542
1521
|
"options",
|
|
543
|
-
"indexes"
|
|
1522
|
+
"indexes",
|
|
1523
|
+
"comment"
|
|
544
1524
|
], path);
|
|
1525
|
+
comment(item, path);
|
|
545
1526
|
identity(item.identity, `${path}.identity`);
|
|
546
1527
|
text(item.name, `${path}.name`);
|
|
547
1528
|
array(item.columns, `${path}.columns`, column);
|
|
@@ -549,6 +1530,49 @@ const table = (value, path) => {
|
|
|
549
1530
|
array(item.constraints, `${path}.constraints`, constraint);
|
|
550
1531
|
properties(item.options, `${path}.options`);
|
|
551
1532
|
};
|
|
1533
|
+
const extension = (value, path) => {
|
|
1534
|
+
const item = record$1(value, path);
|
|
1535
|
+
fields(item, [
|
|
1536
|
+
"name",
|
|
1537
|
+
"schema",
|
|
1538
|
+
"version",
|
|
1539
|
+
"comment"
|
|
1540
|
+
], path);
|
|
1541
|
+
comment(item, path);
|
|
1542
|
+
text(item.name, `${path}.name`);
|
|
1543
|
+
for (const key of ["schema", "version"]) if (Object.hasOwn(item, key)) text(item[key], `${path}.${key}`);
|
|
1544
|
+
};
|
|
1545
|
+
const databaseEnum = (value, path) => {
|
|
1546
|
+
const item = record$1(value, path);
|
|
1547
|
+
fields(item, [
|
|
1548
|
+
"identity",
|
|
1549
|
+
"name",
|
|
1550
|
+
"values",
|
|
1551
|
+
"comment"
|
|
1552
|
+
], path);
|
|
1553
|
+
comment(item, path);
|
|
1554
|
+
identity(item.identity, `${path}.identity`);
|
|
1555
|
+
text(item.name, `${path}.name`);
|
|
1556
|
+
const labels = /* @__PURE__ */ new Set();
|
|
1557
|
+
array(item.values, `${path}.values`, (label, at) => {
|
|
1558
|
+
text(label, at);
|
|
1559
|
+
if (labels.has(label)) fail(at, "a distinct label");
|
|
1560
|
+
labels.add(label);
|
|
1561
|
+
});
|
|
1562
|
+
};
|
|
1563
|
+
const externals = (value, path) => {
|
|
1564
|
+
const item = record$1(value, path);
|
|
1565
|
+
fields(item, [
|
|
1566
|
+
"enums",
|
|
1567
|
+
"extensions",
|
|
1568
|
+
"functions",
|
|
1569
|
+
"tables"
|
|
1570
|
+
], path);
|
|
1571
|
+
if (Object.hasOwn(item, "enums")) array(item.enums, `${path}.enums`, databaseEnum);
|
|
1572
|
+
if (Object.hasOwn(item, "extensions")) array(item.extensions, `${path}.extensions`, extension);
|
|
1573
|
+
if (Object.hasOwn(item, "functions")) array(item.functions, `${path}.functions`, databaseFunction);
|
|
1574
|
+
if (Object.hasOwn(item, "tables")) array(item.tables, `${path}.tables`, table);
|
|
1575
|
+
};
|
|
552
1576
|
/** Validate external/JSON-restored models before core or a dialect consumes them. */
|
|
553
1577
|
const decodeDatabaseSchema = (value) => Effect.try({
|
|
554
1578
|
catch: (cause) => cause instanceof SchemaModelError ? cause : new SchemaModelError({
|
|
@@ -561,16 +1585,41 @@ const decodeDatabaseSchema = (value) => Effect.try({
|
|
|
561
1585
|
"namespaces",
|
|
562
1586
|
"tables",
|
|
563
1587
|
"views",
|
|
564
|
-
"triggers"
|
|
1588
|
+
"triggers",
|
|
1589
|
+
"sequences",
|
|
1590
|
+
"functions",
|
|
1591
|
+
"externals",
|
|
1592
|
+
"extensions",
|
|
1593
|
+
"enums"
|
|
565
1594
|
], "$database");
|
|
1595
|
+
if (Object.hasOwn(database, "enums")) array(database.enums, "$database.enums", databaseEnum);
|
|
1596
|
+
if (Object.hasOwn(database, "extensions")) array(database.extensions, "$database.extensions", extension);
|
|
1597
|
+
if (Object.hasOwn(database, "externals")) externals(database.externals, "$database.externals");
|
|
1598
|
+
if (Object.hasOwn(database, "functions")) array(database.functions, "$database.functions", databaseFunction);
|
|
566
1599
|
if (Object.hasOwn(database, "namespaces")) array(database.namespaces, "$database.namespaces", namespace);
|
|
567
1600
|
array(database.tables, "$database.tables", table);
|
|
568
1601
|
if (Object.hasOwn(database, "views")) array(database.views, "$database.views", view);
|
|
569
1602
|
if (Object.hasOwn(database, "triggers")) array(database.triggers, "$database.triggers", trigger);
|
|
1603
|
+
if (Object.hasOwn(database, "sequences")) array(database.sequences, "$database.sequences", sequence);
|
|
570
1604
|
return value;
|
|
571
1605
|
}
|
|
572
1606
|
});
|
|
573
1607
|
// src/executor.ts
|
|
1608
|
+
const omissionOrder = {
|
|
1609
|
+
dependent: 2,
|
|
1610
|
+
unmanaged: 1,
|
|
1611
|
+
unsupported: 0
|
|
1612
|
+
};
|
|
1613
|
+
const compareCodePoints = (left, right) => {
|
|
1614
|
+
const a = Array.from(left, (character) => character.codePointAt(0));
|
|
1615
|
+
const b = Array.from(right, (character) => character.codePointAt(0));
|
|
1616
|
+
const index = a.findIndex((point, position) => point !== b[position]);
|
|
1617
|
+
return index < 0 ? a.length - b.length : b[index] === void 0 ? 1 : a[index] - b[index];
|
|
1618
|
+
};
|
|
1619
|
+
/** One line per omission, ordered by category (unsupported, unmanaged, dependent), then name. */
|
|
1620
|
+
const formatExtractOmissions = (omitted) => [...omitted].sort((left, right) => omissionOrder[left.category] - omissionOrder[right.category] || compareCodePoints(left.name, right.name)).map(({ category, detail, kind, name }) => {
|
|
1621
|
+
return `${category} ${kind} ${name}${detail === void 0 ? "" : ` (${category === "dependent" ? "depends on " : ""}${detail})`}`;
|
|
1622
|
+
});
|
|
574
1623
|
const checksumPattern = /^[a-f0-9]{64}$/;
|
|
575
1624
|
/** The run, or `undefined` when IDs, baseline, checkpoint, SQL or checksums are invalid. */
|
|
576
1625
|
const migrationRun = (direction, migrations, checkpoint, sha256) => {
|
|
@@ -605,7 +1654,49 @@ const renamedColumnId = (source, target, column) => JSON.stringify([
|
|
|
605
1654
|
column
|
|
606
1655
|
]);
|
|
607
1656
|
const columnId = (change, column) => change._tag === "TableRenamed" ? renamedColumnId(change.before.identity, change.after.identity, column) : idFor(change.table, column);
|
|
1657
|
+
const enumId = (identity) => JSON.stringify(["enum", tableKey(identity)]);
|
|
1658
|
+
const valueId = (source, target, value) => JSON.stringify([
|
|
1659
|
+
"enum",
|
|
1660
|
+
tableKey(source),
|
|
1661
|
+
tableKey(target),
|
|
1662
|
+
value
|
|
1663
|
+
]);
|
|
1664
|
+
const enumLabel = (identity) => [
|
|
1665
|
+
identity.catalog,
|
|
1666
|
+
identity.schema,
|
|
1667
|
+
identity.name
|
|
1668
|
+
].filter((part) => part !== void 0).join(".");
|
|
1669
|
+
const quoted = (value) => `'${JSON.stringify(value).slice(1, -1)}'`;
|
|
1670
|
+
const missingFrom = (values, other) => {
|
|
1671
|
+
const present = new Set(other);
|
|
1672
|
+
return values.filter((value) => !present.has(value));
|
|
1673
|
+
};
|
|
1674
|
+
const enumQuestions = (change, diff) => {
|
|
1675
|
+
if ("enum" in change) {
|
|
1676
|
+
if (change._tag === "EnumAdded") return [];
|
|
1677
|
+
const identity = change.enum.identity;
|
|
1678
|
+
const candidates = diff.changes.flatMap((item) => item._tag === "EnumAdded" && item.enum.identity.catalog === identity.catalog && item.enum.identity.schema === identity.schema ? [item.enum.identity.name] : []);
|
|
1679
|
+
return candidates.length ? [{
|
|
1680
|
+
candidates,
|
|
1681
|
+
enum: identity,
|
|
1682
|
+
id: enumId(identity),
|
|
1683
|
+
question: `Drop type ${enumLabel(identity)}, or rename it to an added type?`,
|
|
1684
|
+
resolution: { action: "pending" }
|
|
1685
|
+
}] : [];
|
|
1686
|
+
}
|
|
1687
|
+
const { after, before } = change;
|
|
1688
|
+
const candidates = missingFrom(after.values, before.values);
|
|
1689
|
+
return candidates.length ? missingFrom(before.values, after.values).map((value) => ({
|
|
1690
|
+
candidates,
|
|
1691
|
+
enum: before.identity,
|
|
1692
|
+
id: valueId(before.identity, after.identity, value),
|
|
1693
|
+
question: `Rename value ${quoted(value)} of ${enumLabel(before.identity)} to an added value?`,
|
|
1694
|
+
resolution: { action: "pending" },
|
|
1695
|
+
value
|
|
1696
|
+
})) : [];
|
|
1697
|
+
};
|
|
608
1698
|
const questions = (diff) => diff.changes.flatMap((change) => {
|
|
1699
|
+
if (change._tag === "EnumAdded" || change._tag === "EnumRemoved" || change._tag === "EnumRenamed" || change._tag === "EnumChanged") return enumQuestions(change, diff);
|
|
609
1700
|
if (change._tag === "TableRemoved") {
|
|
610
1701
|
const candidates = diff.changes.flatMap((item) => item._tag === "TableAdded" && item.table.catalog === change.table.catalog && item.table.schema === change.table.schema ? [item.table.name] : []);
|
|
611
1702
|
return [{
|
|
@@ -645,23 +1736,26 @@ const readAnswers = (pending, inputs, allowPending) => Effect.gen(function* () {
|
|
|
645
1736
|
if (!record(input) || typeof input.id !== "string") return yield* fail("Each decision needs its original id.");
|
|
646
1737
|
const question = expected.get(input.id);
|
|
647
1738
|
if (!question || answers.has(input.id)) return yield* fail(`Unknown or duplicate decision: ${input.id}`);
|
|
648
|
-
if (!equal(input.table, question.table)
|
|
1739
|
+
if (!("enum" in question ? input.table === void 0 && input.column === void 0 && equal(input.enum, question.enum) && input.value === question.value : input.enum === void 0 && input.value === void 0 && equal(input.table, question.table) && input.column === question.column)) return yield* fail(`Decision identity changed: ${input.id}`);
|
|
649
1740
|
const resolution = input.resolution;
|
|
650
1741
|
if (allowPending && record(resolution) && resolution.action === "pending" && Object.keys(resolution).length === 1) {
|
|
651
1742
|
answers.set(input.id, question);
|
|
652
1743
|
continue;
|
|
653
1744
|
}
|
|
654
1745
|
if (!record(resolution) || resolution.action === "pending") return yield* fail(`Unresolved decision: ${question.question}`);
|
|
655
|
-
if ((resolution.action === "drop" || resolution.action === "drop_if_empty") && Object.keys(resolution).length === 1) answers.set(input.id, {
|
|
1746
|
+
if ((resolution.action === "drop" || resolution.action === "drop_if_empty" && !("enum" in question)) && Object.keys(resolution).length === 1) answers.set(input.id, {
|
|
656
1747
|
...question,
|
|
657
1748
|
resolution: { action: resolution.action }
|
|
658
1749
|
});
|
|
659
1750
|
else if (resolution.action === "rename" && Object.keys(resolution).length === 2 && typeof resolution.target === "string" && question.candidates.includes(resolution.target)) {
|
|
660
|
-
const target = question.
|
|
1751
|
+
const [kind, target] = "enum" in question ? question.value === void 0 ? ["enums", enumId({
|
|
1752
|
+
...question.enum,
|
|
1753
|
+
name: resolution.target
|
|
1754
|
+
})] : ["values", valueId(question.enum, question.enum, resolution.target)] : question.column === void 0 ? ["tables", idFor({
|
|
661
1755
|
...question.table,
|
|
662
1756
|
name: resolution.target
|
|
663
|
-
}) : idFor(question.table, resolution.target);
|
|
664
|
-
if (targets.has(target)) return yield* fail(`Multiple source ${
|
|
1757
|
+
})] : ["columns", idFor(question.table, resolution.target)];
|
|
1758
|
+
if (targets.has(target)) return yield* fail(`Multiple source ${kind} map to ${resolution.target}.`);
|
|
665
1759
|
targets.add(target);
|
|
666
1760
|
answers.set(input.id, {
|
|
667
1761
|
...question,
|
|
@@ -676,18 +1770,18 @@ const readAnswers = (pending, inputs, allowPending) => Effect.gen(function* () {
|
|
|
676
1770
|
return answers;
|
|
677
1771
|
});
|
|
678
1772
|
const expandRenames = (diff, answers) => {
|
|
679
|
-
const
|
|
1773
|
+
const tables = new Map(diff.changes.filter((item) => item._tag === "TableAdded").map((item) => [tableKey(item.table), item]));
|
|
1774
|
+
const enums = new Map(diff.changes.flatMap((item) => "enum" in item && item._tag === "EnumAdded" ? [[tableKey(item.enum.identity), item]] : []));
|
|
680
1775
|
const renames = [];
|
|
681
|
-
|
|
682
|
-
|
|
1776
|
+
const consumed = /* @__PURE__ */ new Set();
|
|
1777
|
+
for (const source of diff.changes) if (source._tag === "TableRemoved") {
|
|
683
1778
|
const choice = answers.get(idFor(source.table))?.resolution;
|
|
684
1779
|
if (choice?.action !== "rename") continue;
|
|
685
|
-
const
|
|
1780
|
+
const target = tables.get(tableKey({
|
|
686
1781
|
...source.table,
|
|
687
1782
|
name: choice.target
|
|
688
|
-
});
|
|
689
|
-
|
|
690
|
-
additions.delete(key);
|
|
1783
|
+
}));
|
|
1784
|
+
consumed.add(source).add(target);
|
|
691
1785
|
renames.push({
|
|
692
1786
|
_tag: "TableRenamed",
|
|
693
1787
|
after: target.definition,
|
|
@@ -695,15 +1789,41 @@ const expandRenames = (diff, answers) => {
|
|
|
695
1789
|
changes: compareTable(source.definition, target.definition),
|
|
696
1790
|
table: source.table
|
|
697
1791
|
});
|
|
1792
|
+
} else if (source._tag === "EnumRemoved") {
|
|
1793
|
+
const identity = source.enum.identity;
|
|
1794
|
+
const choice = answers.get(enumId(identity))?.resolution;
|
|
1795
|
+
if (choice?.action !== "rename") continue;
|
|
1796
|
+
const target = enums.get(tableKey({
|
|
1797
|
+
...identity,
|
|
1798
|
+
name: choice.target
|
|
1799
|
+
}));
|
|
1800
|
+
consumed.add(source).add(target);
|
|
1801
|
+
renames.push({
|
|
1802
|
+
_tag: "EnumRenamed",
|
|
1803
|
+
after: target.enum,
|
|
1804
|
+
before: source.enum,
|
|
1805
|
+
values: []
|
|
1806
|
+
});
|
|
698
1807
|
}
|
|
699
1808
|
return {
|
|
700
|
-
diff: { changes: [...diff.changes.filter((item) =>
|
|
701
|
-
if (item._tag === "TableAdded") return additions.has(tableKey(item.table));
|
|
702
|
-
return item._tag !== "TableRemoved" || answers.get(idFor(item.table))?.resolution.action !== "rename";
|
|
703
|
-
}), ...renames] },
|
|
1809
|
+
diff: { changes: [...diff.changes.filter((item) => !consumed.has(item)), ...renames] },
|
|
704
1810
|
nested: questions({ changes: renames })
|
|
705
1811
|
};
|
|
706
1812
|
};
|
|
1813
|
+
const issuedForPairing = (pending, input) => pending.decisions.some((parent) => {
|
|
1814
|
+
if ("enum" in parent) {
|
|
1815
|
+
const value = input.value;
|
|
1816
|
+
return parent.value === void 0 && typeof value === "string" && equal(parent.enum, input.enum) && parent.candidates.some((name) => input.id === valueId(parent.enum, {
|
|
1817
|
+
...parent.enum,
|
|
1818
|
+
name
|
|
1819
|
+
}, value));
|
|
1820
|
+
}
|
|
1821
|
+
const column = input.column;
|
|
1822
|
+
return parent.column === void 0 && typeof column === "string" && equal(parent.table, input.table) && parent.candidates.some((name) => input.id === renamedColumnId(parent.table, {
|
|
1823
|
+
...parent.table,
|
|
1824
|
+
name
|
|
1825
|
+
}, column));
|
|
1826
|
+
});
|
|
707
1827
|
const reviewAnswers = (pending, diff, inputs, merging) => Effect.gen(function* () {
|
|
708
1828
|
const rootIds = new Set(pending.decisions.map((item) => item.id));
|
|
709
1829
|
const roots = inputs.filter((input) => record(input) && typeof input.id === "string" && rootIds.has(input.id));
|
|
@@ -714,25 +1834,40 @@ const reviewAnswers = (pending, diff, inputs, merging) => Effect.gen(function* (
|
|
|
714
1834
|
decisions: [...rootAnswers.values(), ...expanded.nested]
|
|
715
1835
|
};
|
|
716
1836
|
const expected = new Set(review.decisions.map((item) => item.id));
|
|
717
|
-
const current = inputs.filter((input) =>
|
|
718
|
-
if (!merging || !record(input) || typeof input.column !== "string" || typeof input.id !== "string" || expected.has(input.id)) return true;
|
|
719
|
-
const column = input.column;
|
|
720
|
-
return !pending.decisions.some((parent) => parent.column === void 0 && equal(parent.table, input.table) && parent.candidates.some((name) => input.id === renamedColumnId(parent.table, {
|
|
721
|
-
...parent.table,
|
|
722
|
-
name
|
|
723
|
-
}, column)));
|
|
724
|
-
});
|
|
1837
|
+
const current = inputs.filter((input) => !merging || !record(input) || typeof input.id !== "string" || expected.has(input.id) || !issuedForPairing(pending, input));
|
|
725
1838
|
const ids = new Set(current.flatMap((input) => record(input) ? [input.id] : []));
|
|
726
1839
|
const missing = expanded.nested.filter((item) => !ids.has(item.id));
|
|
727
|
-
if (!merging && missing.length)
|
|
728
|
-
|
|
729
|
-
|
|
730
|
-
|
|
1840
|
+
if (!merging && missing.length) {
|
|
1841
|
+
const required = missing.some((item) => "enum" in item) ? "Enum rename requires value decisions" : "Table rename requires column decisions";
|
|
1842
|
+
return yield* Effect.fail(new MigrationDecisionError({
|
|
1843
|
+
message: `${required}. Run --merge-decisions to review them.`,
|
|
1844
|
+
pending: review
|
|
1845
|
+
}));
|
|
1846
|
+
}
|
|
731
1847
|
return {
|
|
732
1848
|
answers: yield* readAnswers(review, [...current, ...missing], merging),
|
|
733
1849
|
diff: expanded.diff
|
|
734
1850
|
};
|
|
735
1851
|
});
|
|
1852
|
+
/** The enum and value renames that the reviewer accepted. */
|
|
1853
|
+
const enumRenames = (diff, answers) => {
|
|
1854
|
+
const enums = /* @__PURE__ */ new Map();
|
|
1855
|
+
const values = /* @__PURE__ */ new Map();
|
|
1856
|
+
for (const change of diff.changes) {
|
|
1857
|
+
if (change._tag !== "EnumRenamed" && change._tag !== "EnumChanged") continue;
|
|
1858
|
+
const { after, before } = change;
|
|
1859
|
+
const key = tableKey(before.identity);
|
|
1860
|
+
if (change._tag === "EnumRenamed") enums.set(key, after.identity);
|
|
1861
|
+
values.set(key, new Map(before.values.flatMap((value) => {
|
|
1862
|
+
const choice = answers.get(valueId(before.identity, after.identity, value))?.resolution;
|
|
1863
|
+
return choice?.action === "rename" ? [[value, choice.target]] : [];
|
|
1864
|
+
})));
|
|
1865
|
+
}
|
|
1866
|
+
return {
|
|
1867
|
+
enums,
|
|
1868
|
+
values
|
|
1869
|
+
};
|
|
1870
|
+
};
|
|
736
1871
|
/** Refresh the questions and snapshots, carrying forward applicable reviewed choices. */
|
|
737
1872
|
const mergeMigrationDecisions = (fresh, previous) => Effect.gen(function* () {
|
|
738
1873
|
if (!record(previous) || previous.version !== 1 || !Array.isArray(previous.decisions)) return yield* Effect.fail(new MigrationDecisionError({
|
|
@@ -790,40 +1925,51 @@ const resolveMigrationDecisions = (before, after, supplied) => Effect.gen(functi
|
|
|
790
1925
|
const reviewed = yield* reviewAnswers(pending, diff, supplied.decisions, false);
|
|
791
1926
|
const answers = reviewed.answers;
|
|
792
1927
|
const dropGuards = [];
|
|
1928
|
+
const changes = reviewed.diff.changes.map((change) => {
|
|
1929
|
+
if (!("changes" in change)) {
|
|
1930
|
+
if (change._tag === "TableRemoved" && answers.get(idFor(change.table))?.resolution.action === "drop_if_empty") dropGuards.push({ table: change.table });
|
|
1931
|
+
return change;
|
|
1932
|
+
}
|
|
1933
|
+
const renamedTargets = /* @__PURE__ */ new Set();
|
|
1934
|
+
const changes = [];
|
|
1935
|
+
for (const item of change.changes) {
|
|
1936
|
+
if (item._tag !== "ColumnRemoved") continue;
|
|
1937
|
+
const answer = answers.get(columnId(change, item.column.key));
|
|
1938
|
+
if (answer.resolution.action === "drop_if_empty") dropGuards.push({
|
|
1939
|
+
column: item.column.name,
|
|
1940
|
+
table: change.table
|
|
1941
|
+
});
|
|
1942
|
+
if (answer.resolution.action !== "rename") continue;
|
|
1943
|
+
const targetKey = answer.resolution.target;
|
|
1944
|
+
const afterColumn = change.after.columns.find((column) => column.key === targetKey);
|
|
1945
|
+
renamedTargets.add(afterColumn.key);
|
|
1946
|
+
const fields = [];
|
|
1947
|
+
if (!equal(item.column.dataType, afterColumn.dataType)) fields.push("dataType");
|
|
1948
|
+
if (!constraintsEqual(item.column.constraints, afterColumn.constraints)) fields.push("constraints");
|
|
1949
|
+
changes.push({
|
|
1950
|
+
_tag: "ColumnRenamed",
|
|
1951
|
+
after: afterColumn,
|
|
1952
|
+
before: item.column,
|
|
1953
|
+
fields
|
|
1954
|
+
});
|
|
1955
|
+
}
|
|
1956
|
+
return {
|
|
1957
|
+
...change,
|
|
1958
|
+
changes: [...changes, ...change.changes.filter((item) => !(item._tag === "ColumnAdded" && renamedTargets.has(item.column.key)) && !(item._tag === "ColumnRemoved" && answers.get(columnId(change, item.column.key))?.resolution.action === "rename"))]
|
|
1959
|
+
};
|
|
1960
|
+
});
|
|
1961
|
+
const renames = sequenceRenames(changes);
|
|
1962
|
+
const kept = changes.filter((change) => !/^(Comment|Enum|Sequence|Trigger)/.test(change._tag));
|
|
1963
|
+
const leading = (change) => /^(Namespace|Extension)/.test(change._tag);
|
|
1964
|
+
const withRenames = [
|
|
1965
|
+
...kept.filter(leading),
|
|
1966
|
+
...diffEnums(before, after, enumRenames(reviewed.diff, answers)),
|
|
1967
|
+
...kept.filter((change) => !leading(change)),
|
|
1968
|
+
...diffTriggers(before, after, renames),
|
|
1969
|
+
...diffSequences(before, after, renames)
|
|
1970
|
+
];
|
|
793
1971
|
const resolved = {
|
|
794
|
-
changes:
|
|
795
|
-
if (!("changes" in change)) {
|
|
796
|
-
if (change._tag === "TableRemoved" && answers.get(idFor(change.table))?.resolution.action === "drop_if_empty") dropGuards.push({ table: change.table });
|
|
797
|
-
return change;
|
|
798
|
-
}
|
|
799
|
-
const renamedTargets = /* @__PURE__ */ new Set();
|
|
800
|
-
const changes = [];
|
|
801
|
-
for (const item of change.changes) {
|
|
802
|
-
if (item._tag !== "ColumnRemoved") continue;
|
|
803
|
-
const answer = answers.get(columnId(change, item.column.key));
|
|
804
|
-
if (answer.resolution.action === "drop_if_empty") dropGuards.push({
|
|
805
|
-
column: item.column.name,
|
|
806
|
-
table: change.table
|
|
807
|
-
});
|
|
808
|
-
if (answer.resolution.action !== "rename") continue;
|
|
809
|
-
const targetKey = answer.resolution.target;
|
|
810
|
-
const afterColumn = change.after.columns.find((column) => column.key === targetKey);
|
|
811
|
-
renamedTargets.add(afterColumn.key);
|
|
812
|
-
const fields = [];
|
|
813
|
-
if (!equal(item.column.dataType, afterColumn.dataType)) fields.push("dataType");
|
|
814
|
-
if (!equal(item.column.constraints, afterColumn.constraints)) fields.push("constraints");
|
|
815
|
-
changes.push({
|
|
816
|
-
_tag: "ColumnRenamed",
|
|
817
|
-
after: afterColumn,
|
|
818
|
-
before: item.column,
|
|
819
|
-
fields
|
|
820
|
-
});
|
|
821
|
-
}
|
|
822
|
-
return {
|
|
823
|
-
...change,
|
|
824
|
-
changes: [...changes, ...change.changes.filter((item) => !(item._tag === "ColumnAdded" && renamedTargets.has(item.column.key)) && !(item._tag === "ColumnRemoved" && answers.get(columnId(change, item.column.key))?.resolution.action === "rename"))]
|
|
825
|
-
};
|
|
826
|
-
}),
|
|
1972
|
+
changes: [...withRenames, ...diffComments(before, after, withRenames)],
|
|
827
1973
|
...dropGuards.length ? { dropGuards } : {}
|
|
828
1974
|
};
|
|
829
1975
|
return {
|
|
@@ -835,10 +1981,68 @@ const resolveMigrationDecisions = (before, after, supplied) => Effect.gen(functi
|
|
|
835
1981
|
};
|
|
836
1982
|
});
|
|
837
1983
|
// src/language-service.ts
|
|
1984
|
+
/**
|
|
1985
|
+
* Selects the completion candidates for `completion` from the analyses, in analysis then declaration order.
|
|
1986
|
+
* A declaration is a candidate when its `kind` is `expected` and it has `text`, and either its `scope` is the
|
|
1987
|
+
* completion's scope (inserting `text`), or the completion sets `qualify`, the declaration is a table with
|
|
1988
|
+
* `qualifiedText` and another scope (inserting `qualifiedText`). One candidate per key: the first one wins.
|
|
1989
|
+
*/
|
|
1990
|
+
const completionCandidates = (completion, analyses) => {
|
|
1991
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1992
|
+
const candidates = [];
|
|
1993
|
+
analyses.forEach((analysis, source) => {
|
|
1994
|
+
for (const declaration of analysis.declarations) {
|
|
1995
|
+
if (declaration.kind !== completion.expected || keys.has(declaration.key)) continue;
|
|
1996
|
+
let text;
|
|
1997
|
+
if (declaration.scope === completion.scope) text = declaration.text;
|
|
1998
|
+
else if (completion.qualify && declaration.kind === "table" && declaration.qualifiedText !== void 0) text = declaration.qualifiedText;
|
|
1999
|
+
if (text === void 0) continue;
|
|
2000
|
+
keys.add(declaration.key);
|
|
2001
|
+
candidates.push({
|
|
2002
|
+
key: declaration.key,
|
|
2003
|
+
kind: completion.expected,
|
|
2004
|
+
source,
|
|
2005
|
+
text
|
|
2006
|
+
});
|
|
2007
|
+
}
|
|
2008
|
+
});
|
|
2009
|
+
return candidates;
|
|
2010
|
+
};
|
|
838
2011
|
/** Returns the first reference whose half-open span contains `offset`. */
|
|
839
2012
|
const referenceAt = (analysis, offset) => analysis.references.find((reference) => reference.start <= offset && offset < reference.end);
|
|
2013
|
+
/** Returns the first declaration whose half-open selection contains `offset`. */
|
|
2014
|
+
const declarationAt = (analysis, offset) => analysis.declarations.find((declaration) => declaration.selection.start <= offset && offset < declaration.selection.end);
|
|
2015
|
+
/** Returns the spans of the references whose key equals `key`, then the declaration selections with that key when `includeDeclaration` is set. */
|
|
2016
|
+
const referencesTo = (key, analysis, includeDeclaration) => [...analysis.references.filter((reference) => reference.key === key).map(({ end, start }) => ({
|
|
2017
|
+
end,
|
|
2018
|
+
start
|
|
2019
|
+
})), ...includeDeclaration ? analysis.declarations.filter((declaration) => declaration.key === key).map((declaration) => declaration.selection) : []];
|
|
840
2020
|
/** Returns the selections of the declarations whose key equals the reference key, in declaration order. */
|
|
841
2021
|
const definitions = (reference, analysis) => analysis.declarations.filter((declaration) => declaration.key === reference.key).map((declaration) => declaration.selection);
|
|
2022
|
+
/** The span that rename replaces: `selection` when present, else the reference span. */
|
|
2023
|
+
const nameSpan = (reference) => reference.selection ?? {
|
|
2024
|
+
end: reference.end,
|
|
2025
|
+
start: reference.start
|
|
2026
|
+
};
|
|
2027
|
+
const contains = (span, offset) => span.start <= offset && offset < span.end;
|
|
2028
|
+
/** The declaration, reference or occurrence whose name span contains `offset`. */
|
|
2029
|
+
const renameSiteAt = (analysis, offset) => {
|
|
2030
|
+
const declaration = analysis.declarations.find((candidate) => contains(candidate.selection, offset));
|
|
2031
|
+
if (declaration) return {
|
|
2032
|
+
key: declaration.key,
|
|
2033
|
+
span: declaration.selection
|
|
2034
|
+
};
|
|
2035
|
+
const site = [...analysis.references, ...analysis.occurrences ?? []].find((reference) => contains(nameSpan(reference), offset));
|
|
2036
|
+
return site ? {
|
|
2037
|
+
key: site.key,
|
|
2038
|
+
span: nameSpan(site)
|
|
2039
|
+
} : void 0;
|
|
2040
|
+
};
|
|
2041
|
+
/** The name spans of every declaration, reference and occurrence with `key`, in document order. */
|
|
2042
|
+
const renameSpans = (key, analysis) => [...analysis.declarations.filter((declaration) => declaration.key === key).map((declaration) => declaration.selection), ...[...analysis.references, ...analysis.occurrences ?? []].filter((reference) => reference.key === key).map(nameSpan)].map(({ end, start }) => ({
|
|
2043
|
+
end,
|
|
2044
|
+
start
|
|
2045
|
+
})).sort((left, right) => left.start - right.start);
|
|
842
2046
|
// src/codegen.ts
|
|
843
2047
|
var CodegenError = class extends Data.TaggedError("CodegenError") {};
|
|
844
2048
|
const generatedFileHeader = "// Generated by UncutSQL. Do not edit.";
|
|
@@ -993,4 +2197,4 @@ const decodeGeneratorPlugin = (value) => validate(() => {
|
|
|
993
2197
|
})
|
|
994
2198
|
};
|
|
995
2199
|
}, "invalid-plugin", "generator");
|
|
996
|
-
export { CodegenError, MigrationDecisionError, SchemaDiffError, SchemaModelError, artifactPathSchema, codegenDiagnosticSchema, compareTable, continuesRun, decodeDatabaseSchema, decodeGenerationResult, decodeGeneratorPlugin, decodeMapperPlugin, definitions, diffDatabases, generatedFileHeader, mergeMigrationDecisions, migrationRun, namespaceKey, namespaceOf, nextMigration, planDatabaseMigration, planDownMigration, prepareMigrationDecisions, referenceAt, relationIdentitySchema, resolveMigrationDecisions, schemaValueEqual, tableKey, validateApplicationCompatibility, validateDatabase };
|
|
2200
|
+
export { CodegenError, MigrationDecisionError, SchemaDiffError, SchemaModelError, artifactPathSchema, checkManagedScope, codegenDiagnosticSchema, commentObjects, compareTable, completionCandidates, constraintsEqual, continuesRun, declarationAt, decodeDatabaseSchema, decodeGenerationResult, decodeGeneratorPlugin, decodeMapperPlugin, definitions, diffComments, diffDatabases, diffEnums, diffFunctions, diffSequences, diffTriggers, enumObjects, extensionObjects, externalObjects, formatExtractOmissions, functionKey, functionLabel, generatedFileHeader, mergeMigrationDecisions, migrationRun, nameSpan, namespaceKey, namespaceOf, nextMigration, planDatabaseMigration, planDownMigration, prepareMigrationDecisions, referenceAt, referencesTo, relationIdentitySchema, renameSiteAt, renameSpans, resolveMigrationDecisions, schemaValueEqual, sequenceRenames, tableKey, triggerKey, validateApplicationCompatibility, validateDatabase };
|