@elizaos/plugin-sql 2.0.0-alpha.16 → 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.
@@ -2311,7 +2311,12 @@ function generateCreateTableSQL(fullTableName, table) {
2311
2311
  const foreignKeys = table.foreignKeys || {};
2312
2312
  for (const [fkName, fkDef] of Object.entries(foreignKeys)) {
2313
2313
  const fk = fkDef;
2314
- const fkSQL = `ALTER TABLE "${schema}"."${tableName}" ADD CONSTRAINT "${fkName}" FOREIGN KEY (${fk.columnsFrom.map((c) => `"${c}"`).join(", ")}) REFERENCES "${fk.schemaTo || "public"}"."${fk.tableTo}" (${fk.columnsTo.map((c) => `"${c}"`).join(", ")})${fk.onDelete ? ` ON DELETE ${fk.onDelete}` : ""}${fk.onUpdate ? ` ON UPDATE ${fk.onUpdate}` : ""};`;
2314
+ const fkSQL = wrapConstraintCreationGuard(fkName, buildCreateForeignKeyBodySQL({
2315
+ ...fk,
2316
+ name: fkName,
2317
+ schemaFrom: schema,
2318
+ tableFrom: tableName
2319
+ }));
2315
2320
  fkSQLs.push(fkSQL);
2316
2321
  }
2317
2322
  return { tableSQL, fkSQLs };
@@ -2490,19 +2495,7 @@ function generateDropIndexSQL(index7) {
2490
2495
  return `DROP INDEX IF EXISTS "${indexName}";`;
2491
2496
  }
2492
2497
  function generateCreateForeignKeySQL(fk) {
2493
- const schemaFrom = fk.schemaFrom || "public";
2494
- const schemaTo = fk.schemaTo || "public";
2495
- const tableFrom = fk.tableFrom;
2496
- const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
2497
- const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
2498
- let sql22 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
2499
- if (fk.onDelete) {
2500
- sql22 += ` ON DELETE ${fk.onDelete}`;
2501
- }
2502
- if (fk.onUpdate) {
2503
- sql22 += ` ON UPDATE ${fk.onUpdate}`;
2504
- }
2505
- return `${sql22};`;
2498
+ return wrapConstraintCreationGuard(fk.name, buildCreateForeignKeyBodySQL(fk));
2506
2499
  }
2507
2500
  function generateDropForeignKeySQL(fk) {
2508
2501
  const [schema, tableName] = fk.tableFrom ? fk.tableFrom.includes(".") ? fk.tableFrom.split(".") : ["public", fk.tableFrom] : ["public", ""];
@@ -2537,6 +2530,25 @@ function generateDropCheckConstraintSQL(constraint) {
2537
2530
  const [schema, tableName] = table.includes(".") ? table.split(".") : ["public", table];
2538
2531
  return `ALTER TABLE "${schema}"."${tableName}" DROP CONSTRAINT "${constraint.name}";`;
2539
2532
  }
2533
+ function buildCreateForeignKeyBodySQL(fk) {
2534
+ const schemaFrom = fk.schemaFrom || "public";
2535
+ const schemaTo = fk.schemaTo || "public";
2536
+ const tableFrom = fk.tableFrom;
2537
+ const columnsFrom = fk.columnsFrom.map((c) => `"${c}"`).join(", ");
2538
+ const columnsTo = fk.columnsTo.map((c) => `"${c}"`).join(", ");
2539
+ let sql22 = `ALTER TABLE "${schemaFrom}"."${tableFrom}" ADD CONSTRAINT "${fk.name}" FOREIGN KEY (${columnsFrom}) REFERENCES "${schemaTo}"."${fk.tableTo}" (${columnsTo})`;
2540
+ if (fk.onDelete) {
2541
+ sql22 += ` ON DELETE ${fk.onDelete}`;
2542
+ }
2543
+ if (fk.onUpdate) {
2544
+ sql22 += ` ON UPDATE ${fk.onUpdate}`;
2545
+ }
2546
+ return sql22;
2547
+ }
2548
+ function wrapConstraintCreationGuard(constraintName, statement) {
2549
+ const escapedConstraintName = constraintName.replace(/'/g, "''");
2550
+ return `DO $$ BEGIN IF NOT EXISTS (SELECT 1 FROM pg_constraint WHERE conname = '${escapedConstraintName}') THEN ${statement}; END IF; END $$;`;
2551
+ }
2540
2552
  var import_core5;
2541
2553
  var init_sql_generator = __esm(() => {
2542
2554
  import_core5 = require("@elizaos/core");
@@ -3818,7 +3830,7 @@ class BaseDrizzleAdapter extends import_core10.DatabaseAdapter {
3818
3830
  return [String(names)];
3819
3831
  }
3820
3832
  isValidUUID(value) {
3821
- 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);
3833
+ return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(value);
3822
3834
  }
3823
3835
  normalizeWorldData(world) {
3824
3836
  const worldData = {
@@ -7118,5 +7130,5 @@ var plugin = {
7118
7130
  };
7119
7131
  var typescript_default = plugin;
7120
7132
 
7121
- //# debugId=23D0E08B0FF7FC6364756E2164756E21
7133
+ //# debugId=30BB64705897BABA64756E2164756E21
7122
7134
  //# sourceMappingURL=index.node.cjs.map