@mastra/pg 1.23.0-alpha.4 → 1.23.0-alpha.6

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.
@@ -3,7 +3,7 @@ name: mastra-pg
3
3
  description: Documentation for @mastra/pg. Use when working with @mastra/pg APIs, configuration, or implementation.
4
4
  metadata:
5
5
  package: "@mastra/pg"
6
- version: "1.23.0-alpha.4"
6
+ version: "1.23.0-alpha.6"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.23.0-alpha.4",
2
+ "version": "1.23.0-alpha.6",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -5577,6 +5577,17 @@ var ChannelsPG = class ChannelsPG extends _mastra_core_storage.ChannelsStorage {
5577
5577
  function jsonbArg(value) {
5578
5578
  return value === void 0 || value === null ? null : JSON.stringify(value);
5579
5579
  }
5580
+ function parseStoredJSON(value) {
5581
+ if (typeof value === "string") try {
5582
+ return JSON.parse(value);
5583
+ } catch {
5584
+ return value;
5585
+ }
5586
+ return value;
5587
+ }
5588
+ function parseOptionalJSON(value, emptyValue) {
5589
+ return value === null || value === void 0 ? emptyValue : parseStoredJSON(value);
5590
+ }
5580
5591
  var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
5581
5592
  #db;
5582
5593
  #schema;
@@ -5753,6 +5764,8 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
5753
5764
  };
5754
5765
  }
5755
5766
  transformItemRow(row) {
5767
+ const metadata = parseOptionalJSON(row.metadata, void 0);
5768
+ const emptyValue = metadata?.__purged === true ? null : void 0;
5756
5769
  return {
5757
5770
  id: row.id,
5758
5771
  datasetId: row.datasetId,
@@ -5760,20 +5773,22 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
5760
5773
  externalId: row.externalId ?? null,
5761
5774
  organizationId: row.organizationId ?? null,
5762
5775
  projectId: row.projectId ?? null,
5763
- input: (0, _mastra_core_storage.safelyParseJSON)(row.input),
5764
- groundTruth: row.groundTruth ? (0, _mastra_core_storage.safelyParseJSON)(row.groundTruth) : void 0,
5765
- expectedTrajectory: row.expectedTrajectory ? (0, _mastra_core_storage.safelyParseJSON)(row.expectedTrajectory) : void 0,
5766
- toolMocks: row.toolMocks ? (0, _mastra_core_storage.safelyParseJSON)(row.toolMocks) : void 0,
5767
- unmockedToolPolicy: row.unmockedToolPolicy ?? void 0,
5768
- scorerIds: row.scorerIds ? (0, _mastra_core_storage.safelyParseJSON)(row.scorerIds) : void 0,
5769
- requestContext: row.requestContext ? (0, _mastra_core_storage.safelyParseJSON)(row.requestContext) : void 0,
5770
- metadata: row.metadata ? (0, _mastra_core_storage.safelyParseJSON)(row.metadata) : void 0,
5771
- source: row.source ? (0, _mastra_core_storage.safelyParseJSON)(row.source) : void 0,
5776
+ input: row.input === null ? null : parseStoredJSON(row.input),
5777
+ groundTruth: parseOptionalJSON(row.groundTruth, emptyValue),
5778
+ expectedTrajectory: parseOptionalJSON(row.expectedTrajectory, emptyValue),
5779
+ toolMocks: parseOptionalJSON(row.toolMocks, emptyValue),
5780
+ unmockedToolPolicy: row.unmockedToolPolicy ?? emptyValue,
5781
+ scorerIds: parseOptionalJSON(row.scorerIds, emptyValue),
5782
+ requestContext: parseOptionalJSON(row.requestContext, emptyValue),
5783
+ metadata,
5784
+ source: parseOptionalJSON(row.source, emptyValue),
5772
5785
  createdAt: (0, _mastra_core_storage.ensureDate)(row.createdAtZ || row.createdAt),
5773
5786
  updatedAt: (0, _mastra_core_storage.ensureDate)(row.updatedAtZ || row.updatedAt)
5774
5787
  };
5775
5788
  }
5776
5789
  transformItemRowFull(row) {
5790
+ const metadata = parseOptionalJSON(row.metadata, void 0);
5791
+ const emptyValue = metadata?.__purged === true ? null : void 0;
5777
5792
  return {
5778
5793
  id: row.id,
5779
5794
  datasetId: row.datasetId,
@@ -5783,15 +5798,15 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
5783
5798
  projectId: row.projectId ?? null,
5784
5799
  validTo: row.validTo,
5785
5800
  isDeleted: Boolean(row.isDeleted),
5786
- input: (0, _mastra_core_storage.safelyParseJSON)(row.input),
5787
- groundTruth: row.groundTruth ? (0, _mastra_core_storage.safelyParseJSON)(row.groundTruth) : void 0,
5788
- expectedTrajectory: row.expectedTrajectory ? (0, _mastra_core_storage.safelyParseJSON)(row.expectedTrajectory) : void 0,
5789
- toolMocks: row.toolMocks ? (0, _mastra_core_storage.safelyParseJSON)(row.toolMocks) : void 0,
5790
- unmockedToolPolicy: row.unmockedToolPolicy ?? void 0,
5791
- scorerIds: row.scorerIds ? (0, _mastra_core_storage.safelyParseJSON)(row.scorerIds) : void 0,
5792
- requestContext: row.requestContext ? (0, _mastra_core_storage.safelyParseJSON)(row.requestContext) : void 0,
5793
- metadata: row.metadata ? (0, _mastra_core_storage.safelyParseJSON)(row.metadata) : void 0,
5794
- source: row.source ? (0, _mastra_core_storage.safelyParseJSON)(row.source) : void 0,
5801
+ input: row.input === null ? null : parseStoredJSON(row.input),
5802
+ groundTruth: parseOptionalJSON(row.groundTruth, emptyValue),
5803
+ expectedTrajectory: parseOptionalJSON(row.expectedTrajectory, emptyValue),
5804
+ toolMocks: parseOptionalJSON(row.toolMocks, emptyValue),
5805
+ unmockedToolPolicy: row.unmockedToolPolicy ?? emptyValue,
5806
+ scorerIds: parseOptionalJSON(row.scorerIds, emptyValue),
5807
+ requestContext: parseOptionalJSON(row.requestContext, emptyValue),
5808
+ metadata,
5809
+ source: parseOptionalJSON(row.source, emptyValue),
5795
5810
  createdAt: (0, _mastra_core_storage.ensureDate)(row.createdAtZ || row.createdAt),
5796
5811
  updatedAt: (0, _mastra_core_storage.ensureDate)(row.updatedAtZ || row.updatedAt)
5797
5812
  };
@@ -6367,6 +6382,52 @@ var DatasetsPG = class DatasetsPG extends _mastra_core_storage.DatasetsStorage {
6367
6382
  }, error);
6368
6383
  }
6369
6384
  }
6385
+ async _doPurgeItem({ id, datasetId }) {
6386
+ try {
6387
+ const datasetsTable = getTableName$5({
6388
+ indexName: _mastra_core_storage.TABLE_DATASETS,
6389
+ schemaName: getSchemaName$5(this.#schema)
6390
+ });
6391
+ const itemsTable = getTableName$5({
6392
+ indexName: _mastra_core_storage.TABLE_DATASET_ITEMS,
6393
+ schemaName: getSchemaName$5(this.#schema)
6394
+ });
6395
+ const experimentsTable = getTableName$5({
6396
+ indexName: _mastra_core_storage.TABLE_EXPERIMENTS,
6397
+ schemaName: getSchemaName$5(this.#schema)
6398
+ });
6399
+ const experimentResultsTable = getTableName$5({
6400
+ indexName: _mastra_core_storage.TABLE_EXPERIMENT_RESULTS,
6401
+ schemaName: getSchemaName$5(this.#schema)
6402
+ });
6403
+ const purgedAt = (/* @__PURE__ */ new Date()).toISOString();
6404
+ const purgedMetadata = JSON.stringify({
6405
+ __purged: true,
6406
+ purgedAt
6407
+ });
6408
+ await this.#db.client.tx(async (t) => {
6409
+ if (!await t.oneOrNone(`SELECT "id" FROM ${datasetsTable} WHERE "id" = $1 FOR UPDATE`, [datasetId])) return;
6410
+ if (!await t.oneOrNone(`SELECT "id" FROM ${itemsTable} WHERE "id" = $1 AND "datasetId" = $2 LIMIT 1 FOR UPDATE`, [id, datasetId])) return;
6411
+ await t.none(`UPDATE ${itemsTable} SET "input" = 'null'::jsonb, "groundTruth" = NULL, "expectedTrajectory" = NULL, "toolMocks" = NULL, "unmockedToolPolicy" = NULL, "scorerIds" = NULL, "requestContext" = NULL, "metadata" = $2::jsonb, "source" = NULL WHERE "id" = $1 AND "datasetId" = $3`, [
6412
+ id,
6413
+ purgedMetadata,
6414
+ datasetId
6415
+ ]);
6416
+ if ((await t.one(`SELECT to_regclass($1) IS NOT NULL AND to_regclass($2) IS NOT NULL AS exists`, [experimentResultsTable, experimentsTable])).exists) await t.none(`UPDATE ${experimentResultsTable} SET "input" = 'null'::jsonb, "output" = NULL, "groundTruth" = NULL, "error" = NULL, "toolMockReport" = NULL, "tags" = NULL, "comment" = NULL, "metadata" = $2::jsonb WHERE "itemId" = $1 AND "experimentId" IN (SELECT "id" FROM ${experimentsTable} WHERE "datasetId" = $3)`, [
6417
+ id,
6418
+ purgedMetadata,
6419
+ datasetId
6420
+ ]);
6421
+ });
6422
+ } catch (error) {
6423
+ if (error instanceof _mastra_core_error.MastraError) throw error;
6424
+ throw new _mastra_core_error.MastraError({
6425
+ id: (0, _mastra_core_storage.createStorageErrorId)("PG", "PURGE_ITEM", "FAILED"),
6426
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
6427
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY
6428
+ }, error);
6429
+ }
6430
+ }
6370
6431
  async _doBatchInsertItems(input) {
6371
6432
  try {
6372
6433
  if (input.items.length === 0) return [];
@@ -7018,7 +7079,7 @@ var ExperimentsPG = class ExperimentsPG extends _mastra_core_storage.Experiments
7018
7079
  itemDatasetVersion: row.itemDatasetVersion != null ? row.itemDatasetVersion : null,
7019
7080
  organizationId: row.organizationId ?? null,
7020
7081
  projectId: row.projectId ?? null,
7021
- input: (0, _mastra_core_storage.safelyParseJSON)(row.input),
7082
+ input: row.input === null ? null : (0, _mastra_core_storage.safelyParseJSON)(row.input),
7022
7083
  output: row.output ? (0, _mastra_core_storage.safelyParseJSON)(row.output) : null,
7023
7084
  groundTruth: row.groundTruth ? (0, _mastra_core_storage.safelyParseJSON)(row.groundTruth) : null,
7024
7085
  metadata: row.metadata ? (0, _mastra_core_storage.safelyParseJSON)(row.metadata) : null,
@@ -7320,58 +7381,65 @@ var ExperimentsPG = class ExperimentsPG extends _mastra_core_storage.Experiments
7320
7381
  }, error);
7321
7382
  }
7322
7383
  }
7384
+ async #resolvePurgeMetadata(t, datasetId, itemId) {
7385
+ if (!datasetId) return null;
7386
+ const datasetsTable = getTableName$5({
7387
+ indexName: _mastra_core_storage.TABLE_DATASETS,
7388
+ schemaName: getSchemaName$5(this.#schema)
7389
+ });
7390
+ const itemsTable = getTableName$5({
7391
+ indexName: _mastra_core_storage.TABLE_DATASET_ITEMS,
7392
+ schemaName: getSchemaName$5(this.#schema)
7393
+ });
7394
+ if (!await t.oneOrNone(`SELECT "id" FROM ${datasetsTable} WHERE "id" = $1 FOR UPDATE`, [datasetId])) return null;
7395
+ return (await t.oneOrNone(`SELECT "metadata" FROM ${itemsTable}
7396
+ WHERE "id" = $1 AND "datasetId" = $2 AND "metadata"->>'__purged' = 'true'
7397
+ LIMIT 1`, [itemId, datasetId]))?.metadata ?? null;
7398
+ }
7323
7399
  async addExperimentResult(input) {
7324
7400
  try {
7325
7401
  const id = input.id ?? crypto.randomUUID();
7326
- const now = /* @__PURE__ */ new Date();
7327
- const nowIso = now.toISOString();
7328
- await this.#db.insert({
7329
- tableName: _mastra_core_storage.TABLE_EXPERIMENT_RESULTS,
7330
- record: {
7402
+ const resultsTable = getTableName$5({
7403
+ indexName: _mastra_core_storage.TABLE_EXPERIMENT_RESULTS,
7404
+ schemaName: getSchemaName$5(this.#schema)
7405
+ });
7406
+ const experimentsTable = getTableName$5({
7407
+ indexName: _mastra_core_storage.TABLE_EXPERIMENTS,
7408
+ schemaName: getSchemaName$5(this.#schema)
7409
+ });
7410
+ const row = await this.#db.client.tx(async (t) => {
7411
+ const owner = await t.oneOrNone(`SELECT "datasetId" FROM ${experimentsTable} WHERE "id" = $1`, [input.experimentId]);
7412
+ const purgeMetadata = await this.#resolvePurgeMetadata(t, owner?.datasetId, input.itemId);
7413
+ const nowIso = (/* @__PURE__ */ new Date()).toISOString();
7414
+ return t.one(`INSERT INTO ${resultsTable} (
7415
+ "id", "experimentId", "itemId", "itemDatasetVersion", "organizationId", "projectId",
7416
+ "input", "output", "groundTruth", "metadata", "error", "startedAt", "completedAt",
7417
+ "retryCount", "attempt", "traceId", "status", "tags", "toolMockReport", "createdAt"
7418
+ ) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14, $15, $16, $17, $18, $19, $20)
7419
+ RETURNING *`, [
7331
7420
  id,
7332
- experimentId: input.experimentId,
7333
- itemId: input.itemId,
7334
- itemDatasetVersion: input.itemDatasetVersion ?? null,
7335
- organizationId: input.organizationId ?? null,
7336
- projectId: input.projectId ?? null,
7337
- input: input.input,
7338
- output: input.output ?? null,
7339
- groundTruth: input.groundTruth ?? null,
7340
- metadata: input.metadata ?? null,
7341
- error: input.error ?? null,
7342
- startedAt: input.startedAt.toISOString(),
7343
- completedAt: input.completedAt.toISOString(),
7344
- retryCount: input.retryCount,
7345
- attempt: input.attempt ?? 0,
7346
- traceId: input.traceId ?? null,
7347
- status: input.status ?? null,
7348
- tags: input.tags ?? null,
7349
- toolMockReport: input.toolMockReport ?? null,
7350
- createdAt: nowIso
7351
- }
7421
+ input.experimentId,
7422
+ input.itemId,
7423
+ input.itemDatasetVersion ?? null,
7424
+ input.organizationId ?? null,
7425
+ input.projectId ?? null,
7426
+ JSON.stringify(purgeMetadata ? null : input.input),
7427
+ purgeMetadata || input.output == null ? null : JSON.stringify(input.output),
7428
+ purgeMetadata || input.groundTruth == null ? null : JSON.stringify(input.groundTruth),
7429
+ JSON.stringify(purgeMetadata ?? input.metadata ?? null),
7430
+ purgeMetadata || input.error == null ? null : JSON.stringify(input.error),
7431
+ input.startedAt.toISOString(),
7432
+ input.completedAt.toISOString(),
7433
+ input.retryCount,
7434
+ input.attempt ?? 0,
7435
+ input.traceId ?? null,
7436
+ input.status ?? null,
7437
+ purgeMetadata || input.tags == null ? null : JSON.stringify(input.tags),
7438
+ purgeMetadata || input.toolMockReport == null ? null : JSON.stringify(input.toolMockReport),
7439
+ nowIso
7440
+ ]);
7352
7441
  });
7353
- return {
7354
- id,
7355
- experimentId: input.experimentId,
7356
- itemId: input.itemId,
7357
- itemDatasetVersion: input.itemDatasetVersion ?? null,
7358
- organizationId: input.organizationId ?? null,
7359
- projectId: input.projectId ?? null,
7360
- input: input.input,
7361
- output: input.output ?? null,
7362
- groundTruth: input.groundTruth ?? null,
7363
- metadata: input.metadata ?? null,
7364
- error: input.error ?? null,
7365
- startedAt: input.startedAt,
7366
- completedAt: input.completedAt,
7367
- retryCount: input.retryCount,
7368
- attempt: input.attempt ?? 0,
7369
- traceId: input.traceId ?? null,
7370
- status: input.status ?? null,
7371
- tags: input.tags ?? null,
7372
- toolMockReport: input.toolMockReport ?? null,
7373
- createdAt: now
7374
- };
7442
+ return this.transformExperimentResultRow(row);
7375
7443
  } catch (error) {
7376
7444
  throw new _mastra_core_error.MastraError({
7377
7445
  id: (0, _mastra_core_storage.createStorageErrorId)("PG", "ADD_EXPERIMENT_RESULT", "FAILED"),
@@ -7386,8 +7454,14 @@ var ExperimentsPG = class ExperimentsPG extends _mastra_core_storage.Experiments
7386
7454
  indexName: _mastra_core_storage.TABLE_EXPERIMENT_RESULTS,
7387
7455
  schemaName: getSchemaName$5(this.#schema)
7388
7456
  });
7457
+ const experimentsTable = getTableName$5({
7458
+ indexName: _mastra_core_storage.TABLE_EXPERIMENTS,
7459
+ schemaName: getSchemaName$5(this.#schema)
7460
+ });
7389
7461
  const attempt = input.attempt ?? 0;
7390
7462
  const row = await this.#db.client.tx(async (t) => {
7463
+ const owner = await t.oneOrNone(`SELECT "datasetId" FROM ${experimentsTable} WHERE "id" = $1`, [input.experimentId]);
7464
+ const purgeMetadata = await this.#resolvePurgeMetadata(t, owner?.datasetId, input.itemId);
7391
7465
  const existing = await t.oneOrNone(`SELECT "id" FROM ${tableName} WHERE "experimentId" = $1 AND "itemId" = $2 AND COALESCE("attempt", 0) = $3 FOR UPDATE`, [
7392
7466
  input.experimentId,
7393
7467
  input.itemId,
@@ -7407,19 +7481,19 @@ var ExperimentsPG = class ExperimentsPG extends _mastra_core_storage.Experiments
7407
7481
  input.itemDatasetVersion ?? null,
7408
7482
  input.organizationId ?? null,
7409
7483
  input.projectId ?? null,
7410
- JSON.stringify(input.input),
7411
- input.output != null ? JSON.stringify(input.output) : null,
7412
- input.groundTruth != null ? JSON.stringify(input.groundTruth) : null,
7413
- input.metadata != null ? JSON.stringify(input.metadata) : null,
7414
- input.error != null ? JSON.stringify(input.error) : null,
7484
+ JSON.stringify(purgeMetadata ? null : input.input),
7485
+ purgeMetadata || input.output == null ? null : JSON.stringify(input.output),
7486
+ purgeMetadata || input.groundTruth == null ? null : JSON.stringify(input.groundTruth),
7487
+ JSON.stringify(purgeMetadata ?? input.metadata ?? null),
7488
+ purgeMetadata || input.error == null ? null : JSON.stringify(input.error),
7415
7489
  input.startedAt.toISOString(),
7416
7490
  input.completedAt.toISOString(),
7417
7491
  input.retryCount,
7418
7492
  attempt,
7419
7493
  input.traceId ?? null,
7420
7494
  input.status ?? null,
7421
- input.tags != null ? JSON.stringify(input.tags) : null,
7422
- input.toolMockReport != null ? JSON.stringify(input.toolMockReport) : null,
7495
+ purgeMetadata || input.tags == null ? null : JSON.stringify(input.tags),
7496
+ purgeMetadata || input.toolMockReport == null ? null : JSON.stringify(input.toolMockReport),
7423
7497
  (/* @__PURE__ */ new Date()).toISOString()
7424
7498
  ]);
7425
7499
  }
@@ -7433,19 +7507,19 @@ var ExperimentsPG = class ExperimentsPG extends _mastra_core_storage.Experiments
7433
7507
  input.itemDatasetVersion ?? null,
7434
7508
  input.organizationId ?? null,
7435
7509
  input.projectId ?? null,
7436
- JSON.stringify(input.input),
7437
- input.output != null ? JSON.stringify(input.output) : null,
7438
- input.groundTruth != null ? JSON.stringify(input.groundTruth) : null,
7439
- input.metadata != null ? JSON.stringify(input.metadata) : null,
7440
- input.error != null ? JSON.stringify(input.error) : null,
7510
+ JSON.stringify(purgeMetadata ? null : input.input),
7511
+ purgeMetadata || input.output == null ? null : JSON.stringify(input.output),
7512
+ purgeMetadata || input.groundTruth == null ? null : JSON.stringify(input.groundTruth),
7513
+ JSON.stringify(purgeMetadata ?? input.metadata ?? null),
7514
+ purgeMetadata || input.error == null ? null : JSON.stringify(input.error),
7441
7515
  input.startedAt.toISOString(),
7442
7516
  input.completedAt.toISOString(),
7443
7517
  input.retryCount,
7444
7518
  attempt,
7445
7519
  input.traceId ?? null,
7446
7520
  input.status ?? null,
7447
- input.tags != null ? JSON.stringify(input.tags) : null,
7448
- input.toolMockReport != null ? JSON.stringify(input.toolMockReport) : null
7521
+ purgeMetadata || input.tags == null ? null : JSON.stringify(input.tags),
7522
+ purgeMetadata || input.toolMockReport == null ? null : JSON.stringify(input.toolMockReport)
7449
7523
  ]);
7450
7524
  });
7451
7525
  return this.transformExperimentResultRow(row);
@@ -7463,39 +7537,34 @@ var ExperimentsPG = class ExperimentsPG extends _mastra_core_storage.Experiments
7463
7537
  indexName: _mastra_core_storage.TABLE_EXPERIMENT_RESULTS,
7464
7538
  schemaName: getSchemaName$5(this.#schema)
7465
7539
  });
7466
- const setClauses = [];
7467
- const values = [];
7468
- let paramIndex = 1;
7469
- if (input.status !== void 0) {
7470
- setClauses.push(`"status" = $${paramIndex++}`);
7471
- values.push(input.status);
7472
- }
7473
- if (input.tags !== void 0) {
7474
- setClauses.push(`"tags" = $${paramIndex++}`);
7475
- values.push(JSON.stringify(input.tags));
7476
- }
7477
- if (input.comment !== void 0) {
7478
- setClauses.push(`"comment" = $${paramIndex++}`);
7479
- values.push(input.comment);
7480
- }
7481
- if (setClauses.length === 0) {
7482
- const existing = await this.#getExperimentResultById(this.#db.client, { id: input.id });
7483
- if (!existing) throw new _mastra_core_error.MastraError({
7484
- id: (0, _mastra_core_storage.createStorageErrorId)("PG", "UPDATE_EXPERIMENT_RESULT", "NOT_FOUND"),
7485
- domain: _mastra_core_error.ErrorDomain.STORAGE,
7486
- category: _mastra_core_error.ErrorCategory.USER,
7487
- details: { resultId: input.id }
7488
- });
7489
- return existing;
7490
- }
7491
- values.push(input.id);
7492
- let whereClause = `"id" = $${paramIndex}`;
7493
- if (input.experimentId) {
7494
- paramIndex++;
7495
- values.push(input.experimentId);
7496
- whereClause += ` AND "experimentId" = $${paramIndex}`;
7497
- }
7498
- const row = await this.#db.client.oneOrNone(`UPDATE ${tableName} SET ${setClauses.join(", ")} WHERE ${whereClause} RETURNING *`, values);
7540
+ const experimentsTable = getTableName$5({
7541
+ indexName: _mastra_core_storage.TABLE_EXPERIMENTS,
7542
+ schemaName: getSchemaName$5(this.#schema)
7543
+ });
7544
+ const row = await this.#db.client.tx(async (t) => {
7545
+ const owner = await t.oneOrNone(`SELECT e."datasetId", r."itemId"
7546
+ FROM ${tableName} r
7547
+ JOIN ${experimentsTable} e ON e."id" = r."experimentId"
7548
+ WHERE r."id" = $1${input.experimentId !== void 0 ? " AND r.\"experimentId\" = $2" : ""}`, input.experimentId !== void 0 ? [input.id, input.experimentId] : [input.id]);
7549
+ if (!owner) return null;
7550
+ const purgeMetadata = await this.#resolvePurgeMetadata(t, owner.datasetId, owner.itemId);
7551
+ return t.oneOrNone(`UPDATE ${tableName}
7552
+ SET "status" = CASE WHEN $2 THEN $3 ELSE "status" END,
7553
+ "tags" = CASE WHEN $4 THEN NULL WHEN $5 THEN $6::jsonb ELSE "tags" END,
7554
+ "comment" = CASE WHEN $4 THEN NULL WHEN $7 THEN $8 ELSE "comment" END
7555
+ WHERE "id" = $1${input.experimentId !== void 0 ? " AND \"experimentId\" = $9" : ""}
7556
+ RETURNING *`, [
7557
+ input.id,
7558
+ input.status !== void 0,
7559
+ input.status ?? null,
7560
+ Boolean(purgeMetadata),
7561
+ input.tags !== void 0,
7562
+ input.tags === void 0 ? null : JSON.stringify(input.tags),
7563
+ input.comment !== void 0,
7564
+ input.comment ?? null,
7565
+ ...input.experimentId !== void 0 ? [input.experimentId] : []
7566
+ ]);
7567
+ });
7499
7568
  if (!row) throw new _mastra_core_error.MastraError({
7500
7569
  id: (0, _mastra_core_storage.createStorageErrorId)("PG", "UPDATE_EXPERIMENT_RESULT", "NOT_FOUND"),
7501
7570
  domain: _mastra_core_error.ErrorDomain.STORAGE,
@@ -10895,12 +10964,13 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
10895
10964
  /**
10896
10965
  * Reads one page of messages together with the total row count.
10897
10966
  *
10898
- * `COUNT(*) OVER ()` reports the count over the whole WHERE result on the same
10899
- * statement as the page, so the page costs one database round-trip instead of
10900
- * two. The page and the count also come from one snapshot, so the count always
10901
- * describes the returned rows. A separate `COUNT(*)` runs only when the page is
10902
- * empty and the caller asked for a page after the last row, because a window
10903
- * function has no row to carry the count on.
10967
+ * Every page query carries a skinny scalar `(SELECT COUNT(*) ...)` subquery that
10968
+ * reports the total over the whole WHERE result on the same statement as the page,
10969
+ * so the page costs one database round-trip instead of two. The page and the count
10970
+ * also come from one snapshot, so the count always describes the returned rows. A
10971
+ * separate `COUNT(*)` runs only as a fallback when the page is empty and the caller
10972
+ * asked for a page after the last row, because there is then no row to carry the
10973
+ * count on.
10904
10974
  */
10905
10975
  async #fetchMessagePage({ selectStatement, tableName, whereClause, orderByStatement, queryParams, perPageInput, perPage, offset }) {
10906
10976
  const limitClause = perPageInput === false ? "" : ` LIMIT $${queryParams.length + 1} OFFSET $${queryParams.length + 2}`;
@@ -10909,7 +10979,7 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
10909
10979
  perPage,
10910
10980
  offset
10911
10981
  ];
10912
- const rows = await this.#db.readClient.manyOrNone(`${selectStatement}, COUNT(*) OVER () AS "__total" FROM ${tableName} ${whereClause} ${orderByStatement}${limitClause}`, dataParams) || [];
10982
+ const rows = await this.#db.readClient.manyOrNone(`${selectStatement}, (SELECT COUNT(*) FROM ${tableName} ${whereClause}) AS "__total" FROM ${tableName} ${whereClause} ${orderByStatement}${limitClause}`, dataParams) || [];
10913
10983
  if (rows.length > 0) return {
10914
10984
  total: Number(rows[0].__total),
10915
10985
  messages: rows
@@ -10948,7 +11018,7 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
10948
11018
  const metadataFilter = (0, _mastra_core_storage.validateStorageMetadataFilter)(filter?.metadata);
10949
11019
  try {
10950
11020
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
10951
- const orderByStatement = `ORDER BY COALESCE("${field}Z", "${field}") ${direction}`;
11021
+ const orderByStatement = `ORDER BY "${field}" ${direction}`;
10952
11022
  const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
10953
11023
  const tableName = getTableName$3({
10954
11024
  indexName: _mastra_core_storage.TABLE_MESSAGES,
@@ -11096,7 +11166,7 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
11096
11166
  const metadataFilter = (0, _mastra_core_storage.validateStorageMetadataFilter)(filter?.metadata);
11097
11167
  try {
11098
11168
  const { field, direction } = this.parseOrderBy(orderBy, "ASC");
11099
- const orderByStatement = `ORDER BY COALESCE("${field}Z", "${field}") ${direction}`;
11169
+ const orderByStatement = `ORDER BY "${field}" ${direction}`;
11100
11170
  const selectStatement = `SELECT id, content, role, type, "createdAt", "createdAtZ", thread_id AS "threadId", "resourceId"`;
11101
11171
  const tableName = getTableName$3({
11102
11172
  indexName: _mastra_core_storage.TABLE_MESSAGES,
@@ -23057,6 +23127,11 @@ var PgFactoryStorage = class extends _mastra_core_storage.FactoryStorage {
23057
23127
  if (!spec.nullable || spec.type === "uuid-pk" || spec.primaryKey) continue;
23058
23128
  await this.#pool.query(`ALTER TABLE "${schema.name}" ALTER COLUMN "${name}" DROP NOT NULL`);
23059
23129
  }
23130
+ for (const [name, spec] of Object.entries(schema.columns)) {
23131
+ if (spec.type !== "bigint") continue;
23132
+ const { rows } = await this.#pool.query(`SELECT data_type FROM information_schema.columns WHERE table_schema = current_schema() AND table_name = $1 AND column_name = $2`, [schema.name, name]);
23133
+ if (rows[0]?.data_type === "integer") await this.#pool.query(`ALTER TABLE "${schema.name}" ALTER COLUMN "${name}" TYPE BIGINT`);
23134
+ }
23060
23135
  for (const index of schema.uniqueIndexes ?? []) {
23061
23136
  assertIdentifier("index", index.name);
23062
23137
  index.columns.forEach((column) => assertIdentifier("column", column));