okmodel 0.0.0 → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +44 -1
- package/dist/adapters/capabilities.d.ts +62 -0
- package/dist/adapters/error.d.ts +91 -0
- package/dist/adapters/failure.d.ts +39 -0
- package/dist/adapters/pg/batch.d.ts +36 -0
- package/dist/adapters/pg/call.d.ts +59 -0
- package/dist/adapters/pg/pglite.d.ts +21 -0
- package/dist/adapters/pg/postgres-extra.d.ts +43 -0
- package/dist/adapters/pg/postgresjs.d.ts +95 -0
- package/dist/adapters/pg/result.d.ts +30 -0
- package/dist/contracts/catalog/build.d.ts +18 -0
- package/dist/contracts/catalog/document.d.ts +79 -0
- package/dist/contracts/catalog/enum.d.ts +35 -0
- package/dist/contracts/catalog/identifier.d.ts +71 -0
- package/dist/contracts/catalog/identity.d.ts +98 -0
- package/dist/contracts/catalog/index.d.ts +11 -0
- package/dist/contracts/catalog/json.d.ts +17 -0
- package/dist/contracts/catalog/object.d.ts +142 -0
- package/dist/contracts/catalog/order.d.ts +43 -0
- package/dist/contracts/catalog/rewrite.d.ts +18 -0
- package/dist/contracts/catalog/types.d.ts +292 -0
- package/dist/contracts/catalog/words.d.ts +13 -0
- package/dist/contracts/connection.d.ts +33 -0
- package/dist/contracts/driver.d.ts +163 -0
- package/dist/contracts/error.d.ts +298 -0
- package/dist/contracts/index.d.ts +11 -0
- package/dist/contracts/index.js +16 -0
- package/dist/contracts/internal.d.ts +15 -0
- package/dist/contracts/internal.js +68 -0
- package/dist/contracts/nearest.d.ts +15 -0
- package/dist/contracts/rows.d.ts +84 -0
- package/dist/contracts/sha256.d.ts +14 -0
- package/dist/contracts/utf8.d.ts +33 -0
- package/dist/dialects/pg/array-literal.d.ts +25 -0
- package/dist/dialects/pg/bool.d.ts +46 -0
- package/dist/dialects/pg/column.d.ts +386 -0
- package/dist/dialects/pg/compile.d.ts +81 -0
- package/dist/dialects/pg/custom.d.ts +21 -0
- package/dist/dialects/pg/ddl.d.ts +70 -0
- package/dist/dialects/pg/decimal.d.ts +62 -0
- package/dist/dialects/pg/declarations.d.ts +34 -0
- package/dist/dialects/pg/emit.d.ts +18 -0
- package/dist/dialects/pg/enum.d.ts +30 -0
- package/dist/dialects/pg/errors.d.ts +30 -0
- package/dist/dialects/pg/finite.d.ts +15 -0
- package/dist/dialects/pg/geometry.d.ts +55 -0
- package/dist/dialects/pg/index.d.ts +45 -0
- package/dist/dialects/pg/index.js +1821 -0
- package/dist/dialects/pg/integer.d.ts +96 -0
- package/dist/dialects/pg/introspect.d.ts +43 -0
- package/dist/dialects/pg/json.d.ts +43 -0
- package/dist/dialects/pg/keys.d.ts +32 -0
- package/dist/dialects/pg/misuse.d.ts +31 -0
- package/dist/dialects/pg/model.d.ts +71 -0
- package/dist/dialects/pg/namespace.d.ts +63 -0
- package/dist/dialects/pg/network.d.ts +28 -0
- package/dist/dialects/pg/operators.d.ts +71 -0
- package/dist/dialects/pg/ops/between.d.ts +10 -0
- package/dist/dialects/pg/ops/contains.d.ts +8 -0
- package/dist/dialects/pg/ops/endsWith.d.ts +8 -0
- package/dist/dialects/pg/ops/eq.d.ts +9 -0
- package/dist/dialects/pg/ops/every.d.ts +9 -0
- package/dist/dialects/pg/ops/gt.d.ts +9 -0
- package/dist/dialects/pg/ops/gte.d.ts +9 -0
- package/dist/dialects/pg/ops/has.d.ts +9 -0
- package/dist/dialects/pg/ops/ilike.d.ts +8 -0
- package/dist/dialects/pg/ops/inList.d.ts +9 -0
- package/dist/dialects/pg/ops/inc.d.ts +13 -0
- package/dist/dialects/pg/ops/like.d.ts +8 -0
- package/dist/dialects/pg/ops/lt.d.ts +9 -0
- package/dist/dialects/pg/ops/lte.d.ts +9 -0
- package/dist/dialects/pg/ops/none.d.ts +9 -0
- package/dist/dialects/pg/ops/not.d.ts +9 -0
- package/dist/dialects/pg/ops/notIn.d.ts +9 -0
- package/dist/dialects/pg/ops/or.d.ts +9 -0
- package/dist/dialects/pg/ops/startsWith.d.ts +8 -0
- package/dist/dialects/pg/quote.d.ts +24 -0
- package/dist/dialects/pg/range.d.ts +50 -0
- package/dist/dialects/pg/relations.d.ts +45 -0
- package/dist/dialects/pg/schema.d.ts +77 -0
- package/dist/dialects/pg/search.d.ts +23 -0
- package/dist/dialects/pg/table.d.ts +201 -0
- package/dist/dialects/pg/temporal.d.ts +211 -0
- package/dist/dialects/pg/text.d.ts +44 -0
- package/dist/dialects/pg/time.d.ts +46 -0
- package/dist/okm.js +2 -0
- package/dist/okmodel.js +2 -0
- package/dist/runtime/client.d.ts +47 -0
- package/dist/runtime/conflict.d.ts +38 -0
- package/dist/runtime/drift-detail.d.ts +32 -0
- package/dist/runtime/drift.d.ts +29 -0
- package/dist/runtime/include.d.ts +15 -0
- package/dist/runtime/pg/pglite.d.ts +25 -0
- package/dist/runtime/pg/pglite.js +48 -0
- package/dist/runtime/pg/postgresjs.d.ts +29 -0
- package/dist/runtime/pg/postgresjs.js +62 -0
- package/dist/runtime/plan.d.ts +167 -0
- package/dist/runtime/tx.d.ts +29 -0
- package/dist/runtime/types.d.ts +369 -0
- package/dist/runtime/write.d.ts +48 -0
- package/dist/shared/4j72449d.js +37 -0
- package/dist/shared/79ggp945.js +148 -0
- package/dist/shared/7ck0dtry.js +259 -0
- package/dist/shared/8b04d28c.js +14 -0
- package/dist/shared/ax16zpvm.js +171 -0
- package/dist/shared/ckb1p0a4.js +566 -0
- package/dist/shared/cn0n68ft.js +16 -0
- package/dist/shared/dynq16vb.js +81 -0
- package/dist/shared/f5thc1s0.js +1050 -0
- package/dist/shared/fgqgsb5z.js +281 -0
- package/dist/shared/g2v6gnc8.js +347 -0
- package/dist/shared/g5fhqmwe.js +188 -0
- package/dist/shared/gk7cs7sp.js +566 -0
- package/dist/shared/hd293j9g.js +98 -0
- package/dist/shared/j5cn2963.js +701 -0
- package/dist/shared/kynx7vbm.js +1490 -0
- package/dist/shared/mkjmqjv4.js +456 -0
- package/dist/shared/mtkwv8wx.js +18 -0
- package/dist/shared/ngk8gj61.js +40 -0
- package/dist/shared/p0cbr3g2.js +79 -0
- package/dist/shared/px90pf9p.js +396 -0
- package/dist/shared/qk3hxfja.js +83 -0
- package/dist/shared/tb4ze1tr.js +119 -0
- package/dist/shared/vkm3q4x1.js +30 -0
- package/dist/shared/w8samvk1.js +18 -0
- package/dist/shared/xy79j5hq.js +177 -0
- package/dist/shared/y9m15dfv.js +888 -0
- package/dist/shared/ycwz42az.js +412 -0
- package/dist/shared/zhc5vmgf.js +257 -0
- package/dist/tooling/cli.d.ts +1 -0
- package/dist/tooling/cli.js +193 -0
- package/dist/tooling/errors/registry.d.ts +33 -0
- package/dist/tooling/migrate/apply.d.ts +86 -0
- package/dist/tooling/migrate/commands.d.ts +21 -0
- package/dist/tooling/migrate/config.d.ts +48 -0
- package/dist/tooling/migrate/dev.d.ts +16 -0
- package/dist/tooling/migrate/equal.d.ts +15 -0
- package/dist/tooling/migrate/files.d.ts +19 -0
- package/dist/tooling/migrate/index.d.ts +7 -0
- package/dist/tooling/migrate/index.js +7 -0
- package/dist/tooling/migrate/plan.d.ts +76 -0
- package/dist/tooling/migrate/policy.d.ts +78 -0
- package/dist/tooling/migrate/project.d.ts +69 -0
- package/dist/tooling/migrate/status.d.ts +74 -0
- package/dist/tooling/migrate/values.d.ts +29 -0
- package/package.json +94 -7
- package/dist/index.js +0 -0
|
@@ -0,0 +1,888 @@
|
|
|
1
|
+
import {
|
|
2
|
+
dependencyOrder,
|
|
3
|
+
catalog,
|
|
4
|
+
enumType
|
|
5
|
+
} from "./g5fhqmwe.js";
|
|
6
|
+
import {
|
|
7
|
+
OWNERS2,
|
|
8
|
+
BUILT_KINDS2,
|
|
9
|
+
referentialAction,
|
|
10
|
+
CATALOG_VERSION2,
|
|
11
|
+
canonicalJson,
|
|
12
|
+
staticNamespace2,
|
|
13
|
+
templateNamespace2,
|
|
14
|
+
identityKey2,
|
|
15
|
+
identityLabel2,
|
|
16
|
+
sameRef2,
|
|
17
|
+
sameNamespace2,
|
|
18
|
+
identityToJson,
|
|
19
|
+
namespaceToJson,
|
|
20
|
+
table3,
|
|
21
|
+
column2,
|
|
22
|
+
index3,
|
|
23
|
+
constraint2,
|
|
24
|
+
sequence2,
|
|
25
|
+
compareText
|
|
26
|
+
} from "./j5cn2963.js";
|
|
27
|
+
import {
|
|
28
|
+
sha2562
|
|
29
|
+
} from "./mkjmqjv4.js";
|
|
30
|
+
import {
|
|
31
|
+
catalogError2
|
|
32
|
+
} from "./ycwz42az.js";
|
|
33
|
+
|
|
34
|
+
// src/contracts/catalog/rewrite.ts
|
|
35
|
+
function replaceIdentifier(text, from, to) {
|
|
36
|
+
if (from.length === 0 || from === to || !text.includes(from)) {
|
|
37
|
+
return text;
|
|
38
|
+
}
|
|
39
|
+
let out = "";
|
|
40
|
+
let index = 0;
|
|
41
|
+
while (index < text.length) {
|
|
42
|
+
const char = text[index] ?? "";
|
|
43
|
+
if (char === "'") {
|
|
44
|
+
const end = endOfLiteral(text, index, "'");
|
|
45
|
+
out += text.slice(index, end);
|
|
46
|
+
index = end;
|
|
47
|
+
continue;
|
|
48
|
+
}
|
|
49
|
+
if (char === '"') {
|
|
50
|
+
const end = endOfLiteral(text, index, '"');
|
|
51
|
+
const body = text.slice(index + 1, end - 1).replaceAll('""', '"');
|
|
52
|
+
out += body === from ? `"${to.replaceAll('"', '""')}"` : text.slice(index, end);
|
|
53
|
+
index = end;
|
|
54
|
+
continue;
|
|
55
|
+
}
|
|
56
|
+
if (isIdentStart(char)) {
|
|
57
|
+
let end = index + 1;
|
|
58
|
+
while (end < text.length && isIdentPart(text[end] ?? ""))
|
|
59
|
+
end += 1;
|
|
60
|
+
const word = text.slice(index, end);
|
|
61
|
+
out += word === from ? to : word;
|
|
62
|
+
index = end;
|
|
63
|
+
continue;
|
|
64
|
+
}
|
|
65
|
+
out += char;
|
|
66
|
+
index += 1;
|
|
67
|
+
}
|
|
68
|
+
return out;
|
|
69
|
+
}
|
|
70
|
+
function endOfLiteral(text, start, quote) {
|
|
71
|
+
let index = start + 1;
|
|
72
|
+
while (index < text.length) {
|
|
73
|
+
if (text[index] === quote && text[index + 1] === quote) {
|
|
74
|
+
index += 2;
|
|
75
|
+
continue;
|
|
76
|
+
}
|
|
77
|
+
if (text[index] === quote)
|
|
78
|
+
return index + 1;
|
|
79
|
+
index += 1;
|
|
80
|
+
}
|
|
81
|
+
return text.length;
|
|
82
|
+
}
|
|
83
|
+
function isIdentStart(char) {
|
|
84
|
+
return char >= "A" && char <= "Z" || char >= "a" && char <= "z" || char === "_";
|
|
85
|
+
}
|
|
86
|
+
function isIdentPart(char) {
|
|
87
|
+
return isIdentStart(char) || char >= "0" && char <= "9";
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
// src/contracts/catalog/document.ts
|
|
91
|
+
function creationOrder2(source) {
|
|
92
|
+
return dependencyOrder(source.objects);
|
|
93
|
+
}
|
|
94
|
+
function renameColumn2(source, change) {
|
|
95
|
+
const fromIdentity = {
|
|
96
|
+
kind: "column",
|
|
97
|
+
parent: change.parent,
|
|
98
|
+
name: change.from
|
|
99
|
+
};
|
|
100
|
+
const toIdentity = {
|
|
101
|
+
kind: "column",
|
|
102
|
+
parent: change.parent,
|
|
103
|
+
name: change.to
|
|
104
|
+
};
|
|
105
|
+
const fromKey = identityKey2(fromIdentity);
|
|
106
|
+
const found = source.objects.some((object) => identityKey2(object.identity) === fromKey);
|
|
107
|
+
if (!found) {
|
|
108
|
+
catalogError2("OKM1020", `Column ${change.from} is not in the catalog.`);
|
|
109
|
+
}
|
|
110
|
+
const next = source.objects.map((object) => rewriteRenamed(object, change, fromKey, toIdentity));
|
|
111
|
+
return catalog(next);
|
|
112
|
+
}
|
|
113
|
+
function renameTable2(source, change) {
|
|
114
|
+
const fromIdentity = {
|
|
115
|
+
kind: "table",
|
|
116
|
+
namespace: change.namespace,
|
|
117
|
+
name: change.from
|
|
118
|
+
};
|
|
119
|
+
const toIdentity = {
|
|
120
|
+
kind: "table",
|
|
121
|
+
namespace: change.namespace,
|
|
122
|
+
name: change.to
|
|
123
|
+
};
|
|
124
|
+
const fromKey = identityKey2(fromIdentity);
|
|
125
|
+
const found = source.objects.some((object) => identityKey2(object.identity) === fromKey);
|
|
126
|
+
if (!found) {
|
|
127
|
+
catalogError2("OKM1020", `Table ${change.from} is not in the catalog.`);
|
|
128
|
+
}
|
|
129
|
+
const next = source.objects.map((object) => rewriteTableName(object, change, fromKey, toIdentity));
|
|
130
|
+
return catalog(next);
|
|
131
|
+
}
|
|
132
|
+
var serializedText = new WeakMap;
|
|
133
|
+
var serializedHash = new WeakMap;
|
|
134
|
+
function serializeCatalog2(source) {
|
|
135
|
+
const cached = serializedText.get(source);
|
|
136
|
+
if (cached !== undefined)
|
|
137
|
+
return cached;
|
|
138
|
+
const indexed = source.objects.map((object) => ({
|
|
139
|
+
object,
|
|
140
|
+
key: identityKey2(object.identity)
|
|
141
|
+
}));
|
|
142
|
+
indexed.sort((left, right) => compareText(left.key, right.key));
|
|
143
|
+
const text = canonicalJson({
|
|
144
|
+
objects: indexed.map((item) => objectToJson(item.object)),
|
|
145
|
+
version: source.version
|
|
146
|
+
});
|
|
147
|
+
serializedText.set(source, text);
|
|
148
|
+
return text;
|
|
149
|
+
}
|
|
150
|
+
function catalogHash2(source) {
|
|
151
|
+
const cached = serializedHash.get(source);
|
|
152
|
+
if (cached !== undefined)
|
|
153
|
+
return cached;
|
|
154
|
+
const hash = sha2562(serializeCatalog2(source));
|
|
155
|
+
serializedHash.set(source, hash);
|
|
156
|
+
return hash;
|
|
157
|
+
}
|
|
158
|
+
function loadTrustedCatalog2(text, hash) {
|
|
159
|
+
if (sha2562(text) !== hash) {
|
|
160
|
+
catalogError2("OKM1027", "Catalog hash does not match the build artifact.");
|
|
161
|
+
}
|
|
162
|
+
let parsed;
|
|
163
|
+
try {
|
|
164
|
+
parsed = JSON.parse(text);
|
|
165
|
+
} catch {
|
|
166
|
+
catalogError2("OKM1027", "Catalog text is not valid JSON.");
|
|
167
|
+
}
|
|
168
|
+
if (!isPlainRecord(parsed) || !Array.isArray(parsed.objects)) {
|
|
169
|
+
catalogError2("OKM1027", "Catalog document is not an object.");
|
|
170
|
+
}
|
|
171
|
+
if (parsed.version !== CATALOG_VERSION2) {
|
|
172
|
+
catalogError2("OKM1027", `Catalog version ${String(parsed.version)} cannot be read. This build reads version ${String(CATALOG_VERSION2)}.`);
|
|
173
|
+
}
|
|
174
|
+
const loaded = {
|
|
175
|
+
version: CATALOG_VERSION2,
|
|
176
|
+
objects: parsed.objects
|
|
177
|
+
};
|
|
178
|
+
serializedText.set(loaded, text);
|
|
179
|
+
serializedHash.set(loaded, hash);
|
|
180
|
+
return loaded;
|
|
181
|
+
}
|
|
182
|
+
function parseCatalog2(text) {
|
|
183
|
+
let parsed;
|
|
184
|
+
try {
|
|
185
|
+
parsed = JSON.parse(text);
|
|
186
|
+
} catch {
|
|
187
|
+
catalogError2("OKM1027", "Catalog text is not valid JSON.");
|
|
188
|
+
}
|
|
189
|
+
if (!isPlainRecord(parsed)) {
|
|
190
|
+
catalogError2("OKM1027", "Catalog document is not an object.");
|
|
191
|
+
}
|
|
192
|
+
for (const key of Object.keys(parsed)) {
|
|
193
|
+
if (key !== "objects" && key !== "version") {
|
|
194
|
+
catalogError2("OKM1027", `Catalog has unexpected field ${key}.`);
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
if (parsed.version !== CATALOG_VERSION2) {
|
|
198
|
+
catalogError2("OKM1027", `Catalog version ${String(parsed.version)} cannot be read. This build reads version ${String(CATALOG_VERSION2)}.`);
|
|
199
|
+
}
|
|
200
|
+
if (!Array.isArray(parsed.objects)) {
|
|
201
|
+
catalogError2("OKM1027", "Catalog field objects must be an array.");
|
|
202
|
+
}
|
|
203
|
+
return catalog(parsed.objects.map((item) => parseObject(item)));
|
|
204
|
+
}
|
|
205
|
+
function rewriteRenamed(object, change, fromKey, toIdentity) {
|
|
206
|
+
const dependencies = retarget(object.dependencies, fromKey, toIdentity);
|
|
207
|
+
switch (object.kind) {
|
|
208
|
+
case "table":
|
|
209
|
+
return rewriteTable(object, change, dependencies);
|
|
210
|
+
case "column":
|
|
211
|
+
return rewriteColumn(object, change, dependencies);
|
|
212
|
+
case "index":
|
|
213
|
+
return rewriteIndex(object, change, dependencies);
|
|
214
|
+
case "constraint":
|
|
215
|
+
return rewriteConstraint(object, change, dependencies);
|
|
216
|
+
case "sequence":
|
|
217
|
+
return rewriteSequence(object, dependencies);
|
|
218
|
+
case "type":
|
|
219
|
+
return rewriteType(object, dependencies);
|
|
220
|
+
default:
|
|
221
|
+
return assertNever(object);
|
|
222
|
+
}
|
|
223
|
+
}
|
|
224
|
+
function rewriteTable(object, change, dependencies) {
|
|
225
|
+
const partition = object.definition.partition;
|
|
226
|
+
const renamed = partition !== undefined && object.identity.name === change.parent.name && sameNamespace2(object.identity.namespace, change.parent.namespace);
|
|
227
|
+
return table3({
|
|
228
|
+
namespace: object.identity.namespace,
|
|
229
|
+
name: object.identity.name,
|
|
230
|
+
owner: object.owner,
|
|
231
|
+
provenance: object.provenance,
|
|
232
|
+
dependencies,
|
|
233
|
+
...partition === undefined ? {} : {
|
|
234
|
+
partition: {
|
|
235
|
+
method: partition.method,
|
|
236
|
+
columns: renamed ? partition.columns.map((name) => name === change.from ? change.to : name) : [...partition.columns]
|
|
237
|
+
}
|
|
238
|
+
}
|
|
239
|
+
});
|
|
240
|
+
}
|
|
241
|
+
function rewriteColumn(object, change, dependencies) {
|
|
242
|
+
const renamed = sameRef2(object.identity.parent, change.parent) && object.identity.name === change.from;
|
|
243
|
+
const definition = object.definition;
|
|
244
|
+
const local = sameRef2(object.identity.parent, change.parent);
|
|
245
|
+
return column2({
|
|
246
|
+
parent: object.identity.parent,
|
|
247
|
+
name: renamed ? change.to : object.identity.name,
|
|
248
|
+
dataType: definition.dataType,
|
|
249
|
+
nullable: definition.nullable,
|
|
250
|
+
owner: object.owner,
|
|
251
|
+
provenance: object.provenance,
|
|
252
|
+
dependencies,
|
|
253
|
+
...definition.defaultExpression !== undefined ? { defaultExpression: rewriteExpr(definition.defaultExpression, change, local) } : {},
|
|
254
|
+
...definition.identity !== undefined ? { identity: definition.identity } : {},
|
|
255
|
+
...definition.generated !== undefined ? {
|
|
256
|
+
generated: {
|
|
257
|
+
stored: definition.generated.stored,
|
|
258
|
+
expression: rewriteExpr(definition.generated.expression, change, local)
|
|
259
|
+
}
|
|
260
|
+
} : {}
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
function rewriteIndex(object, change, dependencies) {
|
|
264
|
+
const local = sameRef2(object.identity.parent, change.parent);
|
|
265
|
+
const definition = object.definition;
|
|
266
|
+
return index3({
|
|
267
|
+
parent: object.identity.parent,
|
|
268
|
+
name: object.identity.name,
|
|
269
|
+
nameKey: definition.nameKey,
|
|
270
|
+
columns: local ? renameList(definition.columns, change.from, change.to) : [...definition.columns],
|
|
271
|
+
unique: definition.unique,
|
|
272
|
+
owner: object.owner,
|
|
273
|
+
provenance: object.provenance,
|
|
274
|
+
dependencies,
|
|
275
|
+
...definition.predicate !== undefined ? { predicate: rewriteExpr(definition.predicate, change, local) } : {},
|
|
276
|
+
...definition.expression !== undefined ? { expression: rewriteExpr(definition.expression, change, local) } : {}
|
|
277
|
+
});
|
|
278
|
+
}
|
|
279
|
+
function rewriteConstraint(object, change, dependencies) {
|
|
280
|
+
const definition = object.definition;
|
|
281
|
+
const local = sameRef2(object.identity.parent, change.parent);
|
|
282
|
+
const references = definition.references;
|
|
283
|
+
const referenced = references !== undefined && sameRef2(references.parent, change.parent) ? {
|
|
284
|
+
parent: references.parent,
|
|
285
|
+
columns: renameList(references.columns, change.from, change.to),
|
|
286
|
+
...references.onDelete !== undefined ? { onDelete: references.onDelete } : {},
|
|
287
|
+
...references.onUpdate !== undefined ? { onUpdate: references.onUpdate } : {}
|
|
288
|
+
} : references;
|
|
289
|
+
return constraint2({
|
|
290
|
+
parent: object.identity.parent,
|
|
291
|
+
constraintKind: definition.constraintKind,
|
|
292
|
+
name: object.identity.name,
|
|
293
|
+
nameKey: definition.nameKey,
|
|
294
|
+
columns: local ? renameList(definition.columns, change.from, change.to) : [...definition.columns],
|
|
295
|
+
deferrable: definition.deferrable,
|
|
296
|
+
initially: definition.initially,
|
|
297
|
+
nullsNotDistinct: definition.nullsNotDistinct,
|
|
298
|
+
owner: object.owner,
|
|
299
|
+
provenance: object.provenance,
|
|
300
|
+
dependencies,
|
|
301
|
+
...definition.expression !== undefined ? { expression: rewriteExpr(definition.expression, change, local) } : {},
|
|
302
|
+
...referenced !== undefined ? { references: referenced } : {}
|
|
303
|
+
});
|
|
304
|
+
}
|
|
305
|
+
function rewriteExpr(text, change, active) {
|
|
306
|
+
if (!active)
|
|
307
|
+
return text;
|
|
308
|
+
return replaceIdentifier(text, change.from, change.to);
|
|
309
|
+
}
|
|
310
|
+
function rewriteTableName(object, change, fromKey, toIdentity) {
|
|
311
|
+
const dependencies = retarget(object.dependencies, fromKey, toIdentity);
|
|
312
|
+
const named = object.kind === "table" && object.identity.name === change.from && sameNamespace2(object.identity.namespace, change.namespace);
|
|
313
|
+
if (object.kind === "table") {
|
|
314
|
+
return table3({
|
|
315
|
+
namespace: object.identity.namespace,
|
|
316
|
+
name: named ? change.to : object.identity.name,
|
|
317
|
+
owner: object.owner,
|
|
318
|
+
provenance: object.provenance,
|
|
319
|
+
dependencies,
|
|
320
|
+
...object.definition.partition === undefined ? {} : { partition: object.definition.partition }
|
|
321
|
+
});
|
|
322
|
+
}
|
|
323
|
+
if (object.kind === "column" || object.kind === "index" || object.kind === "constraint") {
|
|
324
|
+
const parent = movedParent(object.identity.parent, change);
|
|
325
|
+
const local = parent.name !== object.identity.parent.name;
|
|
326
|
+
if (object.kind === "column") {
|
|
327
|
+
return column2({
|
|
328
|
+
parent,
|
|
329
|
+
name: object.identity.name,
|
|
330
|
+
dataType: object.definition.dataType,
|
|
331
|
+
nullable: object.definition.nullable,
|
|
332
|
+
owner: object.owner,
|
|
333
|
+
provenance: object.provenance,
|
|
334
|
+
dependencies,
|
|
335
|
+
...object.definition.defaultExpression !== undefined ? {
|
|
336
|
+
defaultExpression: rewriteExpr(object.definition.defaultExpression, { from: change.from, to: change.to }, local)
|
|
337
|
+
} : {},
|
|
338
|
+
...object.definition.identity !== undefined ? { identity: object.definition.identity } : {},
|
|
339
|
+
...object.definition.generated !== undefined ? { generated: object.definition.generated } : {}
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
if (object.kind === "index") {
|
|
343
|
+
return index3({
|
|
344
|
+
parent,
|
|
345
|
+
name: object.identity.name,
|
|
346
|
+
nameKey: object.definition.nameKey,
|
|
347
|
+
columns: [...object.definition.columns],
|
|
348
|
+
unique: object.definition.unique,
|
|
349
|
+
owner: object.owner,
|
|
350
|
+
provenance: object.provenance,
|
|
351
|
+
dependencies,
|
|
352
|
+
...object.definition.predicate !== undefined ? {
|
|
353
|
+
predicate: rewriteExpr(object.definition.predicate, { from: change.from, to: change.to }, local)
|
|
354
|
+
} : {},
|
|
355
|
+
...object.definition.expression !== undefined ? {
|
|
356
|
+
expression: rewriteExpr(object.definition.expression, { from: change.from, to: change.to }, local)
|
|
357
|
+
} : {}
|
|
358
|
+
});
|
|
359
|
+
}
|
|
360
|
+
const references = object.definition.references;
|
|
361
|
+
const referenced = references === undefined ? undefined : {
|
|
362
|
+
parent: movedParent(references.parent, change),
|
|
363
|
+
columns: [...references.columns],
|
|
364
|
+
...references.onDelete !== undefined ? { onDelete: references.onDelete } : {},
|
|
365
|
+
...references.onUpdate !== undefined ? { onUpdate: references.onUpdate } : {}
|
|
366
|
+
};
|
|
367
|
+
return constraint2({
|
|
368
|
+
parent,
|
|
369
|
+
constraintKind: object.definition.constraintKind,
|
|
370
|
+
name: object.identity.name,
|
|
371
|
+
nameKey: object.definition.nameKey,
|
|
372
|
+
columns: [...object.definition.columns],
|
|
373
|
+
deferrable: object.definition.deferrable,
|
|
374
|
+
initially: object.definition.initially,
|
|
375
|
+
nullsNotDistinct: object.definition.nullsNotDistinct,
|
|
376
|
+
owner: object.owner,
|
|
377
|
+
provenance: object.provenance,
|
|
378
|
+
dependencies,
|
|
379
|
+
...object.definition.expression !== undefined ? {
|
|
380
|
+
expression: rewriteExpr(object.definition.expression, { from: change.from, to: change.to }, local)
|
|
381
|
+
} : {},
|
|
382
|
+
...referenced !== undefined ? { references: referenced } : {}
|
|
383
|
+
});
|
|
384
|
+
}
|
|
385
|
+
if (object.kind === "sequence")
|
|
386
|
+
return rewriteSequence(object, dependencies);
|
|
387
|
+
if (object.kind === "type")
|
|
388
|
+
return rewriteType(object, dependencies);
|
|
389
|
+
return assertNever(object);
|
|
390
|
+
}
|
|
391
|
+
function movedParent(parent, change) {
|
|
392
|
+
if (parent.name === change.from && sameNamespace2(parent.namespace, change.namespace)) {
|
|
393
|
+
return { namespace: parent.namespace, name: change.to };
|
|
394
|
+
}
|
|
395
|
+
return parent;
|
|
396
|
+
}
|
|
397
|
+
function rewriteType(object, dependencies) {
|
|
398
|
+
return enumType({
|
|
399
|
+
namespace: object.identity.namespace,
|
|
400
|
+
name: object.identity.name,
|
|
401
|
+
labels: object.definition.labels,
|
|
402
|
+
owner: object.owner,
|
|
403
|
+
provenance: object.provenance,
|
|
404
|
+
dependencies
|
|
405
|
+
});
|
|
406
|
+
}
|
|
407
|
+
function rewriteSequence(object, dependencies) {
|
|
408
|
+
return sequence2({
|
|
409
|
+
namespace: object.identity.namespace,
|
|
410
|
+
name: object.identity.name,
|
|
411
|
+
dataType: object.definition.dataType,
|
|
412
|
+
start: object.definition.start,
|
|
413
|
+
increment: object.definition.increment,
|
|
414
|
+
cycle: object.definition.cycle,
|
|
415
|
+
owner: object.owner,
|
|
416
|
+
provenance: object.provenance,
|
|
417
|
+
dependencies
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
function renameList(names, from, to) {
|
|
421
|
+
return names.map((name) => name === from ? to : name);
|
|
422
|
+
}
|
|
423
|
+
function retarget(edges, fromKey, to) {
|
|
424
|
+
return edges.map((edge) => identityKey2(edge.target) === fromKey ? to : edge.target);
|
|
425
|
+
}
|
|
426
|
+
function dependencyJson(edges) {
|
|
427
|
+
const indexed = edges.map((edge) => ({
|
|
428
|
+
key: identityKey2(edge.target),
|
|
429
|
+
target: identityToJson(edge.target)
|
|
430
|
+
}));
|
|
431
|
+
indexed.sort((left, right) => compareText(left.key, right.key));
|
|
432
|
+
return indexed.map((item) => ({ target: item.target }));
|
|
433
|
+
}
|
|
434
|
+
function objectToJson(object) {
|
|
435
|
+
const dependencies = dependencyJson(object.dependencies);
|
|
436
|
+
return {
|
|
437
|
+
definition: definitionToJson(object),
|
|
438
|
+
dependencies,
|
|
439
|
+
identity: identityToJson(object.identity),
|
|
440
|
+
kind: object.kind,
|
|
441
|
+
owner: object.owner,
|
|
442
|
+
provenance: { name: object.provenance.name, origin: object.provenance.origin }
|
|
443
|
+
};
|
|
444
|
+
}
|
|
445
|
+
function definitionToJson(object) {
|
|
446
|
+
switch (object.kind) {
|
|
447
|
+
case "table": {
|
|
448
|
+
const partition = object.definition.partition;
|
|
449
|
+
if (partition === undefined) {
|
|
450
|
+
return {};
|
|
451
|
+
}
|
|
452
|
+
return { partition: { columns: partition.columns, method: partition.method } };
|
|
453
|
+
}
|
|
454
|
+
case "column": {
|
|
455
|
+
const definition = object.definition;
|
|
456
|
+
return {
|
|
457
|
+
dataType: definition.dataType,
|
|
458
|
+
nullable: definition.nullable,
|
|
459
|
+
...definition.defaultExpression !== undefined ? { defaultExpression: definition.defaultExpression } : {},
|
|
460
|
+
...definition.identity !== undefined ? { identity: { always: definition.identity.always } } : {},
|
|
461
|
+
...definition.generated !== undefined ? {
|
|
462
|
+
generated: {
|
|
463
|
+
expression: definition.generated.expression,
|
|
464
|
+
stored: definition.generated.stored
|
|
465
|
+
}
|
|
466
|
+
} : {}
|
|
467
|
+
};
|
|
468
|
+
}
|
|
469
|
+
case "index": {
|
|
470
|
+
const definition = object.definition;
|
|
471
|
+
return {
|
|
472
|
+
columns: definition.columns,
|
|
473
|
+
nameKey: definition.nameKey,
|
|
474
|
+
unique: definition.unique,
|
|
475
|
+
...definition.expression !== undefined ? { expression: definition.expression } : {},
|
|
476
|
+
...definition.predicate !== undefined ? { predicate: definition.predicate } : {}
|
|
477
|
+
};
|
|
478
|
+
}
|
|
479
|
+
case "constraint": {
|
|
480
|
+
const definition = object.definition;
|
|
481
|
+
const references = definition.references;
|
|
482
|
+
return {
|
|
483
|
+
columns: definition.columns,
|
|
484
|
+
constraintKind: definition.constraintKind,
|
|
485
|
+
deferrable: definition.deferrable,
|
|
486
|
+
initially: definition.initially,
|
|
487
|
+
nameKey: definition.nameKey,
|
|
488
|
+
nullsNotDistinct: definition.nullsNotDistinct,
|
|
489
|
+
...definition.expression !== undefined ? { expression: definition.expression } : {},
|
|
490
|
+
...references !== undefined ? {
|
|
491
|
+
references: {
|
|
492
|
+
columns: references.columns,
|
|
493
|
+
...references.onDelete !== undefined ? { onDelete: references.onDelete } : {},
|
|
494
|
+
...references.onUpdate !== undefined ? { onUpdate: references.onUpdate } : {},
|
|
495
|
+
parent: {
|
|
496
|
+
name: references.parent.name,
|
|
497
|
+
namespace: namespaceToJson(references.parent.namespace)
|
|
498
|
+
}
|
|
499
|
+
}
|
|
500
|
+
} : {}
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
case "sequence":
|
|
504
|
+
return {
|
|
505
|
+
cycle: object.definition.cycle,
|
|
506
|
+
dataType: object.definition.dataType,
|
|
507
|
+
increment: object.definition.increment,
|
|
508
|
+
start: object.definition.start
|
|
509
|
+
};
|
|
510
|
+
case "type":
|
|
511
|
+
return { labels: object.definition.labels };
|
|
512
|
+
default:
|
|
513
|
+
return assertNever(object);
|
|
514
|
+
}
|
|
515
|
+
}
|
|
516
|
+
function parseObject(value) {
|
|
517
|
+
const record = requireRecord(value, "catalog object");
|
|
518
|
+
rejectUnknown(record, ["definition", "dependencies", "identity", "kind", "owner", "provenance"], "catalog object");
|
|
519
|
+
const kind = requireString(record.kind, "kind");
|
|
520
|
+
if (!isBuiltKind(kind)) {
|
|
521
|
+
catalogError2("OKM1020", `Kind ${kind} is not built yet. Built kinds are ${BUILT_KINDS2.join(", ")}.`);
|
|
522
|
+
}
|
|
523
|
+
const identity = parseIdentity(record.identity);
|
|
524
|
+
if (identity.kind !== kind) {
|
|
525
|
+
catalogError2("OKM1020", `${identityLabel2(identity)} has kind ${kind}.`);
|
|
526
|
+
}
|
|
527
|
+
const owner = parseOwner(record.owner);
|
|
528
|
+
const provenance = parseProvenance(record.provenance);
|
|
529
|
+
const dependencies = parseDependencies(record.dependencies);
|
|
530
|
+
const definition = requireRecord(record.definition, "definition");
|
|
531
|
+
switch (kind) {
|
|
532
|
+
case "table":
|
|
533
|
+
return parseTable(identity, definition, owner, provenance, dependencies);
|
|
534
|
+
case "column":
|
|
535
|
+
return parseColumn(identity, definition, owner, provenance, dependencies);
|
|
536
|
+
case "index":
|
|
537
|
+
return parseIndex(identity, definition, owner, provenance, dependencies);
|
|
538
|
+
case "constraint":
|
|
539
|
+
return parseConstraint(identity, definition, owner, provenance, dependencies);
|
|
540
|
+
case "sequence":
|
|
541
|
+
return parseSequence(identity, definition, owner, provenance, dependencies);
|
|
542
|
+
case "type":
|
|
543
|
+
return parseType(identity, definition, owner, provenance, dependencies);
|
|
544
|
+
default:
|
|
545
|
+
return assertNeverKind(kind);
|
|
546
|
+
}
|
|
547
|
+
}
|
|
548
|
+
function parseTable(identity, definition, owner, provenance, dependencies) {
|
|
549
|
+
if (identity.kind !== "table") {
|
|
550
|
+
catalogError2("OKM1020", `Table object identity is ${identity.kind}, not a table.`);
|
|
551
|
+
}
|
|
552
|
+
rejectUnknown(definition, ["partition"], "table definition");
|
|
553
|
+
const partition = definition.partition;
|
|
554
|
+
return table3({
|
|
555
|
+
namespace: identity.namespace,
|
|
556
|
+
name: identity.name,
|
|
557
|
+
owner,
|
|
558
|
+
provenance,
|
|
559
|
+
dependencies,
|
|
560
|
+
...partition === undefined ? {} : { partition: parsePartition(partition) }
|
|
561
|
+
});
|
|
562
|
+
}
|
|
563
|
+
function parseColumn(identity, definition, owner, provenance, dependencies) {
|
|
564
|
+
if (identity.kind !== "column") {
|
|
565
|
+
catalogError2("OKM1020", `Column object identity is ${identity.kind}, not a column.`);
|
|
566
|
+
}
|
|
567
|
+
rejectUnknown(definition, ["dataType", "defaultExpression", "generated", "identity", "nullable"], "column definition");
|
|
568
|
+
const generated = definition.generated;
|
|
569
|
+
const columnIdentity = definition.identity;
|
|
570
|
+
return column2({
|
|
571
|
+
parent: identity.parent,
|
|
572
|
+
name: identity.name,
|
|
573
|
+
dataType: requireString(definition.dataType, "data type"),
|
|
574
|
+
nullable: requireBoolean(definition.nullable, "nullable"),
|
|
575
|
+
owner,
|
|
576
|
+
provenance,
|
|
577
|
+
dependencies,
|
|
578
|
+
...definition.defaultExpression === undefined ? {} : { defaultExpression: requireString(definition.defaultExpression, "default") },
|
|
579
|
+
...columnIdentity === undefined ? {} : { identity: parseAlways(columnIdentity) },
|
|
580
|
+
...generated === undefined ? {} : { generated: parseGenerated(generated) }
|
|
581
|
+
});
|
|
582
|
+
}
|
|
583
|
+
function parseIndex(identity, definition, owner, provenance, dependencies) {
|
|
584
|
+
if (identity.kind !== "index") {
|
|
585
|
+
catalogError2("OKM1020", `Index object identity is ${identity.kind}, not an index.`);
|
|
586
|
+
}
|
|
587
|
+
rejectUnknown(definition, ["columns", "expression", "nameKey", "predicate", "unique"], "index definition");
|
|
588
|
+
return index3({
|
|
589
|
+
parent: identity.parent,
|
|
590
|
+
name: identity.name,
|
|
591
|
+
nameKey: requireString(definition.nameKey, "name key"),
|
|
592
|
+
columns: requireStrings(definition.columns, "index columns"),
|
|
593
|
+
unique: requireBoolean(definition.unique, "unique"),
|
|
594
|
+
owner,
|
|
595
|
+
provenance,
|
|
596
|
+
dependencies,
|
|
597
|
+
...definition.predicate === undefined ? {} : { predicate: requireString(definition.predicate, "predicate") },
|
|
598
|
+
...definition.expression === undefined ? {} : { expression: requireString(definition.expression, "expression") }
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
function parseConstraint(identity, definition, owner, provenance, dependencies) {
|
|
602
|
+
if (identity.kind !== "constraint") {
|
|
603
|
+
catalogError2("OKM1020", `Constraint object identity is ${identity.kind}, not a constraint.`);
|
|
604
|
+
}
|
|
605
|
+
rejectUnknown(definition, [
|
|
606
|
+
"columns",
|
|
607
|
+
"constraintKind",
|
|
608
|
+
"deferrable",
|
|
609
|
+
"expression",
|
|
610
|
+
"initially",
|
|
611
|
+
"nameKey",
|
|
612
|
+
"nullsNotDistinct",
|
|
613
|
+
"references"
|
|
614
|
+
], "constraint definition");
|
|
615
|
+
const constraintKind = requireString(definition.constraintKind, "constraint kind");
|
|
616
|
+
if (constraintKind !== "primaryKey" && constraintKind !== "unique" && constraintKind !== "foreignKey" && constraintKind !== "check") {
|
|
617
|
+
catalogError2("OKM1020", `Constraint kind ${constraintKind} must be primaryKey, unique, foreignKey, or check.`);
|
|
618
|
+
}
|
|
619
|
+
const initially = requireString(definition.initially, "initially");
|
|
620
|
+
if (initially !== "immediate" && initially !== "deferred") {
|
|
621
|
+
catalogError2("OKM1020", `Constraint initially ${initially} must be immediate or deferred.`);
|
|
622
|
+
}
|
|
623
|
+
const references = definition.references;
|
|
624
|
+
return constraint2({
|
|
625
|
+
parent: identity.parent,
|
|
626
|
+
constraintKind,
|
|
627
|
+
name: identity.name,
|
|
628
|
+
nameKey: requireString(definition.nameKey, "name key"),
|
|
629
|
+
columns: requireStrings(definition.columns, "constraint columns"),
|
|
630
|
+
deferrable: requireBoolean(definition.deferrable, "deferrable"),
|
|
631
|
+
initially,
|
|
632
|
+
nullsNotDistinct: requireBoolean(definition.nullsNotDistinct, "nulls not distinct"),
|
|
633
|
+
owner,
|
|
634
|
+
provenance,
|
|
635
|
+
dependencies,
|
|
636
|
+
...definition.expression === undefined ? {} : { expression: requireString(definition.expression, "expression") },
|
|
637
|
+
...references === undefined ? {} : { references: parseReferences(references) }
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
function parseSequence(identity, definition, owner, provenance, dependencies) {
|
|
641
|
+
if (identity.kind !== "sequence") {
|
|
642
|
+
catalogError2("OKM1020", `Sequence object identity is ${identity.kind}, not a sequence.`);
|
|
643
|
+
}
|
|
644
|
+
rejectUnknown(definition, ["cycle", "dataType", "increment", "start"], "sequence definition");
|
|
645
|
+
const dataType = requireString(definition.dataType, "sequence type");
|
|
646
|
+
if (dataType !== "smallint" && dataType !== "integer" && dataType !== "bigint") {
|
|
647
|
+
catalogError2("OKM1020", `Sequence type ${dataType} must be smallint, integer, or bigint.`);
|
|
648
|
+
}
|
|
649
|
+
return sequence2({
|
|
650
|
+
namespace: identity.namespace,
|
|
651
|
+
name: identity.name,
|
|
652
|
+
dataType,
|
|
653
|
+
start: requireString(definition.start, "sequence start"),
|
|
654
|
+
increment: requireString(definition.increment, "sequence increment"),
|
|
655
|
+
cycle: requireBoolean(definition.cycle, "cycle"),
|
|
656
|
+
owner,
|
|
657
|
+
provenance,
|
|
658
|
+
dependencies
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
function parseType(identity, definition, owner, provenance, dependencies) {
|
|
662
|
+
if (identity.kind !== "type") {
|
|
663
|
+
catalogError2("OKM1020", `Type object identity is ${identity.kind}, not a type.`);
|
|
664
|
+
}
|
|
665
|
+
rejectUnknown(definition, ["labels"], "type definition");
|
|
666
|
+
return enumType({
|
|
667
|
+
namespace: identity.namespace,
|
|
668
|
+
name: identity.name,
|
|
669
|
+
labels: requireStrings(definition.labels, "enum labels"),
|
|
670
|
+
owner,
|
|
671
|
+
provenance,
|
|
672
|
+
dependencies
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
function parseIdentity(value) {
|
|
676
|
+
const record = requireRecord(value, "identity");
|
|
677
|
+
const kind = requireString(record.kind, "identity kind");
|
|
678
|
+
switch (kind) {
|
|
679
|
+
case "table":
|
|
680
|
+
case "view":
|
|
681
|
+
case "materializedView":
|
|
682
|
+
case "sequence":
|
|
683
|
+
case "type":
|
|
684
|
+
rejectUnknown(record, ["kind", "name", "namespace"], "identity");
|
|
685
|
+
return {
|
|
686
|
+
kind,
|
|
687
|
+
name: requireString(record.name, "name"),
|
|
688
|
+
namespace: parseNamespace(record.namespace)
|
|
689
|
+
};
|
|
690
|
+
case "column":
|
|
691
|
+
case "index":
|
|
692
|
+
case "constraint":
|
|
693
|
+
case "trigger":
|
|
694
|
+
case "policy":
|
|
695
|
+
rejectUnknown(record, ["kind", "name", "parent"], "identity");
|
|
696
|
+
return {
|
|
697
|
+
kind,
|
|
698
|
+
name: requireString(record.name, "name"),
|
|
699
|
+
parent: parseRef(record.parent)
|
|
700
|
+
};
|
|
701
|
+
case "function":
|
|
702
|
+
rejectUnknown(record, ["argTypes", "kind", "name", "namespace"], "identity");
|
|
703
|
+
return {
|
|
704
|
+
kind,
|
|
705
|
+
name: requireString(record.name, "name"),
|
|
706
|
+
namespace: parseNamespace(record.namespace),
|
|
707
|
+
argTypes: requireStrings(record.argTypes, "argument types")
|
|
708
|
+
};
|
|
709
|
+
case "grant":
|
|
710
|
+
rejectUnknown(record, ["kind", "object", "privilege", "role"], "identity");
|
|
711
|
+
return {
|
|
712
|
+
kind,
|
|
713
|
+
role: requireString(record.role, "role"),
|
|
714
|
+
privilege: requireString(record.privilege, "privilege"),
|
|
715
|
+
object: parseGrantObject(record.object)
|
|
716
|
+
};
|
|
717
|
+
case "defaultPrivilege":
|
|
718
|
+
rejectUnknown(record, ["forRole", "grantee", "kind", "namespace", "objectKind", "privilege"], "identity");
|
|
719
|
+
return {
|
|
720
|
+
kind,
|
|
721
|
+
forRole: requireString(record.forRole, "forRole"),
|
|
722
|
+
grantee: requireString(record.grantee, "grantee"),
|
|
723
|
+
namespace: parseNamespace(record.namespace),
|
|
724
|
+
objectKind: requireString(record.objectKind, "object kind"),
|
|
725
|
+
privilege: requireString(record.privilege, "privilege")
|
|
726
|
+
};
|
|
727
|
+
case "role":
|
|
728
|
+
case "extension":
|
|
729
|
+
rejectUnknown(record, ["kind", "name"], "identity");
|
|
730
|
+
return { kind, name: requireString(record.name, "name") };
|
|
731
|
+
default:
|
|
732
|
+
catalogError2("OKM1020", `Identity kind ${kind} is not a catalog identity.`);
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
function parseNamespace(value) {
|
|
736
|
+
const record = requireRecord(value, "namespace");
|
|
737
|
+
const form = requireString(record.form, "namespace form");
|
|
738
|
+
if (form === "static") {
|
|
739
|
+
rejectUnknown(record, ["form", "name"], "namespace");
|
|
740
|
+
return staticNamespace2(requireString(record.name, "namespace"));
|
|
741
|
+
}
|
|
742
|
+
if (form === "template") {
|
|
743
|
+
rejectUnknown(record, ["form", "pattern"], "namespace");
|
|
744
|
+
return templateNamespace2(requireString(record.pattern, "namespace template"));
|
|
745
|
+
}
|
|
746
|
+
catalogError2("OKM1020", `Namespace form ${form} must be static or template.`);
|
|
747
|
+
}
|
|
748
|
+
function parseRef(value) {
|
|
749
|
+
const record = requireRecord(value, "parent");
|
|
750
|
+
rejectUnknown(record, ["name", "namespace"], "parent");
|
|
751
|
+
return {
|
|
752
|
+
name: requireString(record.name, "parent name"),
|
|
753
|
+
namespace: parseNamespace(record.namespace)
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
function parseGrantObject(value) {
|
|
757
|
+
const record = requireRecord(value, "grant object");
|
|
758
|
+
rejectUnknown(record, ["kind", "name", "namespace"], "grant object");
|
|
759
|
+
const kind = requireString(record.kind, "grant object kind");
|
|
760
|
+
if (kind !== "table" && kind !== "sequence" && kind !== "namespace") {
|
|
761
|
+
catalogError2("OKM1020", `Grant object kind ${kind} must be table, sequence, or namespace.`);
|
|
762
|
+
}
|
|
763
|
+
return {
|
|
764
|
+
kind,
|
|
765
|
+
name: requireString(record.name, "grant object name"),
|
|
766
|
+
namespace: parseNamespace(record.namespace)
|
|
767
|
+
};
|
|
768
|
+
}
|
|
769
|
+
function parsePartition(value) {
|
|
770
|
+
const record = requireRecord(value, "partition");
|
|
771
|
+
rejectUnknown(record, ["columns", "method"], "partition");
|
|
772
|
+
const method = requireString(record.method, "partition method");
|
|
773
|
+
if (method !== "range" && method !== "list" && method !== "hash") {
|
|
774
|
+
catalogError2("OKM1020", `Partition method ${method} must be range, list, or hash.`);
|
|
775
|
+
}
|
|
776
|
+
return { method, columns: requireStrings(record.columns, "partition columns") };
|
|
777
|
+
}
|
|
778
|
+
function parseReferences(value) {
|
|
779
|
+
const record = requireRecord(value, "references");
|
|
780
|
+
rejectUnknown(record, ["columns", "onDelete", "onUpdate", "parent"], "references");
|
|
781
|
+
const onDelete = parseAction(record.onDelete, "onDelete");
|
|
782
|
+
const onUpdate = parseAction(record.onUpdate, "onUpdate");
|
|
783
|
+
return {
|
|
784
|
+
parent: parseRef(record.parent),
|
|
785
|
+
columns: requireStrings(record.columns, "referenced columns"),
|
|
786
|
+
...onDelete !== undefined ? { onDelete } : {},
|
|
787
|
+
...onUpdate !== undefined ? { onUpdate } : {}
|
|
788
|
+
};
|
|
789
|
+
}
|
|
790
|
+
function parseAction(value, role) {
|
|
791
|
+
if (value === undefined) {
|
|
792
|
+
return;
|
|
793
|
+
}
|
|
794
|
+
const text = requireString(value, role);
|
|
795
|
+
const action = referentialAction(text);
|
|
796
|
+
if (action === undefined) {
|
|
797
|
+
catalogError2("OKM1020", `Referential action ${text} is not a catalog action. Accepted actions: cascade, no action, restrict, set default, set null.`);
|
|
798
|
+
}
|
|
799
|
+
return action;
|
|
800
|
+
}
|
|
801
|
+
function parseAlways(value) {
|
|
802
|
+
const record = requireRecord(value, "identity");
|
|
803
|
+
rejectUnknown(record, ["always"], "identity");
|
|
804
|
+
return { always: requireBoolean(record.always, "always") };
|
|
805
|
+
}
|
|
806
|
+
function parseGenerated(value) {
|
|
807
|
+
const record = requireRecord(value, "generated");
|
|
808
|
+
rejectUnknown(record, ["expression", "stored"], "generated");
|
|
809
|
+
return {
|
|
810
|
+
stored: requireBoolean(record.stored, "stored"),
|
|
811
|
+
expression: requireString(record.expression, "generated expression")
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
function parseOwner(value) {
|
|
815
|
+
const owner = requireString(value, "owner");
|
|
816
|
+
if (!isOwner(owner)) {
|
|
817
|
+
catalogError2("OKM1020", `Owner ${owner} must be managed, external, or ignored.`);
|
|
818
|
+
}
|
|
819
|
+
return owner;
|
|
820
|
+
}
|
|
821
|
+
function parseProvenance(value) {
|
|
822
|
+
const record = requireRecord(value, "provenance");
|
|
823
|
+
rejectUnknown(record, ["name", "origin"], "provenance");
|
|
824
|
+
const origin = requireString(record.origin, "provenance origin");
|
|
825
|
+
const name = requireString(record.name, "provenance name");
|
|
826
|
+
if (origin !== "file" && origin !== "trait" && origin !== "extension") {
|
|
827
|
+
catalogError2("OKM1020", `Provenance origin ${origin} must be file, trait, or extension.`);
|
|
828
|
+
}
|
|
829
|
+
return { origin, name };
|
|
830
|
+
}
|
|
831
|
+
function parseDependencies(value) {
|
|
832
|
+
if (!Array.isArray(value)) {
|
|
833
|
+
catalogError2("OKM1020", "Dependencies must be an array of targets.");
|
|
834
|
+
}
|
|
835
|
+
return value.map((item) => {
|
|
836
|
+
const record = requireRecord(item, "dependency");
|
|
837
|
+
rejectUnknown(record, ["target"], "dependency");
|
|
838
|
+
return parseIdentity(record.target);
|
|
839
|
+
});
|
|
840
|
+
}
|
|
841
|
+
function isBuiltKind(kind) {
|
|
842
|
+
return BUILT_KINDS2.includes(kind);
|
|
843
|
+
}
|
|
844
|
+
function isOwner(value) {
|
|
845
|
+
return OWNERS2.includes(value);
|
|
846
|
+
}
|
|
847
|
+
function requireRecord(value, role) {
|
|
848
|
+
if (!isPlainRecord(value)) {
|
|
849
|
+
catalogError2("OKM1020", `${role} is not an object.`);
|
|
850
|
+
}
|
|
851
|
+
return value;
|
|
852
|
+
}
|
|
853
|
+
function isPlainRecord(value) {
|
|
854
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
855
|
+
}
|
|
856
|
+
function requireString(value, role) {
|
|
857
|
+
if (typeof value !== "string") {
|
|
858
|
+
catalogError2("OKM1020", `${role} is not a string.`);
|
|
859
|
+
}
|
|
860
|
+
return value;
|
|
861
|
+
}
|
|
862
|
+
function requireBoolean(value, role) {
|
|
863
|
+
if (typeof value !== "boolean") {
|
|
864
|
+
catalogError2("OKM1020", `${role} is not a boolean.`);
|
|
865
|
+
}
|
|
866
|
+
return value;
|
|
867
|
+
}
|
|
868
|
+
function requireStrings(value, role) {
|
|
869
|
+
if (!Array.isArray(value)) {
|
|
870
|
+
catalogError2("OKM1020", `${role} is not an array.`);
|
|
871
|
+
}
|
|
872
|
+
return value.map((item) => requireString(item, role));
|
|
873
|
+
}
|
|
874
|
+
function rejectUnknown(record, allowed, role) {
|
|
875
|
+
for (const key of Object.keys(record)) {
|
|
876
|
+
if (!allowed.includes(key)) {
|
|
877
|
+
catalogError2("OKM1020", `${role} has unexpected field ${key}.`);
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
}
|
|
881
|
+
function assertNever(value) {
|
|
882
|
+
return catalogError2("OKM1020", `Unexpected catalog object ${JSON.stringify(value)}.`);
|
|
883
|
+
}
|
|
884
|
+
function assertNeverKind(value) {
|
|
885
|
+
return catalogError2("OKM1020", `Unexpected kind ${JSON.stringify(value)}.`);
|
|
886
|
+
}
|
|
887
|
+
|
|
888
|
+
export { creationOrder2, renameColumn2, renameTable2, serializeCatalog2, catalogHash2, loadTrustedCatalog2, parseCatalog2 };
|