@mastra/mysql 0.14.1-alpha.0 → 0.14.1-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -11335,6 +11335,7 @@ var WorkflowDefinitionsMySQL = class extends WorkflowDefinitionsStorage {
11335
11335
  }
11336
11336
  };
11337
11337
  // src/storage/domains/workflows/index.ts
11338
+ const DUPLICATE_KEY_ERROR_CODES = /* @__PURE__ */ new Set([1062, "ER_DUP_ENTRY"]);
11338
11339
  function parseSnapshot(snapshot) {
11339
11340
  if (typeof snapshot === "string") try {
11340
11341
  return JSON.parse(snapshot);
@@ -11418,6 +11419,9 @@ var WorkflowsMySQL = class WorkflowsMySQL extends WorkflowsStorage {
11418
11419
  this.#skipDefaultIndexes = skipDefaultIndexes;
11419
11420
  this.#indexes = indexes?.filter((idx) => WorkflowsMySQL.MANAGED_TABLES.includes(idx.table));
11420
11421
  }
11422
+ supportsGuardedSnapshotWrites() {
11423
+ return true;
11424
+ }
11421
11425
  supportsConcurrentUpdates() {
11422
11426
  return true;
11423
11427
  }
@@ -11702,25 +11706,45 @@ var WorkflowsMySQL = class WorkflowsMySQL extends WorkflowsStorage {
11702
11706
  connection.release();
11703
11707
  }
11704
11708
  }
11705
- async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence }) {
11709
+ async persistWorkflowSnapshot({ workflowName, runId, resourceId, snapshot, createdAt, updatedAt, fence, expectedStatus }) {
11706
11710
  const now = /* @__PURE__ */ new Date();
11711
+ const expectedStatuses = expectedStatus === void 0 ? void 0 : Array.isArray(expectedStatus) ? expectedStatus : [expectedStatus];
11712
+ const guard = expectedStatuses === void 0 ? "" : expectedStatuses.length === 0 ? " AND FALSE" : ` AND JSON_UNQUOTE(JSON_EXTRACT(${quoteIdentifier("snapshot", "column name")}, '$.status')) IN (${expectedStatuses.map(() => "?").join(", ")})`;
11707
11713
  const createdAtValue = createdAt ?? now;
11708
11714
  const updatedAtValue = updatedAt ?? now;
11709
11715
  const tableName = formatTableName(TABLE_WORKFLOW_SNAPSHOT);
11710
11716
  try {
11711
- await withRunFence(this.pool, RUN_OWNERS_TABLE, resolveRunFence(this, fence, runId), "persistWorkflowSnapshot", async (q) => {
11712
- const [updateResult] = await q.execute(`UPDATE ${tableName}
11717
+ return await withRunFence(this.pool, RUN_OWNERS_TABLE, resolveRunFence(this, fence, runId), "persistWorkflowSnapshot", async (q) => {
11718
+ const update = () => q.execute(`UPDATE ${tableName}
11713
11719
  SET ${quoteIdentifier("resourceId", "column name")} = ?,
11714
11720
  ${quoteIdentifier("snapshot", "column name")} = ?,
11715
11721
  ${quoteIdentifier("updatedAt", "column name")} = ?
11716
- WHERE ${quoteIdentifier("workflow_name", "column name")} = ? AND ${quoteIdentifier("run_id", "column name")} = ?`, [
11722
+ WHERE ${quoteIdentifier("workflow_name", "column name")} = ? AND ${quoteIdentifier("run_id", "column name")} = ?${guard}`, [
11717
11723
  resourceId ?? null,
11718
11724
  JSON.stringify(snapshot),
11719
11725
  transformToSqlValue(updatedAtValue),
11720
11726
  workflowName,
11721
- runId
11727
+ runId,
11728
+ ...expectedStatuses ?? []
11722
11729
  ]);
11723
- if (updateResult.affectedRows > 0) return;
11730
+ const [updateResult] = await update();
11731
+ if (updateResult.affectedRows > 0) return true;
11732
+ if (expectedStatuses !== void 0) try {
11733
+ await q.execute(`INSERT INTO ${tableName} (${quoteIdentifier("workflow_name", "column name")}, ${quoteIdentifier("run_id", "column name")}, ${quoteIdentifier("resourceId", "column name")}, ${quoteIdentifier("snapshot", "column name")}, ${quoteIdentifier("createdAt", "column name")}, ${quoteIdentifier("updatedAt", "column name")})
11734
+ VALUES (?, ?, ?, ?, ?, ?)`, [
11735
+ workflowName,
11736
+ runId,
11737
+ resourceId ?? null,
11738
+ JSON.stringify(snapshot),
11739
+ transformToSqlValue(createdAtValue),
11740
+ transformToSqlValue(updatedAtValue)
11741
+ ]);
11742
+ return true;
11743
+ } catch (error) {
11744
+ if (!hasErrorCode(error, DUPLICATE_KEY_ERROR_CODES)) throw error;
11745
+ const [retryResult] = await update();
11746
+ return retryResult.affectedRows > 0;
11747
+ }
11724
11748
  await q.execute(`INSERT INTO ${tableName} (${quoteIdentifier("workflow_name", "column name")}, ${quoteIdentifier("run_id", "column name")}, ${quoteIdentifier("resourceId", "column name")}, ${quoteIdentifier("snapshot", "column name")}, ${quoteIdentifier("createdAt", "column name")}, ${quoteIdentifier("updatedAt", "column name")})
11725
11749
  VALUES (?, ?, ?, ?, ?, ?)
11726
11750
  ON DUPLICATE KEY UPDATE ${quoteIdentifier("resourceId", "column name")} = VALUES(${quoteIdentifier("resourceId", "column name")}), ${quoteIdentifier("snapshot", "column name")} = VALUES(${quoteIdentifier("snapshot", "column name")}), ${quoteIdentifier("updatedAt", "column name")} = VALUES(${quoteIdentifier("updatedAt", "column name")})`, [
@@ -11731,6 +11755,7 @@ var WorkflowsMySQL = class WorkflowsMySQL extends WorkflowsStorage {
11731
11755
  transformToSqlValue(createdAtValue),
11732
11756
  transformToSqlValue(updatedAtValue)
11733
11757
  ]);
11758
+ return true;
11734
11759
  });
11735
11760
  } catch (error) {
11736
11761
  if (isRunFenceConflictError(error)) throw error;
@@ -11745,6 +11770,22 @@ var WorkflowsMySQL = class WorkflowsMySQL extends WorkflowsStorage {
11745
11770
  }, error);
11746
11771
  }
11747
11772
  }
11773
+ async loadWorkflowRunStatus({ workflowName, runId }) {
11774
+ try {
11775
+ const [rows] = await this.pool.execute(`SELECT JSON_UNQUOTE(JSON_EXTRACT(${quoteIdentifier("snapshot", "column name")}, '$.status')) AS status FROM ${formatTableName(TABLE_WORKFLOW_SNAPSHOT)} WHERE ${quoteIdentifier("workflow_name", "column name")} = ? AND ${quoteIdentifier("run_id", "column name")} = ? LIMIT 1`, [workflowName, runId]);
11776
+ return rows[0]?.status ?? null;
11777
+ } catch (error) {
11778
+ throw new MastraError({
11779
+ id: createStorageErrorId("MYSQL", "LOAD_WORKFLOW_RUN_STATUS", "FAILED"),
11780
+ domain: ErrorDomain.STORAGE,
11781
+ category: ErrorCategory.THIRD_PARTY,
11782
+ details: {
11783
+ workflowName,
11784
+ runId
11785
+ }
11786
+ }, error);
11787
+ }
11788
+ }
11748
11789
  async loadWorkflowSnapshot({ workflowName, runId }) {
11749
11790
  try {
11750
11791
  const row = await this.operations.load({