@elizaos/plugin-sql 2.0.0-alpha.16 → 2.0.0-alpha.17

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.
@@ -885,527 +885,167 @@ async function applyEntityRLSToAllTables(adapter) {
885
885
  logger2.warn("[Entity RLS] Failed to apply entity RLS to some tables:", String(error));
886
886
  }
887
887
  }
888
- var init_rls = () => {};
888
+ var init_rls = __esm(() => {
889
+ init_agent();
890
+ init_server();
891
+ });
889
892
 
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);
893
+ // runtime-migrator/drizzle-adapters/diff-calculator.ts
894
+ var exports_diff_calculator = {};
895
+ __export(exports_diff_calculator, {
896
+ hasDiffChanges: () => hasDiffChanges,
897
+ calculateDiff: () => calculateDiff
898
+ });
899
+ function normalizeType(type) {
900
+ if (!type)
901
+ return "";
902
+ const normalized = type.toLowerCase().trim();
903
+ if (normalized === "timestamp without time zone" || normalized === "timestamp with time zone") {
904
+ return "timestamp";
902
905
  }
903
- return (hash >>> 0).toString(16).padStart(8, "0");
904
- }
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;
906
+ if (normalized === "serial") {
907
+ return "integer";
912
908
  }
913
- return lockId;
914
- }
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;
909
+ if (normalized === "bigserial") {
910
+ return "bigint";
911
+ }
912
+ if (normalized === "smallserial") {
913
+ return "smallint";
914
+ }
915
+ if (normalized.startsWith("numeric") || normalized.startsWith("decimal")) {
916
+ const match = normalized.match(/\((\d+)(?:,\s*(\d+))?\)/);
917
+ if (match) {
918
+ return `numeric(${match[1]}${match[2] ? `,${match[2]}` : ""})`;
919
+ }
920
+ return "numeric";
921
+ }
922
+ if (normalized.startsWith("character varying")) {
923
+ return normalized.replace("character varying", "varchar");
924
+ }
925
+ if (normalized === "text[]" || normalized === "_text") {
926
+ return "text[]";
927
+ }
928
+ return normalized;
921
929
  }
922
-
923
- class DatabaseIntrospector {
924
- db;
925
- constructor(db) {
926
- this.db = db;
930
+ function isIndexChanged(prevIndex, currIndex) {
931
+ if (prevIndex.isUnique !== currIndex.isUnique)
932
+ return true;
933
+ if (prevIndex.method !== currIndex.method)
934
+ return true;
935
+ if (prevIndex.where !== currIndex.where)
936
+ return true;
937
+ if (prevIndex.concurrently !== currIndex.concurrently)
938
+ return true;
939
+ const prevColumns = prevIndex.columns || [];
940
+ const currColumns = currIndex.columns || [];
941
+ if (prevColumns.length !== currColumns.length)
942
+ return true;
943
+ for (let i = 0;i < prevColumns.length; i++) {
944
+ const prevCol = prevColumns[i];
945
+ const currCol = currColumns[i];
946
+ if (typeof prevCol === "string" && typeof currCol === "string") {
947
+ if (prevCol !== currCol)
948
+ return true;
949
+ } else if (typeof prevCol === "object" && typeof currCol === "object") {
950
+ if (prevCol.expression !== currCol.expression)
951
+ return true;
952
+ if (prevCol.isExpression !== currCol.isExpression)
953
+ return true;
954
+ if (prevCol.asc !== currCol.asc)
955
+ return true;
956
+ if (prevCol.nulls !== currCol.nulls)
957
+ return true;
958
+ } else {
959
+ return true;
960
+ }
927
961
  }
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
- };
962
+ return false;
963
+ }
964
+ async function calculateDiff(previousSnapshot, currentSnapshot) {
965
+ const diff = {
966
+ tables: {
967
+ created: [],
968
+ deleted: [],
969
+ modified: []
970
+ },
971
+ columns: {
972
+ added: [],
973
+ deleted: [],
974
+ modified: []
975
+ },
976
+ indexes: {
977
+ created: [],
978
+ deleted: [],
979
+ altered: []
980
+ },
981
+ foreignKeys: {
982
+ created: [],
983
+ deleted: [],
984
+ altered: []
985
+ },
986
+ uniqueConstraints: {
987
+ created: [],
988
+ deleted: []
989
+ },
990
+ checkConstraints: {
991
+ created: [],
992
+ deleted: []
993
+ }
994
+ };
995
+ if (!previousSnapshot) {
996
+ diff.tables.created = Object.keys(currentSnapshot.tables);
997
+ for (const tableName in currentSnapshot.tables) {
998
+ const table = currentSnapshot.tables[tableName];
999
+ if (table.indexes) {
1000
+ for (const indexName in table.indexes) {
1001
+ diff.indexes.created.push({
1002
+ ...table.indexes[indexName],
1003
+ table: tableName
1004
+ });
1005
+ }
949
1006
  }
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
- }
1007
+ if (table.foreignKeys) {
1008
+ for (const fkName in table.foreignKeys) {
1009
+ diff.foreignKeys.created.push(table.foreignKeys[fkName]);
965
1010
  }
966
1011
  }
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
- };
1012
+ }
1013
+ return diff;
1014
+ }
1015
+ const prevTables = previousSnapshot.tables || {};
1016
+ const currTables = currentSnapshot.tables || {};
1017
+ for (const tableName in currTables) {
1018
+ if (!(tableName in prevTables)) {
1019
+ diff.tables.created.push(tableName);
1020
+ const table = currTables[tableName];
1021
+ if (table.indexes) {
1022
+ for (const indexName in table.indexes) {
1023
+ diff.indexes.created.push({
1024
+ ...table.indexes[indexName],
1025
+ table: tableName
1026
+ });
1027
+ }
981
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
+ if (table.uniqueConstraints) {
1030
+ for (const uqName in table.uniqueConstraints) {
1031
+ diff.uniqueConstraints.created.push({
1032
+ ...table.uniqueConstraints[uqName],
1033
+ table: tableName
1034
+ });
1035
+ }
989
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
+ if (table.checkConstraints) {
1038
+ for (const checkName in table.checkConstraints) {
1039
+ diff.checkConstraints.created.push({
1040
+ ...table.checkConstraints[checkName],
1041
+ table: tableName
1042
+ });
1043
+ }
997
1044
  }
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
- };
1005
- }
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
- }
1045
+ if (table.foreignKeys) {
1046
+ for (const fkName in table.foreignKeys) {
1047
+ diff.foreignKeys.created.push(table.foreignKeys[fkName]);
1048
+ }
1409
1049
  }
1410
1050
  }
1411
1051
  }
@@ -1581,6 +1221,32 @@ function hasDiffChanges(diff) {
1581
1221
  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;
1582
1222
  }
1583
1223
 
1224
+ // runtime-migrator/crypto-utils.ts
1225
+ function extendedHash(str) {
1226
+ const h1 = hashWithSeed(str, 5381);
1227
+ const h2 = hashWithSeed(str, 7919);
1228
+ const h3 = hashWithSeed(str, 104729);
1229
+ const h4 = hashWithSeed(str, 224737);
1230
+ return h1 + h2 + h3 + h4;
1231
+ }
1232
+ function hashWithSeed(str, seed) {
1233
+ let hash = seed;
1234
+ for (let i = 0;i < str.length; i++) {
1235
+ hash = hash * 33 ^ str.charCodeAt(i);
1236
+ }
1237
+ return (hash >>> 0).toString(16).padStart(8, "0");
1238
+ }
1239
+ function stringToBigInt(str) {
1240
+ const hash = extendedHash(str);
1241
+ let lockId = BigInt(`0x${hash.slice(0, 16)}`);
1242
+ const mask63Bits = 0x7fffffffffffffffn;
1243
+ lockId = lockId & mask63Bits;
1244
+ if (lockId === 0n) {
1245
+ lockId = 1n;
1246
+ }
1247
+ return lockId;
1248
+ }
1249
+
1584
1250
  // runtime-migrator/drizzle-adapters/snapshot-generator.ts
1585
1251
  import { is, SQL } from "drizzle-orm";
1586
1252
  import { getTableConfig, PgDialect, PgTable } from "drizzle-orm/pg-core";
@@ -1809,7 +1475,7 @@ function hasChanges(previousSnapshot, currentSnapshot) {
1809
1475
  const currHash = hashSnapshot(currentSnapshot);
1810
1476
  return prevHash !== currHash;
1811
1477
  }
1812
- var sqlToStr = (sql22, _casing) => {
1478
+ var sqlToStr = (sql21, _casing) => {
1813
1479
  const config = {
1814
1480
  escapeName: () => {
1815
1481
  throw new Error("we don't support params for `sql` default values");
@@ -1822,12 +1488,12 @@ var sqlToStr = (sql22, _casing) => {
1822
1488
  },
1823
1489
  casing: undefined
1824
1490
  };
1825
- return sql22.toQuery(config).sql;
1491
+ return sql21.toQuery(config).sql;
1826
1492
  };
1827
1493
  var init_snapshot_generator = () => {};
1828
1494
 
1829
1495
  // runtime-migrator/drizzle-adapters/sql-generator.ts
1830
- import { logger as logger4 } from "@elizaos/core";
1496
+ import { logger as logger3 } from "@elizaos/core";
1831
1497
  function checkForDataLoss(diff) {
1832
1498
  const result = {
1833
1499
  hasDataLoss: false,
@@ -1957,7 +1623,7 @@ async function generateMigrationSQL(previousSnapshot, currentSnapshot, diff) {
1957
1623
  }
1958
1624
  const dataLossCheck = checkForDataLoss(diff);
1959
1625
  if (dataLossCheck.warnings.length > 0) {
1960
- logger4.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
1626
+ logger3.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
1961
1627
  }
1962
1628
  const schemasToCreate = new Set;
1963
1629
  for (const tableName of diff.tables.created) {
@@ -2116,18 +1782,18 @@ function generateCreateTableSQL(fullTableName, table) {
2116
1782
  return { tableSQL, fkSQLs };
2117
1783
  }
2118
1784
  function generateColumnDefinition(name, def) {
2119
- let sql22 = `"${name}" ${def.type}`;
1785
+ let sql21 = `"${name}" ${def.type}`;
2120
1786
  if (def.primaryKey && !def.type.includes("SERIAL")) {
2121
- sql22 += " PRIMARY KEY";
1787
+ sql21 += " PRIMARY KEY";
2122
1788
  }
2123
1789
  if (def.notNull) {
2124
- sql22 += " NOT NULL";
1790
+ sql21 += " NOT NULL";
2125
1791
  }
2126
1792
  if (def.default !== undefined) {
2127
1793
  const defaultValue = formatDefaultValue(def.default, def.type);
2128
- sql22 += ` DEFAULT ${defaultValue}`;
1794
+ sql21 += ` DEFAULT ${defaultValue}`;
2129
1795
  }
2130
- return sql22;
1796
+ return sql21;
2131
1797
  }
2132
1798
  function generateAddColumnSQL(table, column, definition) {
2133
1799
  const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
@@ -2147,196 +1813,533 @@ function generateAddColumnSQL(table, column, definition) {
2147
1813
  if (definitionWithGenerated.generated) {
2148
1814
  parts.push(`GENERATED ALWAYS AS (${definitionWithGenerated.generated}) STORED`);
2149
1815
  }
2150
- if (definition.notNull) {
2151
- parts.push("NOT NULL");
1816
+ if (definition.notNull) {
1817
+ parts.push("NOT NULL");
1818
+ }
1819
+ return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
1820
+ }
1821
+ function generateDropColumnSQL(table, column) {
1822
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
1823
+ const tableNameWithSchema = `"${schema}"."${tableName}"`;
1824
+ return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
1825
+ }
1826
+ function generateAlterColumnSQL(table, column, changes) {
1827
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
1828
+ const tableNameWithSchema = `"${schema}"."${tableName}"`;
1829
+ const statements = [];
1830
+ const changesTo = changes.to;
1831
+ const changesFrom = changes.from;
1832
+ const changesToType = changesTo?.type;
1833
+ const changesFromType = changesFrom?.type;
1834
+ if (changesToType !== changesFromType) {
1835
+ const newType = changesToType || "TEXT";
1836
+ const needsUsing = checkIfNeedsUsingClause(changesFromType || "", newType);
1837
+ if (needsUsing) {
1838
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" TYPE ${newType} USING "${column}"::text::${newType};`);
1839
+ } else {
1840
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DATA TYPE ${newType};`);
1841
+ }
1842
+ }
1843
+ const changesToNotNull = changesTo?.notNull;
1844
+ const changesFromNotNull = changesFrom?.notNull;
1845
+ if (changesToNotNull !== changesFromNotNull) {
1846
+ if (changesToNotNull) {
1847
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET NOT NULL;`);
1848
+ } else {
1849
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP NOT NULL;`);
1850
+ }
1851
+ }
1852
+ const changesToDefault = changesTo?.default;
1853
+ const changesFromDefault = changesFrom?.default;
1854
+ if (changesToDefault !== changesFromDefault) {
1855
+ if (changesToDefault !== undefined) {
1856
+ const defaultValue = formatDefaultValue(changesToDefault, changesToType || "");
1857
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DEFAULT ${defaultValue};`);
1858
+ } else {
1859
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP DEFAULT;`);
1860
+ }
1861
+ }
1862
+ return statements;
1863
+ }
1864
+ function checkIfNeedsUsingClause(fromType, toType) {
1865
+ if (!fromType || !toType)
1866
+ return false;
1867
+ if (fromType.includes("enum") || toType.includes("enum")) {
1868
+ return true;
1869
+ }
1870
+ const fromBase = fromType.split("(")[0].toLowerCase();
1871
+ const toBase = toType.split("(")[0].toLowerCase();
1872
+ if ((fromBase === "text" || fromBase === "varchar" || fromBase === "character varying") && (toBase === "jsonb" || toBase === "json")) {
1873
+ return true;
1874
+ }
1875
+ const needsUsingPairs = [
1876
+ ["integer", "boolean"],
1877
+ ["boolean", "integer"],
1878
+ ["text", "integer"],
1879
+ ["text", "numeric"],
1880
+ ["text", "boolean"],
1881
+ ["text", "uuid"],
1882
+ ["text", "jsonb"],
1883
+ ["text", "json"],
1884
+ ["varchar", "integer"],
1885
+ ["varchar", "numeric"],
1886
+ ["varchar", "boolean"],
1887
+ ["varchar", "uuid"],
1888
+ ["varchar", "jsonb"],
1889
+ ["varchar", "json"],
1890
+ ["character varying", "jsonb"],
1891
+ ["character varying", "json"]
1892
+ ];
1893
+ for (const [from, to] of needsUsingPairs) {
1894
+ if (fromBase === from && toBase === to || fromBase === to && toBase === from) {
1895
+ return true;
1896
+ }
1897
+ }
1898
+ return false;
1899
+ }
1900
+ function formatDefaultValue(value, type) {
1901
+ if (value === null || value === "NULL") {
1902
+ return "NULL";
1903
+ }
1904
+ if (type && (type.toLowerCase().includes("boolean") || type.toLowerCase() === "bool")) {
1905
+ if (value === true || value === "true" || value === "t" || value === 1) {
1906
+ return "true";
1907
+ }
1908
+ if (value === false || value === "false" || value === "f" || value === 0) {
1909
+ return "false";
1910
+ }
1911
+ }
1912
+ if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
1913
+ return String(value);
1914
+ }
1915
+ if (typeof value === "string") {
1916
+ if (value.includes("::")) {
1917
+ return value;
1918
+ }
1919
+ if (value.startsWith("'") && value.endsWith("'")) {
1920
+ return value;
1921
+ }
1922
+ if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
1923
+ return value;
1924
+ }
1925
+ if (value.toUpperCase().startsWith("CURRENT_")) {
1926
+ return value;
1927
+ }
1928
+ return `'${value.replace(/'/g, "''")}'`;
1929
+ }
1930
+ return String(value);
1931
+ }
1932
+ function generateCreateIndexSQL(index7) {
1933
+ const unique3 = index7.isUnique ? "UNIQUE " : "";
1934
+ const method = index7.method || "btree";
1935
+ const columns = index7.columns.map((c) => {
1936
+ if (c.isExpression) {
1937
+ return c.expression;
1938
+ }
1939
+ return `"${c.expression}"${c.asc === false ? " DESC" : ""}`;
1940
+ }).join(", ");
1941
+ const indexName = index7.name.includes(".") ? index7.name.split(".")[1] : index7.name;
1942
+ let tableRef;
1943
+ const indexTable = index7.table;
1944
+ if (indexTable?.includes(".")) {
1945
+ const [schema, table] = indexTable.split(".");
1946
+ tableRef = `"${schema}"."${table}"`;
1947
+ } else {
1948
+ tableRef = `"${indexTable || ""}"`;
1949
+ }
1950
+ return `CREATE ${unique3}INDEX "${indexName}" ON ${tableRef} USING ${method} (${columns});`;
1951
+ }
1952
+ function generateDropIndexSQL(index7) {
1953
+ const indexNameFull = typeof index7 === "string" ? index7 : index7.name;
1954
+ const indexName = indexNameFull.includes(".") ? indexNameFull.split(".")[1] : indexNameFull;
1955
+ return `DROP INDEX IF EXISTS "${indexName}";`;
1956
+ }
1957
+ function generateCreateForeignKeySQL(fk) {
1958
+ const schemaFrom = fk.schemaFrom || "public";
1959
+ const schemaTo = fk.schemaTo || "public";
1960
+ const tableFrom = fk.tableFrom;
1961
+ const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
1962
+ const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
1963
+ let sql21 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
1964
+ if (fk.onDelete) {
1965
+ sql21 += ` ON DELETE ${fk.onDelete}`;
1966
+ }
1967
+ if (fk.onUpdate) {
1968
+ sql21 += ` ON UPDATE ${fk.onUpdate}`;
2152
1969
  }
2153
- return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
1970
+ return `${sql21};`;
2154
1971
  }
2155
- function generateDropColumnSQL(table, column) {
1972
+ function generateDropForeignKeySQL(fk) {
1973
+ const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
1974
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${fk.name}";`;
1975
+ }
1976
+ function generateCreateUniqueConstraintSQL(constraint) {
1977
+ const table = constraint.table || "";
2156
1978
  const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2157
- const tableNameWithSchema = `"${schema}"."${tableName}"`;
2158
- return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
1979
+ const name = constraint.name;
1980
+ const columns = constraint.columns.map((c) => `"${c}"`).join(", ");
1981
+ let sql21 = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" UNIQUE`;
1982
+ if (constraint.nullsNotDistinct) {
1983
+ sql21 += ` NULLS NOT DISTINCT`;
1984
+ }
1985
+ sql21 += ` (${columns});`;
1986
+ return sql21;
2159
1987
  }
2160
- function generateAlterColumnSQL(table, column, changes) {
1988
+ function generateDropUniqueConstraintSQL(constraint) {
1989
+ const table = constraint.table || "";
2161
1990
  const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
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};`);
2175
- }
1991
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
1992
+ }
1993
+ function generateCreateCheckConstraintSQL(constraint) {
1994
+ const table = constraint.table || "";
1995
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
1996
+ const name = constraint.name;
1997
+ const value = constraint.value;
1998
+ return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" CHECK (${value});`;
1999
+ }
2000
+ function generateDropCheckConstraintSQL(constraint) {
2001
+ const table = constraint.table || "";
2002
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2003
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2004
+ }
2005
+ var init_sql_generator = () => {};
2006
+
2007
+ // runtime-migrator/drizzle-adapters/database-introspector.ts
2008
+ import { logger as logger4 } from "@elizaos/core";
2009
+ import { sql as sql21 } from "drizzle-orm";
2010
+ function getRows2(result) {
2011
+ return result.rows;
2012
+ }
2013
+
2014
+ class DatabaseIntrospector {
2015
+ db;
2016
+ constructor(db) {
2017
+ this.db = db;
2176
2018
  }
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;`);
2019
+ async introspectSchema(schemaName = "public") {
2020
+ logger4.info({ src: "plugin:sql", schemaName }, "Starting database introspection");
2021
+ const tables = {};
2022
+ const schemas = {};
2023
+ const enums = {};
2024
+ const allTables = await this.getTables(schemaName);
2025
+ for (const tableInfo of allTables) {
2026
+ const tableName = tableInfo.table_name;
2027
+ const tableSchema = tableInfo.table_schema || "public";
2028
+ logger4.debug({ src: "plugin:sql", tableSchema, tableName }, "Introspecting table");
2029
+ const columns = await this.getColumns(tableSchema, tableName);
2030
+ const columnsObject = {};
2031
+ const uniqueConstraintObject = {};
2032
+ for (const col of columns) {
2033
+ columnsObject[col.column_name] = {
2034
+ name: col.column_name,
2035
+ type: col.data_type,
2036
+ primaryKey: col.is_primary || false,
2037
+ notNull: col.is_nullable === "NO",
2038
+ default: col.column_default ? this.parseDefault(col.column_default, col.data_type) : undefined
2039
+ };
2040
+ }
2041
+ const indexes = await this.getIndexes(tableSchema, tableName);
2042
+ const indexesObject = {};
2043
+ for (const idx of indexes) {
2044
+ if (!idx.is_primary && !idx.is_unique_constraint) {
2045
+ if (idx.columns && Array.isArray(idx.columns) && idx.columns.length > 0) {
2046
+ indexesObject[idx.name] = {
2047
+ name: idx.name,
2048
+ columns: idx.columns.map((col) => ({
2049
+ expression: col,
2050
+ isExpression: false
2051
+ })),
2052
+ isUnique: idx.is_unique,
2053
+ method: idx.method || "btree"
2054
+ };
2055
+ }
2056
+ }
2057
+ }
2058
+ const foreignKeys = await this.getForeignKeys(tableSchema, tableName);
2059
+ const foreignKeysObject = {};
2060
+ for (const fk of foreignKeys) {
2061
+ foreignKeysObject[fk.name] = {
2062
+ name: fk.name,
2063
+ tableFrom: tableName,
2064
+ schemaFrom: tableSchema,
2065
+ tableTo: fk.foreign_table_name,
2066
+ schemaTo: fk.foreign_table_schema || "public",
2067
+ columnsFrom: [fk.column_name],
2068
+ columnsTo: [fk.foreign_column_name],
2069
+ onDelete: fk.delete_rule?.toLowerCase() || "no action",
2070
+ onUpdate: fk.update_rule?.toLowerCase() || "no action"
2071
+ };
2072
+ }
2073
+ const primaryKeys = await this.getPrimaryKeys(tableSchema, tableName);
2074
+ const primaryKeysObject = {};
2075
+ for (const pk of primaryKeys) {
2076
+ primaryKeysObject[pk.name] = {
2077
+ name: pk.name,
2078
+ columns: pk.columns
2079
+ };
2080
+ }
2081
+ const uniqueConstraints = await this.getUniqueConstraints(tableSchema, tableName);
2082
+ for (const unq of uniqueConstraints) {
2083
+ uniqueConstraintObject[unq.name] = {
2084
+ name: unq.name,
2085
+ columns: unq.columns,
2086
+ nullsNotDistinct: false
2087
+ };
2088
+ }
2089
+ const checkConstraints = await this.getCheckConstraints(tableSchema, tableName);
2090
+ const checksObject = {};
2091
+ for (const check3 of checkConstraints) {
2092
+ checksObject[check3.name] = {
2093
+ name: check3.name,
2094
+ value: check3.definition
2095
+ };
2096
+ }
2097
+ tables[`${tableSchema}.${tableName}`] = {
2098
+ name: tableName,
2099
+ schema: tableSchema,
2100
+ columns: columnsObject,
2101
+ indexes: indexesObject,
2102
+ foreignKeys: foreignKeysObject,
2103
+ compositePrimaryKeys: primaryKeysObject,
2104
+ uniqueConstraints: uniqueConstraintObject,
2105
+ checkConstraints: checksObject
2106
+ };
2107
+ if (tableSchema && tableSchema !== "public") {
2108
+ schemas[tableSchema] = tableSchema;
2109
+ }
2184
2110
  }
2185
- }
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;`);
2111
+ const enumsResult = await this.getEnums(schemaName);
2112
+ for (const enumInfo of enumsResult) {
2113
+ const key = `${enumInfo.schema}.${enumInfo.name}`;
2114
+ if (!enums[key]) {
2115
+ enums[key] = {
2116
+ name: enumInfo.name,
2117
+ schema: enumInfo.schema,
2118
+ values: []
2119
+ };
2120
+ }
2121
+ enums[key].values.push(enumInfo.value);
2194
2122
  }
2123
+ logger4.info({ src: "plugin:sql", tableCount: Object.keys(tables).length }, "Database introspection complete");
2124
+ return {
2125
+ version: "7",
2126
+ dialect: "postgresql",
2127
+ tables,
2128
+ schemas,
2129
+ enums,
2130
+ _meta: {
2131
+ schemas: {},
2132
+ tables: {},
2133
+ columns: {}
2134
+ }
2135
+ };
2195
2136
  }
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;
2137
+ async getTables(schemaName) {
2138
+ const result = await this.db.execute(sql21`SELECT
2139
+ table_schema,
2140
+ table_name
2141
+ FROM information_schema.tables
2142
+ WHERE table_schema = ${schemaName}
2143
+ AND table_type = 'BASE TABLE'
2144
+ ORDER BY table_name`);
2145
+ return getRows2(result);
2146
+ }
2147
+ async getColumns(schemaName, tableName) {
2148
+ const result = await this.db.execute(sql21`SELECT
2149
+ a.attname AS column_name,
2150
+ CASE
2151
+ WHEN a.attnotnull THEN 'NO'
2152
+ ELSE 'YES'
2153
+ END AS is_nullable,
2154
+ CASE
2155
+ WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
2156
+ AND EXISTS (
2157
+ SELECT FROM pg_attrdef ad
2158
+ WHERE ad.adrelid = a.attrelid
2159
+ AND ad.adnum = a.attnum
2160
+ AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
2161
+ || pg_get_serial_sequence(a.attrelid::regclass::text, a.attname)::regclass || '''::regclass)'
2162
+ )
2163
+ THEN CASE a.atttypid
2164
+ WHEN 'int'::regtype THEN 'serial'
2165
+ WHEN 'int8'::regtype THEN 'bigserial'
2166
+ WHEN 'int2'::regtype THEN 'smallserial'
2167
+ END
2168
+ ELSE format_type(a.atttypid, a.atttypmod)
2169
+ END AS data_type,
2170
+ pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
2171
+ CASE
2172
+ WHEN con.contype = 'p' THEN true
2173
+ ELSE false
2174
+ END AS is_primary
2175
+ FROM pg_attribute a
2176
+ JOIN pg_class cls ON cls.oid = a.attrelid
2177
+ JOIN pg_namespace ns ON ns.oid = cls.relnamespace
2178
+ LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
2179
+ LEFT JOIN pg_constraint con ON con.conrelid = a.attrelid
2180
+ AND a.attnum = ANY(con.conkey)
2181
+ AND con.contype = 'p'
2182
+ WHERE
2183
+ a.attnum > 0
2184
+ AND NOT a.attisdropped
2185
+ AND ns.nspname = ${schemaName}
2186
+ AND cls.relname = ${tableName}
2187
+ ORDER BY a.attnum`);
2188
+ return getRows2(result);
2189
+ }
2190
+ async getIndexes(schemaName, tableName) {
2191
+ const result = await this.db.execute(sql21`SELECT
2192
+ i.relname AS name,
2193
+ idx.indisunique AS is_unique,
2194
+ idx.indisprimary AS is_primary,
2195
+ con.contype = 'u' AS is_unique_constraint,
2196
+ ARRAY(
2197
+ SELECT a.attname
2198
+ FROM pg_attribute a
2199
+ WHERE a.attrelid = idx.indrelid
2200
+ AND a.attnum = ANY(idx.indkey::int[])
2201
+ ORDER BY a.attnum
2202
+ ) AS columns,
2203
+ am.amname AS method
2204
+ FROM pg_index idx
2205
+ JOIN pg_class i ON i.oid = idx.indexrelid
2206
+ JOIN pg_class c ON c.oid = idx.indrelid
2207
+ JOIN pg_namespace n ON n.oid = c.relnamespace
2208
+ JOIN pg_am am ON am.oid = i.relam
2209
+ LEFT JOIN pg_constraint con ON con.conindid = idx.indexrelid
2210
+ WHERE n.nspname = ${schemaName}
2211
+ AND c.relname = ${tableName}`);
2212
+ return getRows2(result);
2203
2213
  }
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;
2214
+ async getForeignKeys(schemaName, tableName) {
2215
+ const result = await this.db.execute(sql21`SELECT
2216
+ con.conname AS name,
2217
+ att.attname AS column_name,
2218
+ fnsp.nspname AS foreign_table_schema,
2219
+ frel.relname AS foreign_table_name,
2220
+ fatt.attname AS foreign_column_name,
2221
+ CASE con.confupdtype
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 update_rule,
2228
+ CASE con.confdeltype
2229
+ WHEN 'a' THEN 'NO ACTION'
2230
+ WHEN 'r' THEN 'RESTRICT'
2231
+ WHEN 'n' THEN 'SET NULL'
2232
+ WHEN 'c' THEN 'CASCADE'
2233
+ WHEN 'd' THEN 'SET DEFAULT'
2234
+ END AS delete_rule
2235
+ FROM pg_catalog.pg_constraint con
2236
+ JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
2237
+ JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
2238
+ LEFT JOIN pg_catalog.pg_attribute att ON att.attnum = ANY (con.conkey)
2239
+ AND att.attrelid = con.conrelid
2240
+ LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
2241
+ LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
2242
+ LEFT JOIN pg_catalog.pg_attribute fatt ON fatt.attnum = ANY (con.confkey)
2243
+ AND fatt.attrelid = con.confrelid
2244
+ WHERE con.contype = 'f'
2245
+ AND nsp.nspname = ${schemaName}
2246
+ AND rel.relname = ${tableName}`);
2247
+ return getRows2(result);
2208
2248
  }
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
- }
2249
+ async getPrimaryKeys(schemaName, tableName) {
2250
+ const result = await this.db.execute(sql21`SELECT
2251
+ con.conname AS name,
2252
+ ARRAY(
2253
+ SELECT a.attname
2254
+ FROM pg_attribute a
2255
+ WHERE a.attrelid = con.conrelid
2256
+ AND a.attnum = ANY(con.conkey)
2257
+ ORDER BY a.attnum
2258
+ ) AS columns
2259
+ FROM pg_constraint con
2260
+ JOIN pg_class rel ON rel.oid = con.conrelid
2261
+ JOIN pg_namespace nsp ON nsp.oid = con.connamespace
2262
+ WHERE con.contype = 'p'
2263
+ AND nsp.nspname = ${schemaName}
2264
+ AND rel.relname = ${tableName}`);
2265
+ return getRows2(result);
2231
2266
  }
2232
- return false;
2233
- }
2234
- function formatDefaultValue(value, type) {
2235
- if (value === null || value === "NULL") {
2236
- return "NULL";
2267
+ async getUniqueConstraints(schemaName, tableName) {
2268
+ const result = await this.db.execute(sql21`SELECT
2269
+ con.conname AS name,
2270
+ ARRAY(
2271
+ SELECT a.attname
2272
+ FROM pg_attribute a
2273
+ WHERE a.attrelid = con.conrelid
2274
+ AND a.attnum = ANY(con.conkey)
2275
+ ORDER BY a.attnum
2276
+ ) AS columns
2277
+ FROM pg_constraint con
2278
+ JOIN pg_class rel ON rel.oid = con.conrelid
2279
+ JOIN pg_namespace nsp ON nsp.oid = con.connamespace
2280
+ WHERE con.contype = 'u'
2281
+ AND nsp.nspname = ${schemaName}
2282
+ AND rel.relname = ${tableName}`);
2283
+ return getRows2(result);
2237
2284
  }
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
- }
2285
+ async getCheckConstraints(schemaName, tableName) {
2286
+ const result = await this.db.execute(sql21`SELECT
2287
+ con.conname AS name,
2288
+ pg_get_constraintdef(con.oid) AS definition
2289
+ FROM pg_constraint con
2290
+ JOIN pg_class rel ON rel.oid = con.conrelid
2291
+ JOIN pg_namespace nsp ON nsp.oid = con.connamespace
2292
+ WHERE con.contype = 'c'
2293
+ AND nsp.nspname = ${schemaName}
2294
+ AND rel.relname = ${tableName}`);
2295
+ return getRows2(result);
2245
2296
  }
2246
- if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
2247
- return String(value);
2297
+ async getEnums(schemaName) {
2298
+ const result = await this.db.execute(sql21`SELECT
2299
+ n.nspname AS schema,
2300
+ t.typname AS name,
2301
+ e.enumlabel AS value,
2302
+ e.enumsortorder AS sort_order
2303
+ FROM pg_type t
2304
+ JOIN pg_enum e ON t.oid = e.enumtypid
2305
+ JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
2306
+ WHERE n.nspname = ${schemaName}
2307
+ ORDER BY schema, name, sort_order`);
2308
+ return getRows2(result);
2248
2309
  }
2249
- if (typeof value === "string") {
2250
- if (value.includes("::")) {
2251
- return value;
2252
- }
2253
- if (value.startsWith("'") && value.endsWith("'")) {
2254
- return value;
2255
- }
2256
- if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
2257
- return value;
2310
+ parseDefault(defaultValue, dataType) {
2311
+ if (!defaultValue)
2312
+ return;
2313
+ const match = defaultValue.match(/^'(.*)'::/);
2314
+ if (match) {
2315
+ return `'${match[1]}'`;
2258
2316
  }
2259
- if (value.toUpperCase().startsWith("CURRENT_")) {
2260
- return value;
2317
+ if (defaultValue.includes("nextval(")) {
2318
+ return;
2261
2319
  }
2262
- return `'${value.replace(/'/g, "''")}'`;
2263
- }
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;
2320
+ if (dataType === "boolean") {
2321
+ if (defaultValue === "true")
2322
+ return "true";
2323
+ if (defaultValue === "false")
2324
+ return "false";
2272
2325
  }
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 || ""}"`;
2283
- }
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}`;
2326
+ return defaultValue;
2300
2327
  }
2301
- if (fk.onUpdate) {
2302
- sql22 += ` ON UPDATE ${fk.onUpdate}`;
2328
+ async hasExistingTables(pluginName) {
2329
+ const schemaName = pluginName === "@elizaos/plugin-sql" ? "public" : this.deriveSchemaName(pluginName);
2330
+ const result = await this.db.execute(sql21`SELECT COUNT(*) AS count
2331
+ FROM information_schema.tables
2332
+ WHERE table_schema = ${schemaName}
2333
+ AND table_type = 'BASE TABLE'`);
2334
+ const firstRow = result.rows?.[0];
2335
+ const count = parseInt(firstRow && firstRow.count || "0", 10);
2336
+ return count > 0;
2303
2337
  }
2304
- return `${sql22};`;
2305
- }
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`;
2338
+ deriveSchemaName(pluginName) {
2339
+ return pluginName.replace("@", "").replace("/", "_").replace(/-/g, "_").toLowerCase();
2318
2340
  }
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
2341
  }
2339
- var init_sql_generator = () => {};
2342
+ var init_database_introspector = () => {};
2340
2343
 
2341
2344
  // runtime-migrator/extension-manager.ts
2342
2345
  import { logger as logger5 } from "@elizaos/core";
@@ -3012,10 +3015,14 @@ var init_runtime_migrator = __esm(() => {
3012
3015
  init_migration_tracker();
3013
3016
  init_snapshot_storage();
3014
3017
  });
3015
-
3016
3018
  // runtime-migrator/index.ts
3017
3019
  var init_runtime_migrator2 = __esm(() => {
3020
+ init_snapshot_generator();
3021
+ init_sql_generator();
3018
3022
  init_runtime_migrator();
3023
+ init_journal_storage();
3024
+ init_migration_tracker();
3025
+ init_snapshot_storage();
3019
3026
  });
3020
3027
 
3021
3028
  // migration-service.ts
@@ -3155,11 +3162,11 @@ import {
3155
3162
  or,
3156
3163
  sql as sql27
3157
3164
  } from "drizzle-orm";
3158
- // node_modules/uuid/dist/native.js
3165
+ // ../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/native.js
3159
3166
  var randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
3160
3167
  var native_default = { randomUUID };
3161
3168
 
3162
- // node_modules/uuid/dist/rng.js
3169
+ // ../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/rng.js
3163
3170
  var getRandomValues;
3164
3171
  var rnds8 = new Uint8Array(16);
3165
3172
  function rng() {
@@ -3172,7 +3179,7 @@ function rng() {
3172
3179
  return getRandomValues(rnds8);
3173
3180
  }
3174
3181
 
3175
- // node_modules/uuid/dist/stringify.js
3182
+ // ../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/stringify.js
3176
3183
  var byteToHex = [];
3177
3184
  for (let i = 0;i < 256; ++i) {
3178
3185
  byteToHex.push((i + 256).toString(16).slice(1));
@@ -3181,7 +3188,7 @@ function unsafeStringify(arr, offset = 0) {
3181
3188
  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
3189
  }
3183
3190
 
3184
- // node_modules/uuid/dist/v4.js
3191
+ // ../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/v4.js
3185
3192
  function _v4(options, buf, offset) {
3186
3193
  options = options || {};
3187
3194
  const rnds = options.random ?? options.rng?.() ?? rng();
@@ -6708,5 +6715,5 @@ export {
6708
6715
  DatabaseMigrationService
6709
6716
  };
6710
6717
 
6711
- //# debugId=E44EA4F58C58A8D764756E2164756E21
6718
+ //# debugId=BA2494BACAF2A1BD64756E2164756E21
6712
6719
  //# sourceMappingURL=index.browser.js.map