@mastra/pg 1.31.1-alpha.1 → 1.31.2-alpha.0

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.31.1-alpha.1"
6
+ version: "1.31.2-alpha.0"
7
7
  ---
8
8
 
9
9
  ## When to use
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.31.1-alpha.1",
2
+ "version": "1.31.2-alpha.0",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -14563,6 +14563,7 @@ var MemoryPG = class MemoryPG extends _mastra_core_storage.MemoryStorage {
14563
14563
  };
14564
14564
  //#endregion
14565
14565
  //#region src/storage/domains/notifications/index.ts
14566
+ const COMPOSITE_PRIMARY_KEY$1 = _mastra_core_storage.TABLE_CONFIGS[_mastra_core_storage.TABLE_NOTIFICATIONS].compositePrimaryKey;
14566
14567
  const statusTimestamp = (status, now) => {
14567
14568
  if (status === "delivered") return { deliveredAt: now };
14568
14569
  if (status === "seen") return { seenAt: now };
@@ -14674,11 +14675,58 @@ var NotificationsPG = class NotificationsPG extends _mastra_core_storage.Notific
14674
14675
  async init() {
14675
14676
  await this.#db.createTable({
14676
14677
  tableName: _mastra_core_storage.TABLE_NOTIFICATIONS,
14677
- schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_NOTIFICATIONS]
14678
+ schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_NOTIFICATIONS],
14679
+ compositePrimaryKey: COMPOSITE_PRIMARY_KEY$1
14678
14680
  });
14681
+ await this.ensurePrimaryKey();
14679
14682
  await this.createDefaultIndexes();
14680
14683
  await this.createCustomIndexes();
14681
14684
  }
14685
+ async #hasPrimaryKey() {
14686
+ return !!(await this.#db.client.oneOrNone(`SELECT EXISTS (
14687
+ SELECT 1 FROM pg_index i
14688
+ JOIN pg_class c ON c.oid = i.indrelid
14689
+ JOIN pg_namespace n ON n.oid = c.relnamespace
14690
+ WHERE c.relname = $1 AND n.nspname = $2 AND i.indisprimary
14691
+ ) AS exists`, [_mastra_core_storage.TABLE_NOTIFICATIONS, this.#schema]))?.exists;
14692
+ }
14693
+ /**
14694
+ * Tables created before the composite primary key was declared have no key at
14695
+ * all, which allows duplicate ids and leaves the table without a replica
14696
+ * identity (UPDATE/DELETE fail under logical replication). `createTable` is a
14697
+ * no-op for existing tables, so add the key here. If duplicate rows already
14698
+ * exist, or the role does not own the table, the key cannot be added; warn with
14699
+ * remediation instead of blocking init.
14700
+ */
14701
+ async ensurePrimaryKey() {
14702
+ if (await this.#hasPrimaryKey()) return;
14703
+ const tableName = getTableName$5({
14704
+ indexName: _mastra_core_storage.TABLE_NOTIFICATIONS,
14705
+ schemaName: getSchemaName$5(this.#schema)
14706
+ });
14707
+ const keyColumns = COMPOSITE_PRIMARY_KEY$1.map((column) => `"${(0, _mastra_core_utils.parseSqlIdentifier)(column, "column name")}"`).join(", ");
14708
+ const remediation = `ALTER TABLE ${tableName} ADD PRIMARY KEY (${keyColumns});`;
14709
+ if (await this.#db.client.oneOrNone(`SELECT 1 FROM ${tableName} GROUP BY ${keyColumns} HAVING count(*) > 1 LIMIT 1`)) {
14710
+ this.logger?.warn?.(`${tableName} contains duplicate (${keyColumns}) rows, so its PRIMARY KEY could not be added. Remove the duplicates, then run: ${remediation}`);
14711
+ return;
14712
+ }
14713
+ try {
14714
+ await this.#db.client.none(`ALTER TABLE ${tableName} ADD PRIMARY KEY (${keyColumns})`);
14715
+ this.logger?.info?.(`Added PRIMARY KEY (${keyColumns}) to ${tableName}`);
14716
+ } catch (error) {
14717
+ if (await this.#hasPrimaryKey()) return;
14718
+ if (error?.code === "42501") {
14719
+ this.logger?.warn?.(`Insufficient privilege to add PRIMARY KEY (${keyColumns}) to ${tableName}. Ask the table owner to run: ${remediation}`);
14720
+ return;
14721
+ }
14722
+ throw new _mastra_core_error.MastraError({
14723
+ id: (0, _mastra_core_storage.createStorageErrorId)("PG", "ADD_NOTIFICATIONS_PRIMARY_KEY", "FAILED"),
14724
+ domain: _mastra_core_error.ErrorDomain.STORAGE,
14725
+ category: _mastra_core_error.ErrorCategory.THIRD_PARTY,
14726
+ details: { tableName }
14727
+ }, error);
14728
+ }
14729
+ }
14682
14730
  /**
14683
14731
  * Lazily ensures a btree index exists on each configured policy's retention
14684
14732
  * anchor column so age-based `prune()` deletes stay fast on large tables.
@@ -14762,6 +14810,7 @@ var NotificationsPG = class NotificationsPG extends _mastra_core_storage.Notific
14762
14810
  tableName: _mastra_core_storage.TABLE_NOTIFICATIONS,
14763
14811
  schema: _mastra_core_storage.TABLE_SCHEMAS[_mastra_core_storage.TABLE_NOTIFICATIONS],
14764
14812
  schemaName,
14813
+ compositePrimaryKey: COMPOSITE_PRIMARY_KEY$1,
14765
14814
  includeAllConstraints: true
14766
14815
  }));
14767
14816
  for (const idx of NotificationsPG.getDefaultIndexDefs(schemaPrefix)) statements.push(generateIndexSQL(idx, schemaName));