diff --git a/src/sql/recent-query.ts b/src/sql/recent-query.ts index ede7d9f..1c57fe0 100644 --- a/src/sql/recent-query.ts +++ b/src/sql/recent-query.ts @@ -11,6 +11,7 @@ import { type TableReference, } from "@query-doctor/core"; import { parse } from "@libpg-query/parser"; +import { Sema } from "async-sema"; import z from "zod"; import type { LiveQueryOptimization } from "../remote/optimization.ts"; @@ -21,6 +22,7 @@ import type { LiveQueryOptimization } from "../remote/optimization.ts"; export class RecentQuery { private static HARDCODED_LIMIT = 50; private static rewriter = new PssRewriter(); + private static prettierMutex = new Sema(1); readonly formattedQuery: string; readonly username: string; @@ -134,6 +136,7 @@ export class RecentQuery { } private static async formatQuery(query: string): Promise { + await RecentQuery.prettierMutex.acquire(); try { return await prettier.format(query, { parser: "sql", @@ -141,8 +144,11 @@ export class RecentQuery { language: "postgresql", keywordCase: "upper", }); - } catch { + } catch (error) { + console.error(`[prettier] Failed to format query: ${error}`); return query; + } finally { + RecentQuery.prettierMutex.release(); } }