@uncutsql/core 0.2.0 → 0.3.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/dist/index.cjs +446 -48
- package/dist/index.d.cts +202 -2
- package/dist/index.d.ts +202 -2
- package/dist/index.js +434 -49
- package/package.json +2 -1
package/dist/index.js
CHANGED
|
@@ -5,6 +5,41 @@ const tableKey = (table) => JSON.stringify([
|
|
|
5
5
|
table.schema ?? null,
|
|
6
6
|
table.name
|
|
7
7
|
]);
|
|
8
|
+
const functionKey = (fn) => JSON.stringify([
|
|
9
|
+
fn.identity.catalog ?? null,
|
|
10
|
+
fn.identity.schema ?? null,
|
|
11
|
+
fn.identity.name,
|
|
12
|
+
fn.signature
|
|
13
|
+
]);
|
|
14
|
+
/** `schema.name(type, …)` for diagnostics; signature entries that are not strings print as JSON. */
|
|
15
|
+
const functionLabel = (fn) => `${[
|
|
16
|
+
fn.identity.catalog,
|
|
17
|
+
fn.identity.schema,
|
|
18
|
+
fn.identity.name
|
|
19
|
+
].filter((part) => part !== void 0).join(".")}(${fn.signature.map((item) => typeof item === "string" ? item : JSON.stringify(item)).join(", ")})`;
|
|
20
|
+
/** The external objects of the models, tables before functions, each listed once by its key. */
|
|
21
|
+
const externalObjects = (...databases) => {
|
|
22
|
+
const found = /* @__PURE__ */ new Map();
|
|
23
|
+
for (const { externals } of databases) {
|
|
24
|
+
for (const { identity } of externals?.tables ?? []) {
|
|
25
|
+
const name = [
|
|
26
|
+
identity.catalog,
|
|
27
|
+
identity.schema,
|
|
28
|
+
identity.name
|
|
29
|
+
].filter((part) => part !== void 0).join(".");
|
|
30
|
+
found.set(`table ${tableKey(identity)}`, {
|
|
31
|
+
identity,
|
|
32
|
+
label: `External table ${name}`
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
for (const fn of externals?.functions ?? []) found.set(`function ${functionKey(fn)}`, {
|
|
36
|
+
function: fn,
|
|
37
|
+
identity: fn.identity,
|
|
38
|
+
label: `External function ${functionLabel(fn)}`
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
return [...found.values()];
|
|
42
|
+
};
|
|
8
43
|
const namespaceKey = (namespace) => JSON.stringify([namespace.catalog ?? null, namespace.name]);
|
|
9
44
|
/** The namespace that contains an object, or undefined when its identity has no schema level. */
|
|
10
45
|
const namespaceOf = (identity) => identity.schema === void 0 ? void 0 : identity.catalog === void 0 ? { name: identity.schema } : {
|
|
@@ -31,26 +66,29 @@ const validateDatabase = (database, side) => {
|
|
|
31
66
|
const diagnostics = [];
|
|
32
67
|
const tables = /* @__PURE__ */ new Set();
|
|
33
68
|
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);
|
|
69
|
+
const duplicateColumns = (table) => {
|
|
43
70
|
const columns = /* @__PURE__ */ new Set();
|
|
44
71
|
for (const column of table.columns) {
|
|
45
72
|
if (columns.has(column.key)) diagnostics.push({
|
|
46
73
|
code: "DuplicateColumn",
|
|
47
74
|
column: column.key,
|
|
48
|
-
message: `Duplicate column ${JSON.stringify(column.key)} in table ${
|
|
75
|
+
message: `Duplicate column ${JSON.stringify(column.key)} in table ${tableKey(table.identity)}`,
|
|
49
76
|
side,
|
|
50
77
|
table: table.identity
|
|
51
78
|
});
|
|
52
79
|
columns.add(column.key);
|
|
53
80
|
}
|
|
81
|
+
};
|
|
82
|
+
for (const table of database.tables) {
|
|
83
|
+
const id = tableKey(table.identity);
|
|
84
|
+
if (tables.has(id)) diagnostics.push({
|
|
85
|
+
code: "DuplicateTable",
|
|
86
|
+
message: `Duplicate table ${id}`,
|
|
87
|
+
side,
|
|
88
|
+
table: table.identity
|
|
89
|
+
});
|
|
90
|
+
tables.add(id);
|
|
91
|
+
duplicateColumns(table);
|
|
54
92
|
}
|
|
55
93
|
for (const table of database.tables) for (const index of table.indexes ?? []) {
|
|
56
94
|
const id = JSON.stringify([tableKey(table.identity), index.key]);
|
|
@@ -90,6 +128,38 @@ const validateDatabase = (database, side) => {
|
|
|
90
128
|
});
|
|
91
129
|
triggers.add(id);
|
|
92
130
|
}
|
|
131
|
+
const sequences = /* @__PURE__ */ new Set();
|
|
132
|
+
for (const sequence of database.sequences ?? []) {
|
|
133
|
+
const id = tableKey(sequence.identity);
|
|
134
|
+
if (sequences.has(id)) diagnostics.push({
|
|
135
|
+
code: "DuplicateSequence",
|
|
136
|
+
message: `Duplicate sequence ${id}`,
|
|
137
|
+
side,
|
|
138
|
+
table: sequence.identity
|
|
139
|
+
});
|
|
140
|
+
sequences.add(id);
|
|
141
|
+
const owner = sequence.owner;
|
|
142
|
+
if (owner) {
|
|
143
|
+
if (!database.tables.find((table) => tableKey(table.identity) === tableKey(owner.table))?.columns.some((column) => column.key === owner.column)) diagnostics.push({
|
|
144
|
+
code: "UndeclaredSequenceOwner",
|
|
145
|
+
column: owner.column,
|
|
146
|
+
message: `Sequence ${id} is owned by ${tableKey(owner.table)} column ${JSON.stringify(owner.column)}, which is not in the model`,
|
|
147
|
+
side,
|
|
148
|
+
table: sequence.identity
|
|
149
|
+
});
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
const functions = /* @__PURE__ */ new Set();
|
|
153
|
+
for (const fn of database.functions ?? []) {
|
|
154
|
+
const id = functionKey(fn);
|
|
155
|
+
if (functions.has(id)) diagnostics.push({
|
|
156
|
+
code: "DuplicateFunction",
|
|
157
|
+
message: `Duplicate function ${id}`,
|
|
158
|
+
side,
|
|
159
|
+
table: fn.identity
|
|
160
|
+
});
|
|
161
|
+
functions.add(id);
|
|
162
|
+
}
|
|
93
163
|
const namespaces = /* @__PURE__ */ new Set();
|
|
94
164
|
for (const namespace of database.namespaces ?? []) {
|
|
95
165
|
const id = namespaceKey(namespace.identity);
|
|
@@ -101,6 +171,33 @@ const validateDatabase = (database, side) => {
|
|
|
101
171
|
});
|
|
102
172
|
namespaces.add(id);
|
|
103
173
|
}
|
|
174
|
+
const externals = /* @__PURE__ */ new Set();
|
|
175
|
+
const external = (key, identity, label, statement) => {
|
|
176
|
+
if (externals.has(key)) diagnostics.push({
|
|
177
|
+
code: "DuplicateExternal",
|
|
178
|
+
message: `Duplicate ${label}`,
|
|
179
|
+
side,
|
|
180
|
+
table: identity
|
|
181
|
+
});
|
|
182
|
+
externals.add(key);
|
|
183
|
+
const namespace = namespaceOf(identity);
|
|
184
|
+
if (namespace && namespaces.has(namespaceKey(namespace))) diagnostics.push({
|
|
185
|
+
code: "ExternalInManagedNamespace",
|
|
186
|
+
message: `External ${label} is in managed schema ${namespace.name}; declare it with ${statement}, or move it out of the managed scope`,
|
|
187
|
+
side,
|
|
188
|
+
table: identity
|
|
189
|
+
});
|
|
190
|
+
};
|
|
191
|
+
for (const table of database.externals?.tables ?? []) {
|
|
192
|
+
const { catalog, name, schema } = table.identity;
|
|
193
|
+
external(`table ${tableKey(table.identity)}`, table.identity, `table ${[
|
|
194
|
+
catalog,
|
|
195
|
+
schema,
|
|
196
|
+
name
|
|
197
|
+
].filter((part) => part !== void 0).join(".")}`, "CREATE TABLE");
|
|
198
|
+
duplicateColumns(table);
|
|
199
|
+
}
|
|
200
|
+
for (const fn of database.externals?.functions ?? []) external(`function ${functionKey(fn)}`, fn.identity, `function ${functionLabel(fn)}`, "CREATE FUNCTION");
|
|
104
201
|
if (database.namespaces) {
|
|
105
202
|
const undeclared = (identity, owner, role) => {
|
|
106
203
|
const namespace = namespaceOf(identity);
|
|
@@ -117,9 +214,30 @@ const validateDatabase = (database, side) => {
|
|
|
117
214
|
undeclared(trigger.identity, trigger.identity, "Trigger");
|
|
118
215
|
undeclared(trigger.target, trigger.identity, "Target of trigger");
|
|
119
216
|
}
|
|
217
|
+
for (const sequence of database.sequences ?? []) {
|
|
218
|
+
undeclared(sequence.identity, sequence.identity, "Sequence");
|
|
219
|
+
if (sequence.owner) undeclared(sequence.owner.table, sequence.identity, "Owner table of sequence");
|
|
220
|
+
}
|
|
221
|
+
for (const fn of database.functions ?? []) undeclared(fn.identity, fn.identity, "Function");
|
|
120
222
|
}
|
|
121
223
|
return diagnostics;
|
|
122
224
|
};
|
|
225
|
+
/**
|
|
226
|
+
* Checks an explicit list of managed schemas against the namespaces of a model built from schema
|
|
227
|
+
* files. Names are compared as catalog names, case-sensitively. The shape of the list (non-empty,
|
|
228
|
+
* unique, including `public`) is the caller's to validate.
|
|
229
|
+
*/
|
|
230
|
+
const checkManagedScope = (model, managedSchemas) => {
|
|
231
|
+
const managed = new Set(managedSchemas);
|
|
232
|
+
const created = new Set((model.namespaces ?? []).map((namespace) => namespace.identity.name));
|
|
233
|
+
const diagnostic = (code, name, message) => ({
|
|
234
|
+
code,
|
|
235
|
+
message,
|
|
236
|
+
side: "after",
|
|
237
|
+
table: { name }
|
|
238
|
+
});
|
|
239
|
+
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`))];
|
|
240
|
+
};
|
|
123
241
|
const constraintsEqual = (before, after) => before.length === after.length && before.every((item, index) => item.kind === after[index].kind && schemaValueEqual(item.properties, after[index].properties));
|
|
124
242
|
const compareTable = (before, after) => {
|
|
125
243
|
const changes = [];
|
|
@@ -179,7 +297,7 @@ const compareTable = (before, after) => {
|
|
|
179
297
|
_tag: "IndexAdded",
|
|
180
298
|
index
|
|
181
299
|
});
|
|
182
|
-
else if (old.unique !== index.unique || !schemaValueEqual(old.terms, index.terms) || !schemaValueEqual(old.where, index.where)) changes.push({
|
|
300
|
+
else if (old.unique !== index.unique || !schemaValueEqual(old.terms, index.terms) || !schemaValueEqual(old.where, index.where) || !schemaValueEqual(old.options ?? {}, index.options ?? {})) changes.push({
|
|
183
301
|
_tag: "IndexChanged",
|
|
184
302
|
after: index,
|
|
185
303
|
before: old
|
|
@@ -187,6 +305,119 @@ const compareTable = (before, after) => {
|
|
|
187
305
|
}
|
|
188
306
|
return changes;
|
|
189
307
|
};
|
|
308
|
+
/** Collect the table and column renames that a list of changes carries. */
|
|
309
|
+
const sequenceRenames = (changes) => {
|
|
310
|
+
const tables = /* @__PURE__ */ new Map();
|
|
311
|
+
const columns = /* @__PURE__ */ new Map();
|
|
312
|
+
for (const change of changes) {
|
|
313
|
+
if (change._tag !== "TableChanged" && change._tag !== "TableRenamed") continue;
|
|
314
|
+
const key = tableKey(change.before.identity);
|
|
315
|
+
if (change._tag === "TableRenamed") tables.set(key, change.after.identity);
|
|
316
|
+
for (const item of change.changes) if (item._tag === "ColumnRenamed") {
|
|
317
|
+
const renamed = columns.get(key) ?? /* @__PURE__ */ new Map();
|
|
318
|
+
renamed.set(item.before.key, item.after.key);
|
|
319
|
+
columns.set(key, renamed);
|
|
320
|
+
}
|
|
321
|
+
}
|
|
322
|
+
return {
|
|
323
|
+
columns,
|
|
324
|
+
tables
|
|
325
|
+
};
|
|
326
|
+
};
|
|
327
|
+
/** The owner of a `before` sequence expressed in the `after` model, or its key when unowned. */
|
|
328
|
+
const ownerAfterRenames = (sequence, renames) => {
|
|
329
|
+
const owner = sequence.owner;
|
|
330
|
+
if (!owner) return;
|
|
331
|
+
const source = tableKey(owner.table);
|
|
332
|
+
return {
|
|
333
|
+
column: renames.columns.get(source)?.get(owner.column) ?? owner.column,
|
|
334
|
+
table: renames.tables.get(source) ?? owner.table
|
|
335
|
+
};
|
|
336
|
+
};
|
|
337
|
+
const ownerKey = (owner) => owner ? JSON.stringify([tableKey(owner.table), owner.column]) : null;
|
|
338
|
+
/** Function changes. A changed signature is another key: a removal and an addition. */
|
|
339
|
+
const diffFunctions = (before, after) => {
|
|
340
|
+
const changes = [];
|
|
341
|
+
const oldFunctions = new Map((before.functions ?? []).map((item) => [functionKey(item), item]));
|
|
342
|
+
const newFunctions = new Map((after.functions ?? []).map((item) => [functionKey(item), item]));
|
|
343
|
+
for (const [key, fn] of oldFunctions) if (!newFunctions.has(key)) changes.push({
|
|
344
|
+
_tag: "FunctionRemoved",
|
|
345
|
+
function: fn
|
|
346
|
+
});
|
|
347
|
+
for (const [key, fn] of newFunctions) {
|
|
348
|
+
const old = oldFunctions.get(key);
|
|
349
|
+
if (!old) changes.push({
|
|
350
|
+
_tag: "FunctionAdded",
|
|
351
|
+
function: fn
|
|
352
|
+
});
|
|
353
|
+
else if (!schemaValueEqual(old.definition, fn.definition)) changes.push({
|
|
354
|
+
_tag: "FunctionChanged",
|
|
355
|
+
after: fn,
|
|
356
|
+
before: old
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
return changes;
|
|
360
|
+
};
|
|
361
|
+
/** Sequence changes. `renames` are the table and column renames the migration applies. */
|
|
362
|
+
const diffSequences = (before, after, renames = {
|
|
363
|
+
columns: /* @__PURE__ */ new Map(),
|
|
364
|
+
tables: /* @__PURE__ */ new Map()
|
|
365
|
+
}) => {
|
|
366
|
+
const changes = [];
|
|
367
|
+
const oldSequences = new Map((before.sequences ?? []).map((item) => [tableKey(item.identity), item]));
|
|
368
|
+
const newSequences = new Map((after.sequences ?? []).map((item) => [tableKey(item.identity), item]));
|
|
369
|
+
const removed = [];
|
|
370
|
+
const added = [];
|
|
371
|
+
for (const [key, sequence] of oldSequences) if (!newSequences.has(key)) removed.push(sequence);
|
|
372
|
+
for (const [key, sequence] of newSequences) {
|
|
373
|
+
const old = oldSequences.get(key);
|
|
374
|
+
if (!old) added.push(sequence);
|
|
375
|
+
else if (!schemaValueEqual(old.definition, sequence.definition) || ownerKey(ownerAfterRenames(old, renames)) !== ownerKey(sequence.owner)) changes.push({
|
|
376
|
+
_tag: "SequenceChanged",
|
|
377
|
+
after: sequence,
|
|
378
|
+
before: old
|
|
379
|
+
});
|
|
380
|
+
}
|
|
381
|
+
const groupKey = (sequence, owner) => JSON.stringify([namespaceOf(sequence.identity) ?? null, owner]);
|
|
382
|
+
const groups = /* @__PURE__ */ new Map();
|
|
383
|
+
const group = (key) => {
|
|
384
|
+
const found = groups.get(key) ?? {
|
|
385
|
+
added: [],
|
|
386
|
+
removed: []
|
|
387
|
+
};
|
|
388
|
+
groups.set(key, found);
|
|
389
|
+
return found;
|
|
390
|
+
};
|
|
391
|
+
for (const sequence of removed) {
|
|
392
|
+
const owner = ownerKey(ownerAfterRenames(sequence, renames));
|
|
393
|
+
if (owner !== null) group(groupKey(sequence, owner)).removed.push(sequence);
|
|
394
|
+
}
|
|
395
|
+
for (const sequence of added) {
|
|
396
|
+
const owner = ownerKey(sequence.owner);
|
|
397
|
+
if (owner !== null) group(groupKey(sequence, owner)).added.push(sequence);
|
|
398
|
+
}
|
|
399
|
+
const paired = /* @__PURE__ */ new Set();
|
|
400
|
+
for (const found of groups.values()) if (found.removed.length === 1 && found.added.length === 1) {
|
|
401
|
+
const [old] = found.removed;
|
|
402
|
+
const [next] = found.added;
|
|
403
|
+
paired.add(old);
|
|
404
|
+
paired.add(next);
|
|
405
|
+
changes.push({
|
|
406
|
+
_tag: "SequenceRenamed",
|
|
407
|
+
after: next,
|
|
408
|
+
before: old
|
|
409
|
+
});
|
|
410
|
+
}
|
|
411
|
+
for (const sequence of removed) if (!paired.has(sequence)) changes.push({
|
|
412
|
+
_tag: "SequenceRemoved",
|
|
413
|
+
sequence
|
|
414
|
+
});
|
|
415
|
+
for (const sequence of added) if (!paired.has(sequence)) changes.push({
|
|
416
|
+
_tag: "SequenceAdded",
|
|
417
|
+
sequence
|
|
418
|
+
});
|
|
419
|
+
return changes;
|
|
420
|
+
};
|
|
190
421
|
/** Compare normalized relational models without parsing SQL or consulting a dialect. */
|
|
191
422
|
const diffDatabases = (before, after) => Effect.gen(function* () {
|
|
192
423
|
const diagnostics = [...validateDatabase(before, "before"), ...validateDatabase(after, "after")];
|
|
@@ -247,7 +478,7 @@ const diffDatabases = (before, after) => Effect.gen(function* () {
|
|
|
247
478
|
_tag: "ViewAdded",
|
|
248
479
|
view
|
|
249
480
|
});
|
|
250
|
-
else if (!schemaValueEqual(old.columns ?? null, view.columns ?? null) || !schemaValueEqual(old.query, view.query)) changes.push({
|
|
481
|
+
else if (!schemaValueEqual(old.columns ?? null, view.columns ?? null) || !schemaValueEqual(old.query, view.query) || !schemaValueEqual(old.options ?? {}, view.options ?? {})) changes.push({
|
|
251
482
|
_tag: "ViewChanged",
|
|
252
483
|
after: view,
|
|
253
484
|
before: old
|
|
@@ -271,6 +502,8 @@ const diffDatabases = (before, after) => Effect.gen(function* () {
|
|
|
271
502
|
before: old
|
|
272
503
|
});
|
|
273
504
|
}
|
|
505
|
+
changes.push(...diffSequences(before, after));
|
|
506
|
+
changes.push(...diffFunctions(before, after));
|
|
274
507
|
return { changes };
|
|
275
508
|
});
|
|
276
509
|
// src/migration.ts
|
|
@@ -289,6 +522,31 @@ const planDatabaseMigration = (diff) => ({
|
|
|
289
522
|
after: change.after,
|
|
290
523
|
before: change.before
|
|
291
524
|
};
|
|
525
|
+
case "FunctionAdded":
|
|
526
|
+
case "FunctionRemoved": return {
|
|
527
|
+
_tag: change._tag === "FunctionAdded" ? "CreateFunction" : "DropFunction",
|
|
528
|
+
function: change.function
|
|
529
|
+
};
|
|
530
|
+
case "FunctionChanged": return {
|
|
531
|
+
_tag: "ReplaceFunction",
|
|
532
|
+
after: change.after,
|
|
533
|
+
before: change.before
|
|
534
|
+
};
|
|
535
|
+
case "SequenceAdded":
|
|
536
|
+
case "SequenceRemoved": return {
|
|
537
|
+
_tag: change._tag === "SequenceAdded" ? "CreateSequence" : "DropSequence",
|
|
538
|
+
sequence: change.sequence
|
|
539
|
+
};
|
|
540
|
+
case "SequenceChanged": return {
|
|
541
|
+
_tag: "AlterSequence",
|
|
542
|
+
after: change.after,
|
|
543
|
+
before: change.before
|
|
544
|
+
};
|
|
545
|
+
case "SequenceRenamed": return {
|
|
546
|
+
_tag: "RenameSequence",
|
|
547
|
+
after: change.after,
|
|
548
|
+
before: change.before
|
|
549
|
+
};
|
|
292
550
|
case "TriggerAdded":
|
|
293
551
|
case "TriggerRemoved": return {
|
|
294
552
|
_tag: change._tag === "TriggerAdded" ? "CreateTrigger" : "DropTrigger",
|
|
@@ -340,6 +598,27 @@ const planDownMigration = (up) => {
|
|
|
340
598
|
_tag: "DropTable"
|
|
341
599
|
});
|
|
342
600
|
break;
|
|
601
|
+
case "CreateFunction":
|
|
602
|
+
case "DropFunction":
|
|
603
|
+
procedures.push({
|
|
604
|
+
...step,
|
|
605
|
+
_tag: step._tag === "CreateFunction" ? "DropFunction" : "CreateFunction"
|
|
606
|
+
});
|
|
607
|
+
break;
|
|
608
|
+
case "ReplaceFunction":
|
|
609
|
+
procedures.push(swap(step));
|
|
610
|
+
break;
|
|
611
|
+
case "CreateSequence":
|
|
612
|
+
case "DropSequence":
|
|
613
|
+
procedures.push({
|
|
614
|
+
...step,
|
|
615
|
+
_tag: step._tag === "CreateSequence" ? "DropSequence" : "CreateSequence"
|
|
616
|
+
});
|
|
617
|
+
break;
|
|
618
|
+
case "AlterSequence":
|
|
619
|
+
case "RenameSequence":
|
|
620
|
+
procedures.push(swap(step));
|
|
621
|
+
break;
|
|
343
622
|
case "CreateTrigger":
|
|
344
623
|
case "DropTrigger":
|
|
345
624
|
procedures.push({
|
|
@@ -474,10 +753,12 @@ const index = (value, path) => {
|
|
|
474
753
|
"name",
|
|
475
754
|
"unique",
|
|
476
755
|
"terms",
|
|
477
|
-
"where"
|
|
756
|
+
"where",
|
|
757
|
+
"options"
|
|
478
758
|
], path);
|
|
479
759
|
text(item.key, `${path}.key`);
|
|
480
760
|
text(item.name, `${path}.name`);
|
|
761
|
+
if (Object.hasOwn(item, "options")) properties(item.options, `${path}.options`);
|
|
481
762
|
if (typeof item.unique !== "boolean") fail(`${path}.unique`, "a boolean");
|
|
482
763
|
array(item.terms, `${path}.terms`, semanticValue);
|
|
483
764
|
semanticValue(item.where, `${path}.where`);
|
|
@@ -505,17 +786,50 @@ const trigger = (value, path) => {
|
|
|
505
786
|
identity(item.target, `${path}.target`);
|
|
506
787
|
semanticValue(item.definition, `${path}.definition`);
|
|
507
788
|
};
|
|
789
|
+
const sequence = (value, path) => {
|
|
790
|
+
const item = record$1(value, path);
|
|
791
|
+
fields(item, [
|
|
792
|
+
"identity",
|
|
793
|
+
"name",
|
|
794
|
+
"definition",
|
|
795
|
+
"owner"
|
|
796
|
+
], path);
|
|
797
|
+
identity(item.identity, `${path}.identity`);
|
|
798
|
+
text(item.name, `${path}.name`);
|
|
799
|
+
properties(item.definition, `${path}.definition`);
|
|
800
|
+
if (Object.hasOwn(item, "owner")) {
|
|
801
|
+
const owner = record$1(item.owner, `${path}.owner`);
|
|
802
|
+
fields(owner, ["column", "table"], `${path}.owner`);
|
|
803
|
+
text(owner.column, `${path}.owner.column`);
|
|
804
|
+
identity(owner.table, `${path}.owner.table`);
|
|
805
|
+
}
|
|
806
|
+
};
|
|
807
|
+
const databaseFunction = (value, path) => {
|
|
808
|
+
const item = record$1(value, path);
|
|
809
|
+
fields(item, [
|
|
810
|
+
"identity",
|
|
811
|
+
"name",
|
|
812
|
+
"signature",
|
|
813
|
+
"definition"
|
|
814
|
+
], path);
|
|
815
|
+
identity(item.identity, `${path}.identity`);
|
|
816
|
+
text(item.name, `${path}.name`);
|
|
817
|
+
array(item.signature, `${path}.signature`, semanticValue);
|
|
818
|
+
properties(item.definition, `${path}.definition`);
|
|
819
|
+
};
|
|
508
820
|
const view = (value, path) => {
|
|
509
821
|
const item = record$1(value, path);
|
|
510
822
|
fields(item, [
|
|
511
823
|
"identity",
|
|
512
824
|
"name",
|
|
513
825
|
"columns",
|
|
514
|
-
"query"
|
|
826
|
+
"query",
|
|
827
|
+
"options"
|
|
515
828
|
], path);
|
|
516
829
|
identity(item.identity, `${path}.identity`);
|
|
517
830
|
text(item.name, `${path}.name`);
|
|
518
831
|
if (Object.hasOwn(item, "columns")) array(item.columns, `${path}.columns`, text);
|
|
832
|
+
if (Object.hasOwn(item, "options")) properties(item.options, `${path}.options`);
|
|
519
833
|
semanticValue(item.query, `${path}.query`);
|
|
520
834
|
};
|
|
521
835
|
const namespace = (value, path) => {
|
|
@@ -549,6 +863,12 @@ const table = (value, path) => {
|
|
|
549
863
|
array(item.constraints, `${path}.constraints`, constraint);
|
|
550
864
|
properties(item.options, `${path}.options`);
|
|
551
865
|
};
|
|
866
|
+
const externals = (value, path) => {
|
|
867
|
+
const item = record$1(value, path);
|
|
868
|
+
fields(item, ["functions", "tables"], path);
|
|
869
|
+
if (Object.hasOwn(item, "functions")) array(item.functions, `${path}.functions`, databaseFunction);
|
|
870
|
+
if (Object.hasOwn(item, "tables")) array(item.tables, `${path}.tables`, table);
|
|
871
|
+
};
|
|
552
872
|
/** Validate external/JSON-restored models before core or a dialect consumes them. */
|
|
553
873
|
const decodeDatabaseSchema = (value) => Effect.try({
|
|
554
874
|
catch: (cause) => cause instanceof SchemaModelError ? cause : new SchemaModelError({
|
|
@@ -561,12 +881,18 @@ const decodeDatabaseSchema = (value) => Effect.try({
|
|
|
561
881
|
"namespaces",
|
|
562
882
|
"tables",
|
|
563
883
|
"views",
|
|
564
|
-
"triggers"
|
|
884
|
+
"triggers",
|
|
885
|
+
"sequences",
|
|
886
|
+
"functions",
|
|
887
|
+
"externals"
|
|
565
888
|
], "$database");
|
|
889
|
+
if (Object.hasOwn(database, "externals")) externals(database.externals, "$database.externals");
|
|
890
|
+
if (Object.hasOwn(database, "functions")) array(database.functions, "$database.functions", databaseFunction);
|
|
566
891
|
if (Object.hasOwn(database, "namespaces")) array(database.namespaces, "$database.namespaces", namespace);
|
|
567
892
|
array(database.tables, "$database.tables", table);
|
|
568
893
|
if (Object.hasOwn(database, "views")) array(database.views, "$database.views", view);
|
|
569
894
|
if (Object.hasOwn(database, "triggers")) array(database.triggers, "$database.triggers", trigger);
|
|
895
|
+
if (Object.hasOwn(database, "sequences")) array(database.sequences, "$database.sequences", sequence);
|
|
570
896
|
return value;
|
|
571
897
|
}
|
|
572
898
|
});
|
|
@@ -790,40 +1116,41 @@ const resolveMigrationDecisions = (before, after, supplied) => Effect.gen(functi
|
|
|
790
1116
|
const reviewed = yield* reviewAnswers(pending, diff, supplied.decisions, false);
|
|
791
1117
|
const answers = reviewed.answers;
|
|
792
1118
|
const dropGuards = [];
|
|
1119
|
+
const changes = reviewed.diff.changes.map((change) => {
|
|
1120
|
+
if (!("changes" in change)) {
|
|
1121
|
+
if (change._tag === "TableRemoved" && answers.get(idFor(change.table))?.resolution.action === "drop_if_empty") dropGuards.push({ table: change.table });
|
|
1122
|
+
return change;
|
|
1123
|
+
}
|
|
1124
|
+
const renamedTargets = /* @__PURE__ */ new Set();
|
|
1125
|
+
const changes = [];
|
|
1126
|
+
for (const item of change.changes) {
|
|
1127
|
+
if (item._tag !== "ColumnRemoved") continue;
|
|
1128
|
+
const answer = answers.get(columnId(change, item.column.key));
|
|
1129
|
+
if (answer.resolution.action === "drop_if_empty") dropGuards.push({
|
|
1130
|
+
column: item.column.name,
|
|
1131
|
+
table: change.table
|
|
1132
|
+
});
|
|
1133
|
+
if (answer.resolution.action !== "rename") continue;
|
|
1134
|
+
const targetKey = answer.resolution.target;
|
|
1135
|
+
const afterColumn = change.after.columns.find((column) => column.key === targetKey);
|
|
1136
|
+
renamedTargets.add(afterColumn.key);
|
|
1137
|
+
const fields = [];
|
|
1138
|
+
if (!equal(item.column.dataType, afterColumn.dataType)) fields.push("dataType");
|
|
1139
|
+
if (!equal(item.column.constraints, afterColumn.constraints)) fields.push("constraints");
|
|
1140
|
+
changes.push({
|
|
1141
|
+
_tag: "ColumnRenamed",
|
|
1142
|
+
after: afterColumn,
|
|
1143
|
+
before: item.column,
|
|
1144
|
+
fields
|
|
1145
|
+
});
|
|
1146
|
+
}
|
|
1147
|
+
return {
|
|
1148
|
+
...change,
|
|
1149
|
+
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"))]
|
|
1150
|
+
};
|
|
1151
|
+
});
|
|
793
1152
|
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
|
-
}),
|
|
1153
|
+
changes: [...changes.filter((change) => !change._tag.startsWith("Sequence")), ...diffSequences(before, after, sequenceRenames(changes))],
|
|
827
1154
|
...dropGuards.length ? { dropGuards } : {}
|
|
828
1155
|
};
|
|
829
1156
|
return {
|
|
@@ -835,10 +1162,68 @@ const resolveMigrationDecisions = (before, after, supplied) => Effect.gen(functi
|
|
|
835
1162
|
};
|
|
836
1163
|
});
|
|
837
1164
|
// src/language-service.ts
|
|
1165
|
+
/**
|
|
1166
|
+
* Selects the completion candidates for `completion` from the analyses, in analysis then declaration order.
|
|
1167
|
+
* A declaration is a candidate when its `kind` is `expected` and it has `text`, and either its `scope` is the
|
|
1168
|
+
* completion's scope (inserting `text`), or the completion sets `qualify`, the declaration is a table with
|
|
1169
|
+
* `qualifiedText` and another scope (inserting `qualifiedText`). One candidate per key: the first one wins.
|
|
1170
|
+
*/
|
|
1171
|
+
const completionCandidates = (completion, analyses) => {
|
|
1172
|
+
const keys = /* @__PURE__ */ new Set();
|
|
1173
|
+
const candidates = [];
|
|
1174
|
+
analyses.forEach((analysis, source) => {
|
|
1175
|
+
for (const declaration of analysis.declarations) {
|
|
1176
|
+
if (declaration.kind !== completion.expected || keys.has(declaration.key)) continue;
|
|
1177
|
+
let text;
|
|
1178
|
+
if (declaration.scope === completion.scope) text = declaration.text;
|
|
1179
|
+
else if (completion.qualify && declaration.kind === "table" && declaration.qualifiedText !== void 0) text = declaration.qualifiedText;
|
|
1180
|
+
if (text === void 0) continue;
|
|
1181
|
+
keys.add(declaration.key);
|
|
1182
|
+
candidates.push({
|
|
1183
|
+
key: declaration.key,
|
|
1184
|
+
kind: completion.expected,
|
|
1185
|
+
source,
|
|
1186
|
+
text
|
|
1187
|
+
});
|
|
1188
|
+
}
|
|
1189
|
+
});
|
|
1190
|
+
return candidates;
|
|
1191
|
+
};
|
|
838
1192
|
/** Returns the first reference whose half-open span contains `offset`. */
|
|
839
1193
|
const referenceAt = (analysis, offset) => analysis.references.find((reference) => reference.start <= offset && offset < reference.end);
|
|
1194
|
+
/** Returns the first declaration whose half-open selection contains `offset`. */
|
|
1195
|
+
const declarationAt = (analysis, offset) => analysis.declarations.find((declaration) => declaration.selection.start <= offset && offset < declaration.selection.end);
|
|
1196
|
+
/** Returns the spans of the references whose key equals `key`, then the declaration selections with that key when `includeDeclaration` is set. */
|
|
1197
|
+
const referencesTo = (key, analysis, includeDeclaration) => [...analysis.references.filter((reference) => reference.key === key).map(({ end, start }) => ({
|
|
1198
|
+
end,
|
|
1199
|
+
start
|
|
1200
|
+
})), ...includeDeclaration ? analysis.declarations.filter((declaration) => declaration.key === key).map((declaration) => declaration.selection) : []];
|
|
840
1201
|
/** Returns the selections of the declarations whose key equals the reference key, in declaration order. */
|
|
841
1202
|
const definitions = (reference, analysis) => analysis.declarations.filter((declaration) => declaration.key === reference.key).map((declaration) => declaration.selection);
|
|
1203
|
+
/** The span that rename replaces: `selection` when present, else the reference span. */
|
|
1204
|
+
const nameSpan = (reference) => reference.selection ?? {
|
|
1205
|
+
end: reference.end,
|
|
1206
|
+
start: reference.start
|
|
1207
|
+
};
|
|
1208
|
+
const contains = (span, offset) => span.start <= offset && offset < span.end;
|
|
1209
|
+
/** The declaration, reference or occurrence whose name span contains `offset`. */
|
|
1210
|
+
const renameSiteAt = (analysis, offset) => {
|
|
1211
|
+
const declaration = analysis.declarations.find((candidate) => contains(candidate.selection, offset));
|
|
1212
|
+
if (declaration) return {
|
|
1213
|
+
key: declaration.key,
|
|
1214
|
+
span: declaration.selection
|
|
1215
|
+
};
|
|
1216
|
+
const site = [...analysis.references, ...analysis.occurrences ?? []].find((reference) => contains(nameSpan(reference), offset));
|
|
1217
|
+
return site ? {
|
|
1218
|
+
key: site.key,
|
|
1219
|
+
span: nameSpan(site)
|
|
1220
|
+
} : void 0;
|
|
1221
|
+
};
|
|
1222
|
+
/** The name spans of every declaration, reference and occurrence with `key`, in document order. */
|
|
1223
|
+
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 }) => ({
|
|
1224
|
+
end,
|
|
1225
|
+
start
|
|
1226
|
+
})).sort((left, right) => left.start - right.start);
|
|
842
1227
|
// src/codegen.ts
|
|
843
1228
|
var CodegenError = class extends Data.TaggedError("CodegenError") {};
|
|
844
1229
|
const generatedFileHeader = "// Generated by UncutSQL. Do not edit.";
|
|
@@ -993,4 +1378,4 @@ const decodeGeneratorPlugin = (value) => validate(() => {
|
|
|
993
1378
|
})
|
|
994
1379
|
};
|
|
995
1380
|
}, "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 };
|
|
1381
|
+
export { CodegenError, MigrationDecisionError, SchemaDiffError, SchemaModelError, artifactPathSchema, checkManagedScope, codegenDiagnosticSchema, compareTable, completionCandidates, continuesRun, declarationAt, decodeDatabaseSchema, decodeGenerationResult, decodeGeneratorPlugin, decodeMapperPlugin, definitions, diffDatabases, diffFunctions, diffSequences, externalObjects, functionKey, functionLabel, generatedFileHeader, mergeMigrationDecisions, migrationRun, nameSpan, namespaceKey, namespaceOf, nextMigration, planDatabaseMigration, planDownMigration, prepareMigrationDecisions, referenceAt, referencesTo, relationIdentitySchema, renameSiteAt, renameSpans, resolveMigrationDecisions, schemaValueEqual, sequenceRenames, tableKey, validateApplicationCompatibility, validateDatabase };
|