@elizaos/plugin-sql 2.0.0-alpha.15 → 2.0.0-alpha.16
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/browser/index.browser.js +711 -708
- package/dist/browser/index.browser.js.map +7 -7
- package/dist/cjs/index.node.cjs +760 -739
- package/dist/cjs/index.node.cjs.map +5 -5
- package/dist/node/index.node.js +718 -714
- package/dist/node/index.node.js.map +5 -5
- package/package.json +1 -2
package/dist/cjs/index.node.cjs
CHANGED
|
@@ -4,38 +4,59 @@ var __defProp = Object.defineProperty;
|
|
|
4
4
|
var __getOwnPropNames = Object.getOwnPropertyNames;
|
|
5
5
|
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
|
|
6
6
|
var __hasOwnProp = Object.prototype.hasOwnProperty;
|
|
7
|
+
function __accessProp(key) {
|
|
8
|
+
return this[key];
|
|
9
|
+
}
|
|
10
|
+
var __toESMCache_node;
|
|
11
|
+
var __toESMCache_esm;
|
|
7
12
|
var __toESM = (mod, isNodeMode, target) => {
|
|
13
|
+
var canCache = mod != null && typeof mod === "object";
|
|
14
|
+
if (canCache) {
|
|
15
|
+
var cache = isNodeMode ? __toESMCache_node ??= new WeakMap : __toESMCache_esm ??= new WeakMap;
|
|
16
|
+
var cached = cache.get(mod);
|
|
17
|
+
if (cached)
|
|
18
|
+
return cached;
|
|
19
|
+
}
|
|
8
20
|
target = mod != null ? __create(__getProtoOf(mod)) : {};
|
|
9
21
|
const to = isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", { value: mod, enumerable: true }) : target;
|
|
10
22
|
for (let key of __getOwnPropNames(mod))
|
|
11
23
|
if (!__hasOwnProp.call(to, key))
|
|
12
24
|
__defProp(to, key, {
|
|
13
|
-
get: (
|
|
25
|
+
get: __accessProp.bind(mod, key),
|
|
14
26
|
enumerable: true
|
|
15
27
|
});
|
|
28
|
+
if (canCache)
|
|
29
|
+
cache.set(mod, to);
|
|
16
30
|
return to;
|
|
17
31
|
};
|
|
18
|
-
var __moduleCache = /* @__PURE__ */ new WeakMap;
|
|
19
32
|
var __toCommonJS = (from) => {
|
|
20
|
-
var entry = __moduleCache.get(from), desc;
|
|
33
|
+
var entry = (__moduleCache ??= new WeakMap).get(from), desc;
|
|
21
34
|
if (entry)
|
|
22
35
|
return entry;
|
|
23
36
|
entry = __defProp({}, "__esModule", { value: true });
|
|
24
|
-
if (from && typeof from === "object" || typeof from === "function")
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
37
|
+
if (from && typeof from === "object" || typeof from === "function") {
|
|
38
|
+
for (var key of __getOwnPropNames(from))
|
|
39
|
+
if (!__hasOwnProp.call(entry, key))
|
|
40
|
+
__defProp(entry, key, {
|
|
41
|
+
get: __accessProp.bind(from, key),
|
|
42
|
+
enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
|
|
43
|
+
});
|
|
44
|
+
}
|
|
29
45
|
__moduleCache.set(from, entry);
|
|
30
46
|
return entry;
|
|
31
47
|
};
|
|
48
|
+
var __moduleCache;
|
|
49
|
+
var __returnValue = (v) => v;
|
|
50
|
+
function __exportSetter(name, newValue) {
|
|
51
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
52
|
+
}
|
|
32
53
|
var __export = (target, all) => {
|
|
33
54
|
for (var name in all)
|
|
34
55
|
__defProp(target, name, {
|
|
35
56
|
get: all[name],
|
|
36
57
|
enumerable: true,
|
|
37
58
|
configurable: true,
|
|
38
|
-
set: (
|
|
59
|
+
set: __exportSetter.bind(all, name)
|
|
39
60
|
});
|
|
40
61
|
};
|
|
41
62
|
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
@@ -1065,167 +1086,532 @@ var init_rls = __esm(() => {
|
|
|
1065
1086
|
import_drizzle_orm20 = require("drizzle-orm");
|
|
1066
1087
|
});
|
|
1067
1088
|
|
|
1068
|
-
// runtime-migrator/
|
|
1069
|
-
|
|
1070
|
-
|
|
1071
|
-
|
|
1072
|
-
|
|
1073
|
-
|
|
1074
|
-
|
|
1075
|
-
|
|
1076
|
-
|
|
1077
|
-
|
|
1078
|
-
|
|
1079
|
-
|
|
1080
|
-
}
|
|
1081
|
-
if (normalized === "serial") {
|
|
1082
|
-
return "integer";
|
|
1083
|
-
}
|
|
1084
|
-
if (normalized === "bigserial") {
|
|
1085
|
-
return "bigint";
|
|
1086
|
-
}
|
|
1087
|
-
if (normalized === "smallserial") {
|
|
1088
|
-
return "smallint";
|
|
1089
|
-
}
|
|
1090
|
-
if (normalized.startsWith("numeric") || normalized.startsWith("decimal")) {
|
|
1091
|
-
const match = normalized.match(/\((\d+)(?:,\s*(\d+))?\)/);
|
|
1092
|
-
if (match) {
|
|
1093
|
-
return `numeric(${match[1]}${match[2] ? `,${match[2]}` : ""})`;
|
|
1094
|
-
}
|
|
1095
|
-
return "numeric";
|
|
1096
|
-
}
|
|
1097
|
-
if (normalized.startsWith("character varying")) {
|
|
1098
|
-
return normalized.replace("character varying", "varchar");
|
|
1099
|
-
}
|
|
1100
|
-
if (normalized === "text[]" || normalized === "_text") {
|
|
1101
|
-
return "text[]";
|
|
1089
|
+
// runtime-migrator/crypto-utils.ts
|
|
1090
|
+
function extendedHash(str) {
|
|
1091
|
+
const h1 = hashWithSeed(str, 5381);
|
|
1092
|
+
const h2 = hashWithSeed(str, 7919);
|
|
1093
|
+
const h3 = hashWithSeed(str, 104729);
|
|
1094
|
+
const h4 = hashWithSeed(str, 224737);
|
|
1095
|
+
return h1 + h2 + h3 + h4;
|
|
1096
|
+
}
|
|
1097
|
+
function hashWithSeed(str, seed) {
|
|
1098
|
+
let hash = seed;
|
|
1099
|
+
for (let i = 0;i < str.length; i++) {
|
|
1100
|
+
hash = hash * 33 ^ str.charCodeAt(i);
|
|
1102
1101
|
}
|
|
1103
|
-
return
|
|
1102
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
1104
1103
|
}
|
|
1105
|
-
function
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
if (
|
|
1111
|
-
|
|
1112
|
-
if (prevIndex.concurrently !== currIndex.concurrently)
|
|
1113
|
-
return true;
|
|
1114
|
-
const prevColumns = prevIndex.columns || [];
|
|
1115
|
-
const currColumns = currIndex.columns || [];
|
|
1116
|
-
if (prevColumns.length !== currColumns.length)
|
|
1117
|
-
return true;
|
|
1118
|
-
for (let i = 0;i < prevColumns.length; i++) {
|
|
1119
|
-
const prevCol = prevColumns[i];
|
|
1120
|
-
const currCol = currColumns[i];
|
|
1121
|
-
if (typeof prevCol === "string" && typeof currCol === "string") {
|
|
1122
|
-
if (prevCol !== currCol)
|
|
1123
|
-
return true;
|
|
1124
|
-
} else if (typeof prevCol === "object" && typeof currCol === "object") {
|
|
1125
|
-
if (prevCol.expression !== currCol.expression)
|
|
1126
|
-
return true;
|
|
1127
|
-
if (prevCol.isExpression !== currCol.isExpression)
|
|
1128
|
-
return true;
|
|
1129
|
-
if (prevCol.asc !== currCol.asc)
|
|
1130
|
-
return true;
|
|
1131
|
-
if (prevCol.nulls !== currCol.nulls)
|
|
1132
|
-
return true;
|
|
1133
|
-
} else {
|
|
1134
|
-
return true;
|
|
1135
|
-
}
|
|
1104
|
+
function stringToBigInt(str) {
|
|
1105
|
+
const hash = extendedHash(str);
|
|
1106
|
+
let lockId = BigInt(`0x${hash.slice(0, 16)}`);
|
|
1107
|
+
const mask63Bits = 0x7fffffffffffffffn;
|
|
1108
|
+
lockId = lockId & mask63Bits;
|
|
1109
|
+
if (lockId === 0n) {
|
|
1110
|
+
lockId = 1n;
|
|
1136
1111
|
}
|
|
1137
|
-
return
|
|
1112
|
+
return lockId;
|
|
1138
1113
|
}
|
|
1139
|
-
|
|
1140
|
-
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
|
|
1144
|
-
|
|
1145
|
-
|
|
1146
|
-
|
|
1147
|
-
|
|
1148
|
-
|
|
1149
|
-
|
|
1150
|
-
|
|
1151
|
-
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1164
|
-
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
|
|
1170
|
-
|
|
1171
|
-
diff.tables.created = Object.keys(currentSnapshot.tables);
|
|
1172
|
-
for (const tableName in currentSnapshot.tables) {
|
|
1173
|
-
const table = currentSnapshot.tables[tableName];
|
|
1174
|
-
if (table.indexes) {
|
|
1175
|
-
for (const indexName in table.indexes) {
|
|
1176
|
-
diff.indexes.created.push({
|
|
1177
|
-
...table.indexes[indexName],
|
|
1178
|
-
table: tableName
|
|
1179
|
-
});
|
|
1180
|
-
}
|
|
1114
|
+
|
|
1115
|
+
// runtime-migrator/drizzle-adapters/database-introspector.ts
|
|
1116
|
+
function getRows2(result) {
|
|
1117
|
+
return result.rows;
|
|
1118
|
+
}
|
|
1119
|
+
|
|
1120
|
+
class DatabaseIntrospector {
|
|
1121
|
+
db;
|
|
1122
|
+
constructor(db) {
|
|
1123
|
+
this.db = db;
|
|
1124
|
+
}
|
|
1125
|
+
async introspectSchema(schemaName = "public") {
|
|
1126
|
+
import_core4.logger.info({ src: "plugin:sql", schemaName }, "Starting database introspection");
|
|
1127
|
+
const tables = {};
|
|
1128
|
+
const schemas = {};
|
|
1129
|
+
const enums = {};
|
|
1130
|
+
const allTables = await this.getTables(schemaName);
|
|
1131
|
+
for (const tableInfo of allTables) {
|
|
1132
|
+
const tableName = tableInfo.table_name;
|
|
1133
|
+
const tableSchema = tableInfo.table_schema || "public";
|
|
1134
|
+
import_core4.logger.debug({ src: "plugin:sql", tableSchema, tableName }, "Introspecting table");
|
|
1135
|
+
const columns = await this.getColumns(tableSchema, tableName);
|
|
1136
|
+
const columnsObject = {};
|
|
1137
|
+
const uniqueConstraintObject = {};
|
|
1138
|
+
for (const col of columns) {
|
|
1139
|
+
columnsObject[col.column_name] = {
|
|
1140
|
+
name: col.column_name,
|
|
1141
|
+
type: col.data_type,
|
|
1142
|
+
primaryKey: col.is_primary || false,
|
|
1143
|
+
notNull: col.is_nullable === "NO",
|
|
1144
|
+
default: col.column_default ? this.parseDefault(col.column_default, col.data_type) : undefined
|
|
1145
|
+
};
|
|
1181
1146
|
}
|
|
1182
|
-
|
|
1183
|
-
|
|
1184
|
-
|
|
1147
|
+
const indexes = await this.getIndexes(tableSchema, tableName);
|
|
1148
|
+
const indexesObject = {};
|
|
1149
|
+
for (const idx of indexes) {
|
|
1150
|
+
if (!idx.is_primary && !idx.is_unique_constraint) {
|
|
1151
|
+
if (idx.columns && Array.isArray(idx.columns) && idx.columns.length > 0) {
|
|
1152
|
+
indexesObject[idx.name] = {
|
|
1153
|
+
name: idx.name,
|
|
1154
|
+
columns: idx.columns.map((col) => ({
|
|
1155
|
+
expression: col,
|
|
1156
|
+
isExpression: false
|
|
1157
|
+
})),
|
|
1158
|
+
isUnique: idx.is_unique,
|
|
1159
|
+
method: idx.method || "btree"
|
|
1160
|
+
};
|
|
1161
|
+
}
|
|
1185
1162
|
}
|
|
1186
1163
|
}
|
|
1187
|
-
|
|
1188
|
-
|
|
1189
|
-
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
});
|
|
1202
|
-
}
|
|
1164
|
+
const foreignKeys = await this.getForeignKeys(tableSchema, tableName);
|
|
1165
|
+
const foreignKeysObject = {};
|
|
1166
|
+
for (const fk of foreignKeys) {
|
|
1167
|
+
foreignKeysObject[fk.name] = {
|
|
1168
|
+
name: fk.name,
|
|
1169
|
+
tableFrom: tableName,
|
|
1170
|
+
schemaFrom: tableSchema,
|
|
1171
|
+
tableTo: fk.foreign_table_name,
|
|
1172
|
+
schemaTo: fk.foreign_table_schema || "public",
|
|
1173
|
+
columnsFrom: [fk.column_name],
|
|
1174
|
+
columnsTo: [fk.foreign_column_name],
|
|
1175
|
+
onDelete: fk.delete_rule?.toLowerCase() || "no action",
|
|
1176
|
+
onUpdate: fk.update_rule?.toLowerCase() || "no action"
|
|
1177
|
+
};
|
|
1203
1178
|
}
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1208
|
-
|
|
1209
|
-
|
|
1210
|
-
}
|
|
1179
|
+
const primaryKeys = await this.getPrimaryKeys(tableSchema, tableName);
|
|
1180
|
+
const primaryKeysObject = {};
|
|
1181
|
+
for (const pk of primaryKeys) {
|
|
1182
|
+
primaryKeysObject[pk.name] = {
|
|
1183
|
+
name: pk.name,
|
|
1184
|
+
columns: pk.columns
|
|
1185
|
+
};
|
|
1211
1186
|
}
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
}
|
|
1187
|
+
const uniqueConstraints = await this.getUniqueConstraints(tableSchema, tableName);
|
|
1188
|
+
for (const unq of uniqueConstraints) {
|
|
1189
|
+
uniqueConstraintObject[unq.name] = {
|
|
1190
|
+
name: unq.name,
|
|
1191
|
+
columns: unq.columns,
|
|
1192
|
+
nullsNotDistinct: false
|
|
1193
|
+
};
|
|
1219
1194
|
}
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1195
|
+
const checkConstraints = await this.getCheckConstraints(tableSchema, tableName);
|
|
1196
|
+
const checksObject = {};
|
|
1197
|
+
for (const check3 of checkConstraints) {
|
|
1198
|
+
checksObject[check3.name] = {
|
|
1199
|
+
name: check3.name,
|
|
1200
|
+
value: check3.definition
|
|
1201
|
+
};
|
|
1224
1202
|
}
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1203
|
+
tables[`${tableSchema}.${tableName}`] = {
|
|
1204
|
+
name: tableName,
|
|
1205
|
+
schema: tableSchema,
|
|
1206
|
+
columns: columnsObject,
|
|
1207
|
+
indexes: indexesObject,
|
|
1208
|
+
foreignKeys: foreignKeysObject,
|
|
1209
|
+
compositePrimaryKeys: primaryKeysObject,
|
|
1210
|
+
uniqueConstraints: uniqueConstraintObject,
|
|
1211
|
+
checkConstraints: checksObject
|
|
1212
|
+
};
|
|
1213
|
+
if (tableSchema && tableSchema !== "public") {
|
|
1214
|
+
schemas[tableSchema] = tableSchema;
|
|
1215
|
+
}
|
|
1216
|
+
}
|
|
1217
|
+
const enumsResult = await this.getEnums(schemaName);
|
|
1218
|
+
for (const enumInfo of enumsResult) {
|
|
1219
|
+
const key = `${enumInfo.schema}.${enumInfo.name}`;
|
|
1220
|
+
if (!enums[key]) {
|
|
1221
|
+
enums[key] = {
|
|
1222
|
+
name: enumInfo.name,
|
|
1223
|
+
schema: enumInfo.schema,
|
|
1224
|
+
values: []
|
|
1225
|
+
};
|
|
1226
|
+
}
|
|
1227
|
+
enums[key].values.push(enumInfo.value);
|
|
1228
|
+
}
|
|
1229
|
+
import_core4.logger.info({ src: "plugin:sql", tableCount: Object.keys(tables).length }, "Database introspection complete");
|
|
1230
|
+
return {
|
|
1231
|
+
version: "7",
|
|
1232
|
+
dialect: "postgresql",
|
|
1233
|
+
tables,
|
|
1234
|
+
schemas,
|
|
1235
|
+
enums,
|
|
1236
|
+
_meta: {
|
|
1237
|
+
schemas: {},
|
|
1238
|
+
tables: {},
|
|
1239
|
+
columns: {}
|
|
1240
|
+
}
|
|
1241
|
+
};
|
|
1242
|
+
}
|
|
1243
|
+
async getTables(schemaName) {
|
|
1244
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
|
|
1245
|
+
table_schema,
|
|
1246
|
+
table_name
|
|
1247
|
+
FROM information_schema.tables
|
|
1248
|
+
WHERE table_schema = ${schemaName}
|
|
1249
|
+
AND table_type = 'BASE TABLE'
|
|
1250
|
+
ORDER BY table_name`);
|
|
1251
|
+
return getRows2(result);
|
|
1252
|
+
}
|
|
1253
|
+
async getColumns(schemaName, tableName) {
|
|
1254
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
|
|
1255
|
+
a.attname AS column_name,
|
|
1256
|
+
CASE
|
|
1257
|
+
WHEN a.attnotnull THEN 'NO'
|
|
1258
|
+
ELSE 'YES'
|
|
1259
|
+
END AS is_nullable,
|
|
1260
|
+
CASE
|
|
1261
|
+
WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
|
|
1262
|
+
AND EXISTS (
|
|
1263
|
+
SELECT FROM pg_attrdef ad
|
|
1264
|
+
WHERE ad.adrelid = a.attrelid
|
|
1265
|
+
AND ad.adnum = a.attnum
|
|
1266
|
+
AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
|
|
1267
|
+
|| pg_get_serial_sequence(a.attrelid::regclass::text, a.attname)::regclass || '''::regclass)'
|
|
1268
|
+
)
|
|
1269
|
+
THEN CASE a.atttypid
|
|
1270
|
+
WHEN 'int'::regtype THEN 'serial'
|
|
1271
|
+
WHEN 'int8'::regtype THEN 'bigserial'
|
|
1272
|
+
WHEN 'int2'::regtype THEN 'smallserial'
|
|
1273
|
+
END
|
|
1274
|
+
ELSE format_type(a.atttypid, a.atttypmod)
|
|
1275
|
+
END AS data_type,
|
|
1276
|
+
pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
|
|
1277
|
+
CASE
|
|
1278
|
+
WHEN con.contype = 'p' THEN true
|
|
1279
|
+
ELSE false
|
|
1280
|
+
END AS is_primary
|
|
1281
|
+
FROM pg_attribute a
|
|
1282
|
+
JOIN pg_class cls ON cls.oid = a.attrelid
|
|
1283
|
+
JOIN pg_namespace ns ON ns.oid = cls.relnamespace
|
|
1284
|
+
LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
|
|
1285
|
+
LEFT JOIN pg_constraint con ON con.conrelid = a.attrelid
|
|
1286
|
+
AND a.attnum = ANY(con.conkey)
|
|
1287
|
+
AND con.contype = 'p'
|
|
1288
|
+
WHERE
|
|
1289
|
+
a.attnum > 0
|
|
1290
|
+
AND NOT a.attisdropped
|
|
1291
|
+
AND ns.nspname = ${schemaName}
|
|
1292
|
+
AND cls.relname = ${tableName}
|
|
1293
|
+
ORDER BY a.attnum`);
|
|
1294
|
+
return getRows2(result);
|
|
1295
|
+
}
|
|
1296
|
+
async getIndexes(schemaName, tableName) {
|
|
1297
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
|
|
1298
|
+
i.relname AS name,
|
|
1299
|
+
idx.indisunique AS is_unique,
|
|
1300
|
+
idx.indisprimary AS is_primary,
|
|
1301
|
+
con.contype = 'u' AS is_unique_constraint,
|
|
1302
|
+
ARRAY(
|
|
1303
|
+
SELECT a.attname
|
|
1304
|
+
FROM pg_attribute a
|
|
1305
|
+
WHERE a.attrelid = idx.indrelid
|
|
1306
|
+
AND a.attnum = ANY(idx.indkey::int[])
|
|
1307
|
+
ORDER BY a.attnum
|
|
1308
|
+
) AS columns,
|
|
1309
|
+
am.amname AS method
|
|
1310
|
+
FROM pg_index idx
|
|
1311
|
+
JOIN pg_class i ON i.oid = idx.indexrelid
|
|
1312
|
+
JOIN pg_class c ON c.oid = idx.indrelid
|
|
1313
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
1314
|
+
JOIN pg_am am ON am.oid = i.relam
|
|
1315
|
+
LEFT JOIN pg_constraint con ON con.conindid = idx.indexrelid
|
|
1316
|
+
WHERE n.nspname = ${schemaName}
|
|
1317
|
+
AND c.relname = ${tableName}`);
|
|
1318
|
+
return getRows2(result);
|
|
1319
|
+
}
|
|
1320
|
+
async getForeignKeys(schemaName, tableName) {
|
|
1321
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
|
|
1322
|
+
con.conname AS name,
|
|
1323
|
+
att.attname AS column_name,
|
|
1324
|
+
fnsp.nspname AS foreign_table_schema,
|
|
1325
|
+
frel.relname AS foreign_table_name,
|
|
1326
|
+
fatt.attname AS foreign_column_name,
|
|
1327
|
+
CASE con.confupdtype
|
|
1328
|
+
WHEN 'a' THEN 'NO ACTION'
|
|
1329
|
+
WHEN 'r' THEN 'RESTRICT'
|
|
1330
|
+
WHEN 'n' THEN 'SET NULL'
|
|
1331
|
+
WHEN 'c' THEN 'CASCADE'
|
|
1332
|
+
WHEN 'd' THEN 'SET DEFAULT'
|
|
1333
|
+
END AS update_rule,
|
|
1334
|
+
CASE con.confdeltype
|
|
1335
|
+
WHEN 'a' THEN 'NO ACTION'
|
|
1336
|
+
WHEN 'r' THEN 'RESTRICT'
|
|
1337
|
+
WHEN 'n' THEN 'SET NULL'
|
|
1338
|
+
WHEN 'c' THEN 'CASCADE'
|
|
1339
|
+
WHEN 'd' THEN 'SET DEFAULT'
|
|
1340
|
+
END AS delete_rule
|
|
1341
|
+
FROM pg_catalog.pg_constraint con
|
|
1342
|
+
JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
|
|
1343
|
+
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
|
|
1344
|
+
LEFT JOIN pg_catalog.pg_attribute att ON att.attnum = ANY (con.conkey)
|
|
1345
|
+
AND att.attrelid = con.conrelid
|
|
1346
|
+
LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
|
|
1347
|
+
LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
|
|
1348
|
+
LEFT JOIN pg_catalog.pg_attribute fatt ON fatt.attnum = ANY (con.confkey)
|
|
1349
|
+
AND fatt.attrelid = con.confrelid
|
|
1350
|
+
WHERE con.contype = 'f'
|
|
1351
|
+
AND nsp.nspname = ${schemaName}
|
|
1352
|
+
AND rel.relname = ${tableName}`);
|
|
1353
|
+
return getRows2(result);
|
|
1354
|
+
}
|
|
1355
|
+
async getPrimaryKeys(schemaName, tableName) {
|
|
1356
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
|
|
1357
|
+
con.conname AS name,
|
|
1358
|
+
ARRAY(
|
|
1359
|
+
SELECT a.attname
|
|
1360
|
+
FROM pg_attribute a
|
|
1361
|
+
WHERE a.attrelid = con.conrelid
|
|
1362
|
+
AND a.attnum = ANY(con.conkey)
|
|
1363
|
+
ORDER BY a.attnum
|
|
1364
|
+
) AS columns
|
|
1365
|
+
FROM pg_constraint con
|
|
1366
|
+
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
1367
|
+
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
1368
|
+
WHERE con.contype = 'p'
|
|
1369
|
+
AND nsp.nspname = ${schemaName}
|
|
1370
|
+
AND rel.relname = ${tableName}`);
|
|
1371
|
+
return getRows2(result);
|
|
1372
|
+
}
|
|
1373
|
+
async getUniqueConstraints(schemaName, tableName) {
|
|
1374
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
|
|
1375
|
+
con.conname AS name,
|
|
1376
|
+
ARRAY(
|
|
1377
|
+
SELECT a.attname
|
|
1378
|
+
FROM pg_attribute a
|
|
1379
|
+
WHERE a.attrelid = con.conrelid
|
|
1380
|
+
AND a.attnum = ANY(con.conkey)
|
|
1381
|
+
ORDER BY a.attnum
|
|
1382
|
+
) AS columns
|
|
1383
|
+
FROM pg_constraint con
|
|
1384
|
+
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
1385
|
+
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
1386
|
+
WHERE con.contype = 'u'
|
|
1387
|
+
AND nsp.nspname = ${schemaName}
|
|
1388
|
+
AND rel.relname = ${tableName}`);
|
|
1389
|
+
return getRows2(result);
|
|
1390
|
+
}
|
|
1391
|
+
async getCheckConstraints(schemaName, tableName) {
|
|
1392
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
|
|
1393
|
+
con.conname AS name,
|
|
1394
|
+
pg_get_constraintdef(con.oid) AS definition
|
|
1395
|
+
FROM pg_constraint con
|
|
1396
|
+
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
1397
|
+
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
1398
|
+
WHERE con.contype = 'c'
|
|
1399
|
+
AND nsp.nspname = ${schemaName}
|
|
1400
|
+
AND rel.relname = ${tableName}`);
|
|
1401
|
+
return getRows2(result);
|
|
1402
|
+
}
|
|
1403
|
+
async getEnums(schemaName) {
|
|
1404
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
|
|
1405
|
+
n.nspname AS schema,
|
|
1406
|
+
t.typname AS name,
|
|
1407
|
+
e.enumlabel AS value,
|
|
1408
|
+
e.enumsortorder AS sort_order
|
|
1409
|
+
FROM pg_type t
|
|
1410
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
1411
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
|
|
1412
|
+
WHERE n.nspname = ${schemaName}
|
|
1413
|
+
ORDER BY schema, name, sort_order`);
|
|
1414
|
+
return getRows2(result);
|
|
1415
|
+
}
|
|
1416
|
+
parseDefault(defaultValue, dataType) {
|
|
1417
|
+
if (!defaultValue)
|
|
1418
|
+
return;
|
|
1419
|
+
const match = defaultValue.match(/^'(.*)'::/);
|
|
1420
|
+
if (match) {
|
|
1421
|
+
return `'${match[1]}'`;
|
|
1422
|
+
}
|
|
1423
|
+
if (defaultValue.includes("nextval(")) {
|
|
1424
|
+
return;
|
|
1425
|
+
}
|
|
1426
|
+
if (dataType === "boolean") {
|
|
1427
|
+
if (defaultValue === "true")
|
|
1428
|
+
return "true";
|
|
1429
|
+
if (defaultValue === "false")
|
|
1430
|
+
return "false";
|
|
1431
|
+
}
|
|
1432
|
+
return defaultValue;
|
|
1433
|
+
}
|
|
1434
|
+
async hasExistingTables(pluginName) {
|
|
1435
|
+
const schemaName = pluginName === "@elizaos/plugin-sql" ? "public" : this.deriveSchemaName(pluginName);
|
|
1436
|
+
const result = await this.db.execute(import_drizzle_orm21.sql`SELECT COUNT(*) AS count
|
|
1437
|
+
FROM information_schema.tables
|
|
1438
|
+
WHERE table_schema = ${schemaName}
|
|
1439
|
+
AND table_type = 'BASE TABLE'`);
|
|
1440
|
+
const firstRow = result.rows?.[0];
|
|
1441
|
+
const count = parseInt(firstRow && firstRow.count || "0", 10);
|
|
1442
|
+
return count > 0;
|
|
1443
|
+
}
|
|
1444
|
+
deriveSchemaName(pluginName) {
|
|
1445
|
+
return pluginName.replace("@", "").replace("/", "_").replace(/-/g, "_").toLowerCase();
|
|
1446
|
+
}
|
|
1447
|
+
}
|
|
1448
|
+
var import_core4, import_drizzle_orm21;
|
|
1449
|
+
var init_database_introspector = __esm(() => {
|
|
1450
|
+
import_core4 = require("@elizaos/core");
|
|
1451
|
+
import_drizzle_orm21 = require("drizzle-orm");
|
|
1452
|
+
});
|
|
1453
|
+
|
|
1454
|
+
// runtime-migrator/drizzle-adapters/diff-calculator.ts
|
|
1455
|
+
var exports_diff_calculator = {};
|
|
1456
|
+
__export(exports_diff_calculator, {
|
|
1457
|
+
hasDiffChanges: () => hasDiffChanges,
|
|
1458
|
+
calculateDiff: () => calculateDiff
|
|
1459
|
+
});
|
|
1460
|
+
function normalizeType(type) {
|
|
1461
|
+
if (!type)
|
|
1462
|
+
return "";
|
|
1463
|
+
const normalized = type.toLowerCase().trim();
|
|
1464
|
+
if (normalized === "timestamp without time zone" || normalized === "timestamp with time zone") {
|
|
1465
|
+
return "timestamp";
|
|
1466
|
+
}
|
|
1467
|
+
if (normalized === "serial") {
|
|
1468
|
+
return "integer";
|
|
1469
|
+
}
|
|
1470
|
+
if (normalized === "bigserial") {
|
|
1471
|
+
return "bigint";
|
|
1472
|
+
}
|
|
1473
|
+
if (normalized === "smallserial") {
|
|
1474
|
+
return "smallint";
|
|
1475
|
+
}
|
|
1476
|
+
if (normalized.startsWith("numeric") || normalized.startsWith("decimal")) {
|
|
1477
|
+
const match = normalized.match(/\((\d+)(?:,\s*(\d+))?\)/);
|
|
1478
|
+
if (match) {
|
|
1479
|
+
return `numeric(${match[1]}${match[2] ? `,${match[2]}` : ""})`;
|
|
1480
|
+
}
|
|
1481
|
+
return "numeric";
|
|
1482
|
+
}
|
|
1483
|
+
if (normalized.startsWith("character varying")) {
|
|
1484
|
+
return normalized.replace("character varying", "varchar");
|
|
1485
|
+
}
|
|
1486
|
+
if (normalized === "text[]" || normalized === "_text") {
|
|
1487
|
+
return "text[]";
|
|
1488
|
+
}
|
|
1489
|
+
return normalized;
|
|
1490
|
+
}
|
|
1491
|
+
function isIndexChanged(prevIndex, currIndex) {
|
|
1492
|
+
if (prevIndex.isUnique !== currIndex.isUnique)
|
|
1493
|
+
return true;
|
|
1494
|
+
if (prevIndex.method !== currIndex.method)
|
|
1495
|
+
return true;
|
|
1496
|
+
if (prevIndex.where !== currIndex.where)
|
|
1497
|
+
return true;
|
|
1498
|
+
if (prevIndex.concurrently !== currIndex.concurrently)
|
|
1499
|
+
return true;
|
|
1500
|
+
const prevColumns = prevIndex.columns || [];
|
|
1501
|
+
const currColumns = currIndex.columns || [];
|
|
1502
|
+
if (prevColumns.length !== currColumns.length)
|
|
1503
|
+
return true;
|
|
1504
|
+
for (let i = 0;i < prevColumns.length; i++) {
|
|
1505
|
+
const prevCol = prevColumns[i];
|
|
1506
|
+
const currCol = currColumns[i];
|
|
1507
|
+
if (typeof prevCol === "string" && typeof currCol === "string") {
|
|
1508
|
+
if (prevCol !== currCol)
|
|
1509
|
+
return true;
|
|
1510
|
+
} else if (typeof prevCol === "object" && typeof currCol === "object") {
|
|
1511
|
+
if (prevCol.expression !== currCol.expression)
|
|
1512
|
+
return true;
|
|
1513
|
+
if (prevCol.isExpression !== currCol.isExpression)
|
|
1514
|
+
return true;
|
|
1515
|
+
if (prevCol.asc !== currCol.asc)
|
|
1516
|
+
return true;
|
|
1517
|
+
if (prevCol.nulls !== currCol.nulls)
|
|
1518
|
+
return true;
|
|
1519
|
+
} else {
|
|
1520
|
+
return true;
|
|
1521
|
+
}
|
|
1522
|
+
}
|
|
1523
|
+
return false;
|
|
1524
|
+
}
|
|
1525
|
+
async function calculateDiff(previousSnapshot, currentSnapshot) {
|
|
1526
|
+
const diff = {
|
|
1527
|
+
tables: {
|
|
1528
|
+
created: [],
|
|
1529
|
+
deleted: [],
|
|
1530
|
+
modified: []
|
|
1531
|
+
},
|
|
1532
|
+
columns: {
|
|
1533
|
+
added: [],
|
|
1534
|
+
deleted: [],
|
|
1535
|
+
modified: []
|
|
1536
|
+
},
|
|
1537
|
+
indexes: {
|
|
1538
|
+
created: [],
|
|
1539
|
+
deleted: [],
|
|
1540
|
+
altered: []
|
|
1541
|
+
},
|
|
1542
|
+
foreignKeys: {
|
|
1543
|
+
created: [],
|
|
1544
|
+
deleted: [],
|
|
1545
|
+
altered: []
|
|
1546
|
+
},
|
|
1547
|
+
uniqueConstraints: {
|
|
1548
|
+
created: [],
|
|
1549
|
+
deleted: []
|
|
1550
|
+
},
|
|
1551
|
+
checkConstraints: {
|
|
1552
|
+
created: [],
|
|
1553
|
+
deleted: []
|
|
1554
|
+
}
|
|
1555
|
+
};
|
|
1556
|
+
if (!previousSnapshot) {
|
|
1557
|
+
diff.tables.created = Object.keys(currentSnapshot.tables);
|
|
1558
|
+
for (const tableName in currentSnapshot.tables) {
|
|
1559
|
+
const table = currentSnapshot.tables[tableName];
|
|
1560
|
+
if (table.indexes) {
|
|
1561
|
+
for (const indexName in table.indexes) {
|
|
1562
|
+
diff.indexes.created.push({
|
|
1563
|
+
...table.indexes[indexName],
|
|
1564
|
+
table: tableName
|
|
1565
|
+
});
|
|
1566
|
+
}
|
|
1567
|
+
}
|
|
1568
|
+
if (table.foreignKeys) {
|
|
1569
|
+
for (const fkName in table.foreignKeys) {
|
|
1570
|
+
diff.foreignKeys.created.push(table.foreignKeys[fkName]);
|
|
1571
|
+
}
|
|
1572
|
+
}
|
|
1573
|
+
}
|
|
1574
|
+
return diff;
|
|
1575
|
+
}
|
|
1576
|
+
const prevTables = previousSnapshot.tables || {};
|
|
1577
|
+
const currTables = currentSnapshot.tables || {};
|
|
1578
|
+
for (const tableName in currTables) {
|
|
1579
|
+
if (!(tableName in prevTables)) {
|
|
1580
|
+
diff.tables.created.push(tableName);
|
|
1581
|
+
const table = currTables[tableName];
|
|
1582
|
+
if (table.indexes) {
|
|
1583
|
+
for (const indexName in table.indexes) {
|
|
1584
|
+
diff.indexes.created.push({
|
|
1585
|
+
...table.indexes[indexName],
|
|
1586
|
+
table: tableName
|
|
1587
|
+
});
|
|
1588
|
+
}
|
|
1589
|
+
}
|
|
1590
|
+
if (table.uniqueConstraints) {
|
|
1591
|
+
for (const uqName in table.uniqueConstraints) {
|
|
1592
|
+
diff.uniqueConstraints.created.push({
|
|
1593
|
+
...table.uniqueConstraints[uqName],
|
|
1594
|
+
table: tableName
|
|
1595
|
+
});
|
|
1596
|
+
}
|
|
1597
|
+
}
|
|
1598
|
+
if (table.checkConstraints) {
|
|
1599
|
+
for (const checkName in table.checkConstraints) {
|
|
1600
|
+
diff.checkConstraints.created.push({
|
|
1601
|
+
...table.checkConstraints[checkName],
|
|
1602
|
+
table: tableName
|
|
1603
|
+
});
|
|
1604
|
+
}
|
|
1605
|
+
}
|
|
1606
|
+
if (table.foreignKeys) {
|
|
1607
|
+
for (const fkName in table.foreignKeys) {
|
|
1608
|
+
diff.foreignKeys.created.push(table.foreignKeys[fkName]);
|
|
1609
|
+
}
|
|
1610
|
+
}
|
|
1611
|
+
}
|
|
1612
|
+
}
|
|
1613
|
+
for (const tableName in prevTables) {
|
|
1614
|
+
if (!(tableName in currTables)) {
|
|
1229
1615
|
diff.tables.deleted.push(tableName);
|
|
1230
1616
|
}
|
|
1231
1617
|
}
|
|
@@ -1396,32 +1782,6 @@ function hasDiffChanges(diff) {
|
|
|
1396
1782
|
return diff.tables.created.length > 0 || diff.tables.deleted.length > 0 || diff.tables.modified.length > 0 || diff.columns.added.length > 0 || diff.columns.deleted.length > 0 || diff.columns.modified.length > 0 || diff.indexes.created.length > 0 || diff.indexes.deleted.length > 0 || diff.indexes.altered.length > 0 || diff.foreignKeys.created.length > 0 || diff.foreignKeys.deleted.length > 0 || diff.foreignKeys.altered.length > 0 || diff.uniqueConstraints.created.length > 0 || diff.uniqueConstraints.deleted.length > 0 || diff.checkConstraints.created.length > 0 || diff.checkConstraints.deleted.length > 0;
|
|
1397
1783
|
}
|
|
1398
1784
|
|
|
1399
|
-
// runtime-migrator/crypto-utils.ts
|
|
1400
|
-
function extendedHash(str) {
|
|
1401
|
-
const h1 = hashWithSeed(str, 5381);
|
|
1402
|
-
const h2 = hashWithSeed(str, 7919);
|
|
1403
|
-
const h3 = hashWithSeed(str, 104729);
|
|
1404
|
-
const h4 = hashWithSeed(str, 224737);
|
|
1405
|
-
return h1 + h2 + h3 + h4;
|
|
1406
|
-
}
|
|
1407
|
-
function hashWithSeed(str, seed) {
|
|
1408
|
-
let hash = seed;
|
|
1409
|
-
for (let i = 0;i < str.length; i++) {
|
|
1410
|
-
hash = hash * 33 ^ str.charCodeAt(i);
|
|
1411
|
-
}
|
|
1412
|
-
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
1413
|
-
}
|
|
1414
|
-
function stringToBigInt(str) {
|
|
1415
|
-
const hash = extendedHash(str);
|
|
1416
|
-
let lockId = BigInt(`0x${hash.slice(0, 16)}`);
|
|
1417
|
-
const mask63Bits = 0x7fffffffffffffffn;
|
|
1418
|
-
lockId = lockId & mask63Bits;
|
|
1419
|
-
if (lockId === 0n) {
|
|
1420
|
-
lockId = 1n;
|
|
1421
|
-
}
|
|
1422
|
-
return lockId;
|
|
1423
|
-
}
|
|
1424
|
-
|
|
1425
1785
|
// runtime-migrator/drizzle-adapters/snapshot-generator.ts
|
|
1426
1786
|
function escapeSingleQuotes(str) {
|
|
1427
1787
|
return str.replace(/'/g, "''");
|
|
@@ -1457,7 +1817,7 @@ function extractTablesFromSchema(schema) {
|
|
|
1457
1817
|
const tables = [];
|
|
1458
1818
|
const exports2 = Object.values(schema);
|
|
1459
1819
|
exports2.forEach((t) => {
|
|
1460
|
-
if (
|
|
1820
|
+
if (import_drizzle_orm22.is(t, import_pg_core21.PgTable)) {
|
|
1461
1821
|
tables.push(t);
|
|
1462
1822
|
}
|
|
1463
1823
|
});
|
|
@@ -1500,7 +1860,7 @@ async function generateSnapshot(schema) {
|
|
|
1500
1860
|
notNull
|
|
1501
1861
|
};
|
|
1502
1862
|
if (column.default !== undefined) {
|
|
1503
|
-
if (
|
|
1863
|
+
if (import_drizzle_orm22.is(column.default, import_drizzle_orm22.SQL)) {
|
|
1504
1864
|
columnToSet.default = sqlToStr(column.default, undefined);
|
|
1505
1865
|
} else {
|
|
1506
1866
|
if (typeof column.default === "string") {
|
|
@@ -1574,7 +1934,7 @@ async function generateSnapshot(schema) {
|
|
|
1574
1934
|
indexes.forEach((idx) => {
|
|
1575
1935
|
const indexCols = idx.config.columns;
|
|
1576
1936
|
const indexColumns = indexCols.map((col) => {
|
|
1577
|
-
if (
|
|
1937
|
+
if (import_drizzle_orm22.is(col, import_drizzle_orm22.SQL)) {
|
|
1578
1938
|
return {
|
|
1579
1939
|
expression: dialect.sqlToQuery(col).sql,
|
|
1580
1940
|
isExpression: true
|
|
@@ -1648,7 +2008,7 @@ function hasChanges(previousSnapshot, currentSnapshot) {
|
|
|
1648
2008
|
const currHash = hashSnapshot(currentSnapshot);
|
|
1649
2009
|
return prevHash !== currHash;
|
|
1650
2010
|
}
|
|
1651
|
-
var
|
|
2011
|
+
var import_drizzle_orm22, import_pg_core21, sqlToStr = (sql22, _casing) => {
|
|
1652
2012
|
const config = {
|
|
1653
2013
|
escapeName: () => {
|
|
1654
2014
|
throw new Error("we don't support params for `sql` default values");
|
|
@@ -1661,10 +2021,10 @@ var import_drizzle_orm21, import_pg_core21, sqlToStr = (sql21, _casing) => {
|
|
|
1661
2021
|
},
|
|
1662
2022
|
casing: undefined
|
|
1663
2023
|
};
|
|
1664
|
-
return
|
|
2024
|
+
return sql22.toQuery(config).sql;
|
|
1665
2025
|
};
|
|
1666
2026
|
var init_snapshot_generator = __esm(() => {
|
|
1667
|
-
|
|
2027
|
+
import_drizzle_orm22 = require("drizzle-orm");
|
|
1668
2028
|
import_pg_core21 = require("drizzle-orm/pg-core");
|
|
1669
2029
|
});
|
|
1670
2030
|
|
|
@@ -1798,7 +2158,7 @@ async function generateMigrationSQL(previousSnapshot, currentSnapshot, diff) {
|
|
|
1798
2158
|
}
|
|
1799
2159
|
const dataLossCheck = checkForDataLoss(diff);
|
|
1800
2160
|
if (dataLossCheck.warnings.length > 0) {
|
|
1801
|
-
|
|
2161
|
+
import_core5.logger.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
|
|
1802
2162
|
}
|
|
1803
2163
|
const schemasToCreate = new Set;
|
|
1804
2164
|
for (const tableName of diff.tables.created) {
|
|
@@ -1951,574 +2311,235 @@ function generateCreateTableSQL(fullTableName, table) {
|
|
|
1951
2311
|
const foreignKeys = table.foreignKeys || {};
|
|
1952
2312
|
for (const [fkName, fkDef] of Object.entries(foreignKeys)) {
|
|
1953
2313
|
const fk = fkDef;
|
|
1954
|
-
const fkSQL = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${fkName}" FOREIGN KEY (${fk.columnsFrom.map((c) => `"${c}"`).join(", ")}) REFERENCES "${fk.schemaTo || "public"}"."${fk.tableTo}" (${fk.columnsTo.map((c) => `"${c}"`).join(", ")})${fk.onDelete ? ` ON DELETE ${fk.onDelete}` : ""}${fk.onUpdate ? ` ON UPDATE ${fk.onUpdate}` : ""};`;
|
|
1955
|
-
fkSQLs.push(fkSQL);
|
|
1956
|
-
}
|
|
1957
|
-
return { tableSQL, fkSQLs };
|
|
1958
|
-
}
|
|
1959
|
-
function generateColumnDefinition(name, def) {
|
|
1960
|
-
let sql21 = `"${name}" ${def.type}`;
|
|
1961
|
-
if (def.primaryKey && !def.type.includes("SERIAL")) {
|
|
1962
|
-
sql21 += " PRIMARY KEY";
|
|
1963
|
-
}
|
|
1964
|
-
if (def.notNull) {
|
|
1965
|
-
sql21 += " NOT NULL";
|
|
1966
|
-
}
|
|
1967
|
-
if (def.default !== undefined) {
|
|
1968
|
-
const defaultValue = formatDefaultValue(def.default, def.type);
|
|
1969
|
-
sql21 += ` DEFAULT ${defaultValue}`;
|
|
1970
|
-
}
|
|
1971
|
-
return sql21;
|
|
1972
|
-
}
|
|
1973
|
-
function generateAddColumnSQL(table, column, definition) {
|
|
1974
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
1975
|
-
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
1976
|
-
const parts = [`"${column}"`];
|
|
1977
|
-
parts.push(definition.type);
|
|
1978
|
-
if (definition.primaryKey) {
|
|
1979
|
-
parts.push("PRIMARY KEY");
|
|
1980
|
-
}
|
|
1981
|
-
if (definition.default !== undefined) {
|
|
1982
|
-
const defaultValue = formatDefaultValue(definition.default, definition.type);
|
|
1983
|
-
if (defaultValue) {
|
|
1984
|
-
parts.push(`DEFAULT ${defaultValue}`);
|
|
1985
|
-
}
|
|
1986
|
-
}
|
|
1987
|
-
const definitionWithGenerated = definition;
|
|
1988
|
-
if (definitionWithGenerated.generated) {
|
|
1989
|
-
parts.push(`GENERATED ALWAYS AS (${definitionWithGenerated.generated}) STORED`);
|
|
1990
|
-
}
|
|
1991
|
-
if (definition.notNull) {
|
|
1992
|
-
parts.push("NOT NULL");
|
|
1993
|
-
}
|
|
1994
|
-
return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
|
|
1995
|
-
}
|
|
1996
|
-
function generateDropColumnSQL(table, column) {
|
|
1997
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
1998
|
-
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
1999
|
-
return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
|
|
2000
|
-
}
|
|
2001
|
-
function generateAlterColumnSQL(table, column, changes) {
|
|
2002
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2003
|
-
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
2004
|
-
const statements = [];
|
|
2005
|
-
const changesTo = changes.to;
|
|
2006
|
-
const changesFrom = changes.from;
|
|
2007
|
-
const changesToType = changesTo?.type;
|
|
2008
|
-
const changesFromType = changesFrom?.type;
|
|
2009
|
-
if (changesToType !== changesFromType) {
|
|
2010
|
-
const newType = changesToType || "TEXT";
|
|
2011
|
-
const needsUsing = checkIfNeedsUsingClause(changesFromType || "", newType);
|
|
2012
|
-
if (needsUsing) {
|
|
2013
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" TYPE ${newType} USING "${column}"::text::${newType};`);
|
|
2014
|
-
} else {
|
|
2015
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DATA TYPE ${newType};`);
|
|
2016
|
-
}
|
|
2017
|
-
}
|
|
2018
|
-
const changesToNotNull = changesTo?.notNull;
|
|
2019
|
-
const changesFromNotNull = changesFrom?.notNull;
|
|
2020
|
-
if (changesToNotNull !== changesFromNotNull) {
|
|
2021
|
-
if (changesToNotNull) {
|
|
2022
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET NOT NULL;`);
|
|
2023
|
-
} else {
|
|
2024
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP NOT NULL;`);
|
|
2025
|
-
}
|
|
2026
|
-
}
|
|
2027
|
-
const changesToDefault = changesTo?.default;
|
|
2028
|
-
const changesFromDefault = changesFrom?.default;
|
|
2029
|
-
if (changesToDefault !== changesFromDefault) {
|
|
2030
|
-
if (changesToDefault !== undefined) {
|
|
2031
|
-
const defaultValue = formatDefaultValue(changesToDefault, changesToType || "");
|
|
2032
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DEFAULT ${defaultValue};`);
|
|
2033
|
-
} else {
|
|
2034
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP DEFAULT;`);
|
|
2035
|
-
}
|
|
2036
|
-
}
|
|
2037
|
-
return statements;
|
|
2038
|
-
}
|
|
2039
|
-
function checkIfNeedsUsingClause(fromType, toType) {
|
|
2040
|
-
if (!fromType || !toType)
|
|
2041
|
-
return false;
|
|
2042
|
-
if (fromType.includes("enum") || toType.includes("enum")) {
|
|
2043
|
-
return true;
|
|
2044
|
-
}
|
|
2045
|
-
const fromBase = fromType.split("(")[0].toLowerCase();
|
|
2046
|
-
const toBase = toType.split("(")[0].toLowerCase();
|
|
2047
|
-
if ((fromBase === "text" || fromBase === "varchar" || fromBase === "character varying") && (toBase === "jsonb" || toBase === "json")) {
|
|
2048
|
-
return true;
|
|
2049
|
-
}
|
|
2050
|
-
const needsUsingPairs = [
|
|
2051
|
-
["integer", "boolean"],
|
|
2052
|
-
["boolean", "integer"],
|
|
2053
|
-
["text", "integer"],
|
|
2054
|
-
["text", "numeric"],
|
|
2055
|
-
["text", "boolean"],
|
|
2056
|
-
["text", "uuid"],
|
|
2057
|
-
["text", "jsonb"],
|
|
2058
|
-
["text", "json"],
|
|
2059
|
-
["varchar", "integer"],
|
|
2060
|
-
["varchar", "numeric"],
|
|
2061
|
-
["varchar", "boolean"],
|
|
2062
|
-
["varchar", "uuid"],
|
|
2063
|
-
["varchar", "jsonb"],
|
|
2064
|
-
["varchar", "json"],
|
|
2065
|
-
["character varying", "jsonb"],
|
|
2066
|
-
["character varying", "json"]
|
|
2067
|
-
];
|
|
2068
|
-
for (const [from, to] of needsUsingPairs) {
|
|
2069
|
-
if (fromBase === from && toBase === to || fromBase === to && toBase === from) {
|
|
2070
|
-
return true;
|
|
2071
|
-
}
|
|
2072
|
-
}
|
|
2073
|
-
return false;
|
|
2074
|
-
}
|
|
2075
|
-
function formatDefaultValue(value, type) {
|
|
2076
|
-
if (value === null || value === "NULL") {
|
|
2077
|
-
return "NULL";
|
|
2078
|
-
}
|
|
2079
|
-
if (type && (type.toLowerCase().includes("boolean") || type.toLowerCase() === "bool")) {
|
|
2080
|
-
if (value === true || value === "true" || value === "t" || value === 1) {
|
|
2081
|
-
return "true";
|
|
2082
|
-
}
|
|
2083
|
-
if (value === false || value === "false" || value === "f" || value === 0) {
|
|
2084
|
-
return "false";
|
|
2085
|
-
}
|
|
2086
|
-
}
|
|
2087
|
-
if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
|
|
2088
|
-
return String(value);
|
|
2089
|
-
}
|
|
2090
|
-
if (typeof value === "string") {
|
|
2091
|
-
if (value.includes("::")) {
|
|
2092
|
-
return value;
|
|
2093
|
-
}
|
|
2094
|
-
if (value.startsWith("'") && value.endsWith("'")) {
|
|
2095
|
-
return value;
|
|
2096
|
-
}
|
|
2097
|
-
if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
|
|
2098
|
-
return value;
|
|
2099
|
-
}
|
|
2100
|
-
if (value.toUpperCase().startsWith("CURRENT_")) {
|
|
2101
|
-
return value;
|
|
2102
|
-
}
|
|
2103
|
-
return `'${value.replace(/'/g, "''")}'`;
|
|
2104
|
-
}
|
|
2105
|
-
return String(value);
|
|
2106
|
-
}
|
|
2107
|
-
function generateCreateIndexSQL(index7) {
|
|
2108
|
-
const unique3 = index7.isUnique ? "UNIQUE " : "";
|
|
2109
|
-
const method = index7.method || "btree";
|
|
2110
|
-
const columns = index7.columns.map((c) => {
|
|
2111
|
-
if (c.isExpression) {
|
|
2112
|
-
return c.expression;
|
|
2113
|
-
}
|
|
2114
|
-
return `"${c.expression}"${c.asc === false ? " DESC" : ""}`;
|
|
2115
|
-
}).join(", ");
|
|
2116
|
-
const indexName = index7.name.includes(".") ? index7.name.split(".")[1] : index7.name;
|
|
2117
|
-
let tableRef;
|
|
2118
|
-
const indexTable = index7.table;
|
|
2119
|
-
if (indexTable?.includes(".")) {
|
|
2120
|
-
const [schema, table] = indexTable.split(".");
|
|
2121
|
-
tableRef = `"${schema}"."${table}"`;
|
|
2122
|
-
} else {
|
|
2123
|
-
tableRef = `"${indexTable || ""}"`;
|
|
2124
|
-
}
|
|
2125
|
-
return `CREATE ${unique3}INDEX "${indexName}" ON ${tableRef} USING ${method} (${columns});`;
|
|
2126
|
-
}
|
|
2127
|
-
function generateDropIndexSQL(index7) {
|
|
2128
|
-
const indexNameFull = typeof index7 === "string" ? index7 : index7.name;
|
|
2129
|
-
const indexName = indexNameFull.includes(".") ? indexNameFull.split(".")[1] : indexNameFull;
|
|
2130
|
-
return `DROP INDEX IF EXISTS "${indexName}";`;
|
|
2131
|
-
}
|
|
2132
|
-
function generateCreateForeignKeySQL(fk) {
|
|
2133
|
-
const schemaFrom = fk.schemaFrom || "public";
|
|
2134
|
-
const schemaTo = fk.schemaTo || "public";
|
|
2135
|
-
const tableFrom = fk.tableFrom;
|
|
2136
|
-
const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
|
|
2137
|
-
const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
|
|
2138
|
-
let sql21 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
|
|
2139
|
-
if (fk.onDelete) {
|
|
2140
|
-
sql21 += ` ON DELETE ${fk.onDelete}`;
|
|
2141
|
-
}
|
|
2142
|
-
if (fk.onUpdate) {
|
|
2143
|
-
sql21 += ` ON UPDATE ${fk.onUpdate}`;
|
|
2144
|
-
}
|
|
2145
|
-
return `${sql21};`;
|
|
2146
|
-
}
|
|
2147
|
-
function generateDropForeignKeySQL(fk) {
|
|
2148
|
-
const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
|
|
2149
|
-
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${fk.name}";`;
|
|
2150
|
-
}
|
|
2151
|
-
function generateCreateUniqueConstraintSQL(constraint) {
|
|
2152
|
-
const table = constraint.table || "";
|
|
2153
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2154
|
-
const name = constraint.name;
|
|
2155
|
-
const columns = constraint.columns.map((c) => `"${c}"`).join(", ");
|
|
2156
|
-
let sql21 = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" UNIQUE`;
|
|
2157
|
-
if (constraint.nullsNotDistinct) {
|
|
2158
|
-
sql21 += ` NULLS NOT DISTINCT`;
|
|
2159
|
-
}
|
|
2160
|
-
sql21 += ` (${columns});`;
|
|
2161
|
-
return sql21;
|
|
2162
|
-
}
|
|
2163
|
-
function generateDropUniqueConstraintSQL(constraint) {
|
|
2164
|
-
const table = constraint.table || "";
|
|
2165
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2166
|
-
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
|
|
2314
|
+
const fkSQL = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${fkName}" FOREIGN KEY (${fk.columnsFrom.map((c) => `"${c}"`).join(", ")}) REFERENCES "${fk.schemaTo || "public"}"."${fk.tableTo}" (${fk.columnsTo.map((c) => `"${c}"`).join(", ")})${fk.onDelete ? ` ON DELETE ${fk.onDelete}` : ""}${fk.onUpdate ? ` ON UPDATE ${fk.onUpdate}` : ""};`;
|
|
2315
|
+
fkSQLs.push(fkSQL);
|
|
2316
|
+
}
|
|
2317
|
+
return { tableSQL, fkSQLs };
|
|
2167
2318
|
}
|
|
2168
|
-
function
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2319
|
+
function generateColumnDefinition(name, def) {
|
|
2320
|
+
let sql22 = `"${name}" ${def.type}`;
|
|
2321
|
+
if (def.primaryKey && !def.type.includes("SERIAL")) {
|
|
2322
|
+
sql22 += " PRIMARY KEY";
|
|
2323
|
+
}
|
|
2324
|
+
if (def.notNull) {
|
|
2325
|
+
sql22 += " NOT NULL";
|
|
2326
|
+
}
|
|
2327
|
+
if (def.default !== undefined) {
|
|
2328
|
+
const defaultValue = formatDefaultValue(def.default, def.type);
|
|
2329
|
+
sql22 += ` DEFAULT ${defaultValue}`;
|
|
2330
|
+
}
|
|
2331
|
+
return sql22;
|
|
2174
2332
|
}
|
|
2175
|
-
function
|
|
2176
|
-
const table = constraint.table || "";
|
|
2333
|
+
function generateAddColumnSQL(table, column, definition) {
|
|
2177
2334
|
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2178
|
-
|
|
2179
|
-
}
|
|
2180
|
-
|
|
2181
|
-
|
|
2182
|
-
|
|
2183
|
-
});
|
|
2184
|
-
|
|
2185
|
-
// runtime-migrator/drizzle-adapters/database-introspector.ts
|
|
2186
|
-
function getRows2(result) {
|
|
2187
|
-
return result.rows;
|
|
2188
|
-
}
|
|
2189
|
-
|
|
2190
|
-
class DatabaseIntrospector {
|
|
2191
|
-
db;
|
|
2192
|
-
constructor(db) {
|
|
2193
|
-
this.db = db;
|
|
2335
|
+
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
2336
|
+
const parts = [`"${column}"`];
|
|
2337
|
+
parts.push(definition.type);
|
|
2338
|
+
if (definition.primaryKey) {
|
|
2339
|
+
parts.push("PRIMARY KEY");
|
|
2194
2340
|
}
|
|
2195
|
-
|
|
2196
|
-
|
|
2197
|
-
|
|
2198
|
-
|
|
2199
|
-
const enums = {};
|
|
2200
|
-
const allTables = await this.getTables(schemaName);
|
|
2201
|
-
for (const tableInfo of allTables) {
|
|
2202
|
-
const tableName = tableInfo.table_name;
|
|
2203
|
-
const tableSchema = tableInfo.table_schema || "public";
|
|
2204
|
-
import_core5.logger.debug({ src: "plugin:sql", tableSchema, tableName }, "Introspecting table");
|
|
2205
|
-
const columns = await this.getColumns(tableSchema, tableName);
|
|
2206
|
-
const columnsObject = {};
|
|
2207
|
-
const uniqueConstraintObject = {};
|
|
2208
|
-
for (const col of columns) {
|
|
2209
|
-
columnsObject[col.column_name] = {
|
|
2210
|
-
name: col.column_name,
|
|
2211
|
-
type: col.data_type,
|
|
2212
|
-
primaryKey: col.is_primary || false,
|
|
2213
|
-
notNull: col.is_nullable === "NO",
|
|
2214
|
-
default: col.column_default ? this.parseDefault(col.column_default, col.data_type) : undefined
|
|
2215
|
-
};
|
|
2216
|
-
}
|
|
2217
|
-
const indexes = await this.getIndexes(tableSchema, tableName);
|
|
2218
|
-
const indexesObject = {};
|
|
2219
|
-
for (const idx of indexes) {
|
|
2220
|
-
if (!idx.is_primary && !idx.is_unique_constraint) {
|
|
2221
|
-
if (idx.columns && Array.isArray(idx.columns) && idx.columns.length > 0) {
|
|
2222
|
-
indexesObject[idx.name] = {
|
|
2223
|
-
name: idx.name,
|
|
2224
|
-
columns: idx.columns.map((col) => ({
|
|
2225
|
-
expression: col,
|
|
2226
|
-
isExpression: false
|
|
2227
|
-
})),
|
|
2228
|
-
isUnique: idx.is_unique,
|
|
2229
|
-
method: idx.method || "btree"
|
|
2230
|
-
};
|
|
2231
|
-
}
|
|
2232
|
-
}
|
|
2233
|
-
}
|
|
2234
|
-
const foreignKeys = await this.getForeignKeys(tableSchema, tableName);
|
|
2235
|
-
const foreignKeysObject = {};
|
|
2236
|
-
for (const fk of foreignKeys) {
|
|
2237
|
-
foreignKeysObject[fk.name] = {
|
|
2238
|
-
name: fk.name,
|
|
2239
|
-
tableFrom: tableName,
|
|
2240
|
-
schemaFrom: tableSchema,
|
|
2241
|
-
tableTo: fk.foreign_table_name,
|
|
2242
|
-
schemaTo: fk.foreign_table_schema || "public",
|
|
2243
|
-
columnsFrom: [fk.column_name],
|
|
2244
|
-
columnsTo: [fk.foreign_column_name],
|
|
2245
|
-
onDelete: fk.delete_rule?.toLowerCase() || "no action",
|
|
2246
|
-
onUpdate: fk.update_rule?.toLowerCase() || "no action"
|
|
2247
|
-
};
|
|
2248
|
-
}
|
|
2249
|
-
const primaryKeys = await this.getPrimaryKeys(tableSchema, tableName);
|
|
2250
|
-
const primaryKeysObject = {};
|
|
2251
|
-
for (const pk of primaryKeys) {
|
|
2252
|
-
primaryKeysObject[pk.name] = {
|
|
2253
|
-
name: pk.name,
|
|
2254
|
-
columns: pk.columns
|
|
2255
|
-
};
|
|
2256
|
-
}
|
|
2257
|
-
const uniqueConstraints = await this.getUniqueConstraints(tableSchema, tableName);
|
|
2258
|
-
for (const unq of uniqueConstraints) {
|
|
2259
|
-
uniqueConstraintObject[unq.name] = {
|
|
2260
|
-
name: unq.name,
|
|
2261
|
-
columns: unq.columns,
|
|
2262
|
-
nullsNotDistinct: false
|
|
2263
|
-
};
|
|
2264
|
-
}
|
|
2265
|
-
const checkConstraints = await this.getCheckConstraints(tableSchema, tableName);
|
|
2266
|
-
const checksObject = {};
|
|
2267
|
-
for (const check3 of checkConstraints) {
|
|
2268
|
-
checksObject[check3.name] = {
|
|
2269
|
-
name: check3.name,
|
|
2270
|
-
value: check3.definition
|
|
2271
|
-
};
|
|
2272
|
-
}
|
|
2273
|
-
tables[`${tableSchema}.${tableName}`] = {
|
|
2274
|
-
name: tableName,
|
|
2275
|
-
schema: tableSchema,
|
|
2276
|
-
columns: columnsObject,
|
|
2277
|
-
indexes: indexesObject,
|
|
2278
|
-
foreignKeys: foreignKeysObject,
|
|
2279
|
-
compositePrimaryKeys: primaryKeysObject,
|
|
2280
|
-
uniqueConstraints: uniqueConstraintObject,
|
|
2281
|
-
checkConstraints: checksObject
|
|
2282
|
-
};
|
|
2283
|
-
if (tableSchema && tableSchema !== "public") {
|
|
2284
|
-
schemas[tableSchema] = tableSchema;
|
|
2285
|
-
}
|
|
2286
|
-
}
|
|
2287
|
-
const enumsResult = await this.getEnums(schemaName);
|
|
2288
|
-
for (const enumInfo of enumsResult) {
|
|
2289
|
-
const key = `${enumInfo.schema}.${enumInfo.name}`;
|
|
2290
|
-
if (!enums[key]) {
|
|
2291
|
-
enums[key] = {
|
|
2292
|
-
name: enumInfo.name,
|
|
2293
|
-
schema: enumInfo.schema,
|
|
2294
|
-
values: []
|
|
2295
|
-
};
|
|
2296
|
-
}
|
|
2297
|
-
enums[key].values.push(enumInfo.value);
|
|
2341
|
+
if (definition.default !== undefined) {
|
|
2342
|
+
const defaultValue = formatDefaultValue(definition.default, definition.type);
|
|
2343
|
+
if (defaultValue) {
|
|
2344
|
+
parts.push(`DEFAULT ${defaultValue}`);
|
|
2298
2345
|
}
|
|
2299
|
-
import_core5.logger.info({ src: "plugin:sql", tableCount: Object.keys(tables).length }, "Database introspection complete");
|
|
2300
|
-
return {
|
|
2301
|
-
version: "7",
|
|
2302
|
-
dialect: "postgresql",
|
|
2303
|
-
tables,
|
|
2304
|
-
schemas,
|
|
2305
|
-
enums,
|
|
2306
|
-
_meta: {
|
|
2307
|
-
schemas: {},
|
|
2308
|
-
tables: {},
|
|
2309
|
-
columns: {}
|
|
2310
|
-
}
|
|
2311
|
-
};
|
|
2312
2346
|
}
|
|
2313
|
-
|
|
2314
|
-
|
|
2315
|
-
|
|
2316
|
-
table_name
|
|
2317
|
-
FROM information_schema.tables
|
|
2318
|
-
WHERE table_schema = ${schemaName}
|
|
2319
|
-
AND table_type = 'BASE TABLE'
|
|
2320
|
-
ORDER BY table_name`);
|
|
2321
|
-
return getRows2(result);
|
|
2347
|
+
const definitionWithGenerated = definition;
|
|
2348
|
+
if (definitionWithGenerated.generated) {
|
|
2349
|
+
parts.push(`GENERATED ALWAYS AS (${definitionWithGenerated.generated}) STORED`);
|
|
2322
2350
|
}
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
a.attname AS column_name,
|
|
2326
|
-
CASE
|
|
2327
|
-
WHEN a.attnotnull THEN 'NO'
|
|
2328
|
-
ELSE 'YES'
|
|
2329
|
-
END AS is_nullable,
|
|
2330
|
-
CASE
|
|
2331
|
-
WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
|
|
2332
|
-
AND EXISTS (
|
|
2333
|
-
SELECT FROM pg_attrdef ad
|
|
2334
|
-
WHERE ad.adrelid = a.attrelid
|
|
2335
|
-
AND ad.adnum = a.attnum
|
|
2336
|
-
AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
|
|
2337
|
-
|| pg_get_serial_sequence(a.attrelid::regclass::text, a.attname)::regclass || '''::regclass)'
|
|
2338
|
-
)
|
|
2339
|
-
THEN CASE a.atttypid
|
|
2340
|
-
WHEN 'int'::regtype THEN 'serial'
|
|
2341
|
-
WHEN 'int8'::regtype THEN 'bigserial'
|
|
2342
|
-
WHEN 'int2'::regtype THEN 'smallserial'
|
|
2343
|
-
END
|
|
2344
|
-
ELSE format_type(a.atttypid, a.atttypmod)
|
|
2345
|
-
END AS data_type,
|
|
2346
|
-
pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
|
|
2347
|
-
CASE
|
|
2348
|
-
WHEN con.contype = 'p' THEN true
|
|
2349
|
-
ELSE false
|
|
2350
|
-
END AS is_primary
|
|
2351
|
-
FROM pg_attribute a
|
|
2352
|
-
JOIN pg_class cls ON cls.oid = a.attrelid
|
|
2353
|
-
JOIN pg_namespace ns ON ns.oid = cls.relnamespace
|
|
2354
|
-
LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
|
|
2355
|
-
LEFT JOIN pg_constraint con ON con.conrelid = a.attrelid
|
|
2356
|
-
AND a.attnum = ANY(con.conkey)
|
|
2357
|
-
AND con.contype = 'p'
|
|
2358
|
-
WHERE
|
|
2359
|
-
a.attnum > 0
|
|
2360
|
-
AND NOT a.attisdropped
|
|
2361
|
-
AND ns.nspname = ${schemaName}
|
|
2362
|
-
AND cls.relname = ${tableName}
|
|
2363
|
-
ORDER BY a.attnum`);
|
|
2364
|
-
return getRows2(result);
|
|
2351
|
+
if (definition.notNull) {
|
|
2352
|
+
parts.push("NOT NULL");
|
|
2365
2353
|
}
|
|
2366
|
-
|
|
2367
|
-
|
|
2368
|
-
|
|
2369
|
-
|
|
2370
|
-
|
|
2371
|
-
|
|
2372
|
-
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2376
|
-
|
|
2377
|
-
|
|
2378
|
-
|
|
2379
|
-
|
|
2380
|
-
|
|
2381
|
-
|
|
2382
|
-
|
|
2383
|
-
|
|
2384
|
-
|
|
2385
|
-
|
|
2386
|
-
|
|
2387
|
-
|
|
2388
|
-
|
|
2354
|
+
return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
|
|
2355
|
+
}
|
|
2356
|
+
function generateDropColumnSQL(table, column) {
|
|
2357
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2358
|
+
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
2359
|
+
return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
|
|
2360
|
+
}
|
|
2361
|
+
function generateAlterColumnSQL(table, column, changes) {
|
|
2362
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2363
|
+
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
2364
|
+
const statements = [];
|
|
2365
|
+
const changesTo = changes.to;
|
|
2366
|
+
const changesFrom = changes.from;
|
|
2367
|
+
const changesToType = changesTo?.type;
|
|
2368
|
+
const changesFromType = changesFrom?.type;
|
|
2369
|
+
if (changesToType !== changesFromType) {
|
|
2370
|
+
const newType = changesToType || "TEXT";
|
|
2371
|
+
const needsUsing = checkIfNeedsUsingClause(changesFromType || "", newType);
|
|
2372
|
+
if (needsUsing) {
|
|
2373
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" TYPE ${newType} USING "${column}"::text::${newType};`);
|
|
2374
|
+
} else {
|
|
2375
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DATA TYPE ${newType};`);
|
|
2376
|
+
}
|
|
2389
2377
|
}
|
|
2390
|
-
|
|
2391
|
-
|
|
2392
|
-
|
|
2393
|
-
|
|
2394
|
-
|
|
2395
|
-
|
|
2396
|
-
|
|
2397
|
-
|
|
2398
|
-
WHEN 'a' THEN 'NO ACTION'
|
|
2399
|
-
WHEN 'r' THEN 'RESTRICT'
|
|
2400
|
-
WHEN 'n' THEN 'SET NULL'
|
|
2401
|
-
WHEN 'c' THEN 'CASCADE'
|
|
2402
|
-
WHEN 'd' THEN 'SET DEFAULT'
|
|
2403
|
-
END AS update_rule,
|
|
2404
|
-
CASE con.confdeltype
|
|
2405
|
-
WHEN 'a' THEN 'NO ACTION'
|
|
2406
|
-
WHEN 'r' THEN 'RESTRICT'
|
|
2407
|
-
WHEN 'n' THEN 'SET NULL'
|
|
2408
|
-
WHEN 'c' THEN 'CASCADE'
|
|
2409
|
-
WHEN 'd' THEN 'SET DEFAULT'
|
|
2410
|
-
END AS delete_rule
|
|
2411
|
-
FROM pg_catalog.pg_constraint con
|
|
2412
|
-
JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
|
|
2413
|
-
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
|
|
2414
|
-
LEFT JOIN pg_catalog.pg_attribute att ON att.attnum = ANY (con.conkey)
|
|
2415
|
-
AND att.attrelid = con.conrelid
|
|
2416
|
-
LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
|
|
2417
|
-
LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
|
|
2418
|
-
LEFT JOIN pg_catalog.pg_attribute fatt ON fatt.attnum = ANY (con.confkey)
|
|
2419
|
-
AND fatt.attrelid = con.confrelid
|
|
2420
|
-
WHERE con.contype = 'f'
|
|
2421
|
-
AND nsp.nspname = ${schemaName}
|
|
2422
|
-
AND rel.relname = ${tableName}`);
|
|
2423
|
-
return getRows2(result);
|
|
2378
|
+
const changesToNotNull = changesTo?.notNull;
|
|
2379
|
+
const changesFromNotNull = changesFrom?.notNull;
|
|
2380
|
+
if (changesToNotNull !== changesFromNotNull) {
|
|
2381
|
+
if (changesToNotNull) {
|
|
2382
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET NOT NULL;`);
|
|
2383
|
+
} else {
|
|
2384
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP NOT NULL;`);
|
|
2385
|
+
}
|
|
2424
2386
|
}
|
|
2425
|
-
|
|
2426
|
-
|
|
2427
|
-
|
|
2428
|
-
|
|
2429
|
-
|
|
2430
|
-
|
|
2431
|
-
|
|
2432
|
-
|
|
2433
|
-
|
|
2434
|
-
) AS columns
|
|
2435
|
-
FROM pg_constraint con
|
|
2436
|
-
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
2437
|
-
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
2438
|
-
WHERE con.contype = 'p'
|
|
2439
|
-
AND nsp.nspname = ${schemaName}
|
|
2440
|
-
AND rel.relname = ${tableName}`);
|
|
2441
|
-
return getRows2(result);
|
|
2387
|
+
const changesToDefault = changesTo?.default;
|
|
2388
|
+
const changesFromDefault = changesFrom?.default;
|
|
2389
|
+
if (changesToDefault !== changesFromDefault) {
|
|
2390
|
+
if (changesToDefault !== undefined) {
|
|
2391
|
+
const defaultValue = formatDefaultValue(changesToDefault, changesToType || "");
|
|
2392
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DEFAULT ${defaultValue};`);
|
|
2393
|
+
} else {
|
|
2394
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP DEFAULT;`);
|
|
2395
|
+
}
|
|
2442
2396
|
}
|
|
2443
|
-
|
|
2444
|
-
|
|
2445
|
-
|
|
2446
|
-
|
|
2447
|
-
|
|
2448
|
-
|
|
2449
|
-
|
|
2450
|
-
AND a.attnum = ANY(con.conkey)
|
|
2451
|
-
ORDER BY a.attnum
|
|
2452
|
-
) AS columns
|
|
2453
|
-
FROM pg_constraint con
|
|
2454
|
-
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
2455
|
-
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
2456
|
-
WHERE con.contype = 'u'
|
|
2457
|
-
AND nsp.nspname = ${schemaName}
|
|
2458
|
-
AND rel.relname = ${tableName}`);
|
|
2459
|
-
return getRows2(result);
|
|
2397
|
+
return statements;
|
|
2398
|
+
}
|
|
2399
|
+
function checkIfNeedsUsingClause(fromType, toType) {
|
|
2400
|
+
if (!fromType || !toType)
|
|
2401
|
+
return false;
|
|
2402
|
+
if (fromType.includes("enum") || toType.includes("enum")) {
|
|
2403
|
+
return true;
|
|
2460
2404
|
}
|
|
2461
|
-
|
|
2462
|
-
|
|
2463
|
-
|
|
2464
|
-
|
|
2465
|
-
FROM pg_constraint con
|
|
2466
|
-
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
2467
|
-
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
2468
|
-
WHERE con.contype = 'c'
|
|
2469
|
-
AND nsp.nspname = ${schemaName}
|
|
2470
|
-
AND rel.relname = ${tableName}`);
|
|
2471
|
-
return getRows2(result);
|
|
2405
|
+
const fromBase = fromType.split("(")[0].toLowerCase();
|
|
2406
|
+
const toBase = toType.split("(")[0].toLowerCase();
|
|
2407
|
+
if ((fromBase === "text" || fromBase === "varchar" || fromBase === "character varying") && (toBase === "jsonb" || toBase === "json")) {
|
|
2408
|
+
return true;
|
|
2472
2409
|
}
|
|
2473
|
-
|
|
2474
|
-
|
|
2475
|
-
|
|
2476
|
-
|
|
2477
|
-
|
|
2478
|
-
|
|
2479
|
-
|
|
2480
|
-
|
|
2481
|
-
|
|
2482
|
-
|
|
2483
|
-
|
|
2484
|
-
|
|
2410
|
+
const needsUsingPairs = [
|
|
2411
|
+
["integer", "boolean"],
|
|
2412
|
+
["boolean", "integer"],
|
|
2413
|
+
["text", "integer"],
|
|
2414
|
+
["text", "numeric"],
|
|
2415
|
+
["text", "boolean"],
|
|
2416
|
+
["text", "uuid"],
|
|
2417
|
+
["text", "jsonb"],
|
|
2418
|
+
["text", "json"],
|
|
2419
|
+
["varchar", "integer"],
|
|
2420
|
+
["varchar", "numeric"],
|
|
2421
|
+
["varchar", "boolean"],
|
|
2422
|
+
["varchar", "uuid"],
|
|
2423
|
+
["varchar", "jsonb"],
|
|
2424
|
+
["varchar", "json"],
|
|
2425
|
+
["character varying", "jsonb"],
|
|
2426
|
+
["character varying", "json"]
|
|
2427
|
+
];
|
|
2428
|
+
for (const [from, to] of needsUsingPairs) {
|
|
2429
|
+
if (fromBase === from && toBase === to || fromBase === to && toBase === from) {
|
|
2430
|
+
return true;
|
|
2431
|
+
}
|
|
2485
2432
|
}
|
|
2486
|
-
|
|
2487
|
-
|
|
2488
|
-
|
|
2489
|
-
|
|
2490
|
-
|
|
2491
|
-
|
|
2433
|
+
return false;
|
|
2434
|
+
}
|
|
2435
|
+
function formatDefaultValue(value, type) {
|
|
2436
|
+
if (value === null || value === "NULL") {
|
|
2437
|
+
return "NULL";
|
|
2438
|
+
}
|
|
2439
|
+
if (type && (type.toLowerCase().includes("boolean") || type.toLowerCase() === "bool")) {
|
|
2440
|
+
if (value === true || value === "true" || value === "t" || value === 1) {
|
|
2441
|
+
return "true";
|
|
2492
2442
|
}
|
|
2493
|
-
if (
|
|
2494
|
-
return;
|
|
2443
|
+
if (value === false || value === "false" || value === "f" || value === 0) {
|
|
2444
|
+
return "false";
|
|
2495
2445
|
}
|
|
2496
|
-
|
|
2497
|
-
|
|
2498
|
-
|
|
2499
|
-
|
|
2500
|
-
|
|
2446
|
+
}
|
|
2447
|
+
if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
|
|
2448
|
+
return String(value);
|
|
2449
|
+
}
|
|
2450
|
+
if (typeof value === "string") {
|
|
2451
|
+
if (value.includes("::")) {
|
|
2452
|
+
return value;
|
|
2501
2453
|
}
|
|
2502
|
-
|
|
2454
|
+
if (value.startsWith("'") && value.endsWith("'")) {
|
|
2455
|
+
return value;
|
|
2456
|
+
}
|
|
2457
|
+
if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
|
|
2458
|
+
return value;
|
|
2459
|
+
}
|
|
2460
|
+
if (value.toUpperCase().startsWith("CURRENT_")) {
|
|
2461
|
+
return value;
|
|
2462
|
+
}
|
|
2463
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
2503
2464
|
}
|
|
2504
|
-
|
|
2505
|
-
|
|
2506
|
-
|
|
2507
|
-
|
|
2508
|
-
|
|
2509
|
-
|
|
2510
|
-
|
|
2511
|
-
|
|
2512
|
-
|
|
2465
|
+
return String(value);
|
|
2466
|
+
}
|
|
2467
|
+
function generateCreateIndexSQL(index7) {
|
|
2468
|
+
const unique3 = index7.isUnique ? "UNIQUE " : "";
|
|
2469
|
+
const method = index7.method || "btree";
|
|
2470
|
+
const columns = index7.columns.map((c) => {
|
|
2471
|
+
if (c.isExpression) {
|
|
2472
|
+
return c.expression;
|
|
2473
|
+
}
|
|
2474
|
+
return `"${c.expression}"${c.asc === false ? " DESC" : ""}`;
|
|
2475
|
+
}).join(", ");
|
|
2476
|
+
const indexName = index7.name.includes(".") ? index7.name.split(".")[1] : index7.name;
|
|
2477
|
+
let tableRef;
|
|
2478
|
+
const indexTable = index7.table;
|
|
2479
|
+
if (indexTable?.includes(".")) {
|
|
2480
|
+
const [schema, table] = indexTable.split(".");
|
|
2481
|
+
tableRef = `"${schema}"."${table}"`;
|
|
2482
|
+
} else {
|
|
2483
|
+
tableRef = `"${indexTable || ""}"`;
|
|
2513
2484
|
}
|
|
2514
|
-
|
|
2515
|
-
|
|
2485
|
+
return `CREATE ${unique3}INDEX "${indexName}" ON ${tableRef} USING ${method} (${columns});`;
|
|
2486
|
+
}
|
|
2487
|
+
function generateDropIndexSQL(index7) {
|
|
2488
|
+
const indexNameFull = typeof index7 === "string" ? index7 : index7.name;
|
|
2489
|
+
const indexName = indexNameFull.includes(".") ? indexNameFull.split(".")[1] : indexNameFull;
|
|
2490
|
+
return `DROP INDEX IF EXISTS "${indexName}";`;
|
|
2491
|
+
}
|
|
2492
|
+
function generateCreateForeignKeySQL(fk) {
|
|
2493
|
+
const schemaFrom = fk.schemaFrom || "public";
|
|
2494
|
+
const schemaTo = fk.schemaTo || "public";
|
|
2495
|
+
const tableFrom = fk.tableFrom;
|
|
2496
|
+
const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
|
|
2497
|
+
const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
|
|
2498
|
+
let sql22 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
|
|
2499
|
+
if (fk.onDelete) {
|
|
2500
|
+
sql22 += ` ON DELETE ${fk.onDelete}`;
|
|
2501
|
+
}
|
|
2502
|
+
if (fk.onUpdate) {
|
|
2503
|
+
sql22 += ` ON UPDATE ${fk.onUpdate}`;
|
|
2516
2504
|
}
|
|
2505
|
+
return `${sql22};`;
|
|
2517
2506
|
}
|
|
2518
|
-
|
|
2519
|
-
|
|
2507
|
+
function generateDropForeignKeySQL(fk) {
|
|
2508
|
+
const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
|
|
2509
|
+
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${fk.name}";`;
|
|
2510
|
+
}
|
|
2511
|
+
function generateCreateUniqueConstraintSQL(constraint) {
|
|
2512
|
+
const table = constraint.table || "";
|
|
2513
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2514
|
+
const name = constraint.name;
|
|
2515
|
+
const columns = constraint.columns.map((c) => `"${c}"`).join(", ");
|
|
2516
|
+
let sql22 = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" UNIQUE`;
|
|
2517
|
+
if (constraint.nullsNotDistinct) {
|
|
2518
|
+
sql22 += ` NULLS NOT DISTINCT`;
|
|
2519
|
+
}
|
|
2520
|
+
sql22 += ` (${columns});`;
|
|
2521
|
+
return sql22;
|
|
2522
|
+
}
|
|
2523
|
+
function generateDropUniqueConstraintSQL(constraint) {
|
|
2524
|
+
const table = constraint.table || "";
|
|
2525
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2526
|
+
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
|
|
2527
|
+
}
|
|
2528
|
+
function generateCreateCheckConstraintSQL(constraint) {
|
|
2529
|
+
const table = constraint.table || "";
|
|
2530
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2531
|
+
const name = constraint.name;
|
|
2532
|
+
const value = constraint.value;
|
|
2533
|
+
return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" CHECK (${value});`;
|
|
2534
|
+
}
|
|
2535
|
+
function generateDropCheckConstraintSQL(constraint) {
|
|
2536
|
+
const table = constraint.table || "";
|
|
2537
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2538
|
+
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
|
|
2539
|
+
}
|
|
2540
|
+
var import_core5;
|
|
2541
|
+
var init_sql_generator = __esm(() => {
|
|
2520
2542
|
import_core5 = require("@elizaos/core");
|
|
2521
|
-
import_drizzle_orm22 = require("drizzle-orm");
|
|
2522
2543
|
});
|
|
2523
2544
|
|
|
2524
2545
|
// runtime-migrator/extension-manager.ts
|
|
@@ -7097,5 +7118,5 @@ var plugin = {
|
|
|
7097
7118
|
};
|
|
7098
7119
|
var typescript_default = plugin;
|
|
7099
7120
|
|
|
7100
|
-
//# debugId=
|
|
7121
|
+
//# debugId=23D0E08B0FF7FC6364756E2164756E21
|
|
7101
7122
|
//# sourceMappingURL=index.node.cjs.map
|