From d5994af4311ba27c3c50029b8c2a169cd221c311 Mon Sep 17 00:00:00 2001 From: jacy <18049821889@163.com> Date: Wed, 2 Sep 2026 22:19:40 +0800 Subject: [PATCH] =?UTF-8?q?fix(ops):=20=E4=BF=AE=E5=A4=8D=E4=BC=81?= =?UTF-8?q?=E5=BE=AE=E6=8A=A5=E5=91=8A=E8=A1=A8=E9=9B=B6=E6=97=A5=E6=9C=9F?= =?UTF-8?q?=E5=AF=BC=E8=87=B4=E6=97=A0=E6=B3=95=E4=BF=9D=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 早期 INSERT 未写 updated_at,MySQL 落成 0000-00-00,Prisma 读行失败。 --- .../dukang-api/prisma/migrate-wecom-report.ts | 15 ++++---- server/dukang-api/prisma/schema.prisma | 2 +- .../ops/admin-wecom-reports.service.ts | 34 ++++++++++++++++--- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/server/dukang-api/prisma/migrate-wecom-report.ts b/server/dukang-api/prisma/migrate-wecom-report.ts index 0d9da1a..f61203c 100644 --- a/server/dukang-api/prisma/migrate-wecom-report.ts +++ b/server/dukang-api/prisma/migrate-wecom-report.ts @@ -34,12 +34,15 @@ async function main() { ['monthly', '经营月报', 9], ]; for (const [kind, name, hour] of seeds) { - await prisma.$executeRaw` - INSERT IGNORE INTO wecom_report_push - (kind, name, webhook_url, enabled, send_hour, send_minute, send_weekday, send_month_day) - VALUES - (${kind}, ${name}, ${PLACEHOLDER}, 0, ${hour}, 0, 1, 1) - `; + await prisma.$executeRawUnsafe( + `INSERT IGNORE INTO wecom_report_push + (kind, name, webhook_url, enabled, send_hour, send_minute, send_weekday, send_month_day, created_at, updated_at) + VALUES (?, ?, ?, 0, ?, 0, 1, 1, CURRENT_TIMESTAMP(3), CURRENT_TIMESTAMP(3))`, + kind, + name, + PLACEHOLDER, + hour, + ); } console.log('migrate-wecom-report done'); } diff --git a/server/dukang-api/prisma/schema.prisma b/server/dukang-api/prisma/schema.prisma index d04ab39..76ddcf8 100644 --- a/server/dukang-api/prisma/schema.prisma +++ b/server/dukang-api/prisma/schema.prisma @@ -561,7 +561,7 @@ model WecomReportPush { lastSentPeriod String? @map("last_sent_period") @db.VarChar(16) lastSentAt DateTime? @map("last_sent_at") @db.DateTime(3) createdAt DateTime @default(now()) @map("created_at") @db.DateTime(3) - updatedAt DateTime @updatedAt @map("updated_at") @db.DateTime(3) + updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.DateTime(3) @@map("wecom_report_push") } diff --git a/server/dukang-api/src/modules/ops/admin-wecom-reports.service.ts b/server/dukang-api/src/modules/ops/admin-wecom-reports.service.ts index 9716066..0c19161 100644 --- a/server/dukang-api/src/modules/ops/admin-wecom-reports.service.ts +++ b/server/dukang-api/src/modules/ops/admin-wecom-reports.service.ts @@ -61,6 +61,7 @@ type ReportRow = WecomReportPush; @Injectable() export class AdminWecomReportsService implements OnModuleInit { private readonly logger = new Logger(AdminWecomReportsService.name); + private defaultsReady = false; constructor( private readonly prisma: PrismaService, @@ -77,11 +78,36 @@ export class AdminWecomReportsService implements OnModuleInit { } } + /** + * 早期 INSERT IGNORE 未写 updated_at,Prisma db push 的列又无默认值, + * MySQL 会落成 0000-00-00,后续 Prisma 读行即报 invalid datetime。 + */ + private async repairZeroDatetimes(): Promise { + await this.prisma.$executeRawUnsafe(` + UPDATE wecom_report_push + SET created_at = CURRENT_TIMESTAMP(3) + WHERE CAST(created_at AS CHAR) REGEXP '^0000|-00-' + `); + await this.prisma.$executeRawUnsafe(` + UPDATE wecom_report_push + SET updated_at = CURRENT_TIMESTAMP(3) + WHERE CAST(updated_at AS CHAR) REGEXP '^0000|-00-' + `); + await this.prisma.$executeRawUnsafe(` + UPDATE wecom_report_push + SET last_sent_at = NULL + WHERE last_sent_at IS NOT NULL AND CAST(last_sent_at AS CHAR) REGEXP '^0000|-00-' + `); + } + async ensureDefaults(): Promise { + if (this.defaultsReady) return; + await this.repairZeroDatetimes(); for (const seed of KIND_SEED) { - await this.prisma.wecomReportPush.upsert({ - where: { kind: seed.kind }, - create: { + const existing = await this.prisma.wecomReportPush.findUnique({ where: { kind: seed.kind } }); + if (existing) continue; + await this.prisma.wecomReportPush.create({ + data: { kind: seed.kind, name: seed.name, webhookUrl: WECOM_STORE_AUDIT_PLACEHOLDER_WEBHOOK, @@ -91,9 +117,9 @@ export class AdminWecomReportsService implements OnModuleInit { sendWeekday: 1, sendMonthDay: 1, }, - update: {}, }); } + this.defaultsReady = true; } parseKind(raw: string): WecomReportKind {