@mastra/pg 1.31.0-alpha.1 → 1.31.0-alpha.2

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
@@ -2475,7 +2475,8 @@ async function loadSchemaSnapshot(client, schemaName) {
2475
2475
  const schema = schemaName || "public";
2476
2476
  const [tableRows, columnRows, indexRows] = await Promise.all([
2477
2477
  client.manyOrNone(`SELECT tablename FROM pg_catalog.pg_tables WHERE schemaname = $1`, [schema]),
2478
- client.manyOrNone(`SELECT c.relname AS table_name, a.attname AS column_name, format_type(a.atttypid, a.atttypmod) AS data_type
2478
+ client.manyOrNone(`SELECT c.relname AS table_name, a.attname AS column_name, format_type(a.atttypid, a.atttypmod) AS data_type,
2479
+ a.attnotnull AS not_null
2479
2480
  FROM pg_catalog.pg_class c
2480
2481
  JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
2481
2482
  JOIN pg_catalog.pg_attribute a ON a.attrelid = c.oid
@@ -2491,6 +2492,7 @@ async function loadSchemaSnapshot(client, schemaName) {
2491
2492
  ]);
2492
2493
  const columns = /* @__PURE__ */ new Map();
2493
2494
  const columnTypes = /* @__PURE__ */ new Map();
2495
+ const notNullColumns = /* @__PURE__ */ new Map();
2494
2496
  for (const row of columnRows) {
2495
2497
  let set = columns.get(row.table_name);
2496
2498
  if (!set) {
@@ -2504,6 +2506,14 @@ async function loadSchemaSnapshot(client, schemaName) {
2504
2506
  columnTypes.set(row.table_name, types);
2505
2507
  }
2506
2508
  types.set(row.column_name, row.data_type);
2509
+ if (row.not_null) {
2510
+ let notNull = notNullColumns.get(row.table_name);
2511
+ if (!notNull) {
2512
+ notNull = /* @__PURE__ */ new Set();
2513
+ notNullColumns.set(row.table_name, notNull);
2514
+ }
2515
+ notNull.add(row.column_name);
2516
+ }
2507
2517
  }
2508
2518
  const indexes = /* @__PURE__ */ new Set();
2509
2519
  const replicaIdentityIndexes = /* @__PURE__ */ new Set();
@@ -2520,6 +2530,7 @@ async function loadSchemaSnapshot(client, schemaName) {
2520
2530
  tables: new Set(tableRows.map((r) => r.tablename)),
2521
2531
  columns,
2522
2532
  columnTypes,
2533
+ notNullColumns,
2523
2534
  indexes,
2524
2535
  replicaIdentityIndexes,
2525
2536
  primaryKeyIndexes,
@@ -3598,6 +3609,33 @@ MIGRATION REQUIRED: Duplicate spans detected in ${duplicateInfo.tableName}\n====
3598
3609
  this.columnTypeCache.delete(tableName);
3599
3610
  }
3600
3611
  }
3612
+ /**
3613
+ * Drops NOT NULL from `columns` on `tableName` where it is still set. Answered
3614
+ * from the init snapshot when one is installed, so a converged schema issues
3615
+ * no query and never takes the ACCESS EXCLUSIVE lock.
3616
+ */
3617
+ async dropNotNull({ tableName, columns }) {
3618
+ const parsedColumns = columns.map((c) => parseSqlIdentifier(c, "column name"));
3619
+ const snapshot = this.schemaSnapshot;
3620
+ let toAlter;
3621
+ if (snapshot) {
3622
+ const notNull = snapshot.notNullColumns.get(tableName);
3623
+ toAlter = parsedColumns.filter((c) => notNull?.has(c));
3624
+ } else toAlter = (await this.client.any(`SELECT column_name FROM information_schema.columns
3625
+ WHERE table_schema = $1 AND table_name = $2 AND column_name = ANY($3) AND is_nullable = 'NO'`, [
3626
+ this.schemaName || "public",
3627
+ tableName,
3628
+ parsedColumns
3629
+ ])).map((r) => r.column_name);
3630
+ if (toAlter.length === 0) return;
3631
+ const fullTableName = getTableName$6({
3632
+ indexName: tableName,
3633
+ schemaName: getSchemaName$6(this.schemaName)
3634
+ });
3635
+ await this.client.none(`ALTER TABLE ${fullTableName} ${toAlter.map((c) => `ALTER COLUMN "${c}" DROP NOT NULL`).join(", ")}`);
3636
+ const notNull = snapshot?.notNullColumns.get(tableName);
3637
+ for (const c of toAlter) notNull?.delete(c);
3638
+ }
3601
3639
  async load({ tableName, keys }) {
3602
3640
  try {
3603
3641
  const keyEntries = Object.entries(keys).map(([key, value]) => [parseSqlIdentifier(key, "column name"), value]);
@@ -7088,6 +7126,10 @@ var ExperimentsPG = class ExperimentsPG extends ExperimentsStorage {
7088
7126
  "scorerIds"
7089
7127
  ]
7090
7128
  });
7129
+ await this.#db.dropNotNull({
7130
+ tableName: TABLE_EXPERIMENTS,
7131
+ columns: ["targetType", "targetId"]
7132
+ });
7091
7133
  await this.#db.alterTable({
7092
7134
  tableName: TABLE_EXPERIMENT_RESULTS,
7093
7135
  schema: EXPERIMENT_RESULTS_SCHEMA,
@@ -23491,7 +23533,7 @@ var WorkflowsPG = class WorkflowsPG extends WorkflowsStorage {
23491
23533
  async dangerouslyClearAll() {
23492
23534
  await this.#db.clearTable({ tableName: TABLE_WORKFLOW_SNAPSHOT });
23493
23535
  }
23494
- async updateWorkflowResults({ workflowName, runId, stepId, result, requestContext }) {
23536
+ async updateWorkflowResults({ workflowName, runId, stepId, result, requestContext, state }) {
23495
23537
  try {
23496
23538
  return await this.#db.client.tx(async (t) => {
23497
23539
  const tableName = getTableName({
@@ -23522,7 +23564,8 @@ var WorkflowsPG = class WorkflowsPG extends WorkflowsStorage {
23522
23564
  snapshot,
23523
23565
  stepId,
23524
23566
  result,
23525
- requestContext
23567
+ requestContext,
23568
+ state
23526
23569
  });
23527
23570
  const now = /* @__PURE__ */ new Date();
23528
23571
  const sanitizedSnapshot = toPgJson(snapshot);