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

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,174 +885,534 @@ 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 = __esm(() => {
889
- init_agent();
890
- init_server();
891
- });
888
+ var init_rls = () => {};
892
889
 
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";
905
- }
906
- if (normalized === "serial") {
907
- return "integer";
908
- }
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[]";
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);
927
902
  }
928
- return normalized;
903
+ return (hash >>> 0).toString(16).padStart(8, "0");
929
904
  }
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
- }
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;
961
912
  }
962
- return false;
913
+ return lockId;
963
914
  }
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
- }
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
+ };
1006
949
  }
1007
- if (table.foreignKeys) {
1008
- for (const fkName in table.foreignKeys) {
1009
- diff.foreignKeys.created.push(table.foreignKeys[fkName]);
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
+ }
1010
965
  }
1011
966
  }
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
- }
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
+ };
1028
981
  }
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
- }
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
+ };
1036
989
  }
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
- }
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
+ };
1044
997
  }
1045
- if (table.foreignKeys) {
1046
- for (const fkName in table.foreignKeys) {
1047
- diff.foreignKeys.created.push(table.foreignKeys[fkName]);
1048
- }
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
+ };
1049
1005
  }
1050
- }
1051
- }
1052
- for (const tableName in prevTables) {
1053
- if (!(tableName in currTables)) {
1054
- diff.tables.deleted.push(tableName);
1055
- }
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
+ }
1056
1416
  }
1057
1417
  for (const tableName in currTables) {
1058
1418
  if (tableName in prevTables) {
@@ -1221,32 +1581,6 @@ function hasDiffChanges(diff) {
1221
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;
1222
1582
  }
1223
1583
 
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
-
1250
1584
  // runtime-migrator/drizzle-adapters/snapshot-generator.ts
1251
1585
  import { is, SQL } from "drizzle-orm";
1252
1586
  import { getTableConfig, PgDialect, PgTable } from "drizzle-orm/pg-core";
@@ -1475,7 +1809,7 @@ function hasChanges(previousSnapshot, currentSnapshot) {
1475
1809
  const currHash = hashSnapshot(currentSnapshot);
1476
1810
  return prevHash !== currHash;
1477
1811
  }
1478
- var sqlToStr = (sql21, _casing) => {
1812
+ var sqlToStr = (sql22, _casing) => {
1479
1813
  const config = {
1480
1814
  escapeName: () => {
1481
1815
  throw new Error("we don't support params for `sql` default values");
@@ -1488,12 +1822,12 @@ var sqlToStr = (sql21, _casing) => {
1488
1822
  },
1489
1823
  casing: undefined
1490
1824
  };
1491
- return sql21.toQuery(config).sql;
1825
+ return sql22.toQuery(config).sql;
1492
1826
  };
1493
1827
  var init_snapshot_generator = () => {};
1494
1828
 
1495
1829
  // runtime-migrator/drizzle-adapters/sql-generator.ts
1496
- import { logger as logger3 } from "@elizaos/core";
1830
+ import { logger as logger4 } from "@elizaos/core";
1497
1831
  function checkForDataLoss(diff) {
1498
1832
  const result = {
1499
1833
  hasDataLoss: false,
@@ -1623,7 +1957,7 @@ async function generateMigrationSQL(previousSnapshot, currentSnapshot, diff) {
1623
1957
  }
1624
1958
  const dataLossCheck = checkForDataLoss(diff);
1625
1959
  if (dataLossCheck.warnings.length > 0) {
1626
- logger3.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
1960
+ logger4.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
1627
1961
  }
1628
1962
  const schemasToCreate = new Set;
1629
1963
  for (const tableName of diff.tables.created) {
@@ -1776,24 +2110,29 @@ function generateCreateTableSQL(fullTableName, table) {
1776
2110
  const foreignKeys = table.foreignKeys || {};
1777
2111
  for (const [fkName, fkDef] of Object.entries(foreignKeys)) {
1778
2112
  const fk = fkDef;
1779
- const fkSQL = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${fkName}" FOREIGN KEY (${fk.columnsFrom.map((c) => `"${c}"`).join(", ")}) REFERENCES "${fk.schemaTo || "public"}"."${fk.tableTo}" (${fk.columnsTo.map((c) => `"${c}"`).join(", ")})${fk.onDelete ? ` ON DELETE ${fk.onDelete}` : ""}${fk.onUpdate ? ` ON UPDATE ${fk.onUpdate}` : ""};`;
2113
+ const fkSQL = wrapConstraintCreationGuard(fkName, buildCreateForeignKeyBodySQL({
2114
+ ...fk,
2115
+ name: fkName,
2116
+ schemaFrom: schema,
2117
+ tableFrom: tableName
2118
+ }));
1780
2119
  fkSQLs.push(fkSQL);
1781
2120
  }
1782
2121
  return { tableSQL, fkSQLs };
1783
2122
  }
1784
2123
  function generateColumnDefinition(name, def) {
1785
- let sql21 = `"${name}" ${def.type}`;
2124
+ let sql22 = `"${name}" ${def.type}`;
1786
2125
  if (def.primaryKey && !def.type.includes("SERIAL")) {
1787
- sql21 += " PRIMARY KEY";
2126
+ sql22 += " PRIMARY KEY";
1788
2127
  }
1789
2128
  if (def.notNull) {
1790
- sql21 += " NOT NULL";
2129
+ sql22 += " NOT NULL";
1791
2130
  }
1792
2131
  if (def.default !== undefined) {
1793
2132
  const defaultValue = formatDefaultValue(def.default, def.type);
1794
- sql21 += ` DEFAULT ${defaultValue}`;
2133
+ sql22 += ` DEFAULT ${defaultValue}`;
1795
2134
  }
1796
- return sql21;
2135
+ return sql22;
1797
2136
  }
1798
2137
  function generateAddColumnSQL(table, column, definition) {
1799
2138
  const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
@@ -1803,543 +2142,213 @@ function generateAddColumnSQL(table, column, definition) {
1803
2142
  if (definition.primaryKey) {
1804
2143
  parts.push("PRIMARY KEY");
1805
2144
  }
1806
- if (definition.default !== undefined) {
1807
- const defaultValue = formatDefaultValue(definition.default, definition.type);
1808
- if (defaultValue) {
1809
- parts.push(`DEFAULT ${defaultValue}`);
1810
- }
1811
- }
1812
- const definitionWithGenerated = definition;
1813
- if (definitionWithGenerated.generated) {
1814
- parts.push(`GENERATED ALWAYS AS (${definitionWithGenerated.generated}) STORED`);
1815
- }
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}`;
1969
- }
1970
- return `${sql21};`;
1971
- }
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 || "";
1978
- const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
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`;
2145
+ if (definition.default !== undefined) {
2146
+ const defaultValue = formatDefaultValue(definition.default, definition.type);
2147
+ if (defaultValue) {
2148
+ parts.push(`DEFAULT ${defaultValue}`);
2149
+ }
1984
2150
  }
1985
- sql21 += ` (${columns});`;
1986
- return sql21;
1987
- }
1988
- function generateDropUniqueConstraintSQL(constraint) {
1989
- const table = constraint.table || "";
1990
- const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
1991
- return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2151
+ const definitionWithGenerated = definition;
2152
+ if (definitionWithGenerated.generated) {
2153
+ parts.push(`GENERATED ALWAYS AS (${definitionWithGenerated.generated}) STORED`);
2154
+ }
2155
+ if (definition.notNull) {
2156
+ parts.push("NOT NULL");
2157
+ }
2158
+ return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
1992
2159
  }
1993
- function generateCreateCheckConstraintSQL(constraint) {
1994
- const table = constraint.table || "";
2160
+ function generateDropColumnSQL(table, column) {
1995
2161
  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});`;
2162
+ const tableNameWithSchema = `"${schema}"."${tableName}"`;
2163
+ return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
1999
2164
  }
2000
- function generateDropCheckConstraintSQL(constraint) {
2001
- const table = constraint.table || "";
2165
+ function generateAlterColumnSQL(table, column, changes) {
2002
2166
  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;
2018
- }
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
- }
2110
- }
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);
2167
+ const tableNameWithSchema = `"${schema}"."${tableName}"`;
2168
+ const statements = [];
2169
+ const changesTo = changes.to;
2170
+ const changesFrom = changes.from;
2171
+ const changesToType = changesTo?.type;
2172
+ const changesFromType = changesFrom?.type;
2173
+ if (changesToType !== changesFromType) {
2174
+ const newType = changesToType || "TEXT";
2175
+ const needsUsing = checkIfNeedsUsingClause(changesFromType || "", newType);
2176
+ if (needsUsing) {
2177
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" TYPE ${newType} USING "${column}"::text::${newType};`);
2178
+ } else {
2179
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DATA TYPE ${newType};`);
2122
2180
  }
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
- };
2136
- }
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
2181
  }
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);
2182
+ const changesToNotNull = changesTo?.notNull;
2183
+ const changesFromNotNull = changesFrom?.notNull;
2184
+ if (changesToNotNull !== changesFromNotNull) {
2185
+ if (changesToNotNull) {
2186
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET NOT NULL;`);
2187
+ } else {
2188
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP NOT NULL;`);
2189
+ }
2189
2190
  }
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);
2191
+ const changesToDefault = changesTo?.default;
2192
+ const changesFromDefault = changesFrom?.default;
2193
+ if (changesToDefault !== changesFromDefault) {
2194
+ if (changesToDefault !== undefined) {
2195
+ const defaultValue = formatDefaultValue(changesToDefault, changesToType || "");
2196
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DEFAULT ${defaultValue};`);
2197
+ } else {
2198
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP DEFAULT;`);
2199
+ }
2213
2200
  }
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);
2201
+ return statements;
2202
+ }
2203
+ function checkIfNeedsUsingClause(fromType, toType) {
2204
+ if (!fromType || !toType)
2205
+ return false;
2206
+ if (fromType.includes("enum") || toType.includes("enum")) {
2207
+ return true;
2248
2208
  }
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);
2209
+ const fromBase = fromType.split("(")[0].toLowerCase();
2210
+ const toBase = toType.split("(")[0].toLowerCase();
2211
+ if ((fromBase === "text" || fromBase === "varchar" || fromBase === "character varying") && (toBase === "jsonb" || toBase === "json")) {
2212
+ return true;
2266
2213
  }
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);
2214
+ const needsUsingPairs = [
2215
+ ["integer", "boolean"],
2216
+ ["boolean", "integer"],
2217
+ ["text", "integer"],
2218
+ ["text", "numeric"],
2219
+ ["text", "boolean"],
2220
+ ["text", "uuid"],
2221
+ ["text", "jsonb"],
2222
+ ["text", "json"],
2223
+ ["varchar", "integer"],
2224
+ ["varchar", "numeric"],
2225
+ ["varchar", "boolean"],
2226
+ ["varchar", "uuid"],
2227
+ ["varchar", "jsonb"],
2228
+ ["varchar", "json"],
2229
+ ["character varying", "jsonb"],
2230
+ ["character varying", "json"]
2231
+ ];
2232
+ for (const [from, to] of needsUsingPairs) {
2233
+ if (fromBase === from && toBase === to || fromBase === to && toBase === from) {
2234
+ return true;
2235
+ }
2284
2236
  }
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);
2237
+ return false;
2238
+ }
2239
+ function formatDefaultValue(value, type) {
2240
+ if (value === null || value === "NULL") {
2241
+ return "NULL";
2296
2242
  }
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);
2243
+ if (type && (type.toLowerCase().includes("boolean") || type.toLowerCase() === "bool")) {
2244
+ if (value === true || value === "true" || value === "t" || value === 1) {
2245
+ return "true";
2246
+ }
2247
+ if (value === false || value === "false" || value === "f" || value === 0) {
2248
+ return "false";
2249
+ }
2309
2250
  }
2310
- parseDefault(defaultValue, dataType) {
2311
- if (!defaultValue)
2312
- return;
2313
- const match = defaultValue.match(/^'(.*)'::/);
2314
- if (match) {
2315
- return `'${match[1]}'`;
2251
+ if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
2252
+ return String(value);
2253
+ }
2254
+ if (typeof value === "string") {
2255
+ if (value.includes("::")) {
2256
+ return value;
2316
2257
  }
2317
- if (defaultValue.includes("nextval(")) {
2318
- return;
2258
+ if (value.startsWith("'") && value.endsWith("'")) {
2259
+ return value;
2319
2260
  }
2320
- if (dataType === "boolean") {
2321
- if (defaultValue === "true")
2322
- return "true";
2323
- if (defaultValue === "false")
2324
- return "false";
2261
+ if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
2262
+ return value;
2325
2263
  }
2326
- return defaultValue;
2264
+ if (value.toUpperCase().startsWith("CURRENT_")) {
2265
+ return value;
2266
+ }
2267
+ return `'${value.replace(/'/g, "''")}'`;
2327
2268
  }
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;
2269
+ return String(value);
2270
+ }
2271
+ function generateCreateIndexSQL(index7) {
2272
+ const unique3 = index7.isUnique ? "UNIQUE " : "";
2273
+ const method = index7.method || "btree";
2274
+ const columns = index7.columns.map((c) => {
2275
+ if (c.isExpression) {
2276
+ return c.expression;
2277
+ }
2278
+ return `"${c.expression}"${c.asc === false ? " DESC" : ""}`;
2279
+ }).join(", ");
2280
+ const indexName = index7.name.includes(".") ? index7.name.split(".")[1] : index7.name;
2281
+ let tableRef;
2282
+ const indexTable = index7.table;
2283
+ if (indexTable?.includes(".")) {
2284
+ const [schema, table] = indexTable.split(".");
2285
+ tableRef = `"${schema}"."${table}"`;
2286
+ } else {
2287
+ tableRef = `"${indexTable || ""}"`;
2337
2288
  }
2338
- deriveSchemaName(pluginName) {
2339
- return pluginName.replace("@", "").replace("/", "_").replace(/-/g, "_").toLowerCase();
2289
+ return `CREATE ${unique3}INDEX "${indexName}" ON ${tableRef} USING ${method} (${columns});`;
2290
+ }
2291
+ function generateDropIndexSQL(index7) {
2292
+ const indexNameFull = typeof index7 === "string" ? index7 : index7.name;
2293
+ const indexName = indexNameFull.includes(".") ? indexNameFull.split(".")[1] : indexNameFull;
2294
+ return `DROP INDEX IF EXISTS "${indexName}";`;
2295
+ }
2296
+ function generateCreateForeignKeySQL(fk) {
2297
+ return wrapConstraintCreationGuard(fk.name, buildCreateForeignKeyBodySQL(fk));
2298
+ }
2299
+ function generateDropForeignKeySQL(fk) {
2300
+ const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
2301
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${fk.name}";`;
2302
+ }
2303
+ function generateCreateUniqueConstraintSQL(constraint) {
2304
+ const table = constraint.table || "";
2305
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2306
+ const name = constraint.name;
2307
+ const columns = constraint.columns.map((c) => `"${c}"`).join(", ");
2308
+ let sql22 = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" UNIQUE`;
2309
+ if (constraint.nullsNotDistinct) {
2310
+ sql22 += ` NULLS NOT DISTINCT`;
2340
2311
  }
2312
+ sql22 += ` (${columns});`;
2313
+ return sql22;
2341
2314
  }
2342
- var init_database_introspector = () => {};
2315
+ function generateDropUniqueConstraintSQL(constraint) {
2316
+ const table = constraint.table || "";
2317
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2318
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2319
+ }
2320
+ function generateCreateCheckConstraintSQL(constraint) {
2321
+ const table = constraint.table || "";
2322
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2323
+ const name = constraint.name;
2324
+ const value = constraint.value;
2325
+ return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" CHECK (${value});`;
2326
+ }
2327
+ function generateDropCheckConstraintSQL(constraint) {
2328
+ const table = constraint.table || "";
2329
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2330
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2331
+ }
2332
+ function buildCreateForeignKeyBodySQL(fk) {
2333
+ const schemaFrom = fk.schemaFrom || "public";
2334
+ const schemaTo = fk.schemaTo || "public";
2335
+ const tableFrom = fk.tableFrom;
2336
+ const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
2337
+ const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
2338
+ let sql22 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
2339
+ if (fk.onDelete) {
2340
+ sql22 += ` ON DELETE ${fk.onDelete}`;
2341
+ }
2342
+ if (fk.onUpdate) {
2343
+ sql22 += ` ON UPDATE ${fk.onUpdate}`;
2344
+ }
2345
+ return sql22;
2346
+ }
2347
+ function wrapConstraintCreationGuard(constraintName, statement) {
2348
+ const escapedConstraintName = constraintName.replace(/'/g, "''");
2349
+ return `DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = '${escapedConstraintName}') THEN ${statement}; END IF; END $$;`;
2350
+ }
2351
+ var init_sql_generator = () => {};
2343
2352
 
2344
2353
  // runtime-migrator/extension-manager.ts
2345
2354
  import { logger as logger5 } from "@elizaos/core";
@@ -3015,14 +3024,10 @@ var init_runtime_migrator = __esm(() => {
3015
3024
  init_migration_tracker();
3016
3025
  init_snapshot_storage();
3017
3026
  });
3027
+
3018
3028
  // runtime-migrator/index.ts
3019
3029
  var init_runtime_migrator2 = __esm(() => {
3020
- init_snapshot_generator();
3021
- init_sql_generator();
3022
3030
  init_runtime_migrator();
3023
- init_journal_storage();
3024
- init_migration_tracker();
3025
- init_snapshot_storage();
3026
3031
  });
3027
3032
 
3028
3033
  // migration-service.ts
@@ -3162,11 +3167,11 @@ import {
3162
3167
  or,
3163
3168
  sql as sql27
3164
3169
  } from "drizzle-orm";
3165
- // ../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/native.js
3170
+ // ../../../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/native.js
3166
3171
  var randomUUID = typeof crypto !== "undefined" && crypto.randomUUID && crypto.randomUUID.bind(crypto);
3167
3172
  var native_default = { randomUUID };
3168
3173
 
3169
- // ../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/rng.js
3174
+ // ../../../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/rng.js
3170
3175
  var getRandomValues;
3171
3176
  var rnds8 = new Uint8Array(16);
3172
3177
  function rng() {
@@ -3179,7 +3184,7 @@ function rng() {
3179
3184
  return getRandomValues(rnds8);
3180
3185
  }
3181
3186
 
3182
- // ../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/stringify.js
3187
+ // ../../../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/stringify.js
3183
3188
  var byteToHex = [];
3184
3189
  for (let i = 0;i < 256; ++i) {
3185
3190
  byteToHex.push((i + 256).toString(16).slice(1));
@@ -3188,7 +3193,7 @@ function unsafeStringify(arr, offset = 0) {
3188
3193
  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();
3189
3194
  }
3190
3195
 
3191
- // ../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/v4.js
3196
+ // ../../../node_modules/.bun/uuid@13.0.0/node_modules/uuid/dist/v4.js
3192
3197
  function _v4(options, buf, offset) {
3193
3198
  options = options || {};
3194
3199
  const rnds = options.random ?? options.rng?.() ?? rng();
@@ -3688,7 +3693,7 @@ class BaseDrizzleAdapter extends DatabaseAdapter {
3688
3693
  return [String(names)];
3689
3694
  }
3690
3695
  isValidUUID(value) {
3691
- return /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i.test(value);
3696
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
3692
3697
  }
3693
3698
  normalizeWorldData(world) {
3694
3699
  const worldData = {
@@ -6715,5 +6720,5 @@ export {
6715
6720
  DatabaseMigrationService
6716
6721
  };
6717
6722
 
6718
- //# debugId=BA2494BACAF2A1BD64756E2164756E21
6723
+ //# debugId=1C0FA995699E599E64756E2164756E21
6719
6724
  //# sourceMappingURL=index.browser.js.map