@mastra/pg 1.36.0-alpha.1 → 1.36.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
@@ -2696,7 +2696,8 @@ async function loadSchemaSnapshot(client, schemaName) {
2696
2696
  notNullColumns,
2697
2697
  indexes,
2698
2698
  replicaIdentityIndexes,
2699
- primaryKeyIndexes
2699
+ primaryKeyIndexes,
2700
+ createdTables: /* @__PURE__ */ new Set()
2700
2701
  };
2701
2702
  }
2702
2703
  // src/storage/db/index.ts
@@ -2856,26 +2857,30 @@ function generateTableSQL({ tableName, schema, schemaName, compositePrimaryKey,
2856
2857
  ` : ""}
2857
2858
  `;
2858
2859
  }
2860
+ function indexColumnsSQL(columns, opclass) {
2861
+ return columns.map((col) => {
2862
+ const [colName, ...modifiers] = col.includes(" DESC") || col.includes(" ASC") ? col.split(" ") : [col];
2863
+ if (!colName) throw new Error(`Invalid column specification: ${col}`);
2864
+ return [
2865
+ `"${parseSqlIdentifier(colName, "column name")}"`,
2866
+ opclass,
2867
+ ...modifiers
2868
+ ].filter(Boolean).join(" ");
2869
+ }).join(", ");
2870
+ }
2859
2871
  /**
2860
2872
  * Generates a CREATE INDEX SQL statement from index options.
2861
2873
  * Used by exportSchemas to produce index DDL without a database connection.
2862
2874
  */
2863
2875
  function generateIndexSQL(options, schemaName) {
2864
- const { name, table, columns, unique = false, where, method = "btree" } = options;
2876
+ const { name, table, columns, unique = false, where, method = "btree", opclass } = options;
2865
2877
  const fullTableName = getTableName$6({
2866
2878
  indexName: table,
2867
2879
  schemaName: getSchemaName$6(schemaName)
2868
2880
  });
2869
2881
  const uniqueStr = unique ? "UNIQUE " : "";
2870
2882
  const methodStr = method !== "btree" ? `USING ${method} ` : "";
2871
- const columnsStr = columns.map((col) => {
2872
- if (col.includes(" DESC") || col.includes(" ASC")) {
2873
- const [colName, ...modifiers] = col.split(" ");
2874
- if (!colName) throw new Error(`Invalid column specification: ${col}`);
2875
- return `"${parseSqlIdentifier(colName, "column name")}" ${modifiers.join(" ")}`;
2876
- }
2877
- return `"${parseSqlIdentifier(col, "column name")}"`;
2878
- }).join(", ");
2883
+ const columnsStr = indexColumnsSQL(columns, opclass);
2879
2884
  const whereStr = where ? ` WHERE ${where}` : "";
2880
2885
  return `CREATE ${uniqueStr}INDEX IF NOT EXISTS ${`"${parseSqlIdentifier(name, "index name")}"`} ON ${fullTableName} ${methodStr}(${columnsStr})${whereStr};`;
2881
2886
  }
@@ -3386,6 +3391,7 @@ var PgDB = class extends MastraBase {
3386
3391
  if (this.schemaName) await this.setupSchema();
3387
3392
  const snapshot = this.schemaSnapshot;
3388
3393
  if (!snapshot || !this.snapshotShowsTableConverged(snapshot, tableName)) {
3394
+ const isNewTable = snapshot ? !snapshot.tables.has(tableName) : false;
3389
3395
  const sql = generateTableSQL({
3390
3396
  tableName,
3391
3397
  schema,
@@ -3399,6 +3405,7 @@ var PgDB = class extends MastraBase {
3399
3405
  }
3400
3406
  if (snapshot) {
3401
3407
  snapshot.tables.add(tableName);
3408
+ if (isNewTable) snapshot.createdTables.add(tableName);
3402
3409
  const created = this.snapshotColumns(snapshot, tableName);
3403
3410
  for (const [name, def] of Object.entries(schema)) {
3404
3411
  const parsedName = parseSqlIdentifier(name, "column name");
@@ -3867,18 +3874,9 @@ MIGRATION REQUIRED: Duplicate spans detected in ${duplicateInfo.tableName}\n====
3867
3874
  const snapshot = this.schemaSnapshot;
3868
3875
  if (snapshot?.indexes.has(name)) return;
3869
3876
  const uniqueStr = unique ? "UNIQUE " : "";
3870
- const concurrentStr = concurrent ? "CONCURRENTLY " : "";
3877
+ const concurrentStr = concurrent && !this.isTableCreatedInThisInit(table) ? "CONCURRENTLY " : "";
3871
3878
  const methodStr = method !== "btree" ? `USING ${method} ` : "";
3872
- const columnsStr = columns.map((col) => {
3873
- if (col.includes(" DESC") || col.includes(" ASC")) {
3874
- const [colName, ...modifiers] = col.split(" ");
3875
- if (!colName) throw new Error(`Invalid column specification: ${col}`);
3876
- const quotedCol = `"${parseSqlIdentifier(colName, "column name")}" ${modifiers.join(" ")}`;
3877
- return opclass ? `${quotedCol} ${opclass}` : quotedCol;
3878
- }
3879
- const quotedCol = `"${parseSqlIdentifier(col, "column name")}"`;
3880
- return opclass ? `${quotedCol} ${opclass}` : quotedCol;
3881
- }).join(", ");
3879
+ const columnsStr = indexColumnsSQL(columns, opclass);
3882
3880
  const whereStr = where ? ` WHERE ${where}` : "";
3883
3881
  const tablespaceStr = tablespace ? ` TABLESPACE ${tablespace}` : "";
3884
3882
  let withStr = "";
@@ -3911,6 +3909,13 @@ MIGRATION REQUIRED: Duplicate spans detected in ${duplicateInfo.tableName}\n====
3911
3909
  if (snapshot?.indexes.has(indexName)) return;
3912
3910
  if (await buildIndex(this.client, this.schemaName || "public", indexName, () => this.timedIndexBuild(indexName, sql), this.logger)) snapshot?.indexes.add(indexName);
3913
3911
  }
3912
+ /**
3913
+ * Whether `tableName` was absent when this init's snapshot was read and this
3914
+ * init created it, so its indexes can be built without CONCURRENTLY.
3915
+ */
3916
+ isTableCreatedInThisInit(tableName) {
3917
+ return this.schemaSnapshot?.createdTables.has(tableName) ?? false;
3918
+ }
3914
3919
  async timedIndexBuild(indexName, sql) {
3915
3920
  const startedAt = Date.now();
3916
3921
  this.logger.info(`Creating index ${indexName}`);
@@ -25941,15 +25946,16 @@ var WorkflowsPG = class WorkflowsPG extends WorkflowsStorage {
25941
25946
  this.logger?.warn?.(`Failed to create index ${stateBaseIndexName}:`, error);
25942
25947
  }
25943
25948
  if (await this.#db.getColumnType(TABLE_WORKFLOW_SNAPSHOT, "snapshot") !== "jsonb") return;
25949
+ const concurrently = !this.#db.isTableCreatedInThisInit(TABLE_WORKFLOW_SNAPSHOT);
25944
25950
  const indexName = workflowSnapshotStatusIndexName(this.#schema);
25945
25951
  try {
25946
- await this.#db.createIndexFromStatement(indexName, workflowSnapshotStatusIndexSQL(indexName, this.#schema, true));
25952
+ await this.#db.createIndexFromStatement(indexName, workflowSnapshotStatusIndexSQL(indexName, this.#schema, concurrently));
25947
25953
  } catch (error) {
25948
25954
  this.logger?.warn?.(`Failed to create index ${indexName}:`, error);
25949
25955
  }
25950
25956
  const threadIdIndexName = workflowSnapshotThreadIdIndexName(this.#schema);
25951
25957
  try {
25952
- await this.#db.createIndexFromStatement(threadIdIndexName, workflowSnapshotThreadIdIndexSQL(threadIdIndexName, this.#schema, true));
25958
+ await this.#db.createIndexFromStatement(threadIdIndexName, workflowSnapshotThreadIdIndexSQL(threadIdIndexName, this.#schema, concurrently));
25953
25959
  } catch (error) {
25954
25960
  this.logger?.warn?.(`Failed to create index ${threadIdIndexName}:`, error);
25955
25961
  }