@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
|
@@ -1,11 +1,15 @@
|
|
|
1
1
|
var __defProp = Object.defineProperty;
|
|
2
|
+
var __returnValue = (v) => v;
|
|
3
|
+
function __exportSetter(name, newValue) {
|
|
4
|
+
this[name] = __returnValue.bind(null, newValue);
|
|
5
|
+
}
|
|
2
6
|
var __export = (target, all) => {
|
|
3
7
|
for (var name in all)
|
|
4
8
|
__defProp(target, name, {
|
|
5
9
|
get: all[name],
|
|
6
10
|
enumerable: true,
|
|
7
11
|
configurable: true,
|
|
8
|
-
set: (
|
|
12
|
+
set: __exportSetter.bind(all, name)
|
|
9
13
|
});
|
|
10
14
|
};
|
|
11
15
|
var __esm = (fn, res) => () => (fn && (res = fn(fn = 0)), res);
|
|
@@ -883,169 +887,532 @@ async function applyEntityRLSToAllTables(adapter) {
|
|
|
883
887
|
}
|
|
884
888
|
var init_rls = () => {};
|
|
885
889
|
|
|
886
|
-
// runtime-migrator/
|
|
887
|
-
|
|
888
|
-
|
|
889
|
-
|
|
890
|
-
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
895
|
-
|
|
896
|
-
|
|
897
|
-
|
|
898
|
-
}
|
|
899
|
-
if (normalized === "serial") {
|
|
900
|
-
return "integer";
|
|
901
|
-
}
|
|
902
|
-
if (normalized === "bigserial") {
|
|
903
|
-
return "bigint";
|
|
904
|
-
}
|
|
905
|
-
if (normalized === "smallserial") {
|
|
906
|
-
return "smallint";
|
|
907
|
-
}
|
|
908
|
-
if (normalized.startsWith("numeric") || normalized.startsWith("decimal")) {
|
|
909
|
-
const match = normalized.match(/\((\d+)(?:,\s*(\d+))?\)/);
|
|
910
|
-
if (match) {
|
|
911
|
-
return `numeric(${match[1]}${match[2] ? `,${match[2]}` : ""})`;
|
|
912
|
-
}
|
|
913
|
-
return "numeric";
|
|
914
|
-
}
|
|
915
|
-
if (normalized.startsWith("character varying")) {
|
|
916
|
-
return normalized.replace("character varying", "varchar");
|
|
917
|
-
}
|
|
918
|
-
if (normalized === "text[]" || normalized === "_text") {
|
|
919
|
-
return "text[]";
|
|
890
|
+
// runtime-migrator/crypto-utils.ts
|
|
891
|
+
function extendedHash(str) {
|
|
892
|
+
const h1 = hashWithSeed(str, 5381);
|
|
893
|
+
const h2 = hashWithSeed(str, 7919);
|
|
894
|
+
const h3 = hashWithSeed(str, 104729);
|
|
895
|
+
const h4 = hashWithSeed(str, 224737);
|
|
896
|
+
return h1 + h2 + h3 + h4;
|
|
897
|
+
}
|
|
898
|
+
function hashWithSeed(str, seed) {
|
|
899
|
+
let hash = seed;
|
|
900
|
+
for (let i = 0;i < str.length; i++) {
|
|
901
|
+
hash = hash * 33 ^ str.charCodeAt(i);
|
|
920
902
|
}
|
|
921
|
-
return
|
|
903
|
+
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
922
904
|
}
|
|
923
|
-
function
|
|
924
|
-
|
|
925
|
-
|
|
926
|
-
|
|
927
|
-
|
|
928
|
-
if (
|
|
929
|
-
|
|
930
|
-
if (prevIndex.concurrently !== currIndex.concurrently)
|
|
931
|
-
return true;
|
|
932
|
-
const prevColumns = prevIndex.columns || [];
|
|
933
|
-
const currColumns = currIndex.columns || [];
|
|
934
|
-
if (prevColumns.length !== currColumns.length)
|
|
935
|
-
return true;
|
|
936
|
-
for (let i = 0;i < prevColumns.length; i++) {
|
|
937
|
-
const prevCol = prevColumns[i];
|
|
938
|
-
const currCol = currColumns[i];
|
|
939
|
-
if (typeof prevCol === "string" && typeof currCol === "string") {
|
|
940
|
-
if (prevCol !== currCol)
|
|
941
|
-
return true;
|
|
942
|
-
} else if (typeof prevCol === "object" && typeof currCol === "object") {
|
|
943
|
-
if (prevCol.expression !== currCol.expression)
|
|
944
|
-
return true;
|
|
945
|
-
if (prevCol.isExpression !== currCol.isExpression)
|
|
946
|
-
return true;
|
|
947
|
-
if (prevCol.asc !== currCol.asc)
|
|
948
|
-
return true;
|
|
949
|
-
if (prevCol.nulls !== currCol.nulls)
|
|
950
|
-
return true;
|
|
951
|
-
} else {
|
|
952
|
-
return true;
|
|
953
|
-
}
|
|
905
|
+
function stringToBigInt(str) {
|
|
906
|
+
const hash = extendedHash(str);
|
|
907
|
+
let lockId = BigInt(`0x${hash.slice(0, 16)}`);
|
|
908
|
+
const mask63Bits = 0x7fffffffffffffffn;
|
|
909
|
+
lockId = lockId & mask63Bits;
|
|
910
|
+
if (lockId === 0n) {
|
|
911
|
+
lockId = 1n;
|
|
954
912
|
}
|
|
955
|
-
return
|
|
913
|
+
return lockId;
|
|
956
914
|
}
|
|
957
|
-
|
|
958
|
-
|
|
959
|
-
|
|
960
|
-
|
|
961
|
-
|
|
962
|
-
|
|
963
|
-
|
|
964
|
-
|
|
965
|
-
|
|
966
|
-
|
|
967
|
-
|
|
968
|
-
|
|
969
|
-
|
|
970
|
-
|
|
971
|
-
|
|
972
|
-
|
|
973
|
-
}
|
|
974
|
-
|
|
975
|
-
|
|
976
|
-
|
|
977
|
-
|
|
978
|
-
|
|
979
|
-
|
|
980
|
-
|
|
981
|
-
|
|
982
|
-
|
|
983
|
-
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
const table = currentSnapshot.tables[tableName];
|
|
992
|
-
if (table.indexes) {
|
|
993
|
-
for (const indexName in table.indexes) {
|
|
994
|
-
diff.indexes.created.push({
|
|
995
|
-
...table.indexes[indexName],
|
|
996
|
-
table: tableName
|
|
997
|
-
});
|
|
998
|
-
}
|
|
915
|
+
|
|
916
|
+
// runtime-migrator/drizzle-adapters/database-introspector.ts
|
|
917
|
+
import { logger as logger3 } from "@elizaos/core";
|
|
918
|
+
import { sql as sql21 } from "drizzle-orm";
|
|
919
|
+
function getRows2(result) {
|
|
920
|
+
return result.rows;
|
|
921
|
+
}
|
|
922
|
+
|
|
923
|
+
class DatabaseIntrospector {
|
|
924
|
+
db;
|
|
925
|
+
constructor(db) {
|
|
926
|
+
this.db = db;
|
|
927
|
+
}
|
|
928
|
+
async introspectSchema(schemaName = "public") {
|
|
929
|
+
logger3.info({ src: "plugin:sql", schemaName }, "Starting database introspection");
|
|
930
|
+
const tables = {};
|
|
931
|
+
const schemas = {};
|
|
932
|
+
const enums = {};
|
|
933
|
+
const allTables = await this.getTables(schemaName);
|
|
934
|
+
for (const tableInfo of allTables) {
|
|
935
|
+
const tableName = tableInfo.table_name;
|
|
936
|
+
const tableSchema = tableInfo.table_schema || "public";
|
|
937
|
+
logger3.debug({ src: "plugin:sql", tableSchema, tableName }, "Introspecting table");
|
|
938
|
+
const columns = await this.getColumns(tableSchema, tableName);
|
|
939
|
+
const columnsObject = {};
|
|
940
|
+
const uniqueConstraintObject = {};
|
|
941
|
+
for (const col of columns) {
|
|
942
|
+
columnsObject[col.column_name] = {
|
|
943
|
+
name: col.column_name,
|
|
944
|
+
type: col.data_type,
|
|
945
|
+
primaryKey: col.is_primary || false,
|
|
946
|
+
notNull: col.is_nullable === "NO",
|
|
947
|
+
default: col.column_default ? this.parseDefault(col.column_default, col.data_type) : undefined
|
|
948
|
+
};
|
|
999
949
|
}
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
950
|
+
const indexes = await this.getIndexes(tableSchema, tableName);
|
|
951
|
+
const indexesObject = {};
|
|
952
|
+
for (const idx of indexes) {
|
|
953
|
+
if (!idx.is_primary && !idx.is_unique_constraint) {
|
|
954
|
+
if (idx.columns && Array.isArray(idx.columns) && idx.columns.length > 0) {
|
|
955
|
+
indexesObject[idx.name] = {
|
|
956
|
+
name: idx.name,
|
|
957
|
+
columns: idx.columns.map((col) => ({
|
|
958
|
+
expression: col,
|
|
959
|
+
isExpression: false
|
|
960
|
+
})),
|
|
961
|
+
isUnique: idx.is_unique,
|
|
962
|
+
method: idx.method || "btree"
|
|
963
|
+
};
|
|
964
|
+
}
|
|
1003
965
|
}
|
|
1004
966
|
}
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
});
|
|
1020
|
-
}
|
|
967
|
+
const foreignKeys = await this.getForeignKeys(tableSchema, tableName);
|
|
968
|
+
const foreignKeysObject = {};
|
|
969
|
+
for (const fk of foreignKeys) {
|
|
970
|
+
foreignKeysObject[fk.name] = {
|
|
971
|
+
name: fk.name,
|
|
972
|
+
tableFrom: tableName,
|
|
973
|
+
schemaFrom: tableSchema,
|
|
974
|
+
tableTo: fk.foreign_table_name,
|
|
975
|
+
schemaTo: fk.foreign_table_schema || "public",
|
|
976
|
+
columnsFrom: [fk.column_name],
|
|
977
|
+
columnsTo: [fk.foreign_column_name],
|
|
978
|
+
onDelete: fk.delete_rule?.toLowerCase() || "no action",
|
|
979
|
+
onUpdate: fk.update_rule?.toLowerCase() || "no action"
|
|
980
|
+
};
|
|
1021
981
|
}
|
|
1022
|
-
|
|
1023
|
-
|
|
1024
|
-
|
|
1025
|
-
|
|
1026
|
-
|
|
1027
|
-
|
|
1028
|
-
}
|
|
982
|
+
const primaryKeys = await this.getPrimaryKeys(tableSchema, tableName);
|
|
983
|
+
const primaryKeysObject = {};
|
|
984
|
+
for (const pk of primaryKeys) {
|
|
985
|
+
primaryKeysObject[pk.name] = {
|
|
986
|
+
name: pk.name,
|
|
987
|
+
columns: pk.columns
|
|
988
|
+
};
|
|
1029
989
|
}
|
|
1030
|
-
|
|
1031
|
-
|
|
1032
|
-
|
|
1033
|
-
|
|
1034
|
-
|
|
1035
|
-
|
|
1036
|
-
}
|
|
990
|
+
const uniqueConstraints = await this.getUniqueConstraints(tableSchema, tableName);
|
|
991
|
+
for (const unq of uniqueConstraints) {
|
|
992
|
+
uniqueConstraintObject[unq.name] = {
|
|
993
|
+
name: unq.name,
|
|
994
|
+
columns: unq.columns,
|
|
995
|
+
nullsNotDistinct: false
|
|
996
|
+
};
|
|
1037
997
|
}
|
|
1038
|
-
|
|
1039
|
-
|
|
1040
|
-
|
|
1041
|
-
|
|
998
|
+
const checkConstraints = await this.getCheckConstraints(tableSchema, tableName);
|
|
999
|
+
const checksObject = {};
|
|
1000
|
+
for (const check3 of checkConstraints) {
|
|
1001
|
+
checksObject[check3.name] = {
|
|
1002
|
+
name: check3.name,
|
|
1003
|
+
value: check3.definition
|
|
1004
|
+
};
|
|
1042
1005
|
}
|
|
1043
|
-
|
|
1044
|
-
|
|
1045
|
-
|
|
1046
|
-
|
|
1047
|
-
|
|
1048
|
-
|
|
1006
|
+
tables[`${tableSchema}.${tableName}`] = {
|
|
1007
|
+
name: tableName,
|
|
1008
|
+
schema: tableSchema,
|
|
1009
|
+
columns: columnsObject,
|
|
1010
|
+
indexes: indexesObject,
|
|
1011
|
+
foreignKeys: foreignKeysObject,
|
|
1012
|
+
compositePrimaryKeys: primaryKeysObject,
|
|
1013
|
+
uniqueConstraints: uniqueConstraintObject,
|
|
1014
|
+
checkConstraints: checksObject
|
|
1015
|
+
};
|
|
1016
|
+
if (tableSchema && tableSchema !== "public") {
|
|
1017
|
+
schemas[tableSchema] = tableSchema;
|
|
1018
|
+
}
|
|
1019
|
+
}
|
|
1020
|
+
const enumsResult = await this.getEnums(schemaName);
|
|
1021
|
+
for (const enumInfo of enumsResult) {
|
|
1022
|
+
const key = `${enumInfo.schema}.${enumInfo.name}`;
|
|
1023
|
+
if (!enums[key]) {
|
|
1024
|
+
enums[key] = {
|
|
1025
|
+
name: enumInfo.name,
|
|
1026
|
+
schema: enumInfo.schema,
|
|
1027
|
+
values: []
|
|
1028
|
+
};
|
|
1029
|
+
}
|
|
1030
|
+
enums[key].values.push(enumInfo.value);
|
|
1031
|
+
}
|
|
1032
|
+
logger3.info({ src: "plugin:sql", tableCount: Object.keys(tables).length }, "Database introspection complete");
|
|
1033
|
+
return {
|
|
1034
|
+
version: "7",
|
|
1035
|
+
dialect: "postgresql",
|
|
1036
|
+
tables,
|
|
1037
|
+
schemas,
|
|
1038
|
+
enums,
|
|
1039
|
+
_meta: {
|
|
1040
|
+
schemas: {},
|
|
1041
|
+
tables: {},
|
|
1042
|
+
columns: {}
|
|
1043
|
+
}
|
|
1044
|
+
};
|
|
1045
|
+
}
|
|
1046
|
+
async getTables(schemaName) {
|
|
1047
|
+
const result = await this.db.execute(sql21`SELECT
|
|
1048
|
+
table_schema,
|
|
1049
|
+
table_name
|
|
1050
|
+
FROM information_schema.tables
|
|
1051
|
+
WHERE table_schema = ${schemaName}
|
|
1052
|
+
AND table_type = 'BASE TABLE'
|
|
1053
|
+
ORDER BY table_name`);
|
|
1054
|
+
return getRows2(result);
|
|
1055
|
+
}
|
|
1056
|
+
async getColumns(schemaName, tableName) {
|
|
1057
|
+
const result = await this.db.execute(sql21`SELECT
|
|
1058
|
+
a.attname AS column_name,
|
|
1059
|
+
CASE
|
|
1060
|
+
WHEN a.attnotnull THEN 'NO'
|
|
1061
|
+
ELSE 'YES'
|
|
1062
|
+
END AS is_nullable,
|
|
1063
|
+
CASE
|
|
1064
|
+
WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
|
|
1065
|
+
AND EXISTS (
|
|
1066
|
+
SELECT FROM pg_attrdef ad
|
|
1067
|
+
WHERE ad.adrelid = a.attrelid
|
|
1068
|
+
AND ad.adnum = a.attnum
|
|
1069
|
+
AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
|
|
1070
|
+
|| pg_get_serial_sequence(a.attrelid::regclass::text, a.attname)::regclass || '''::regclass)'
|
|
1071
|
+
)
|
|
1072
|
+
THEN CASE a.atttypid
|
|
1073
|
+
WHEN 'int'::regtype THEN 'serial'
|
|
1074
|
+
WHEN 'int8'::regtype THEN 'bigserial'
|
|
1075
|
+
WHEN 'int2'::regtype THEN 'smallserial'
|
|
1076
|
+
END
|
|
1077
|
+
ELSE format_type(a.atttypid, a.atttypmod)
|
|
1078
|
+
END AS data_type,
|
|
1079
|
+
pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
|
|
1080
|
+
CASE
|
|
1081
|
+
WHEN con.contype = 'p' THEN true
|
|
1082
|
+
ELSE false
|
|
1083
|
+
END AS is_primary
|
|
1084
|
+
FROM pg_attribute a
|
|
1085
|
+
JOIN pg_class cls ON cls.oid = a.attrelid
|
|
1086
|
+
JOIN pg_namespace ns ON ns.oid = cls.relnamespace
|
|
1087
|
+
LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
|
|
1088
|
+
LEFT JOIN pg_constraint con ON con.conrelid = a.attrelid
|
|
1089
|
+
AND a.attnum = ANY(con.conkey)
|
|
1090
|
+
AND con.contype = 'p'
|
|
1091
|
+
WHERE
|
|
1092
|
+
a.attnum > 0
|
|
1093
|
+
AND NOT a.attisdropped
|
|
1094
|
+
AND ns.nspname = ${schemaName}
|
|
1095
|
+
AND cls.relname = ${tableName}
|
|
1096
|
+
ORDER BY a.attnum`);
|
|
1097
|
+
return getRows2(result);
|
|
1098
|
+
}
|
|
1099
|
+
async getIndexes(schemaName, tableName) {
|
|
1100
|
+
const result = await this.db.execute(sql21`SELECT
|
|
1101
|
+
i.relname AS name,
|
|
1102
|
+
idx.indisunique AS is_unique,
|
|
1103
|
+
idx.indisprimary AS is_primary,
|
|
1104
|
+
con.contype = 'u' AS is_unique_constraint,
|
|
1105
|
+
ARRAY(
|
|
1106
|
+
SELECT a.attname
|
|
1107
|
+
FROM pg_attribute a
|
|
1108
|
+
WHERE a.attrelid = idx.indrelid
|
|
1109
|
+
AND a.attnum = ANY(idx.indkey::int[])
|
|
1110
|
+
ORDER BY a.attnum
|
|
1111
|
+
) AS columns,
|
|
1112
|
+
am.amname AS method
|
|
1113
|
+
FROM pg_index idx
|
|
1114
|
+
JOIN pg_class i ON i.oid = idx.indexrelid
|
|
1115
|
+
JOIN pg_class c ON c.oid = idx.indrelid
|
|
1116
|
+
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
1117
|
+
JOIN pg_am am ON am.oid = i.relam
|
|
1118
|
+
LEFT JOIN pg_constraint con ON con.conindid = idx.indexrelid
|
|
1119
|
+
WHERE n.nspname = ${schemaName}
|
|
1120
|
+
AND c.relname = ${tableName}`);
|
|
1121
|
+
return getRows2(result);
|
|
1122
|
+
}
|
|
1123
|
+
async getForeignKeys(schemaName, tableName) {
|
|
1124
|
+
const result = await this.db.execute(sql21`SELECT
|
|
1125
|
+
con.conname AS name,
|
|
1126
|
+
att.attname AS column_name,
|
|
1127
|
+
fnsp.nspname AS foreign_table_schema,
|
|
1128
|
+
frel.relname AS foreign_table_name,
|
|
1129
|
+
fatt.attname AS foreign_column_name,
|
|
1130
|
+
CASE con.confupdtype
|
|
1131
|
+
WHEN 'a' THEN 'NO ACTION'
|
|
1132
|
+
WHEN 'r' THEN 'RESTRICT'
|
|
1133
|
+
WHEN 'n' THEN 'SET NULL'
|
|
1134
|
+
WHEN 'c' THEN 'CASCADE'
|
|
1135
|
+
WHEN 'd' THEN 'SET DEFAULT'
|
|
1136
|
+
END AS update_rule,
|
|
1137
|
+
CASE con.confdeltype
|
|
1138
|
+
WHEN 'a' THEN 'NO ACTION'
|
|
1139
|
+
WHEN 'r' THEN 'RESTRICT'
|
|
1140
|
+
WHEN 'n' THEN 'SET NULL'
|
|
1141
|
+
WHEN 'c' THEN 'CASCADE'
|
|
1142
|
+
WHEN 'd' THEN 'SET DEFAULT'
|
|
1143
|
+
END AS delete_rule
|
|
1144
|
+
FROM pg_catalog.pg_constraint con
|
|
1145
|
+
JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
|
|
1146
|
+
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
|
|
1147
|
+
LEFT JOIN pg_catalog.pg_attribute att ON att.attnum = ANY (con.conkey)
|
|
1148
|
+
AND att.attrelid = con.conrelid
|
|
1149
|
+
LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
|
|
1150
|
+
LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
|
|
1151
|
+
LEFT JOIN pg_catalog.pg_attribute fatt ON fatt.attnum = ANY (con.confkey)
|
|
1152
|
+
AND fatt.attrelid = con.confrelid
|
|
1153
|
+
WHERE con.contype = 'f'
|
|
1154
|
+
AND nsp.nspname = ${schemaName}
|
|
1155
|
+
AND rel.relname = ${tableName}`);
|
|
1156
|
+
return getRows2(result);
|
|
1157
|
+
}
|
|
1158
|
+
async getPrimaryKeys(schemaName, tableName) {
|
|
1159
|
+
const result = await this.db.execute(sql21`SELECT
|
|
1160
|
+
con.conname AS name,
|
|
1161
|
+
ARRAY(
|
|
1162
|
+
SELECT a.attname
|
|
1163
|
+
FROM pg_attribute a
|
|
1164
|
+
WHERE a.attrelid = con.conrelid
|
|
1165
|
+
AND a.attnum = ANY(con.conkey)
|
|
1166
|
+
ORDER BY a.attnum
|
|
1167
|
+
) AS columns
|
|
1168
|
+
FROM pg_constraint con
|
|
1169
|
+
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
1170
|
+
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
1171
|
+
WHERE con.contype = 'p'
|
|
1172
|
+
AND nsp.nspname = ${schemaName}
|
|
1173
|
+
AND rel.relname = ${tableName}`);
|
|
1174
|
+
return getRows2(result);
|
|
1175
|
+
}
|
|
1176
|
+
async getUniqueConstraints(schemaName, tableName) {
|
|
1177
|
+
const result = await this.db.execute(sql21`SELECT
|
|
1178
|
+
con.conname AS name,
|
|
1179
|
+
ARRAY(
|
|
1180
|
+
SELECT a.attname
|
|
1181
|
+
FROM pg_attribute a
|
|
1182
|
+
WHERE a.attrelid = con.conrelid
|
|
1183
|
+
AND a.attnum = ANY(con.conkey)
|
|
1184
|
+
ORDER BY a.attnum
|
|
1185
|
+
) AS columns
|
|
1186
|
+
FROM pg_constraint con
|
|
1187
|
+
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
1188
|
+
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
1189
|
+
WHERE con.contype = 'u'
|
|
1190
|
+
AND nsp.nspname = ${schemaName}
|
|
1191
|
+
AND rel.relname = ${tableName}`);
|
|
1192
|
+
return getRows2(result);
|
|
1193
|
+
}
|
|
1194
|
+
async getCheckConstraints(schemaName, tableName) {
|
|
1195
|
+
const result = await this.db.execute(sql21`SELECT
|
|
1196
|
+
con.conname AS name,
|
|
1197
|
+
pg_get_constraintdef(con.oid) AS definition
|
|
1198
|
+
FROM pg_constraint con
|
|
1199
|
+
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
1200
|
+
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
1201
|
+
WHERE con.contype = 'c'
|
|
1202
|
+
AND nsp.nspname = ${schemaName}
|
|
1203
|
+
AND rel.relname = ${tableName}`);
|
|
1204
|
+
return getRows2(result);
|
|
1205
|
+
}
|
|
1206
|
+
async getEnums(schemaName) {
|
|
1207
|
+
const result = await this.db.execute(sql21`SELECT
|
|
1208
|
+
n.nspname AS schema,
|
|
1209
|
+
t.typname AS name,
|
|
1210
|
+
e.enumlabel AS value,
|
|
1211
|
+
e.enumsortorder AS sort_order
|
|
1212
|
+
FROM pg_type t
|
|
1213
|
+
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
1214
|
+
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
|
|
1215
|
+
WHERE n.nspname = ${schemaName}
|
|
1216
|
+
ORDER BY schema, name, sort_order`);
|
|
1217
|
+
return getRows2(result);
|
|
1218
|
+
}
|
|
1219
|
+
parseDefault(defaultValue, dataType) {
|
|
1220
|
+
if (!defaultValue)
|
|
1221
|
+
return;
|
|
1222
|
+
const match = defaultValue.match(/^'(.*)'::/);
|
|
1223
|
+
if (match) {
|
|
1224
|
+
return `'${match[1]}'`;
|
|
1225
|
+
}
|
|
1226
|
+
if (defaultValue.includes("nextval(")) {
|
|
1227
|
+
return;
|
|
1228
|
+
}
|
|
1229
|
+
if (dataType === "boolean") {
|
|
1230
|
+
if (defaultValue === "true")
|
|
1231
|
+
return "true";
|
|
1232
|
+
if (defaultValue === "false")
|
|
1233
|
+
return "false";
|
|
1234
|
+
}
|
|
1235
|
+
return defaultValue;
|
|
1236
|
+
}
|
|
1237
|
+
async hasExistingTables(pluginName) {
|
|
1238
|
+
const schemaName = pluginName === "@elizaos/plugin-sql" ? "public" : this.deriveSchemaName(pluginName);
|
|
1239
|
+
const result = await this.db.execute(sql21`SELECT COUNT(*) AS count
|
|
1240
|
+
FROM information_schema.tables
|
|
1241
|
+
WHERE table_schema = ${schemaName}
|
|
1242
|
+
AND table_type = 'BASE TABLE'`);
|
|
1243
|
+
const firstRow = result.rows?.[0];
|
|
1244
|
+
const count = parseInt(firstRow && firstRow.count || "0", 10);
|
|
1245
|
+
return count > 0;
|
|
1246
|
+
}
|
|
1247
|
+
deriveSchemaName(pluginName) {
|
|
1248
|
+
return pluginName.replace("@", "").replace("/", "_").replace(/-/g, "_").toLowerCase();
|
|
1249
|
+
}
|
|
1250
|
+
}
|
|
1251
|
+
var init_database_introspector = () => {};
|
|
1252
|
+
|
|
1253
|
+
// runtime-migrator/drizzle-adapters/diff-calculator.ts
|
|
1254
|
+
var exports_diff_calculator = {};
|
|
1255
|
+
__export(exports_diff_calculator, {
|
|
1256
|
+
hasDiffChanges: () => hasDiffChanges,
|
|
1257
|
+
calculateDiff: () => calculateDiff
|
|
1258
|
+
});
|
|
1259
|
+
function normalizeType(type) {
|
|
1260
|
+
if (!type)
|
|
1261
|
+
return "";
|
|
1262
|
+
const normalized = type.toLowerCase().trim();
|
|
1263
|
+
if (normalized === "timestamp without time zone" || normalized === "timestamp with time zone") {
|
|
1264
|
+
return "timestamp";
|
|
1265
|
+
}
|
|
1266
|
+
if (normalized === "serial") {
|
|
1267
|
+
return "integer";
|
|
1268
|
+
}
|
|
1269
|
+
if (normalized === "bigserial") {
|
|
1270
|
+
return "bigint";
|
|
1271
|
+
}
|
|
1272
|
+
if (normalized === "smallserial") {
|
|
1273
|
+
return "smallint";
|
|
1274
|
+
}
|
|
1275
|
+
if (normalized.startsWith("numeric") || normalized.startsWith("decimal")) {
|
|
1276
|
+
const match = normalized.match(/\((\d+)(?:,\s*(\d+))?\)/);
|
|
1277
|
+
if (match) {
|
|
1278
|
+
return `numeric(${match[1]}${match[2] ? `,${match[2]}` : ""})`;
|
|
1279
|
+
}
|
|
1280
|
+
return "numeric";
|
|
1281
|
+
}
|
|
1282
|
+
if (normalized.startsWith("character varying")) {
|
|
1283
|
+
return normalized.replace("character varying", "varchar");
|
|
1284
|
+
}
|
|
1285
|
+
if (normalized === "text[]" || normalized === "_text") {
|
|
1286
|
+
return "text[]";
|
|
1287
|
+
}
|
|
1288
|
+
return normalized;
|
|
1289
|
+
}
|
|
1290
|
+
function isIndexChanged(prevIndex, currIndex) {
|
|
1291
|
+
if (prevIndex.isUnique !== currIndex.isUnique)
|
|
1292
|
+
return true;
|
|
1293
|
+
if (prevIndex.method !== currIndex.method)
|
|
1294
|
+
return true;
|
|
1295
|
+
if (prevIndex.where !== currIndex.where)
|
|
1296
|
+
return true;
|
|
1297
|
+
if (prevIndex.concurrently !== currIndex.concurrently)
|
|
1298
|
+
return true;
|
|
1299
|
+
const prevColumns = prevIndex.columns || [];
|
|
1300
|
+
const currColumns = currIndex.columns || [];
|
|
1301
|
+
if (prevColumns.length !== currColumns.length)
|
|
1302
|
+
return true;
|
|
1303
|
+
for (let i = 0;i < prevColumns.length; i++) {
|
|
1304
|
+
const prevCol = prevColumns[i];
|
|
1305
|
+
const currCol = currColumns[i];
|
|
1306
|
+
if (typeof prevCol === "string" && typeof currCol === "string") {
|
|
1307
|
+
if (prevCol !== currCol)
|
|
1308
|
+
return true;
|
|
1309
|
+
} else if (typeof prevCol === "object" && typeof currCol === "object") {
|
|
1310
|
+
if (prevCol.expression !== currCol.expression)
|
|
1311
|
+
return true;
|
|
1312
|
+
if (prevCol.isExpression !== currCol.isExpression)
|
|
1313
|
+
return true;
|
|
1314
|
+
if (prevCol.asc !== currCol.asc)
|
|
1315
|
+
return true;
|
|
1316
|
+
if (prevCol.nulls !== currCol.nulls)
|
|
1317
|
+
return true;
|
|
1318
|
+
} else {
|
|
1319
|
+
return true;
|
|
1320
|
+
}
|
|
1321
|
+
}
|
|
1322
|
+
return false;
|
|
1323
|
+
}
|
|
1324
|
+
async function calculateDiff(previousSnapshot, currentSnapshot) {
|
|
1325
|
+
const diff = {
|
|
1326
|
+
tables: {
|
|
1327
|
+
created: [],
|
|
1328
|
+
deleted: [],
|
|
1329
|
+
modified: []
|
|
1330
|
+
},
|
|
1331
|
+
columns: {
|
|
1332
|
+
added: [],
|
|
1333
|
+
deleted: [],
|
|
1334
|
+
modified: []
|
|
1335
|
+
},
|
|
1336
|
+
indexes: {
|
|
1337
|
+
created: [],
|
|
1338
|
+
deleted: [],
|
|
1339
|
+
altered: []
|
|
1340
|
+
},
|
|
1341
|
+
foreignKeys: {
|
|
1342
|
+
created: [],
|
|
1343
|
+
deleted: [],
|
|
1344
|
+
altered: []
|
|
1345
|
+
},
|
|
1346
|
+
uniqueConstraints: {
|
|
1347
|
+
created: [],
|
|
1348
|
+
deleted: []
|
|
1349
|
+
},
|
|
1350
|
+
checkConstraints: {
|
|
1351
|
+
created: [],
|
|
1352
|
+
deleted: []
|
|
1353
|
+
}
|
|
1354
|
+
};
|
|
1355
|
+
if (!previousSnapshot) {
|
|
1356
|
+
diff.tables.created = Object.keys(currentSnapshot.tables);
|
|
1357
|
+
for (const tableName in currentSnapshot.tables) {
|
|
1358
|
+
const table = currentSnapshot.tables[tableName];
|
|
1359
|
+
if (table.indexes) {
|
|
1360
|
+
for (const indexName in table.indexes) {
|
|
1361
|
+
diff.indexes.created.push({
|
|
1362
|
+
...table.indexes[indexName],
|
|
1363
|
+
table: tableName
|
|
1364
|
+
});
|
|
1365
|
+
}
|
|
1366
|
+
}
|
|
1367
|
+
if (table.foreignKeys) {
|
|
1368
|
+
for (const fkName in table.foreignKeys) {
|
|
1369
|
+
diff.foreignKeys.created.push(table.foreignKeys[fkName]);
|
|
1370
|
+
}
|
|
1371
|
+
}
|
|
1372
|
+
}
|
|
1373
|
+
return diff;
|
|
1374
|
+
}
|
|
1375
|
+
const prevTables = previousSnapshot.tables || {};
|
|
1376
|
+
const currTables = currentSnapshot.tables || {};
|
|
1377
|
+
for (const tableName in currTables) {
|
|
1378
|
+
if (!(tableName in prevTables)) {
|
|
1379
|
+
diff.tables.created.push(tableName);
|
|
1380
|
+
const table = currTables[tableName];
|
|
1381
|
+
if (table.indexes) {
|
|
1382
|
+
for (const indexName in table.indexes) {
|
|
1383
|
+
diff.indexes.created.push({
|
|
1384
|
+
...table.indexes[indexName],
|
|
1385
|
+
table: tableName
|
|
1386
|
+
});
|
|
1387
|
+
}
|
|
1388
|
+
}
|
|
1389
|
+
if (table.uniqueConstraints) {
|
|
1390
|
+
for (const uqName in table.uniqueConstraints) {
|
|
1391
|
+
diff.uniqueConstraints.created.push({
|
|
1392
|
+
...table.uniqueConstraints[uqName],
|
|
1393
|
+
table: tableName
|
|
1394
|
+
});
|
|
1395
|
+
}
|
|
1396
|
+
}
|
|
1397
|
+
if (table.checkConstraints) {
|
|
1398
|
+
for (const checkName in table.checkConstraints) {
|
|
1399
|
+
diff.checkConstraints.created.push({
|
|
1400
|
+
...table.checkConstraints[checkName],
|
|
1401
|
+
table: tableName
|
|
1402
|
+
});
|
|
1403
|
+
}
|
|
1404
|
+
}
|
|
1405
|
+
if (table.foreignKeys) {
|
|
1406
|
+
for (const fkName in table.foreignKeys) {
|
|
1407
|
+
diff.foreignKeys.created.push(table.foreignKeys[fkName]);
|
|
1408
|
+
}
|
|
1409
|
+
}
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
for (const tableName in prevTables) {
|
|
1413
|
+
if (!(tableName in currTables)) {
|
|
1414
|
+
diff.tables.deleted.push(tableName);
|
|
1415
|
+
}
|
|
1049
1416
|
}
|
|
1050
1417
|
for (const tableName in currTables) {
|
|
1051
1418
|
if (tableName in prevTables) {
|
|
@@ -1214,32 +1581,6 @@ function hasDiffChanges(diff) {
|
|
|
1214
1581
|
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;
|
|
1215
1582
|
}
|
|
1216
1583
|
|
|
1217
|
-
// runtime-migrator/crypto-utils.ts
|
|
1218
|
-
function extendedHash(str) {
|
|
1219
|
-
const h1 = hashWithSeed(str, 5381);
|
|
1220
|
-
const h2 = hashWithSeed(str, 7919);
|
|
1221
|
-
const h3 = hashWithSeed(str, 104729);
|
|
1222
|
-
const h4 = hashWithSeed(str, 224737);
|
|
1223
|
-
return h1 + h2 + h3 + h4;
|
|
1224
|
-
}
|
|
1225
|
-
function hashWithSeed(str, seed) {
|
|
1226
|
-
let hash = seed;
|
|
1227
|
-
for (let i = 0;i < str.length; i++) {
|
|
1228
|
-
hash = hash * 33 ^ str.charCodeAt(i);
|
|
1229
|
-
}
|
|
1230
|
-
return (hash >>> 0).toString(16).padStart(8, "0");
|
|
1231
|
-
}
|
|
1232
|
-
function stringToBigInt(str) {
|
|
1233
|
-
const hash = extendedHash(str);
|
|
1234
|
-
let lockId = BigInt(`0x${hash.slice(0, 16)}`);
|
|
1235
|
-
const mask63Bits = 0x7fffffffffffffffn;
|
|
1236
|
-
lockId = lockId & mask63Bits;
|
|
1237
|
-
if (lockId === 0n) {
|
|
1238
|
-
lockId = 1n;
|
|
1239
|
-
}
|
|
1240
|
-
return lockId;
|
|
1241
|
-
}
|
|
1242
|
-
|
|
1243
1584
|
// runtime-migrator/drizzle-adapters/snapshot-generator.ts
|
|
1244
1585
|
import { is, SQL } from "drizzle-orm";
|
|
1245
1586
|
import { getTableConfig, PgDialect, PgTable } from "drizzle-orm/pg-core";
|
|
@@ -1468,7 +1809,7 @@ function hasChanges(previousSnapshot, currentSnapshot) {
|
|
|
1468
1809
|
const currHash = hashSnapshot(currentSnapshot);
|
|
1469
1810
|
return prevHash !== currHash;
|
|
1470
1811
|
}
|
|
1471
|
-
var sqlToStr = (
|
|
1812
|
+
var sqlToStr = (sql22, _casing) => {
|
|
1472
1813
|
const config = {
|
|
1473
1814
|
escapeName: () => {
|
|
1474
1815
|
throw new Error("we don't support params for `sql` default values");
|
|
@@ -1481,12 +1822,12 @@ var sqlToStr = (sql21, _casing) => {
|
|
|
1481
1822
|
},
|
|
1482
1823
|
casing: undefined
|
|
1483
1824
|
};
|
|
1484
|
-
return
|
|
1825
|
+
return sql22.toQuery(config).sql;
|
|
1485
1826
|
};
|
|
1486
1827
|
var init_snapshot_generator = () => {};
|
|
1487
1828
|
|
|
1488
1829
|
// runtime-migrator/drizzle-adapters/sql-generator.ts
|
|
1489
|
-
import { logger as
|
|
1830
|
+
import { logger as logger4 } from "@elizaos/core";
|
|
1490
1831
|
function checkForDataLoss(diff) {
|
|
1491
1832
|
const result = {
|
|
1492
1833
|
hasDataLoss: false,
|
|
@@ -1616,7 +1957,7 @@ async function generateMigrationSQL(previousSnapshot, currentSnapshot, diff) {
|
|
|
1616
1957
|
}
|
|
1617
1958
|
const dataLossCheck = checkForDataLoss(diff);
|
|
1618
1959
|
if (dataLossCheck.warnings.length > 0) {
|
|
1619
|
-
|
|
1960
|
+
logger4.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
|
|
1620
1961
|
}
|
|
1621
1962
|
const schemasToCreate = new Set;
|
|
1622
1963
|
for (const tableName of diff.tables.created) {
|
|
@@ -1775,18 +2116,18 @@ function generateCreateTableSQL(fullTableName, table) {
|
|
|
1775
2116
|
return { tableSQL, fkSQLs };
|
|
1776
2117
|
}
|
|
1777
2118
|
function generateColumnDefinition(name, def) {
|
|
1778
|
-
let
|
|
2119
|
+
let sql22 = `"${name}" ${def.type}`;
|
|
1779
2120
|
if (def.primaryKey && !def.type.includes("SERIAL")) {
|
|
1780
|
-
|
|
2121
|
+
sql22 += " PRIMARY KEY";
|
|
1781
2122
|
}
|
|
1782
2123
|
if (def.notNull) {
|
|
1783
|
-
|
|
2124
|
+
sql22 += " NOT NULL";
|
|
1784
2125
|
}
|
|
1785
2126
|
if (def.default !== undefined) {
|
|
1786
2127
|
const defaultValue = formatDefaultValue(def.default, def.type);
|
|
1787
|
-
|
|
2128
|
+
sql22 += ` DEFAULT ${defaultValue}`;
|
|
1788
2129
|
}
|
|
1789
|
-
return
|
|
2130
|
+
return sql22;
|
|
1790
2131
|
}
|
|
1791
2132
|
function generateAddColumnSQL(table, column, definition) {
|
|
1792
2133
|
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
@@ -1805,534 +2146,197 @@ function generateAddColumnSQL(table, column, definition) {
|
|
|
1805
2146
|
const definitionWithGenerated = definition;
|
|
1806
2147
|
if (definitionWithGenerated.generated) {
|
|
1807
2148
|
parts.push(`GENERATED ALWAYS AS (${definitionWithGenerated.generated}) STORED`);
|
|
1808
|
-
}
|
|
1809
|
-
if (definition.notNull) {
|
|
1810
|
-
parts.push("NOT NULL");
|
|
1811
|
-
}
|
|
1812
|
-
return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
|
|
1813
|
-
}
|
|
1814
|
-
function generateDropColumnSQL(table, column) {
|
|
1815
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
1816
|
-
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
1817
|
-
return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
|
|
1818
|
-
}
|
|
1819
|
-
function generateAlterColumnSQL(table, column, changes) {
|
|
1820
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
1821
|
-
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
1822
|
-
const statements = [];
|
|
1823
|
-
const changesTo = changes.to;
|
|
1824
|
-
const changesFrom = changes.from;
|
|
1825
|
-
const changesToType = changesTo?.type;
|
|
1826
|
-
const changesFromType = changesFrom?.type;
|
|
1827
|
-
if (changesToType !== changesFromType) {
|
|
1828
|
-
const newType = changesToType || "TEXT";
|
|
1829
|
-
const needsUsing = checkIfNeedsUsingClause(changesFromType || "", newType);
|
|
1830
|
-
if (needsUsing) {
|
|
1831
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" TYPE ${newType} USING "${column}"::text::${newType};`);
|
|
1832
|
-
} else {
|
|
1833
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DATA TYPE ${newType};`);
|
|
1834
|
-
}
|
|
1835
|
-
}
|
|
1836
|
-
const changesToNotNull = changesTo?.notNull;
|
|
1837
|
-
const changesFromNotNull = changesFrom?.notNull;
|
|
1838
|
-
if (changesToNotNull !== changesFromNotNull) {
|
|
1839
|
-
if (changesToNotNull) {
|
|
1840
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET NOT NULL;`);
|
|
1841
|
-
} else {
|
|
1842
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP NOT NULL;`);
|
|
1843
|
-
}
|
|
1844
|
-
}
|
|
1845
|
-
const changesToDefault = changesTo?.default;
|
|
1846
|
-
const changesFromDefault = changesFrom?.default;
|
|
1847
|
-
if (changesToDefault !== changesFromDefault) {
|
|
1848
|
-
if (changesToDefault !== undefined) {
|
|
1849
|
-
const defaultValue = formatDefaultValue(changesToDefault, changesToType || "");
|
|
1850
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DEFAULT ${defaultValue};`);
|
|
1851
|
-
} else {
|
|
1852
|
-
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP DEFAULT;`);
|
|
1853
|
-
}
|
|
1854
|
-
}
|
|
1855
|
-
return statements;
|
|
1856
|
-
}
|
|
1857
|
-
function checkIfNeedsUsingClause(fromType, toType) {
|
|
1858
|
-
if (!fromType || !toType)
|
|
1859
|
-
return false;
|
|
1860
|
-
if (fromType.includes("enum") || toType.includes("enum")) {
|
|
1861
|
-
return true;
|
|
1862
|
-
}
|
|
1863
|
-
const fromBase = fromType.split("(")[0].toLowerCase();
|
|
1864
|
-
const toBase = toType.split("(")[0].toLowerCase();
|
|
1865
|
-
if ((fromBase === "text" || fromBase === "varchar" || fromBase === "character varying") && (toBase === "jsonb" || toBase === "json")) {
|
|
1866
|
-
return true;
|
|
1867
|
-
}
|
|
1868
|
-
const needsUsingPairs = [
|
|
1869
|
-
["integer", "boolean"],
|
|
1870
|
-
["boolean", "integer"],
|
|
1871
|
-
["text", "integer"],
|
|
1872
|
-
["text", "numeric"],
|
|
1873
|
-
["text", "boolean"],
|
|
1874
|
-
["text", "uuid"],
|
|
1875
|
-
["text", "jsonb"],
|
|
1876
|
-
["text", "json"],
|
|
1877
|
-
["varchar", "integer"],
|
|
1878
|
-
["varchar", "numeric"],
|
|
1879
|
-
["varchar", "boolean"],
|
|
1880
|
-
["varchar", "uuid"],
|
|
1881
|
-
["varchar", "jsonb"],
|
|
1882
|
-
["varchar", "json"],
|
|
1883
|
-
["character varying", "jsonb"],
|
|
1884
|
-
["character varying", "json"]
|
|
1885
|
-
];
|
|
1886
|
-
for (const [from, to] of needsUsingPairs) {
|
|
1887
|
-
if (fromBase === from && toBase === to || fromBase === to && toBase === from) {
|
|
1888
|
-
return true;
|
|
1889
|
-
}
|
|
1890
|
-
}
|
|
1891
|
-
return false;
|
|
1892
|
-
}
|
|
1893
|
-
function formatDefaultValue(value, type) {
|
|
1894
|
-
if (value === null || value === "NULL") {
|
|
1895
|
-
return "NULL";
|
|
1896
|
-
}
|
|
1897
|
-
if (type && (type.toLowerCase().includes("boolean") || type.toLowerCase() === "bool")) {
|
|
1898
|
-
if (value === true || value === "true" || value === "t" || value === 1) {
|
|
1899
|
-
return "true";
|
|
1900
|
-
}
|
|
1901
|
-
if (value === false || value === "false" || value === "f" || value === 0) {
|
|
1902
|
-
return "false";
|
|
1903
|
-
}
|
|
1904
|
-
}
|
|
1905
|
-
if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
|
|
1906
|
-
return String(value);
|
|
1907
|
-
}
|
|
1908
|
-
if (typeof value === "string") {
|
|
1909
|
-
if (value.includes("::")) {
|
|
1910
|
-
return value;
|
|
1911
|
-
}
|
|
1912
|
-
if (value.startsWith("'") && value.endsWith("'")) {
|
|
1913
|
-
return value;
|
|
1914
|
-
}
|
|
1915
|
-
if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
|
|
1916
|
-
return value;
|
|
1917
|
-
}
|
|
1918
|
-
if (value.toUpperCase().startsWith("CURRENT_")) {
|
|
1919
|
-
return value;
|
|
1920
|
-
}
|
|
1921
|
-
return `'${value.replace(/'/g, "''")}'`;
|
|
1922
|
-
}
|
|
1923
|
-
return String(value);
|
|
1924
|
-
}
|
|
1925
|
-
function generateCreateIndexSQL(index7) {
|
|
1926
|
-
const unique3 = index7.isUnique ? "UNIQUE " : "";
|
|
1927
|
-
const method = index7.method || "btree";
|
|
1928
|
-
const columns = index7.columns.map((c) => {
|
|
1929
|
-
if (c.isExpression) {
|
|
1930
|
-
return c.expression;
|
|
1931
|
-
}
|
|
1932
|
-
return `"${c.expression}"${c.asc === false ? " DESC" : ""}`;
|
|
1933
|
-
}).join(", ");
|
|
1934
|
-
const indexName = index7.name.includes(".") ? index7.name.split(".")[1] : index7.name;
|
|
1935
|
-
let tableRef;
|
|
1936
|
-
const indexTable = index7.table;
|
|
1937
|
-
if (indexTable?.includes(".")) {
|
|
1938
|
-
const [schema, table] = indexTable.split(".");
|
|
1939
|
-
tableRef = `"${schema}"."${table}"`;
|
|
1940
|
-
} else {
|
|
1941
|
-
tableRef = `"${indexTable || ""}"`;
|
|
1942
|
-
}
|
|
1943
|
-
return `CREATE ${unique3}INDEX "${indexName}" ON ${tableRef} USING ${method} (${columns});`;
|
|
1944
|
-
}
|
|
1945
|
-
function generateDropIndexSQL(index7) {
|
|
1946
|
-
const indexNameFull = typeof index7 === "string" ? index7 : index7.name;
|
|
1947
|
-
const indexName = indexNameFull.includes(".") ? indexNameFull.split(".")[1] : indexNameFull;
|
|
1948
|
-
return `DROP INDEX IF EXISTS "${indexName}";`;
|
|
1949
|
-
}
|
|
1950
|
-
function generateCreateForeignKeySQL(fk) {
|
|
1951
|
-
const schemaFrom = fk.schemaFrom || "public";
|
|
1952
|
-
const schemaTo = fk.schemaTo || "public";
|
|
1953
|
-
const tableFrom = fk.tableFrom;
|
|
1954
|
-
const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
|
|
1955
|
-
const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
|
|
1956
|
-
let sql21 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
|
|
1957
|
-
if (fk.onDelete) {
|
|
1958
|
-
sql21 += ` ON DELETE ${fk.onDelete}`;
|
|
1959
|
-
}
|
|
1960
|
-
if (fk.onUpdate) {
|
|
1961
|
-
sql21 += ` ON UPDATE ${fk.onUpdate}`;
|
|
1962
|
-
}
|
|
1963
|
-
return `${sql21};`;
|
|
1964
|
-
}
|
|
1965
|
-
function generateDropForeignKeySQL(fk) {
|
|
1966
|
-
const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
|
|
1967
|
-
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${fk.name}";`;
|
|
1968
|
-
}
|
|
1969
|
-
function generateCreateUniqueConstraintSQL(constraint) {
|
|
1970
|
-
const table = constraint.table || "";
|
|
1971
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
1972
|
-
const name = constraint.name;
|
|
1973
|
-
const columns = constraint.columns.map((c) => `"${c}"`).join(", ");
|
|
1974
|
-
let sql21 = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" UNIQUE`;
|
|
1975
|
-
if (constraint.nullsNotDistinct) {
|
|
1976
|
-
sql21 += ` NULLS NOT DISTINCT`;
|
|
1977
|
-
}
|
|
1978
|
-
sql21 += ` (${columns});`;
|
|
1979
|
-
return sql21;
|
|
1980
|
-
}
|
|
1981
|
-
function generateDropUniqueConstraintSQL(constraint) {
|
|
1982
|
-
const table = constraint.table || "";
|
|
1983
|
-
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
1984
|
-
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
|
|
2149
|
+
}
|
|
2150
|
+
if (definition.notNull) {
|
|
2151
|
+
parts.push("NOT NULL");
|
|
2152
|
+
}
|
|
2153
|
+
return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
|
|
1985
2154
|
}
|
|
1986
|
-
function
|
|
1987
|
-
const table = constraint.table || "";
|
|
2155
|
+
function generateDropColumnSQL(table, column) {
|
|
1988
2156
|
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
1989
|
-
const
|
|
1990
|
-
|
|
1991
|
-
return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" CHECK (${value});`;
|
|
2157
|
+
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
2158
|
+
return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
|
|
1992
2159
|
}
|
|
1993
|
-
function
|
|
1994
|
-
const table = constraint.table || "";
|
|
2160
|
+
function generateAlterColumnSQL(table, column, changes) {
|
|
1995
2161
|
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
1996
|
-
|
|
1997
|
-
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
|
|
2007
|
-
|
|
2008
|
-
|
|
2009
|
-
constructor(db) {
|
|
2010
|
-
this.db = db;
|
|
2011
|
-
}
|
|
2012
|
-
async introspectSchema(schemaName = "public") {
|
|
2013
|
-
logger4.info({ src: "plugin:sql", schemaName }, "Starting database introspection");
|
|
2014
|
-
const tables = {};
|
|
2015
|
-
const schemas = {};
|
|
2016
|
-
const enums = {};
|
|
2017
|
-
const allTables = await this.getTables(schemaName);
|
|
2018
|
-
for (const tableInfo of allTables) {
|
|
2019
|
-
const tableName = tableInfo.table_name;
|
|
2020
|
-
const tableSchema = tableInfo.table_schema || "public";
|
|
2021
|
-
logger4.debug({ src: "plugin:sql", tableSchema, tableName }, "Introspecting table");
|
|
2022
|
-
const columns = await this.getColumns(tableSchema, tableName);
|
|
2023
|
-
const columnsObject = {};
|
|
2024
|
-
const uniqueConstraintObject = {};
|
|
2025
|
-
for (const col of columns) {
|
|
2026
|
-
columnsObject[col.column_name] = {
|
|
2027
|
-
name: col.column_name,
|
|
2028
|
-
type: col.data_type,
|
|
2029
|
-
primaryKey: col.is_primary || false,
|
|
2030
|
-
notNull: col.is_nullable === "NO",
|
|
2031
|
-
default: col.column_default ? this.parseDefault(col.column_default, col.data_type) : undefined
|
|
2032
|
-
};
|
|
2033
|
-
}
|
|
2034
|
-
const indexes = await this.getIndexes(tableSchema, tableName);
|
|
2035
|
-
const indexesObject = {};
|
|
2036
|
-
for (const idx of indexes) {
|
|
2037
|
-
if (!idx.is_primary && !idx.is_unique_constraint) {
|
|
2038
|
-
if (idx.columns && Array.isArray(idx.columns) && idx.columns.length > 0) {
|
|
2039
|
-
indexesObject[idx.name] = {
|
|
2040
|
-
name: idx.name,
|
|
2041
|
-
columns: idx.columns.map((col) => ({
|
|
2042
|
-
expression: col,
|
|
2043
|
-
isExpression: false
|
|
2044
|
-
})),
|
|
2045
|
-
isUnique: idx.is_unique,
|
|
2046
|
-
method: idx.method || "btree"
|
|
2047
|
-
};
|
|
2048
|
-
}
|
|
2049
|
-
}
|
|
2050
|
-
}
|
|
2051
|
-
const foreignKeys = await this.getForeignKeys(tableSchema, tableName);
|
|
2052
|
-
const foreignKeysObject = {};
|
|
2053
|
-
for (const fk of foreignKeys) {
|
|
2054
|
-
foreignKeysObject[fk.name] = {
|
|
2055
|
-
name: fk.name,
|
|
2056
|
-
tableFrom: tableName,
|
|
2057
|
-
schemaFrom: tableSchema,
|
|
2058
|
-
tableTo: fk.foreign_table_name,
|
|
2059
|
-
schemaTo: fk.foreign_table_schema || "public",
|
|
2060
|
-
columnsFrom: [fk.column_name],
|
|
2061
|
-
columnsTo: [fk.foreign_column_name],
|
|
2062
|
-
onDelete: fk.delete_rule?.toLowerCase() || "no action",
|
|
2063
|
-
onUpdate: fk.update_rule?.toLowerCase() || "no action"
|
|
2064
|
-
};
|
|
2065
|
-
}
|
|
2066
|
-
const primaryKeys = await this.getPrimaryKeys(tableSchema, tableName);
|
|
2067
|
-
const primaryKeysObject = {};
|
|
2068
|
-
for (const pk of primaryKeys) {
|
|
2069
|
-
primaryKeysObject[pk.name] = {
|
|
2070
|
-
name: pk.name,
|
|
2071
|
-
columns: pk.columns
|
|
2072
|
-
};
|
|
2073
|
-
}
|
|
2074
|
-
const uniqueConstraints = await this.getUniqueConstraints(tableSchema, tableName);
|
|
2075
|
-
for (const unq of uniqueConstraints) {
|
|
2076
|
-
uniqueConstraintObject[unq.name] = {
|
|
2077
|
-
name: unq.name,
|
|
2078
|
-
columns: unq.columns,
|
|
2079
|
-
nullsNotDistinct: false
|
|
2080
|
-
};
|
|
2081
|
-
}
|
|
2082
|
-
const checkConstraints = await this.getCheckConstraints(tableSchema, tableName);
|
|
2083
|
-
const checksObject = {};
|
|
2084
|
-
for (const check3 of checkConstraints) {
|
|
2085
|
-
checksObject[check3.name] = {
|
|
2086
|
-
name: check3.name,
|
|
2087
|
-
value: check3.definition
|
|
2088
|
-
};
|
|
2089
|
-
}
|
|
2090
|
-
tables[`${tableSchema}.${tableName}`] = {
|
|
2091
|
-
name: tableName,
|
|
2092
|
-
schema: tableSchema,
|
|
2093
|
-
columns: columnsObject,
|
|
2094
|
-
indexes: indexesObject,
|
|
2095
|
-
foreignKeys: foreignKeysObject,
|
|
2096
|
-
compositePrimaryKeys: primaryKeysObject,
|
|
2097
|
-
uniqueConstraints: uniqueConstraintObject,
|
|
2098
|
-
checkConstraints: checksObject
|
|
2099
|
-
};
|
|
2100
|
-
if (tableSchema && tableSchema !== "public") {
|
|
2101
|
-
schemas[tableSchema] = tableSchema;
|
|
2102
|
-
}
|
|
2103
|
-
}
|
|
2104
|
-
const enumsResult = await this.getEnums(schemaName);
|
|
2105
|
-
for (const enumInfo of enumsResult) {
|
|
2106
|
-
const key = `${enumInfo.schema}.${enumInfo.name}`;
|
|
2107
|
-
if (!enums[key]) {
|
|
2108
|
-
enums[key] = {
|
|
2109
|
-
name: enumInfo.name,
|
|
2110
|
-
schema: enumInfo.schema,
|
|
2111
|
-
values: []
|
|
2112
|
-
};
|
|
2113
|
-
}
|
|
2114
|
-
enums[key].values.push(enumInfo.value);
|
|
2162
|
+
const tableNameWithSchema = `"${schema}"."${tableName}"`;
|
|
2163
|
+
const statements = [];
|
|
2164
|
+
const changesTo = changes.to;
|
|
2165
|
+
const changesFrom = changes.from;
|
|
2166
|
+
const changesToType = changesTo?.type;
|
|
2167
|
+
const changesFromType = changesFrom?.type;
|
|
2168
|
+
if (changesToType !== changesFromType) {
|
|
2169
|
+
const newType = changesToType || "TEXT";
|
|
2170
|
+
const needsUsing = checkIfNeedsUsingClause(changesFromType || "", newType);
|
|
2171
|
+
if (needsUsing) {
|
|
2172
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" TYPE ${newType} USING "${column}"::text::${newType};`);
|
|
2173
|
+
} else {
|
|
2174
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DATA TYPE ${newType};`);
|
|
2115
2175
|
}
|
|
2116
|
-
logger4.info({ src: "plugin:sql", tableCount: Object.keys(tables).length }, "Database introspection complete");
|
|
2117
|
-
return {
|
|
2118
|
-
version: "7",
|
|
2119
|
-
dialect: "postgresql",
|
|
2120
|
-
tables,
|
|
2121
|
-
schemas,
|
|
2122
|
-
enums,
|
|
2123
|
-
_meta: {
|
|
2124
|
-
schemas: {},
|
|
2125
|
-
tables: {},
|
|
2126
|
-
columns: {}
|
|
2127
|
-
}
|
|
2128
|
-
};
|
|
2129
2176
|
}
|
|
2130
|
-
|
|
2131
|
-
|
|
2132
|
-
|
|
2133
|
-
|
|
2134
|
-
|
|
2135
|
-
|
|
2136
|
-
|
|
2137
|
-
|
|
2138
|
-
return getRows2(result);
|
|
2177
|
+
const changesToNotNull = changesTo?.notNull;
|
|
2178
|
+
const changesFromNotNull = changesFrom?.notNull;
|
|
2179
|
+
if (changesToNotNull !== changesFromNotNull) {
|
|
2180
|
+
if (changesToNotNull) {
|
|
2181
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET NOT NULL;`);
|
|
2182
|
+
} else {
|
|
2183
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP NOT NULL;`);
|
|
2184
|
+
}
|
|
2139
2185
|
}
|
|
2140
|
-
|
|
2141
|
-
|
|
2142
|
-
|
|
2143
|
-
|
|
2144
|
-
|
|
2145
|
-
|
|
2146
|
-
|
|
2147
|
-
|
|
2148
|
-
|
|
2149
|
-
AND EXISTS (
|
|
2150
|
-
SELECT FROM pg_attrdef ad
|
|
2151
|
-
WHERE ad.adrelid = a.attrelid
|
|
2152
|
-
AND ad.adnum = a.attnum
|
|
2153
|
-
AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
|
|
2154
|
-
|| pg_get_serial_sequence(a.attrelid::regclass::text, a.attname)::regclass || '''::regclass)'
|
|
2155
|
-
)
|
|
2156
|
-
THEN CASE a.atttypid
|
|
2157
|
-
WHEN 'int'::regtype THEN 'serial'
|
|
2158
|
-
WHEN 'int8'::regtype THEN 'bigserial'
|
|
2159
|
-
WHEN 'int2'::regtype THEN 'smallserial'
|
|
2160
|
-
END
|
|
2161
|
-
ELSE format_type(a.atttypid, a.atttypmod)
|
|
2162
|
-
END AS data_type,
|
|
2163
|
-
pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
|
|
2164
|
-
CASE
|
|
2165
|
-
WHEN con.contype = 'p' THEN true
|
|
2166
|
-
ELSE false
|
|
2167
|
-
END AS is_primary
|
|
2168
|
-
FROM pg_attribute a
|
|
2169
|
-
JOIN pg_class cls ON cls.oid = a.attrelid
|
|
2170
|
-
JOIN pg_namespace ns ON ns.oid = cls.relnamespace
|
|
2171
|
-
LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
|
|
2172
|
-
LEFT JOIN pg_constraint con ON con.conrelid = a.attrelid
|
|
2173
|
-
AND a.attnum = ANY(con.conkey)
|
|
2174
|
-
AND con.contype = 'p'
|
|
2175
|
-
WHERE
|
|
2176
|
-
a.attnum > 0
|
|
2177
|
-
AND NOT a.attisdropped
|
|
2178
|
-
AND ns.nspname = ${schemaName}
|
|
2179
|
-
AND cls.relname = ${tableName}
|
|
2180
|
-
ORDER BY a.attnum`);
|
|
2181
|
-
return getRows2(result);
|
|
2186
|
+
const changesToDefault = changesTo?.default;
|
|
2187
|
+
const changesFromDefault = changesFrom?.default;
|
|
2188
|
+
if (changesToDefault !== changesFromDefault) {
|
|
2189
|
+
if (changesToDefault !== undefined) {
|
|
2190
|
+
const defaultValue = formatDefaultValue(changesToDefault, changesToType || "");
|
|
2191
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DEFAULT ${defaultValue};`);
|
|
2192
|
+
} else {
|
|
2193
|
+
statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP DEFAULT;`);
|
|
2194
|
+
}
|
|
2182
2195
|
}
|
|
2183
|
-
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
SELECT a.attname
|
|
2191
|
-
FROM pg_attribute a
|
|
2192
|
-
WHERE a.attrelid = idx.indrelid
|
|
2193
|
-
AND a.attnum = ANY(idx.indkey::int[])
|
|
2194
|
-
ORDER BY a.attnum
|
|
2195
|
-
) AS columns,
|
|
2196
|
-
am.amname AS method
|
|
2197
|
-
FROM pg_index idx
|
|
2198
|
-
JOIN pg_class i ON i.oid = idx.indexrelid
|
|
2199
|
-
JOIN pg_class c ON c.oid = idx.indrelid
|
|
2200
|
-
JOIN pg_namespace n ON n.oid = c.relnamespace
|
|
2201
|
-
JOIN pg_am am ON am.oid = i.relam
|
|
2202
|
-
LEFT JOIN pg_constraint con ON con.conindid = idx.indexrelid
|
|
2203
|
-
WHERE n.nspname = ${schemaName}
|
|
2204
|
-
AND c.relname = ${tableName}`);
|
|
2205
|
-
return getRows2(result);
|
|
2196
|
+
return statements;
|
|
2197
|
+
}
|
|
2198
|
+
function checkIfNeedsUsingClause(fromType, toType) {
|
|
2199
|
+
if (!fromType || !toType)
|
|
2200
|
+
return false;
|
|
2201
|
+
if (fromType.includes("enum") || toType.includes("enum")) {
|
|
2202
|
+
return true;
|
|
2206
2203
|
}
|
|
2207
|
-
|
|
2208
|
-
|
|
2209
|
-
|
|
2210
|
-
|
|
2211
|
-
fnsp.nspname AS foreign_table_schema,
|
|
2212
|
-
frel.relname AS foreign_table_name,
|
|
2213
|
-
fatt.attname AS foreign_column_name,
|
|
2214
|
-
CASE con.confupdtype
|
|
2215
|
-
WHEN 'a' THEN 'NO ACTION'
|
|
2216
|
-
WHEN 'r' THEN 'RESTRICT'
|
|
2217
|
-
WHEN 'n' THEN 'SET NULL'
|
|
2218
|
-
WHEN 'c' THEN 'CASCADE'
|
|
2219
|
-
WHEN 'd' THEN 'SET DEFAULT'
|
|
2220
|
-
END AS update_rule,
|
|
2221
|
-
CASE con.confdeltype
|
|
2222
|
-
WHEN 'a' THEN 'NO ACTION'
|
|
2223
|
-
WHEN 'r' THEN 'RESTRICT'
|
|
2224
|
-
WHEN 'n' THEN 'SET NULL'
|
|
2225
|
-
WHEN 'c' THEN 'CASCADE'
|
|
2226
|
-
WHEN 'd' THEN 'SET DEFAULT'
|
|
2227
|
-
END AS delete_rule
|
|
2228
|
-
FROM pg_catalog.pg_constraint con
|
|
2229
|
-
JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
|
|
2230
|
-
JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
|
|
2231
|
-
LEFT JOIN pg_catalog.pg_attribute att ON att.attnum = ANY (con.conkey)
|
|
2232
|
-
AND att.attrelid = con.conrelid
|
|
2233
|
-
LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
|
|
2234
|
-
LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
|
|
2235
|
-
LEFT JOIN pg_catalog.pg_attribute fatt ON fatt.attnum = ANY (con.confkey)
|
|
2236
|
-
AND fatt.attrelid = con.confrelid
|
|
2237
|
-
WHERE con.contype = 'f'
|
|
2238
|
-
AND nsp.nspname = ${schemaName}
|
|
2239
|
-
AND rel.relname = ${tableName}`);
|
|
2240
|
-
return getRows2(result);
|
|
2204
|
+
const fromBase = fromType.split("(")[0].toLowerCase();
|
|
2205
|
+
const toBase = toType.split("(")[0].toLowerCase();
|
|
2206
|
+
if ((fromBase === "text" || fromBase === "varchar" || fromBase === "character varying") && (toBase === "jsonb" || toBase === "json")) {
|
|
2207
|
+
return true;
|
|
2241
2208
|
}
|
|
2242
|
-
|
|
2243
|
-
|
|
2244
|
-
|
|
2245
|
-
|
|
2246
|
-
|
|
2247
|
-
|
|
2248
|
-
|
|
2249
|
-
|
|
2250
|
-
|
|
2251
|
-
|
|
2252
|
-
|
|
2253
|
-
|
|
2254
|
-
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2209
|
+
const needsUsingPairs = [
|
|
2210
|
+
["integer", "boolean"],
|
|
2211
|
+
["boolean", "integer"],
|
|
2212
|
+
["text", "integer"],
|
|
2213
|
+
["text", "numeric"],
|
|
2214
|
+
["text", "boolean"],
|
|
2215
|
+
["text", "uuid"],
|
|
2216
|
+
["text", "jsonb"],
|
|
2217
|
+
["text", "json"],
|
|
2218
|
+
["varchar", "integer"],
|
|
2219
|
+
["varchar", "numeric"],
|
|
2220
|
+
["varchar", "boolean"],
|
|
2221
|
+
["varchar", "uuid"],
|
|
2222
|
+
["varchar", "jsonb"],
|
|
2223
|
+
["varchar", "json"],
|
|
2224
|
+
["character varying", "jsonb"],
|
|
2225
|
+
["character varying", "json"]
|
|
2226
|
+
];
|
|
2227
|
+
for (const [from, to] of needsUsingPairs) {
|
|
2228
|
+
if (fromBase === from && toBase === to || fromBase === to && toBase === from) {
|
|
2229
|
+
return true;
|
|
2230
|
+
}
|
|
2259
2231
|
}
|
|
2260
|
-
|
|
2261
|
-
|
|
2262
|
-
|
|
2263
|
-
|
|
2264
|
-
|
|
2265
|
-
FROM pg_attribute a
|
|
2266
|
-
WHERE a.attrelid = con.conrelid
|
|
2267
|
-
AND a.attnum = ANY(con.conkey)
|
|
2268
|
-
ORDER BY a.attnum
|
|
2269
|
-
) AS columns
|
|
2270
|
-
FROM pg_constraint con
|
|
2271
|
-
JOIN pg_class rel ON rel.oid = con.conrelid
|
|
2272
|
-
JOIN pg_namespace nsp ON nsp.oid = con.connamespace
|
|
2273
|
-
WHERE con.contype = 'u'
|
|
2274
|
-
AND nsp.nspname = ${schemaName}
|
|
2275
|
-
AND rel.relname = ${tableName}`);
|
|
2276
|
-
return getRows2(result);
|
|
2232
|
+
return false;
|
|
2233
|
+
}
|
|
2234
|
+
function formatDefaultValue(value, type) {
|
|
2235
|
+
if (value === null || value === "NULL") {
|
|
2236
|
+
return "NULL";
|
|
2277
2237
|
}
|
|
2278
|
-
|
|
2279
|
-
|
|
2280
|
-
|
|
2281
|
-
|
|
2282
|
-
|
|
2283
|
-
|
|
2284
|
-
|
|
2285
|
-
WHERE con.contype = 'c'
|
|
2286
|
-
AND nsp.nspname = ${schemaName}
|
|
2287
|
-
AND rel.relname = ${tableName}`);
|
|
2288
|
-
return getRows2(result);
|
|
2238
|
+
if (type && (type.toLowerCase().includes("boolean") || type.toLowerCase() === "bool")) {
|
|
2239
|
+
if (value === true || value === "true" || value === "t" || value === 1) {
|
|
2240
|
+
return "true";
|
|
2241
|
+
}
|
|
2242
|
+
if (value === false || value === "false" || value === "f" || value === 0) {
|
|
2243
|
+
return "false";
|
|
2244
|
+
}
|
|
2289
2245
|
}
|
|
2290
|
-
|
|
2291
|
-
|
|
2292
|
-
n.nspname AS schema,
|
|
2293
|
-
t.typname AS name,
|
|
2294
|
-
e.enumlabel AS value,
|
|
2295
|
-
e.enumsortorder AS sort_order
|
|
2296
|
-
FROM pg_type t
|
|
2297
|
-
JOIN pg_enum e ON t.oid = e.enumtypid
|
|
2298
|
-
JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
|
|
2299
|
-
WHERE n.nspname = ${schemaName}
|
|
2300
|
-
ORDER BY schema, name, sort_order`);
|
|
2301
|
-
return getRows2(result);
|
|
2246
|
+
if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
|
|
2247
|
+
return String(value);
|
|
2302
2248
|
}
|
|
2303
|
-
|
|
2304
|
-
if (
|
|
2305
|
-
return;
|
|
2306
|
-
const match = defaultValue.match(/^'(.*)'::/);
|
|
2307
|
-
if (match) {
|
|
2308
|
-
return `'${match[1]}'`;
|
|
2249
|
+
if (typeof value === "string") {
|
|
2250
|
+
if (value.includes("::")) {
|
|
2251
|
+
return value;
|
|
2309
2252
|
}
|
|
2310
|
-
if (
|
|
2311
|
-
return;
|
|
2253
|
+
if (value.startsWith("'") && value.endsWith("'")) {
|
|
2254
|
+
return value;
|
|
2312
2255
|
}
|
|
2313
|
-
if (
|
|
2314
|
-
|
|
2315
|
-
return "true";
|
|
2316
|
-
if (defaultValue === "false")
|
|
2317
|
-
return "false";
|
|
2256
|
+
if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
|
|
2257
|
+
return value;
|
|
2318
2258
|
}
|
|
2319
|
-
|
|
2259
|
+
if (value.toUpperCase().startsWith("CURRENT_")) {
|
|
2260
|
+
return value;
|
|
2261
|
+
}
|
|
2262
|
+
return `'${value.replace(/'/g, "''")}'`;
|
|
2320
2263
|
}
|
|
2321
|
-
|
|
2322
|
-
|
|
2323
|
-
|
|
2324
|
-
|
|
2325
|
-
|
|
2326
|
-
|
|
2327
|
-
|
|
2328
|
-
|
|
2329
|
-
|
|
2264
|
+
return String(value);
|
|
2265
|
+
}
|
|
2266
|
+
function generateCreateIndexSQL(index7) {
|
|
2267
|
+
const unique3 = index7.isUnique ? "UNIQUE " : "";
|
|
2268
|
+
const method = index7.method || "btree";
|
|
2269
|
+
const columns = index7.columns.map((c) => {
|
|
2270
|
+
if (c.isExpression) {
|
|
2271
|
+
return c.expression;
|
|
2272
|
+
}
|
|
2273
|
+
return `"${c.expression}"${c.asc === false ? " DESC" : ""}`;
|
|
2274
|
+
}).join(", ");
|
|
2275
|
+
const indexName = index7.name.includes(".") ? index7.name.split(".")[1] : index7.name;
|
|
2276
|
+
let tableRef;
|
|
2277
|
+
const indexTable = index7.table;
|
|
2278
|
+
if (indexTable?.includes(".")) {
|
|
2279
|
+
const [schema, table] = indexTable.split(".");
|
|
2280
|
+
tableRef = `"${schema}"."${table}"`;
|
|
2281
|
+
} else {
|
|
2282
|
+
tableRef = `"${indexTable || ""}"`;
|
|
2330
2283
|
}
|
|
2331
|
-
|
|
2332
|
-
|
|
2284
|
+
return `CREATE ${unique3}INDEX "${indexName}" ON ${tableRef} USING ${method} (${columns});`;
|
|
2285
|
+
}
|
|
2286
|
+
function generateDropIndexSQL(index7) {
|
|
2287
|
+
const indexNameFull = typeof index7 === "string" ? index7 : index7.name;
|
|
2288
|
+
const indexName = indexNameFull.includes(".") ? indexNameFull.split(".")[1] : indexNameFull;
|
|
2289
|
+
return `DROP INDEX IF EXISTS "${indexName}";`;
|
|
2290
|
+
}
|
|
2291
|
+
function generateCreateForeignKeySQL(fk) {
|
|
2292
|
+
const schemaFrom = fk.schemaFrom || "public";
|
|
2293
|
+
const schemaTo = fk.schemaTo || "public";
|
|
2294
|
+
const tableFrom = fk.tableFrom;
|
|
2295
|
+
const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
|
|
2296
|
+
const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
|
|
2297
|
+
let sql22 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
|
|
2298
|
+
if (fk.onDelete) {
|
|
2299
|
+
sql22 += ` ON DELETE ${fk.onDelete}`;
|
|
2333
2300
|
}
|
|
2301
|
+
if (fk.onUpdate) {
|
|
2302
|
+
sql22 += ` ON UPDATE ${fk.onUpdate}`;
|
|
2303
|
+
}
|
|
2304
|
+
return `${sql22};`;
|
|
2334
2305
|
}
|
|
2335
|
-
|
|
2306
|
+
function generateDropForeignKeySQL(fk) {
|
|
2307
|
+
const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
|
|
2308
|
+
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${fk.name}";`;
|
|
2309
|
+
}
|
|
2310
|
+
function generateCreateUniqueConstraintSQL(constraint) {
|
|
2311
|
+
const table = constraint.table || "";
|
|
2312
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2313
|
+
const name = constraint.name;
|
|
2314
|
+
const columns = constraint.columns.map((c) => `"${c}"`).join(", ");
|
|
2315
|
+
let sql22 = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" UNIQUE`;
|
|
2316
|
+
if (constraint.nullsNotDistinct) {
|
|
2317
|
+
sql22 += ` NULLS NOT DISTINCT`;
|
|
2318
|
+
}
|
|
2319
|
+
sql22 += ` (${columns});`;
|
|
2320
|
+
return sql22;
|
|
2321
|
+
}
|
|
2322
|
+
function generateDropUniqueConstraintSQL(constraint) {
|
|
2323
|
+
const table = constraint.table || "";
|
|
2324
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2325
|
+
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
|
|
2326
|
+
}
|
|
2327
|
+
function generateCreateCheckConstraintSQL(constraint) {
|
|
2328
|
+
const table = constraint.table || "";
|
|
2329
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2330
|
+
const name = constraint.name;
|
|
2331
|
+
const value = constraint.value;
|
|
2332
|
+
return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" CHECK (${value});`;
|
|
2333
|
+
}
|
|
2334
|
+
function generateDropCheckConstraintSQL(constraint) {
|
|
2335
|
+
const table = constraint.table || "";
|
|
2336
|
+
const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
|
|
2337
|
+
return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
|
|
2338
|
+
}
|
|
2339
|
+
var init_sql_generator = () => {};
|
|
2336
2340
|
|
|
2337
2341
|
// runtime-migrator/extension-manager.ts
|
|
2338
2342
|
import { logger as logger5 } from "@elizaos/core";
|
|
@@ -3151,15 +3155,9 @@ import {
|
|
|
3151
3155
|
or,
|
|
3152
3156
|
sql as sql27
|
|
3153
3157
|
} from "drizzle-orm";
|
|
3154
|
-
|
|
3155
|
-
|
|
3156
|
-
var
|
|
3157
|
-
for (let i = 0;i < 256; ++i) {
|
|
3158
|
-
byteToHex.push((i + 256).toString(16).slice(1));
|
|
3159
|
-
}
|
|
3160
|
-
function unsafeStringify(arr, offset = 0) {
|
|
3161
|
-
return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
|
|
3162
|
-
}
|
|
3158
|
+
// node_modules/uuid/dist/native.js
|
|
3159
|
+
var randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
|
|
3160
|
+
var native_default = { randomUUID };
|
|
3163
3161
|
|
|
3164
3162
|
// node_modules/uuid/dist/rng.js
|
|
3165
3163
|
var getRandomValues;
|
|
@@ -3174,9 +3172,14 @@ function rng() {
|
|
|
3174
3172
|
return getRandomValues(rnds8);
|
|
3175
3173
|
}
|
|
3176
3174
|
|
|
3177
|
-
// node_modules/uuid/dist/
|
|
3178
|
-
var
|
|
3179
|
-
|
|
3175
|
+
// node_modules/uuid/dist/stringify.js
|
|
3176
|
+
var byteToHex = [];
|
|
3177
|
+
for (let i = 0;i < 256; ++i) {
|
|
3178
|
+
byteToHex.push((i + 256).toString(16).slice(1));
|
|
3179
|
+
}
|
|
3180
|
+
function unsafeStringify(arr, offset = 0) {
|
|
3181
|
+
return (byteToHex[arr[offset + 0]] + byteToHex[arr[offset + 1]] + byteToHex[arr[offset + 2]] + byteToHex[arr[offset + 3]] + "-" + byteToHex[arr[offset + 4]] + byteToHex[arr[offset + 5]] + "-" + byteToHex[arr[offset + 6]] + byteToHex[arr[offset + 7]] + "-" + byteToHex[arr[offset + 8]] + byteToHex[arr[offset + 9]] + "-" + byteToHex[arr[offset + 10]] + byteToHex[arr[offset + 11]] + byteToHex[arr[offset + 12]] + byteToHex[arr[offset + 13]] + byteToHex[arr[offset + 14]] + byteToHex[arr[offset + 15]]).toLowerCase();
|
|
3182
|
+
}
|
|
3180
3183
|
|
|
3181
3184
|
// node_modules/uuid/dist/v4.js
|
|
3182
3185
|
function _v4(options, buf, offset) {
|
|
@@ -6705,5 +6708,5 @@ export {
|
|
|
6705
6708
|
DatabaseMigrationService
|
|
6706
6709
|
};
|
|
6707
6710
|
|
|
6708
|
-
//# debugId=
|
|
6711
|
+
//# debugId=E44EA4F58C58A8D764756E2164756E21
|
|
6709
6712
|
//# sourceMappingURL=index.browser.js.map
|