From 05de9dda5bb57abd1d367469f403d0b67cd138b7 Mon Sep 17 00:00:00 2001 From: alikia2x Date: Tue, 18 Nov 2025 20:34:33 +0800 Subject: [PATCH] fix: drizzle generating malformed SQL causing getVideosNearMilestone() crash --- packages/crawler/db/snapshot.ts | 67 ++++++++++++--------------------- 1 file changed, 24 insertions(+), 43 deletions(-) diff --git a/packages/crawler/db/snapshot.ts b/packages/crawler/db/snapshot.ts index 3b6df94..cb7619d 100644 --- a/packages/crawler/db/snapshot.ts +++ b/packages/crawler/db/snapshot.ts @@ -1,17 +1,7 @@ -import { LatestSnapshotType } from "@core/db/schema"; import { SnapshotNumber } from "mq/task/getVideoStats"; import type { Psql } from "@core/db/psql.d"; -import { - db, - eta, - latestVideoSnapshot as lv, - songs, - videoSnapshot, - VideoSnapshotType -} from "@core/drizzle"; +import { db, LatestVideoSnapshotType, videoSnapshot, VideoSnapshotType } from "@core/drizzle"; import { PartialBy } from "@core/lib"; -import { and, eq, getTableColumns, gte, lte, lt, or } from "drizzle-orm"; -import { union } from "drizzle-orm/pg-core"; import { sql } from "drizzle-orm"; export async function insertVideoSnapshot(data: PartialBy) { @@ -19,37 +9,28 @@ export async function insertVideoSnapshot(data: PartialBy= CEIL(views::float/1000000::float)*1000000-100000`, - sql`views < CEIL(views::float/1000000::float)*1000000)` - ) - ) - ), - db - .select({ ...getTableColumns(lv) }) - .from(lv) - .innerJoin(eta, eq(lv.aid, eta.aid)) - .where(lte(eta.eta, 2300)) - ); + const results = await db.execute(sql` + SELECT ls.* + FROM latest_video_snapshot ls + RIGHT JOIN songs ON songs.aid = ls.aid + WHERE + (views >= 60000 AND views < 100000) OR + (views >= 900000 AND views < 1000000) OR + views > 1000000 + UNION + SELECT ls.* + FROM latest_video_snapshot ls + WHERE + (views >= 90000 AND views < 100000) OR + (views >= 900000 AND views < 1000000) OR + (views >= CEIL(views::float/1000000::float)*1000000-100000 AND views < CEIL(views::float/1000000::float)*1000000) + UNION + SELECT ls.* + FROM latest_video_snapshot ls + JOIN eta ON eta.aid = ls.aid + WHERE eta.eta < 2300 + `); + return results.map((row) => { return { ...row, @@ -62,7 +43,7 @@ export async function getLatestVideoSnapshot( sql: Psql, aid: number ): Promise { - const queryResult = await sql` + const queryResult = await sql` SELECT * FROM latest_video_snapshot WHERE aid = ${aid}