diff --git a/adminSiteServer/apiRouter.ts b/adminSiteServer/apiRouter.ts index 5666f90faca..a88f84b82ff 100644 --- a/adminSiteServer/apiRouter.ts +++ b/adminSiteServer/apiRouter.ts @@ -2423,9 +2423,16 @@ apiRouter.put("/deploy", async (req: Request, res: Response) => { triggerStaticBuild(res.locals.user, "Manually triggered deploy") }) -apiRouter.get("/gdocs", async () => - Gdoc.find({ relations: ["tags"], order: { updatedAt: "DESC" } }) -) +apiRouter.get("/gdocs", async () => { + // orderBy was leading to a sort buffer overflow (ER_OUT_OF_SORTMEMORY) with MySQL's default sort_buffer_size + // when the posts_gdocs table got larger than 9MB, so we sort in memory + return Gdoc.find({ relations: ["tags"] }).then((gdocs) => + gdocs.sort((a, b) => { + if (!a.updatedAt || !b.updatedAt) return 0 + return b.updatedAt.getTime() - a.updatedAt.getTime() + }) + ) +}) apiRouter.get("/gdocs/:id", async (req, res) => { const id = req.params.id diff --git a/db/migration/1695651135934-IndexUpdatedAt.ts b/db/migration/1695651135934-IndexUpdatedAt.ts new file mode 100644 index 00000000000..bda83685700 --- /dev/null +++ b/db/migration/1695651135934-IndexUpdatedAt.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class IndexUpdatedAt1695651135934 implements MigrationInterface { + public async up(queryRunner: QueryRunner): Promise { + queryRunner.query( + `ALTER TABLE posts_gdocs ADD INDEX idx_updatedAt (updatedAt);` + ) + } + + public async down(queryRunner: QueryRunner): Promise { + queryRunner.query( + `ALTER TABLE posts_gdocs DROP INDEX idx_updatedAt (updatedAt);` + ) + } +}