@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.
@@ -1086,371 +1086,6 @@ var init_rls = __esm(() => {
1086
1086
  import_drizzle_orm20 = require("drizzle-orm");
1087
1087
  });
1088
1088
 
1089
- // runtime-migrator/crypto-utils.ts
1090
- function extendedHash(str) {
1091
- const h1 = hashWithSeed(str, 5381);
1092
- const h2 = hashWithSeed(str, 7919);
1093
- const h3 = hashWithSeed(str, 104729);
1094
- const h4 = hashWithSeed(str, 224737);
1095
- return h1 + h2 + h3 + h4;
1096
- }
1097
- function hashWithSeed(str, seed) {
1098
- let hash = seed;
1099
- for (let i = 0;i < str.length; i++) {
1100
- hash = hash * 33 ^ str.charCodeAt(i);
1101
- }
1102
- return (hash >>> 0).toString(16).padStart(8, "0");
1103
- }
1104
- function stringToBigInt(str) {
1105
- const hash = extendedHash(str);
1106
- let lockId = BigInt(`0x${hash.slice(0, 16)}`);
1107
- const mask63Bits = 0x7fffffffffffffffn;
1108
- lockId = lockId & mask63Bits;
1109
- if (lockId === 0n) {
1110
- lockId = 1n;
1111
- }
1112
- return lockId;
1113
- }
1114
-
1115
- // runtime-migrator/drizzle-adapters/database-introspector.ts
1116
- function getRows2(result) {
1117
- return result.rows;
1118
- }
1119
-
1120
- class DatabaseIntrospector {
1121
- db;
1122
- constructor(db) {
1123
- this.db = db;
1124
- }
1125
- async introspectSchema(schemaName = "public") {
1126
- import_core4.logger.info({ src: "plugin:sql", schemaName }, "Starting database introspection");
1127
- const tables = {};
1128
- const schemas = {};
1129
- const enums = {};
1130
- const allTables = await this.getTables(schemaName);
1131
- for (const tableInfo of allTables) {
1132
- const tableName = tableInfo.table_name;
1133
- const tableSchema = tableInfo.table_schema || "public";
1134
- import_core4.logger.debug({ src: "plugin:sql", tableSchema, tableName }, "Introspecting table");
1135
- const columns = await this.getColumns(tableSchema, tableName);
1136
- const columnsObject = {};
1137
- const uniqueConstraintObject = {};
1138
- for (const col of columns) {
1139
- columnsObject[col.column_name] = {
1140
- name: col.column_name,
1141
- type: col.data_type,
1142
- primaryKey: col.is_primary || false,
1143
- notNull: col.is_nullable === "NO",
1144
- default: col.column_default ? this.parseDefault(col.column_default, col.data_type) : undefined
1145
- };
1146
- }
1147
- const indexes = await this.getIndexes(tableSchema, tableName);
1148
- const indexesObject = {};
1149
- for (const idx of indexes) {
1150
- if (!idx.is_primary && !idx.is_unique_constraint) {
1151
- if (idx.columns && Array.isArray(idx.columns) && idx.columns.length > 0) {
1152
- indexesObject[idx.name] = {
1153
- name: idx.name,
1154
- columns: idx.columns.map((col) => ({
1155
- expression: col,
1156
- isExpression: false
1157
- })),
1158
- isUnique: idx.is_unique,
1159
- method: idx.method || "btree"
1160
- };
1161
- }
1162
- }
1163
- }
1164
- const foreignKeys = await this.getForeignKeys(tableSchema, tableName);
1165
- const foreignKeysObject = {};
1166
- for (const fk of foreignKeys) {
1167
- foreignKeysObject[fk.name] = {
1168
- name: fk.name,
1169
- tableFrom: tableName,
1170
- schemaFrom: tableSchema,
1171
- tableTo: fk.foreign_table_name,
1172
- schemaTo: fk.foreign_table_schema || "public",
1173
- columnsFrom: [fk.column_name],
1174
- columnsTo: [fk.foreign_column_name],
1175
- onDelete: fk.delete_rule?.toLowerCase() || "no action",
1176
- onUpdate: fk.update_rule?.toLowerCase() || "no action"
1177
- };
1178
- }
1179
- const primaryKeys = await this.getPrimaryKeys(tableSchema, tableName);
1180
- const primaryKeysObject = {};
1181
- for (const pk of primaryKeys) {
1182
- primaryKeysObject[pk.name] = {
1183
- name: pk.name,
1184
- columns: pk.columns
1185
- };
1186
- }
1187
- const uniqueConstraints = await this.getUniqueConstraints(tableSchema, tableName);
1188
- for (const unq of uniqueConstraints) {
1189
- uniqueConstraintObject[unq.name] = {
1190
- name: unq.name,
1191
- columns: unq.columns,
1192
- nullsNotDistinct: false
1193
- };
1194
- }
1195
- const checkConstraints = await this.getCheckConstraints(tableSchema, tableName);
1196
- const checksObject = {};
1197
- for (const check3 of checkConstraints) {
1198
- checksObject[check3.name] = {
1199
- name: check3.name,
1200
- value: check3.definition
1201
- };
1202
- }
1203
- tables[`${tableSchema}.${tableName}`] = {
1204
- name: tableName,
1205
- schema: tableSchema,
1206
- columns: columnsObject,
1207
- indexes: indexesObject,
1208
- foreignKeys: foreignKeysObject,
1209
- compositePrimaryKeys: primaryKeysObject,
1210
- uniqueConstraints: uniqueConstraintObject,
1211
- checkConstraints: checksObject
1212
- };
1213
- if (tableSchema && tableSchema !== "public") {
1214
- schemas[tableSchema] = tableSchema;
1215
- }
1216
- }
1217
- const enumsResult = await this.getEnums(schemaName);
1218
- for (const enumInfo of enumsResult) {
1219
- const key = `${enumInfo.schema}.${enumInfo.name}`;
1220
- if (!enums[key]) {
1221
- enums[key] = {
1222
- name: enumInfo.name,
1223
- schema: enumInfo.schema,
1224
- values: []
1225
- };
1226
- }
1227
- enums[key].values.push(enumInfo.value);
1228
- }
1229
- import_core4.logger.info({ src: "plugin:sql", tableCount: Object.keys(tables).length }, "Database introspection complete");
1230
- return {
1231
- version: "7",
1232
- dialect: "postgresql",
1233
- tables,
1234
- schemas,
1235
- enums,
1236
- _meta: {
1237
- schemas: {},
1238
- tables: {},
1239
- columns: {}
1240
- }
1241
- };
1242
- }
1243
- async getTables(schemaName) {
1244
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
1245
- table_schema,
1246
- table_name
1247
- FROM information_schema.tables
1248
- WHERE table_schema = ${schemaName}
1249
- AND table_type = 'BASE TABLE'
1250
- ORDER BY table_name`);
1251
- return getRows2(result);
1252
- }
1253
- async getColumns(schemaName, tableName) {
1254
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
1255
- a.attname AS column_name,
1256
- CASE
1257
- WHEN a.attnotnull THEN 'NO'
1258
- ELSE 'YES'
1259
- END AS is_nullable,
1260
- CASE
1261
- WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
1262
- AND EXISTS (
1263
- SELECT FROM pg_attrdef ad
1264
- WHERE ad.adrelid = a.attrelid
1265
- AND ad.adnum = a.attnum
1266
- AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
1267
- || pg_get_serial_sequence(a.attrelid::regclass::text, a.attname)::regclass || '''::regclass)'
1268
- )
1269
- THEN CASE a.atttypid
1270
- WHEN 'int'::regtype THEN 'serial'
1271
- WHEN 'int8'::regtype THEN 'bigserial'
1272
- WHEN 'int2'::regtype THEN 'smallserial'
1273
- END
1274
- ELSE format_type(a.atttypid, a.atttypmod)
1275
- END AS data_type,
1276
- pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
1277
- CASE
1278
- WHEN con.contype = 'p' THEN true
1279
- ELSE false
1280
- END AS is_primary
1281
- FROM pg_attribute a
1282
- JOIN pg_class cls ON cls.oid = a.attrelid
1283
- JOIN pg_namespace ns ON ns.oid = cls.relnamespace
1284
- LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
1285
- LEFT JOIN pg_constraint con ON con.conrelid = a.attrelid
1286
- AND a.attnum = ANY(con.conkey)
1287
- AND con.contype = 'p'
1288
- WHERE
1289
- a.attnum > 0
1290
- AND NOT a.attisdropped
1291
- AND ns.nspname = ${schemaName}
1292
- AND cls.relname = ${tableName}
1293
- ORDER BY a.attnum`);
1294
- return getRows2(result);
1295
- }
1296
- async getIndexes(schemaName, tableName) {
1297
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
1298
- i.relname AS name,
1299
- idx.indisunique AS is_unique,
1300
- idx.indisprimary AS is_primary,
1301
- con.contype = 'u' AS is_unique_constraint,
1302
- ARRAY(
1303
- SELECT a.attname
1304
- FROM pg_attribute a
1305
- WHERE a.attrelid = idx.indrelid
1306
- AND a.attnum = ANY(idx.indkey::int[])
1307
- ORDER BY a.attnum
1308
- ) AS columns,
1309
- am.amname AS method
1310
- FROM pg_index idx
1311
- JOIN pg_class i ON i.oid = idx.indexrelid
1312
- JOIN pg_class c ON c.oid = idx.indrelid
1313
- JOIN pg_namespace n ON n.oid = c.relnamespace
1314
- JOIN pg_am am ON am.oid = i.relam
1315
- LEFT JOIN pg_constraint con ON con.conindid = idx.indexrelid
1316
- WHERE n.nspname = ${schemaName}
1317
- AND c.relname = ${tableName}`);
1318
- return getRows2(result);
1319
- }
1320
- async getForeignKeys(schemaName, tableName) {
1321
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
1322
- con.conname AS name,
1323
- att.attname AS column_name,
1324
- fnsp.nspname AS foreign_table_schema,
1325
- frel.relname AS foreign_table_name,
1326
- fatt.attname AS foreign_column_name,
1327
- CASE con.confupdtype
1328
- WHEN 'a' THEN 'NO ACTION'
1329
- WHEN 'r' THEN 'RESTRICT'
1330
- WHEN 'n' THEN 'SET NULL'
1331
- WHEN 'c' THEN 'CASCADE'
1332
- WHEN 'd' THEN 'SET DEFAULT'
1333
- END AS update_rule,
1334
- CASE con.confdeltype
1335
- WHEN 'a' THEN 'NO ACTION'
1336
- WHEN 'r' THEN 'RESTRICT'
1337
- WHEN 'n' THEN 'SET NULL'
1338
- WHEN 'c' THEN 'CASCADE'
1339
- WHEN 'd' THEN 'SET DEFAULT'
1340
- END AS delete_rule
1341
- FROM pg_catalog.pg_constraint con
1342
- JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
1343
- JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
1344
- LEFT JOIN pg_catalog.pg_attribute att ON att.attnum = ANY (con.conkey)
1345
- AND att.attrelid = con.conrelid
1346
- LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
1347
- LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
1348
- LEFT JOIN pg_catalog.pg_attribute fatt ON fatt.attnum = ANY (con.confkey)
1349
- AND fatt.attrelid = con.confrelid
1350
- WHERE con.contype = 'f'
1351
- AND nsp.nspname = ${schemaName}
1352
- AND rel.relname = ${tableName}`);
1353
- return getRows2(result);
1354
- }
1355
- async getPrimaryKeys(schemaName, tableName) {
1356
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
1357
- con.conname AS name,
1358
- ARRAY(
1359
- SELECT a.attname
1360
- FROM pg_attribute a
1361
- WHERE a.attrelid = con.conrelid
1362
- AND a.attnum = ANY(con.conkey)
1363
- ORDER BY a.attnum
1364
- ) AS columns
1365
- FROM pg_constraint con
1366
- JOIN pg_class rel ON rel.oid = con.conrelid
1367
- JOIN pg_namespace nsp ON nsp.oid = con.connamespace
1368
- WHERE con.contype = 'p'
1369
- AND nsp.nspname = ${schemaName}
1370
- AND rel.relname = ${tableName}`);
1371
- return getRows2(result);
1372
- }
1373
- async getUniqueConstraints(schemaName, tableName) {
1374
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
1375
- con.conname AS name,
1376
- ARRAY(
1377
- SELECT a.attname
1378
- FROM pg_attribute a
1379
- WHERE a.attrelid = con.conrelid
1380
- AND a.attnum = ANY(con.conkey)
1381
- ORDER BY a.attnum
1382
- ) AS columns
1383
- FROM pg_constraint con
1384
- JOIN pg_class rel ON rel.oid = con.conrelid
1385
- JOIN pg_namespace nsp ON nsp.oid = con.connamespace
1386
- WHERE con.contype = 'u'
1387
- AND nsp.nspname = ${schemaName}
1388
- AND rel.relname = ${tableName}`);
1389
- return getRows2(result);
1390
- }
1391
- async getCheckConstraints(schemaName, tableName) {
1392
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
1393
- con.conname AS name,
1394
- pg_get_constraintdef(con.oid) AS definition
1395
- FROM pg_constraint con
1396
- JOIN pg_class rel ON rel.oid = con.conrelid
1397
- JOIN pg_namespace nsp ON nsp.oid = con.connamespace
1398
- WHERE con.contype = 'c'
1399
- AND nsp.nspname = ${schemaName}
1400
- AND rel.relname = ${tableName}`);
1401
- return getRows2(result);
1402
- }
1403
- async getEnums(schemaName) {
1404
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT
1405
- n.nspname AS schema,
1406
- t.typname AS name,
1407
- e.enumlabel AS value,
1408
- e.enumsortorder AS sort_order
1409
- FROM pg_type t
1410
- JOIN pg_enum e ON t.oid = e.enumtypid
1411
- JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
1412
- WHERE n.nspname = ${schemaName}
1413
- ORDER BY schema, name, sort_order`);
1414
- return getRows2(result);
1415
- }
1416
- parseDefault(defaultValue, dataType) {
1417
- if (!defaultValue)
1418
- return;
1419
- const match = defaultValue.match(/^'(.*)'::/);
1420
- if (match) {
1421
- return `'${match[1]}'`;
1422
- }
1423
- if (defaultValue.includes("nextval(")) {
1424
- return;
1425
- }
1426
- if (dataType === "boolean") {
1427
- if (defaultValue === "true")
1428
- return "true";
1429
- if (defaultValue === "false")
1430
- return "false";
1431
- }
1432
- return defaultValue;
1433
- }
1434
- async hasExistingTables(pluginName) {
1435
- const schemaName = pluginName === "@elizaos/plugin-sql" ? "public" : this.deriveSchemaName(pluginName);
1436
- const result = await this.db.execute(import_drizzle_orm21.sql`SELECT COUNT(*) AS count
1437
- FROM information_schema.tables
1438
- WHERE table_schema = ${schemaName}
1439
- AND table_type = 'BASE TABLE'`);
1440
- const firstRow = result.rows?.[0];
1441
- const count = parseInt(firstRow && firstRow.count || "0", 10);
1442
- return count > 0;
1443
- }
1444
- deriveSchemaName(pluginName) {
1445
- return pluginName.replace("@", "").replace("/", "_").replace(/-/g, "_").toLowerCase();
1446
- }
1447
- }
1448
- var import_core4, import_drizzle_orm21;
1449
- var init_database_introspector = __esm(() => {
1450
- import_core4 = require("@elizaos/core");
1451
- import_drizzle_orm21 = require("drizzle-orm");
1452
- });
1453
-
1454
1089
  // runtime-migrator/drizzle-adapters/diff-calculator.ts
1455
1090
  var exports_diff_calculator = {};
1456
1091
  __export(exports_diff_calculator, {
@@ -1782,6 +1417,32 @@ function hasDiffChanges(diff) {
1782
1417
  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;
1783
1418
  }
1784
1419
 
1420
+ // runtime-migrator/crypto-utils.ts
1421
+ function extendedHash(str) {
1422
+ const h1 = hashWithSeed(str, 5381);
1423
+ const h2 = hashWithSeed(str, 7919);
1424
+ const h3 = hashWithSeed(str, 104729);
1425
+ const h4 = hashWithSeed(str, 224737);
1426
+ return h1 + h2 + h3 + h4;
1427
+ }
1428
+ function hashWithSeed(str, seed) {
1429
+ let hash = seed;
1430
+ for (let i = 0;i < str.length; i++) {
1431
+ hash = hash * 33 ^ str.charCodeAt(i);
1432
+ }
1433
+ return (hash >>> 0).toString(16).padStart(8, "0");
1434
+ }
1435
+ function stringToBigInt(str) {
1436
+ const hash = extendedHash(str);
1437
+ let lockId = BigInt(`0x${hash.slice(0, 16)}`);
1438
+ const mask63Bits = 0x7fffffffffffffffn;
1439
+ lockId = lockId & mask63Bits;
1440
+ if (lockId === 0n) {
1441
+ lockId = 1n;
1442
+ }
1443
+ return lockId;
1444
+ }
1445
+
1785
1446
  // runtime-migrator/drizzle-adapters/snapshot-generator.ts
1786
1447
  function escapeSingleQuotes(str) {
1787
1448
  return str.replace(/'/g, "''");
@@ -1817,7 +1478,7 @@ function extractTablesFromSchema(schema) {
1817
1478
  const tables = [];
1818
1479
  const exports2 = Object.values(schema);
1819
1480
  exports2.forEach((t) => {
1820
- if (import_drizzle_orm22.is(t, import_pg_core21.PgTable)) {
1481
+ if (import_drizzle_orm21.is(t, import_pg_core21.PgTable)) {
1821
1482
  tables.push(t);
1822
1483
  }
1823
1484
  });
@@ -1860,7 +1521,7 @@ async function generateSnapshot(schema) {
1860
1521
  notNull
1861
1522
  };
1862
1523
  if (column.default !== undefined) {
1863
- if (import_drizzle_orm22.is(column.default, import_drizzle_orm22.SQL)) {
1524
+ if (import_drizzle_orm21.is(column.default, import_drizzle_orm21.SQL)) {
1864
1525
  columnToSet.default = sqlToStr(column.default, undefined);
1865
1526
  } else {
1866
1527
  if (typeof column.default === "string") {
@@ -1934,7 +1595,7 @@ async function generateSnapshot(schema) {
1934
1595
  indexes.forEach((idx) => {
1935
1596
  const indexCols = idx.config.columns;
1936
1597
  const indexColumns = indexCols.map((col) => {
1937
- if (import_drizzle_orm22.is(col, import_drizzle_orm22.SQL)) {
1598
+ if (import_drizzle_orm21.is(col, import_drizzle_orm21.SQL)) {
1938
1599
  return {
1939
1600
  expression: dialect.sqlToQuery(col).sql,
1940
1601
  isExpression: true
@@ -2008,7 +1669,7 @@ function hasChanges(previousSnapshot, currentSnapshot) {
2008
1669
  const currHash = hashSnapshot(currentSnapshot);
2009
1670
  return prevHash !== currHash;
2010
1671
  }
2011
- var import_drizzle_orm22, import_pg_core21, sqlToStr = (sql22, _casing) => {
1672
+ var import_drizzle_orm21, import_pg_core21, sqlToStr = (sql21, _casing) => {
2012
1673
  const config = {
2013
1674
  escapeName: () => {
2014
1675
  throw new Error("we don't support params for `sql` default values");
@@ -2021,10 +1682,10 @@ var import_drizzle_orm22, import_pg_core21, sqlToStr = (sql22, _casing) => {
2021
1682
  },
2022
1683
  casing: undefined
2023
1684
  };
2024
- return sql22.toQuery(config).sql;
1685
+ return sql21.toQuery(config).sql;
2025
1686
  };
2026
1687
  var init_snapshot_generator = __esm(() => {
2027
- import_drizzle_orm22 = require("drizzle-orm");
1688
+ import_drizzle_orm21 = require("drizzle-orm");
2028
1689
  import_pg_core21 = require("drizzle-orm/pg-core");
2029
1690
  });
2030
1691
 
@@ -2158,7 +1819,7 @@ async function generateMigrationSQL(previousSnapshot, currentSnapshot, diff) {
2158
1819
  }
2159
1820
  const dataLossCheck = checkForDataLoss(diff);
2160
1821
  if (dataLossCheck.warnings.length > 0) {
2161
- import_core5.logger.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
1822
+ import_core4.logger.warn({ src: "plugin:sql", warnings: dataLossCheck.warnings }, "Schema changes may cause data loss");
2162
1823
  }
2163
1824
  const schemasToCreate = new Set;
2164
1825
  for (const tableName of diff.tables.created) {
@@ -2317,18 +1978,18 @@ function generateCreateTableSQL(fullTableName, table) {
2317
1978
  return { tableSQL, fkSQLs };
2318
1979
  }
2319
1980
  function generateColumnDefinition(name, def) {
2320
- let sql22 = `"${name}" ${def.type}`;
1981
+ let sql21 = `"${name}" ${def.type}`;
2321
1982
  if (def.primaryKey && !def.type.includes("SERIAL")) {
2322
- sql22 += " PRIMARY KEY";
1983
+ sql21 += " PRIMARY KEY";
2323
1984
  }
2324
1985
  if (def.notNull) {
2325
- sql22 += " NOT NULL";
1986
+ sql21 += " NOT NULL";
2326
1987
  }
2327
1988
  if (def.default !== undefined) {
2328
1989
  const defaultValue = formatDefaultValue(def.default, def.type);
2329
- sql22 += ` DEFAULT ${defaultValue}`;
1990
+ sql21 += ` DEFAULT ${defaultValue}`;
2330
1991
  }
2331
- return sql22;
1992
+ return sql21;
2332
1993
  }
2333
1994
  function generateAddColumnSQL(table, column, definition) {
2334
1995
  const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
@@ -2344,202 +2005,541 @@ function generateAddColumnSQL(table, column, definition) {
2344
2005
  parts.push(`DEFAULT ${defaultValue}`);
2345
2006
  }
2346
2007
  }
2347
- const definitionWithGenerated = definition;
2348
- if (definitionWithGenerated.generated) {
2349
- parts.push(`GENERATED ALWAYS AS (${definitionWithGenerated.generated}) STORED`);
2008
+ const definitionWithGenerated = definition;
2009
+ if (definitionWithGenerated.generated) {
2010
+ parts.push(`GENERATED ALWAYS AS (${definitionWithGenerated.generated}) STORED`);
2011
+ }
2012
+ if (definition.notNull) {
2013
+ parts.push("NOT NULL");
2014
+ }
2015
+ return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
2016
+ }
2017
+ function generateDropColumnSQL(table, column) {
2018
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2019
+ const tableNameWithSchema = `"${schema}"."${tableName}"`;
2020
+ return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
2021
+ }
2022
+ function generateAlterColumnSQL(table, column, changes) {
2023
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2024
+ const tableNameWithSchema = `"${schema}"."${tableName}"`;
2025
+ const statements = [];
2026
+ const changesTo = changes.to;
2027
+ const changesFrom = changes.from;
2028
+ const changesToType = changesTo?.type;
2029
+ const changesFromType = changesFrom?.type;
2030
+ if (changesToType !== changesFromType) {
2031
+ const newType = changesToType || "TEXT";
2032
+ const needsUsing = checkIfNeedsUsingClause(changesFromType || "", newType);
2033
+ if (needsUsing) {
2034
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" TYPE ${newType} USING "${column}"::text::${newType};`);
2035
+ } else {
2036
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DATA TYPE ${newType};`);
2037
+ }
2038
+ }
2039
+ const changesToNotNull = changesTo?.notNull;
2040
+ const changesFromNotNull = changesFrom?.notNull;
2041
+ if (changesToNotNull !== changesFromNotNull) {
2042
+ if (changesToNotNull) {
2043
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET NOT NULL;`);
2044
+ } else {
2045
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP NOT NULL;`);
2046
+ }
2047
+ }
2048
+ const changesToDefault = changesTo?.default;
2049
+ const changesFromDefault = changesFrom?.default;
2050
+ if (changesToDefault !== changesFromDefault) {
2051
+ if (changesToDefault !== undefined) {
2052
+ const defaultValue = formatDefaultValue(changesToDefault, changesToType || "");
2053
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DEFAULT ${defaultValue};`);
2054
+ } else {
2055
+ statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP DEFAULT;`);
2056
+ }
2057
+ }
2058
+ return statements;
2059
+ }
2060
+ function checkIfNeedsUsingClause(fromType, toType) {
2061
+ if (!fromType || !toType)
2062
+ return false;
2063
+ if (fromType.includes("enum") || toType.includes("enum")) {
2064
+ return true;
2065
+ }
2066
+ const fromBase = fromType.split("(")[0].toLowerCase();
2067
+ const toBase = toType.split("(")[0].toLowerCase();
2068
+ if ((fromBase === "text" || fromBase === "varchar" || fromBase === "character varying") && (toBase === "jsonb" || toBase === "json")) {
2069
+ return true;
2070
+ }
2071
+ const needsUsingPairs = [
2072
+ ["integer", "boolean"],
2073
+ ["boolean", "integer"],
2074
+ ["text", "integer"],
2075
+ ["text", "numeric"],
2076
+ ["text", "boolean"],
2077
+ ["text", "uuid"],
2078
+ ["text", "jsonb"],
2079
+ ["text", "json"],
2080
+ ["varchar", "integer"],
2081
+ ["varchar", "numeric"],
2082
+ ["varchar", "boolean"],
2083
+ ["varchar", "uuid"],
2084
+ ["varchar", "jsonb"],
2085
+ ["varchar", "json"],
2086
+ ["character varying", "jsonb"],
2087
+ ["character varying", "json"]
2088
+ ];
2089
+ for (const [from, to] of needsUsingPairs) {
2090
+ if (fromBase === from && toBase === to || fromBase === to && toBase === from) {
2091
+ return true;
2092
+ }
2093
+ }
2094
+ return false;
2095
+ }
2096
+ function formatDefaultValue(value, type) {
2097
+ if (value === null || value === "NULL") {
2098
+ return "NULL";
2099
+ }
2100
+ if (type && (type.toLowerCase().includes("boolean") || type.toLowerCase() === "bool")) {
2101
+ if (value === true || value === "true" || value === "t" || value === 1) {
2102
+ return "true";
2103
+ }
2104
+ if (value === false || value === "false" || value === "f" || value === 0) {
2105
+ return "false";
2106
+ }
2107
+ }
2108
+ if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
2109
+ return String(value);
2110
+ }
2111
+ if (typeof value === "string") {
2112
+ if (value.includes("::")) {
2113
+ return value;
2114
+ }
2115
+ if (value.startsWith("'") && value.endsWith("'")) {
2116
+ return value;
2117
+ }
2118
+ if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
2119
+ return value;
2120
+ }
2121
+ if (value.toUpperCase().startsWith("CURRENT_")) {
2122
+ return value;
2123
+ }
2124
+ return `'${value.replace(/'/g, "''")}'`;
2125
+ }
2126
+ return String(value);
2127
+ }
2128
+ function generateCreateIndexSQL(index7) {
2129
+ const unique3 = index7.isUnique ? "UNIQUE " : "";
2130
+ const method = index7.method || "btree";
2131
+ const columns = index7.columns.map((c) => {
2132
+ if (c.isExpression) {
2133
+ return c.expression;
2134
+ }
2135
+ return `"${c.expression}"${c.asc === false ? " DESC" : ""}`;
2136
+ }).join(", ");
2137
+ const indexName = index7.name.includes(".") ? index7.name.split(".")[1] : index7.name;
2138
+ let tableRef;
2139
+ const indexTable = index7.table;
2140
+ if (indexTable?.includes(".")) {
2141
+ const [schema, table] = indexTable.split(".");
2142
+ tableRef = `"${schema}"."${table}"`;
2143
+ } else {
2144
+ tableRef = `"${indexTable || ""}"`;
2145
+ }
2146
+ return `CREATE ${unique3}INDEX "${indexName}" ON ${tableRef} USING ${method} (${columns});`;
2147
+ }
2148
+ function generateDropIndexSQL(index7) {
2149
+ const indexNameFull = typeof index7 === "string" ? index7 : index7.name;
2150
+ const indexName = indexNameFull.includes(".") ? indexNameFull.split(".")[1] : indexNameFull;
2151
+ return `DROP INDEX IF EXISTS "${indexName}";`;
2152
+ }
2153
+ function generateCreateForeignKeySQL(fk) {
2154
+ const schemaFrom = fk.schemaFrom || "public";
2155
+ const schemaTo = fk.schemaTo || "public";
2156
+ const tableFrom = fk.tableFrom;
2157
+ const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
2158
+ const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
2159
+ let sql21 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
2160
+ if (fk.onDelete) {
2161
+ sql21 += ` ON DELETE ${fk.onDelete}`;
2162
+ }
2163
+ if (fk.onUpdate) {
2164
+ sql21 += ` ON UPDATE ${fk.onUpdate}`;
2350
2165
  }
2351
- if (definition.notNull) {
2352
- parts.push("NOT NULL");
2166
+ return `${sql21};`;
2167
+ }
2168
+ function generateDropForeignKeySQL(fk) {
2169
+ const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
2170
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${fk.name}";`;
2171
+ }
2172
+ function generateCreateUniqueConstraintSQL(constraint) {
2173
+ const table = constraint.table || "";
2174
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2175
+ const name = constraint.name;
2176
+ const columns = constraint.columns.map((c) => `"${c}"`).join(", ");
2177
+ let sql21 = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" UNIQUE`;
2178
+ if (constraint.nullsNotDistinct) {
2179
+ sql21 += ` NULLS NOT DISTINCT`;
2353
2180
  }
2354
- return `ALTER TABLE ${tableNameWithSchema} ADD COLUMN ${parts.join(" ")};`;
2181
+ sql21 += ` (${columns});`;
2182
+ return sql21;
2355
2183
  }
2356
- function generateDropColumnSQL(table, column) {
2184
+ function generateDropUniqueConstraintSQL(constraint) {
2185
+ const table = constraint.table || "";
2357
2186
  const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2358
- const tableNameWithSchema = `"${schema}"."${tableName}"`;
2359
- return `ALTER TABLE ${tableNameWithSchema} DROP COLUMN "${column}" CASCADE;`;
2187
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2360
2188
  }
2361
- function generateAlterColumnSQL(table, column, changes) {
2189
+ function generateCreateCheckConstraintSQL(constraint) {
2190
+ const table = constraint.table || "";
2362
2191
  const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2363
- const tableNameWithSchema = `"${schema}"."${tableName}"`;
2364
- const statements = [];
2365
- const changesTo = changes.to;
2366
- const changesFrom = changes.from;
2367
- const changesToType = changesTo?.type;
2368
- const changesFromType = changesFrom?.type;
2369
- if (changesToType !== changesFromType) {
2370
- const newType = changesToType || "TEXT";
2371
- const needsUsing = checkIfNeedsUsingClause(changesFromType || "", newType);
2372
- if (needsUsing) {
2373
- statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" TYPE ${newType} USING "${column}"::text::${newType};`);
2374
- } else {
2375
- statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DATA TYPE ${newType};`);
2376
- }
2192
+ const name = constraint.name;
2193
+ const value = constraint.value;
2194
+ return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" CHECK (${value});`;
2195
+ }
2196
+ function generateDropCheckConstraintSQL(constraint) {
2197
+ const table = constraint.table || "";
2198
+ const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2199
+ return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2200
+ }
2201
+ var import_core4;
2202
+ var init_sql_generator = __esm(() => {
2203
+ import_core4 = require("@elizaos/core");
2204
+ });
2205
+
2206
+ // runtime-migrator/drizzle-adapters/database-introspector.ts
2207
+ function getRows2(result) {
2208
+ return result.rows;
2209
+ }
2210
+
2211
+ class DatabaseIntrospector {
2212
+ db;
2213
+ constructor(db) {
2214
+ this.db = db;
2377
2215
  }
2378
- const changesToNotNull = changesTo?.notNull;
2379
- const changesFromNotNull = changesFrom?.notNull;
2380
- if (changesToNotNull !== changesFromNotNull) {
2381
- if (changesToNotNull) {
2382
- statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET NOT NULL;`);
2383
- } else {
2384
- statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP NOT NULL;`);
2216
+ async introspectSchema(schemaName = "public") {
2217
+ import_core5.logger.info({ src: "plugin:sql", schemaName }, "Starting database introspection");
2218
+ const tables = {};
2219
+ const schemas = {};
2220
+ const enums = {};
2221
+ const allTables = await this.getTables(schemaName);
2222
+ for (const tableInfo of allTables) {
2223
+ const tableName = tableInfo.table_name;
2224
+ const tableSchema = tableInfo.table_schema || "public";
2225
+ import_core5.logger.debug({ src: "plugin:sql", tableSchema, tableName }, "Introspecting table");
2226
+ const columns = await this.getColumns(tableSchema, tableName);
2227
+ const columnsObject = {};
2228
+ const uniqueConstraintObject = {};
2229
+ for (const col of columns) {
2230
+ columnsObject[col.column_name] = {
2231
+ name: col.column_name,
2232
+ type: col.data_type,
2233
+ primaryKey: col.is_primary || false,
2234
+ notNull: col.is_nullable === "NO",
2235
+ default: col.column_default ? this.parseDefault(col.column_default, col.data_type) : undefined
2236
+ };
2237
+ }
2238
+ const indexes = await this.getIndexes(tableSchema, tableName);
2239
+ const indexesObject = {};
2240
+ for (const idx of indexes) {
2241
+ if (!idx.is_primary && !idx.is_unique_constraint) {
2242
+ if (idx.columns && Array.isArray(idx.columns) && idx.columns.length > 0) {
2243
+ indexesObject[idx.name] = {
2244
+ name: idx.name,
2245
+ columns: idx.columns.map((col) => ({
2246
+ expression: col,
2247
+ isExpression: false
2248
+ })),
2249
+ isUnique: idx.is_unique,
2250
+ method: idx.method || "btree"
2251
+ };
2252
+ }
2253
+ }
2254
+ }
2255
+ const foreignKeys = await this.getForeignKeys(tableSchema, tableName);
2256
+ const foreignKeysObject = {};
2257
+ for (const fk of foreignKeys) {
2258
+ foreignKeysObject[fk.name] = {
2259
+ name: fk.name,
2260
+ tableFrom: tableName,
2261
+ schemaFrom: tableSchema,
2262
+ tableTo: fk.foreign_table_name,
2263
+ schemaTo: fk.foreign_table_schema || "public",
2264
+ columnsFrom: [fk.column_name],
2265
+ columnsTo: [fk.foreign_column_name],
2266
+ onDelete: fk.delete_rule?.toLowerCase() || "no action",
2267
+ onUpdate: fk.update_rule?.toLowerCase() || "no action"
2268
+ };
2269
+ }
2270
+ const primaryKeys = await this.getPrimaryKeys(tableSchema, tableName);
2271
+ const primaryKeysObject = {};
2272
+ for (const pk of primaryKeys) {
2273
+ primaryKeysObject[pk.name] = {
2274
+ name: pk.name,
2275
+ columns: pk.columns
2276
+ };
2277
+ }
2278
+ const uniqueConstraints = await this.getUniqueConstraints(tableSchema, tableName);
2279
+ for (const unq of uniqueConstraints) {
2280
+ uniqueConstraintObject[unq.name] = {
2281
+ name: unq.name,
2282
+ columns: unq.columns,
2283
+ nullsNotDistinct: false
2284
+ };
2285
+ }
2286
+ const checkConstraints = await this.getCheckConstraints(tableSchema, tableName);
2287
+ const checksObject = {};
2288
+ for (const check3 of checkConstraints) {
2289
+ checksObject[check3.name] = {
2290
+ name: check3.name,
2291
+ value: check3.definition
2292
+ };
2293
+ }
2294
+ tables[`${tableSchema}.${tableName}`] = {
2295
+ name: tableName,
2296
+ schema: tableSchema,
2297
+ columns: columnsObject,
2298
+ indexes: indexesObject,
2299
+ foreignKeys: foreignKeysObject,
2300
+ compositePrimaryKeys: primaryKeysObject,
2301
+ uniqueConstraints: uniqueConstraintObject,
2302
+ checkConstraints: checksObject
2303
+ };
2304
+ if (tableSchema && tableSchema !== "public") {
2305
+ schemas[tableSchema] = tableSchema;
2306
+ }
2385
2307
  }
2386
- }
2387
- const changesToDefault = changesTo?.default;
2388
- const changesFromDefault = changesFrom?.default;
2389
- if (changesToDefault !== changesFromDefault) {
2390
- if (changesToDefault !== undefined) {
2391
- const defaultValue = formatDefaultValue(changesToDefault, changesToType || "");
2392
- statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" SET DEFAULT ${defaultValue};`);
2393
- } else {
2394
- statements.push(`ALTER TABLE ${tableNameWithSchema} ALTER COLUMN "${column}" DROP DEFAULT;`);
2308
+ const enumsResult = await this.getEnums(schemaName);
2309
+ for (const enumInfo of enumsResult) {
2310
+ const key = `${enumInfo.schema}.${enumInfo.name}`;
2311
+ if (!enums[key]) {
2312
+ enums[key] = {
2313
+ name: enumInfo.name,
2314
+ schema: enumInfo.schema,
2315
+ values: []
2316
+ };
2317
+ }
2318
+ enums[key].values.push(enumInfo.value);
2395
2319
  }
2320
+ import_core5.logger.info({ src: "plugin:sql", tableCount: Object.keys(tables).length }, "Database introspection complete");
2321
+ return {
2322
+ version: "7",
2323
+ dialect: "postgresql",
2324
+ tables,
2325
+ schemas,
2326
+ enums,
2327
+ _meta: {
2328
+ schemas: {},
2329
+ tables: {},
2330
+ columns: {}
2331
+ }
2332
+ };
2396
2333
  }
2397
- return statements;
2398
- }
2399
- function checkIfNeedsUsingClause(fromType, toType) {
2400
- if (!fromType || !toType)
2401
- return false;
2402
- if (fromType.includes("enum") || toType.includes("enum")) {
2403
- return true;
2334
+ async getTables(schemaName) {
2335
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT
2336
+ table_schema,
2337
+ table_name
2338
+ FROM information_schema.tables
2339
+ WHERE table_schema = ${schemaName}
2340
+ AND table_type = 'BASE TABLE'
2341
+ ORDER BY table_name`);
2342
+ return getRows2(result);
2343
+ }
2344
+ async getColumns(schemaName, tableName) {
2345
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT
2346
+ a.attname AS column_name,
2347
+ CASE
2348
+ WHEN a.attnotnull THEN 'NO'
2349
+ ELSE 'YES'
2350
+ END AS is_nullable,
2351
+ CASE
2352
+ WHEN a.atttypid = ANY ('{int,int8,int2}'::regtype[])
2353
+ AND EXISTS (
2354
+ SELECT FROM pg_attrdef ad
2355
+ WHERE ad.adrelid = a.attrelid
2356
+ AND ad.adnum = a.attnum
2357
+ AND pg_get_expr(ad.adbin, ad.adrelid) = 'nextval('''
2358
+ || pg_get_serial_sequence(a.attrelid::regclass::text, a.attname)::regclass || '''::regclass)'
2359
+ )
2360
+ THEN CASE a.atttypid
2361
+ WHEN 'int'::regtype THEN 'serial'
2362
+ WHEN 'int8'::regtype THEN 'bigserial'
2363
+ WHEN 'int2'::regtype THEN 'smallserial'
2364
+ END
2365
+ ELSE format_type(a.atttypid, a.atttypmod)
2366
+ END AS data_type,
2367
+ pg_get_expr(ad.adbin, ad.adrelid) AS column_default,
2368
+ CASE
2369
+ WHEN con.contype = 'p' THEN true
2370
+ ELSE false
2371
+ END AS is_primary
2372
+ FROM pg_attribute a
2373
+ JOIN pg_class cls ON cls.oid = a.attrelid
2374
+ JOIN pg_namespace ns ON ns.oid = cls.relnamespace
2375
+ LEFT JOIN pg_attrdef ad ON ad.adrelid = a.attrelid AND ad.adnum = a.attnum
2376
+ LEFT JOIN pg_constraint con ON con.conrelid = a.attrelid
2377
+ AND a.attnum = ANY(con.conkey)
2378
+ AND con.contype = 'p'
2379
+ WHERE
2380
+ a.attnum > 0
2381
+ AND NOT a.attisdropped
2382
+ AND ns.nspname = ${schemaName}
2383
+ AND cls.relname = ${tableName}
2384
+ ORDER BY a.attnum`);
2385
+ return getRows2(result);
2386
+ }
2387
+ async getIndexes(schemaName, tableName) {
2388
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT
2389
+ i.relname AS name,
2390
+ idx.indisunique AS is_unique,
2391
+ idx.indisprimary AS is_primary,
2392
+ con.contype = 'u' AS is_unique_constraint,
2393
+ ARRAY(
2394
+ SELECT a.attname
2395
+ FROM pg_attribute a
2396
+ WHERE a.attrelid = idx.indrelid
2397
+ AND a.attnum = ANY(idx.indkey::int[])
2398
+ ORDER BY a.attnum
2399
+ ) AS columns,
2400
+ am.amname AS method
2401
+ FROM pg_index idx
2402
+ JOIN pg_class i ON i.oid = idx.indexrelid
2403
+ JOIN pg_class c ON c.oid = idx.indrelid
2404
+ JOIN pg_namespace n ON n.oid = c.relnamespace
2405
+ JOIN pg_am am ON am.oid = i.relam
2406
+ LEFT JOIN pg_constraint con ON con.conindid = idx.indexrelid
2407
+ WHERE n.nspname = ${schemaName}
2408
+ AND c.relname = ${tableName}`);
2409
+ return getRows2(result);
2404
2410
  }
2405
- const fromBase = fromType.split("(")[0].toLowerCase();
2406
- const toBase = toType.split("(")[0].toLowerCase();
2407
- if ((fromBase === "text" || fromBase === "varchar" || fromBase === "character varying") && (toBase === "jsonb" || toBase === "json")) {
2408
- return true;
2411
+ async getForeignKeys(schemaName, tableName) {
2412
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT
2413
+ con.conname AS name,
2414
+ att.attname AS column_name,
2415
+ fnsp.nspname AS foreign_table_schema,
2416
+ frel.relname AS foreign_table_name,
2417
+ fatt.attname AS foreign_column_name,
2418
+ CASE con.confupdtype
2419
+ WHEN 'a' THEN 'NO ACTION'
2420
+ WHEN 'r' THEN 'RESTRICT'
2421
+ WHEN 'n' THEN 'SET NULL'
2422
+ WHEN 'c' THEN 'CASCADE'
2423
+ WHEN 'd' THEN 'SET DEFAULT'
2424
+ END AS update_rule,
2425
+ CASE con.confdeltype
2426
+ WHEN 'a' THEN 'NO ACTION'
2427
+ WHEN 'r' THEN 'RESTRICT'
2428
+ WHEN 'n' THEN 'SET NULL'
2429
+ WHEN 'c' THEN 'CASCADE'
2430
+ WHEN 'd' THEN 'SET DEFAULT'
2431
+ END AS delete_rule
2432
+ FROM pg_catalog.pg_constraint con
2433
+ JOIN pg_catalog.pg_class rel ON rel.oid = con.conrelid
2434
+ JOIN pg_catalog.pg_namespace nsp ON nsp.oid = con.connamespace
2435
+ LEFT JOIN pg_catalog.pg_attribute att ON att.attnum = ANY (con.conkey)
2436
+ AND att.attrelid = con.conrelid
2437
+ LEFT JOIN pg_catalog.pg_class frel ON frel.oid = con.confrelid
2438
+ LEFT JOIN pg_catalog.pg_namespace fnsp ON fnsp.oid = frel.relnamespace
2439
+ LEFT JOIN pg_catalog.pg_attribute fatt ON fatt.attnum = ANY (con.confkey)
2440
+ AND fatt.attrelid = con.confrelid
2441
+ WHERE con.contype = 'f'
2442
+ AND nsp.nspname = ${schemaName}
2443
+ AND rel.relname = ${tableName}`);
2444
+ return getRows2(result);
2409
2445
  }
2410
- const needsUsingPairs = [
2411
- ["integer", "boolean"],
2412
- ["boolean", "integer"],
2413
- ["text", "integer"],
2414
- ["text", "numeric"],
2415
- ["text", "boolean"],
2416
- ["text", "uuid"],
2417
- ["text", "jsonb"],
2418
- ["text", "json"],
2419
- ["varchar", "integer"],
2420
- ["varchar", "numeric"],
2421
- ["varchar", "boolean"],
2422
- ["varchar", "uuid"],
2423
- ["varchar", "jsonb"],
2424
- ["varchar", "json"],
2425
- ["character varying", "jsonb"],
2426
- ["character varying", "json"]
2427
- ];
2428
- for (const [from, to] of needsUsingPairs) {
2429
- if (fromBase === from && toBase === to || fromBase === to && toBase === from) {
2430
- return true;
2431
- }
2446
+ async getPrimaryKeys(schemaName, tableName) {
2447
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT
2448
+ con.conname AS name,
2449
+ ARRAY(
2450
+ SELECT a.attname
2451
+ FROM pg_attribute a
2452
+ WHERE a.attrelid = con.conrelid
2453
+ AND a.attnum = ANY(con.conkey)
2454
+ ORDER BY a.attnum
2455
+ ) AS columns
2456
+ FROM pg_constraint con
2457
+ JOIN pg_class rel ON rel.oid = con.conrelid
2458
+ JOIN pg_namespace nsp ON nsp.oid = con.connamespace
2459
+ WHERE con.contype = 'p'
2460
+ AND nsp.nspname = ${schemaName}
2461
+ AND rel.relname = ${tableName}`);
2462
+ return getRows2(result);
2432
2463
  }
2433
- return false;
2434
- }
2435
- function formatDefaultValue(value, type) {
2436
- if (value === null || value === "NULL") {
2437
- return "NULL";
2464
+ async getUniqueConstraints(schemaName, tableName) {
2465
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT
2466
+ con.conname AS name,
2467
+ ARRAY(
2468
+ SELECT a.attname
2469
+ FROM pg_attribute a
2470
+ WHERE a.attrelid = con.conrelid
2471
+ AND a.attnum = ANY(con.conkey)
2472
+ ORDER BY a.attnum
2473
+ ) AS columns
2474
+ FROM pg_constraint con
2475
+ JOIN pg_class rel ON rel.oid = con.conrelid
2476
+ JOIN pg_namespace nsp ON nsp.oid = con.connamespace
2477
+ WHERE con.contype = 'u'
2478
+ AND nsp.nspname = ${schemaName}
2479
+ AND rel.relname = ${tableName}`);
2480
+ return getRows2(result);
2438
2481
  }
2439
- if (type && (type.toLowerCase().includes("boolean") || type.toLowerCase() === "bool")) {
2440
- if (value === true || value === "true" || value === "t" || value === 1) {
2441
- return "true";
2442
- }
2443
- if (value === false || value === "false" || value === "f" || value === 0) {
2444
- return "false";
2445
- }
2482
+ async getCheckConstraints(schemaName, tableName) {
2483
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT
2484
+ con.conname AS name,
2485
+ pg_get_constraintdef(con.oid) AS definition
2486
+ FROM pg_constraint con
2487
+ JOIN pg_class rel ON rel.oid = con.conrelid
2488
+ JOIN pg_namespace nsp ON nsp.oid = con.connamespace
2489
+ WHERE con.contype = 'c'
2490
+ AND nsp.nspname = ${schemaName}
2491
+ AND rel.relname = ${tableName}`);
2492
+ return getRows2(result);
2446
2493
  }
2447
- if (type?.match(/^(integer|bigint|smallint|numeric|decimal|real|double)/i)) {
2448
- return String(value);
2494
+ async getEnums(schemaName) {
2495
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT
2496
+ n.nspname AS schema,
2497
+ t.typname AS name,
2498
+ e.enumlabel AS value,
2499
+ e.enumsortorder AS sort_order
2500
+ FROM pg_type t
2501
+ JOIN pg_enum e ON t.oid = e.enumtypid
2502
+ JOIN pg_catalog.pg_namespace n ON n.oid = t.typnamespace
2503
+ WHERE n.nspname = ${schemaName}
2504
+ ORDER BY schema, name, sort_order`);
2505
+ return getRows2(result);
2449
2506
  }
2450
- if (typeof value === "string") {
2451
- if (value.includes("::")) {
2452
- return value;
2453
- }
2454
- if (value.startsWith("'") && value.endsWith("'")) {
2455
- return value;
2456
- }
2457
- if (value.match(/^\w+\(\)/i) || value.includes("(") && value.includes(")")) {
2458
- return value;
2507
+ parseDefault(defaultValue, dataType) {
2508
+ if (!defaultValue)
2509
+ return;
2510
+ const match = defaultValue.match(/^'(.*)'::/);
2511
+ if (match) {
2512
+ return `'${match[1]}'`;
2459
2513
  }
2460
- if (value.toUpperCase().startsWith("CURRENT_")) {
2461
- return value;
2514
+ if (defaultValue.includes("nextval(")) {
2515
+ return;
2462
2516
  }
2463
- return `'${value.replace(/'/g, "''")}'`;
2464
- }
2465
- return String(value);
2466
- }
2467
- function generateCreateIndexSQL(index7) {
2468
- const unique3 = index7.isUnique ? "UNIQUE " : "";
2469
- const method = index7.method || "btree";
2470
- const columns = index7.columns.map((c) => {
2471
- if (c.isExpression) {
2472
- return c.expression;
2517
+ if (dataType === "boolean") {
2518
+ if (defaultValue === "true")
2519
+ return "true";
2520
+ if (defaultValue === "false")
2521
+ return "false";
2473
2522
  }
2474
- return `"${c.expression}"${c.asc === false ? " DESC" : ""}`;
2475
- }).join(", ");
2476
- const indexName = index7.name.includes(".") ? index7.name.split(".")[1] : index7.name;
2477
- let tableRef;
2478
- const indexTable = index7.table;
2479
- if (indexTable?.includes(".")) {
2480
- const [schema, table] = indexTable.split(".");
2481
- tableRef = `"${schema}"."${table}"`;
2482
- } else {
2483
- tableRef = `"${indexTable || ""}"`;
2484
- }
2485
- return `CREATE ${unique3}INDEX "${indexName}" ON ${tableRef} USING ${method} (${columns});`;
2486
- }
2487
- function generateDropIndexSQL(index7) {
2488
- const indexNameFull = typeof index7 === "string" ? index7 : index7.name;
2489
- const indexName = indexNameFull.includes(".") ? indexNameFull.split(".")[1] : indexNameFull;
2490
- return `DROP INDEX IF EXISTS "${indexName}";`;
2491
- }
2492
- function generateCreateForeignKeySQL(fk) {
2493
- const schemaFrom = fk.schemaFrom || "public";
2494
- const schemaTo = fk.schemaTo || "public";
2495
- const tableFrom = fk.tableFrom;
2496
- const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
2497
- const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
2498
- let sql22 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
2499
- if (fk.onDelete) {
2500
- sql22 += ` ON DELETE ${fk.onDelete}`;
2523
+ return defaultValue;
2501
2524
  }
2502
- if (fk.onUpdate) {
2503
- sql22 += ` ON UPDATE ${fk.onUpdate}`;
2525
+ async hasExistingTables(pluginName) {
2526
+ const schemaName = pluginName === "@elizaos/plugin-sql" ? "public" : this.deriveSchemaName(pluginName);
2527
+ const result = await this.db.execute(import_drizzle_orm22.sql`SELECT COUNT(*) AS count
2528
+ FROM information_schema.tables
2529
+ WHERE table_schema = ${schemaName}
2530
+ AND table_type = 'BASE TABLE'`);
2531
+ const firstRow = result.rows?.[0];
2532
+ const count = parseInt(firstRow && firstRow.count || "0", 10);
2533
+ return count > 0;
2504
2534
  }
2505
- return `${sql22};`;
2506
- }
2507
- function generateDropForeignKeySQL(fk) {
2508
- const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
2509
- return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${fk.name}";`;
2510
- }
2511
- function generateCreateUniqueConstraintSQL(constraint) {
2512
- const table = constraint.table || "";
2513
- const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2514
- const name = constraint.name;
2515
- const columns = constraint.columns.map((c) => `"${c}"`).join(", ");
2516
- let sql22 = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" UNIQUE`;
2517
- if (constraint.nullsNotDistinct) {
2518
- sql22 += ` NULLS NOT DISTINCT`;
2535
+ deriveSchemaName(pluginName) {
2536
+ return pluginName.replace("@", "").replace("/", "_").replace(/-/g, "_").toLowerCase();
2519
2537
  }
2520
- sql22 += ` (${columns});`;
2521
- return sql22;
2522
- }
2523
- function generateDropUniqueConstraintSQL(constraint) {
2524
- const table = constraint.table || "";
2525
- const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2526
- return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2527
- }
2528
- function generateCreateCheckConstraintSQL(constraint) {
2529
- const table = constraint.table || "";
2530
- const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2531
- const name = constraint.name;
2532
- const value = constraint.value;
2533
- return `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${name}" CHECK (${value});`;
2534
- }
2535
- function generateDropCheckConstraintSQL(constraint) {
2536
- const table = constraint.table || "";
2537
- const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2538
- return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2539
2538
  }
2540
- var import_core5;
2541
- var init_sql_generator = __esm(() => {
2539
+ var import_core5, import_drizzle_orm22;
2540
+ var init_database_introspector = __esm(() => {
2542
2541
  import_core5 = require("@elizaos/core");
2542
+ import_drizzle_orm22 = require("drizzle-orm");
2543
2543
  });
2544
2544
 
2545
2545
  // runtime-migrator/extension-manager.ts
@@ -3222,10 +3222,14 @@ var init_runtime_migrator = __esm(() => {
3222
3222
  import_core8 = require("@elizaos/core");
3223
3223
  import_drizzle_orm27 = require("drizzle-orm");
3224
3224
  });
3225
-
3226
3225
  // runtime-migrator/index.ts
3227
3226
  var init_runtime_migrator2 = __esm(() => {
3227
+ init_snapshot_generator();
3228
+ init_sql_generator();
3228
3229
  init_runtime_migrator();
3230
+ init_journal_storage();
3231
+ init_migration_tracker();
3232
+ init_snapshot_storage();
3229
3233
  });
3230
3234
 
3231
3235
  // migration-service.ts
@@ -7118,5 +7122,5 @@ var plugin = {
7118
7122
  };
7119
7123
  var typescript_default = plugin;
7120
7124
 
7121
- //# debugId=23D0E08B0FF7FC6364756E2164756E21
7125
+ //# debugId=13D17A8DF9243CD764756E2164756E21
7122
7126
  //# sourceMappingURL=index.node.cjs.map