@mastra/pg 1.1.0-alpha.1 → 1.1.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/CHANGELOG.md CHANGED
@@ -1,5 +1,16 @@
1
1
  # @mastra/pg
2
2
 
3
+ ## 1.1.0-alpha.2
4
+
5
+ ### Patch Changes
6
+
7
+ - Stored agent edits no longer fail silently. PATCH requests now save changes correctly. ([#12504](https://github.com/mastra-ai/mastra/pull/12504))
8
+
9
+ - Fix PATCH request JSON-body handling in `@mastra/client-js` so stored agent edit flows work correctly. Fix stored agent schema migration in `@mastra/libsql` and `@mastra/pg` to drop and recreate the versions table when the old snapshot-based schema is detected, clean up stale draft records from partial create failures, and remove lingering legacy tables. Restores create and edit flows for stored agents. ([#12504](https://github.com/mastra-ai/mastra/pull/12504))
10
+
11
+ - Updated dependencies:
12
+ - @mastra/core@1.1.0-alpha.2
13
+
3
14
  ## 1.1.0-alpha.1
4
15
 
5
16
  ### Minor Changes
@@ -33,4 +33,4 @@ docs/
33
33
  ## Version
34
34
 
35
35
  Package: @mastra/pg
36
- Version: 1.1.0-alpha.1
36
+ Version: 1.1.0-alpha.2
@@ -5,7 +5,7 @@ description: Documentation for @mastra/pg. Includes links to type definitions an
5
5
 
6
6
  # @mastra/pg Documentation
7
7
 
8
- > **Version**: 1.1.0-alpha.1
8
+ > **Version**: 1.1.0-alpha.2
9
9
  > **Package**: @mastra/pg
10
10
 
11
11
  ## Quick Navigation
@@ -1,5 +1,5 @@
1
1
  {
2
- "version": "1.1.0-alpha.1",
2
+ "version": "1.1.0-alpha.2",
3
3
  "package": "@mastra/pg",
4
4
  "exports": {},
5
5
  "modules": {}
package/dist/index.cjs CHANGED
@@ -3143,10 +3143,124 @@ var AgentsPG = class _AgentsPG extends storage.AgentsStorage {
3143
3143
  }
3144
3144
  }
3145
3145
  async init() {
3146
+ await this.#migrateFromLegacySchema();
3147
+ await this.#migrateVersionsSchema();
3146
3148
  await this.#db.createTable({ tableName: storage.TABLE_AGENTS, schema: storage.TABLE_SCHEMAS[storage.TABLE_AGENTS] });
3147
3149
  await this.#db.createTable({ tableName: storage.TABLE_AGENT_VERSIONS, schema: storage.TABLE_SCHEMAS[storage.TABLE_AGENT_VERSIONS] });
3150
+ await this.#db.alterTable({
3151
+ tableName: storage.TABLE_AGENTS,
3152
+ schema: storage.TABLE_SCHEMAS[storage.TABLE_AGENTS],
3153
+ ifNotExists: ["status", "authorId"]
3154
+ });
3148
3155
  await this.createDefaultIndexes();
3149
3156
  await this.createCustomIndexes();
3157
+ await this.#cleanupStaleDrafts();
3158
+ }
3159
+ /**
3160
+ * Migrates from the legacy flat agent schema (where config fields like name, instructions, model
3161
+ * were stored directly on mastra_agents) to the new versioned schema (thin agent record + versions table).
3162
+ */
3163
+ async #migrateFromLegacySchema() {
3164
+ const fullTableName = getTableName2({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName2(this.#schema) });
3165
+ const fullVersionsTableName = getTableName2({
3166
+ indexName: storage.TABLE_AGENT_VERSIONS,
3167
+ schemaName: getSchemaName2(this.#schema)
3168
+ });
3169
+ const legacyTableName = getTableName2({
3170
+ indexName: `${storage.TABLE_AGENTS}_legacy`,
3171
+ schemaName: getSchemaName2(this.#schema)
3172
+ });
3173
+ const hasLegacyColumns = await this.#db.hasColumn(storage.TABLE_AGENTS, "name");
3174
+ if (hasLegacyColumns) {
3175
+ await this.#db.client.none(`ALTER TABLE ${fullTableName} RENAME TO "${storage.TABLE_AGENTS}_legacy"`);
3176
+ await this.#db.client.none(`DROP TABLE IF EXISTS ${fullVersionsTableName}`);
3177
+ }
3178
+ const legacyExists = await this.#db.hasColumn(`${storage.TABLE_AGENTS}_legacy`, "name");
3179
+ if (!legacyExists) return;
3180
+ const oldAgents = await this.#db.client.manyOrNone(`SELECT * FROM ${legacyTableName}`);
3181
+ await this.#db.createTable({ tableName: storage.TABLE_AGENTS, schema: storage.TABLE_SCHEMAS[storage.TABLE_AGENTS] });
3182
+ await this.#db.createTable({ tableName: storage.TABLE_AGENT_VERSIONS, schema: storage.TABLE_SCHEMAS[storage.TABLE_AGENT_VERSIONS] });
3183
+ for (const row of oldAgents) {
3184
+ const agentId = row.id;
3185
+ if (!agentId) continue;
3186
+ const versionId = crypto.randomUUID();
3187
+ const now = /* @__PURE__ */ new Date();
3188
+ await this.#db.client.none(
3189
+ `INSERT INTO ${fullTableName} (id, status, "activeVersionId", "authorId", metadata, "createdAt", "updatedAt")
3190
+ VALUES ($1, $2, $3, $4, $5, $6, $7)
3191
+ ON CONFLICT (id) DO NOTHING`,
3192
+ [
3193
+ agentId,
3194
+ "published",
3195
+ versionId,
3196
+ row.ownerId ?? row.authorId ?? null,
3197
+ row.metadata ? JSON.stringify(row.metadata) : null,
3198
+ row.createdAt ?? now,
3199
+ row.updatedAt ?? now
3200
+ ]
3201
+ );
3202
+ await this.#db.client.none(
3203
+ `INSERT INTO ${fullVersionsTableName}
3204
+ (id, "agentId", "versionNumber", name, description, instructions, model, tools,
3205
+ "defaultOptions", workflows, agents, "integrationTools", "inputProcessors",
3206
+ "outputProcessors", memory, scorers, "changedFields", "changeMessage", "createdAt")
3207
+ VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,$14,$15,$16,$17,$18,$19)
3208
+ ON CONFLICT (id) DO NOTHING`,
3209
+ [
3210
+ versionId,
3211
+ agentId,
3212
+ 1,
3213
+ row.name ?? agentId,
3214
+ row.description ?? null,
3215
+ row.instructions ?? "",
3216
+ row.model ? JSON.stringify(row.model) : "{}",
3217
+ row.tools ? JSON.stringify(row.tools) : null,
3218
+ row.defaultOptions ? JSON.stringify(row.defaultOptions) : null,
3219
+ row.workflows ? JSON.stringify(row.workflows) : null,
3220
+ row.agents ? JSON.stringify(row.agents) : null,
3221
+ row.integrationTools ? JSON.stringify(row.integrationTools) : null,
3222
+ row.inputProcessors ? JSON.stringify(row.inputProcessors) : null,
3223
+ row.outputProcessors ? JSON.stringify(row.outputProcessors) : null,
3224
+ row.memory ? JSON.stringify(row.memory) : null,
3225
+ row.scorers ? JSON.stringify(row.scorers) : null,
3226
+ null,
3227
+ "Migrated from legacy schema",
3228
+ row.createdAt ?? now
3229
+ ]
3230
+ );
3231
+ }
3232
+ await this.#db.client.none(`DROP TABLE IF EXISTS ${legacyTableName}`);
3233
+ }
3234
+ /**
3235
+ * Migrates the agent_versions table from the old snapshot-based schema (single `snapshot` JSON column)
3236
+ * to the new flat schema (individual config columns). This handles the case where the agents table
3237
+ * was already migrated but the versions table still has the old schema.
3238
+ */
3239
+ async #migrateVersionsSchema() {
3240
+ const hasSnapshotColumn = await this.#db.hasColumn(storage.TABLE_AGENT_VERSIONS, "snapshot");
3241
+ if (!hasSnapshotColumn) return;
3242
+ const fullVersionsTableName = getTableName2({
3243
+ indexName: storage.TABLE_AGENT_VERSIONS,
3244
+ schemaName: getSchemaName2(this.#schema)
3245
+ });
3246
+ const legacyTableName = getTableName2({
3247
+ indexName: `${storage.TABLE_AGENTS}_legacy`,
3248
+ schemaName: getSchemaName2(this.#schema)
3249
+ });
3250
+ await this.#db.client.none(`DROP TABLE IF EXISTS ${fullVersionsTableName}`);
3251
+ await this.#db.client.none(`DROP TABLE IF EXISTS ${legacyTableName}`);
3252
+ }
3253
+ /**
3254
+ * Removes stale draft agent records that have no activeVersionId.
3255
+ * These are left behind when createAgent partially fails (inserts thin record
3256
+ * but fails to create the version due to schema mismatch).
3257
+ */
3258
+ async #cleanupStaleDrafts() {
3259
+ try {
3260
+ const fullTableName = getTableName2({ indexName: storage.TABLE_AGENTS, schemaName: getSchemaName2(this.#schema) });
3261
+ await this.#db.client.none(`DELETE FROM ${fullTableName} WHERE status = 'draft' AND "activeVersionId" IS NULL`);
3262
+ } catch {
3263
+ }
3150
3264
  }
3151
3265
  /**
3152
3266
  * Creates custom user-defined indexes for this domain's tables.