Conversation
…in mastra store Prevent Mastra DB pool initialization issues from blocking backend server startup, ensuring port 3000 always starts promptly. Co-authored-by: Cursor <cursoragent@cursor.com>
Bugbot couldn't run - usage limit reachedBugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit. A user or team admin can review and increase usage limits in the Cursor dashboard. (requestId: serverGenReqId_c55599c8-b5f2-4942-9dbb-0505149e3d46) |
There was a problem hiding this comment.
Code Review
This pull request wraps the Mastra MCP server initialization in a try-catch block to handle startup failures gracefully, and updates the Mastra PostgresStore connection string to fall back to DATABASE_URL if DATABASE_DIRECT_URL is not set. The review feedback recommends adding explicit validation for these environment variables to avoid runtime crashes caused by unsafe non-null assertions.
| export const pStore = new PostgresStore({ | ||
| id: 'postiz-store', | ||
| connectionString: process.env.DATABASE_URL!, | ||
| connectionString: process.env.DATABASE_DIRECT_URL || process.env.DATABASE_URL!, |
There was a problem hiding this comment.
Using the non-null assertion operator ! on process.env.DATABASE_URL is unsafe because if both DATABASE_DIRECT_URL and DATABASE_URL are missing, it will pass undefined to PostgresStore, leading to a runtime crash with a cryptic error. It is safer to validate that at least one of these environment variables is defined and throw a clear configuration error if they are missing.
connectionString: (() => {
const url = process.env.DATABASE_DIRECT_URL || process.env.DATABASE_URL;
if (!url) {
throw new Error('Database connection URL is missing. Please set DATABASE_DIRECT_URL or DATABASE_URL.');
}
return url;
})(),
What kind of change does this PR introduce?
Bug fix & Stability
Why was this change needed?
startMcpdoes not blockapp.listen(port)during backend boot if Mastra's internal table migrations encounter connection limits.PostgresStoreto useDATABASE_DIRECT_URL(direct PostgreSQL connection) instead of pooler session mode.Checklist:
Note
Medium Risk
Failed MCP init is swallowed so Copilot/MCP may be unavailable without a hard crash; switching Mastra to a direct DB URL changes connection behavior and must match deployment env vars.
Overview
Backend boot no longer fails when Mastra MCP initialization errors (e.g. Postgres migration/connection limits during startup).
startMcpis wrapped in try/catch so failures are logged and the API can still reachapp.listen.Mastra
PostgresStorenow prefersDATABASE_DIRECT_URLoverDATABASE_URL, so Mastra uses a direct Postgres connection instead of a pooler session URL when both are set.Reviewed by Cursor Bugbot for commit d935773. Bugbot is set up for automated code reviews on this repo. Configure here.