Skip to content

πŸ“¨ The ultimate agentic social media scheduling tool πŸ€–

fix(chat): run the Mastra storage migration at boot with a retry

Nevo David6b40c6447ccf
diff --git a/libraries/nestjs-libraries/src/chat/mastra.service.ts b/libraries/nestjs-libraries/src/chat/mastra.service.ts
index 27f1b0a36..b40e3f9ac 100644
--- a/libraries/nestjs-libraries/src/chat/mastra.service.ts
+++ b/libraries/nestjs-libraries/src/chat/mastra.service.ts
@@ -1,7 +1,7 @@
import { Mastra } from '@mastra/core/mastra';
import { ConsoleLogger } from '@mastra/core/logger';
import { pStore } from '@gitroom/nestjs-libraries/chat/mastra.store';
-import { Injectable } from '@nestjs/common';
+import { Injectable, Logger } from '@nestjs/common';
import { LoadToolsService } from '@gitroom/nestjs-libraries/chat/load.tools.service';
@Injectable()
@@ -9,6 +9,15 @@ export class MastraService {
static mastra: Mastra;
constructor(private _loadToolsService: LoadToolsService) {}
async mastra() {
+ if (!MastraService.mastra) {
+ // @mastra/pg migrates its tables on first use, when a few instances boot together
+ // the ADD COLUMN can race, the retry runs against the already migrated schema
+ await pStore
+ .init()
+ .catch(() => pStore.init())
+ .catch((err) => Logger.warn(`Mastra storage init failed: ${err}`));
+ }
+
MastraService.mastra =
MastraService.mastra ||
new Mastra({