|
| 1 | +/** |
| 2 | + * Corrige les mission_event incoherents: |
| 3 | + * - type = 'delete' |
| 4 | + * - changes.deletedAt.current = null |
| 5 | + * - changes.deletedAt.previous non null |
| 6 | + * |
| 7 | + * Dans ce cas, le type attendu est 'update' (reactivation / undelete). |
| 8 | + * |
| 9 | + * Execution: |
| 10 | + * npx ts-node scripts/fix-mission-event-undelete-type.ts |
| 11 | + * npx ts-node scripts/fix-mission-event-undelete-type.ts --dry-run |
| 12 | + * npx ts-node scripts/fix-mission-event-undelete-type.ts --dry-run --sample 50 |
| 13 | + * npx ts-node scripts/fix-mission-event-undelete-type.ts --batch 1000 |
| 14 | + * |
| 15 | + * Comportement: |
| 16 | + * - par defaut: applique l'update en base |
| 17 | + * - avec --dry-run: preview (aucune ecriture) |
| 18 | + */ |
| 19 | +import dotenv from "dotenv"; |
| 20 | +dotenv.config(); |
| 21 | + |
| 22 | +import { prismaCore } from "../src/db/postgres"; |
| 23 | + |
| 24 | +type CandidateRow = { |
| 25 | + id: string; |
| 26 | + mission_id: string; |
| 27 | + type: string; |
| 28 | + created_at: Date; |
| 29 | + updated_at: Date; |
| 30 | + deleted_at_previous: string | null; |
| 31 | +}; |
| 32 | + |
| 33 | +const parseFlagValue = (flag: string): string | null => { |
| 34 | + const index = process.argv.indexOf(flag); |
| 35 | + if (index === -1) { |
| 36 | + return null; |
| 37 | + } |
| 38 | + return process.argv[index + 1] ?? null; |
| 39 | +}; |
| 40 | + |
| 41 | +const isDryRun = process.argv.includes("--dry-run"); |
| 42 | +const sampleSize = Math.max(Number(parseFlagValue("--sample") ?? "20"), 1); |
| 43 | +const batchSize = Math.max(Number(parseFlagValue("--batch") ?? "1000"), 1); |
| 44 | + |
| 45 | +const run = async () => { |
| 46 | + const startedAt = new Date(); |
| 47 | + console.log(`[MissionEventFix] Started at ${startedAt.toISOString()}`); |
| 48 | + console.log(`[MissionEventFix] Mode: ${isDryRun ? "dry-run" : "execute"}`); |
| 49 | + console.log(`[MissionEventFix] Batch size: ${batchSize}`); |
| 50 | + |
| 51 | + await prismaCore.$connect(); |
| 52 | + |
| 53 | + const [countRow] = await prismaCore.$queryRaw<{ total: number }[]>` |
| 54 | + select count(*)::int as total |
| 55 | + from "mission_event" me |
| 56 | + where |
| 57 | + me."type" = 'delete' |
| 58 | + and jsonb_typeof(me."changes") = 'object' |
| 59 | + and me."changes" ? 'deletedAt' |
| 60 | + and jsonb_typeof(me."changes"->'deletedAt') = 'object' |
| 61 | + and (me."changes"->'deletedAt'->'current') = 'null'::jsonb |
| 62 | + and (me."changes"->'deletedAt'->>'previous') is not null |
| 63 | + `; |
| 64 | + |
| 65 | + const total = countRow?.total ?? 0; |
| 66 | + console.log(`[MissionEventFix] Lignes candidates: ${total}`); |
| 67 | + |
| 68 | + if (!total) { |
| 69 | + console.log("[MissionEventFix] Rien a corriger."); |
| 70 | + return; |
| 71 | + } |
| 72 | + |
| 73 | + const sample = await prismaCore.$queryRaw<CandidateRow[]>` |
| 74 | + select |
| 75 | + me."id", |
| 76 | + me."mission_id", |
| 77 | + me."type", |
| 78 | + me."created_at", |
| 79 | + me."updated_at", |
| 80 | + me."changes"->'deletedAt'->>'previous' as deleted_at_previous |
| 81 | + from "mission_event" me |
| 82 | + where |
| 83 | + me."type" = 'delete' |
| 84 | + and jsonb_typeof(me."changes") = 'object' |
| 85 | + and me."changes" ? 'deletedAt' |
| 86 | + and jsonb_typeof(me."changes"->'deletedAt') = 'object' |
| 87 | + and (me."changes"->'deletedAt'->'current') = 'null'::jsonb |
| 88 | + and (me."changes"->'deletedAt'->>'previous') is not null |
| 89 | + order by me."updated_at" desc |
| 90 | + limit ${sampleSize} |
| 91 | + `; |
| 92 | + |
| 93 | + console.log(`[MissionEventFix] Echantillon (max ${sampleSize}):`); |
| 94 | + for (const row of sample) { |
| 95 | + console.log( |
| 96 | + `- id=${row.id} mission_id=${row.mission_id} type=${row.type} previous=${row.deleted_at_previous} updated_at=${row.updated_at.toISOString()}` |
| 97 | + ); |
| 98 | + } |
| 99 | + |
| 100 | + if (isDryRun) { |
| 101 | + console.log("[MissionEventFix] Dry-run uniquement. Relancer sans --dry-run pour appliquer."); |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + let updatedTotal = 0; |
| 106 | + let lastSeenId: string | null = null; |
| 107 | + |
| 108 | + while (true) { |
| 109 | + const batch: { id: string }[] = await prismaCore.$queryRaw<{ id: string }[]>` |
| 110 | + select me."id" |
| 111 | + from "mission_event" me |
| 112 | + where |
| 113 | + me."type" = 'delete' |
| 114 | + and jsonb_typeof(me."changes") = 'object' |
| 115 | + and me."changes" ? 'deletedAt' |
| 116 | + and jsonb_typeof(me."changes"->'deletedAt') = 'object' |
| 117 | + and (me."changes"->'deletedAt'->'current') = 'null'::jsonb |
| 118 | + and (me."changes"->'deletedAt'->>'previous') is not null |
| 119 | + and (${lastSeenId}::uuid is null or me."id" > ${lastSeenId}::uuid) |
| 120 | + order by me."id" asc |
| 121 | + limit ${batchSize} |
| 122 | + `; |
| 123 | + |
| 124 | + if (!batch.length) { |
| 125 | + break; |
| 126 | + } |
| 127 | + |
| 128 | + const ids: string[] = batch.map((row: { id: string }) => row.id); |
| 129 | + lastSeenId = ids[ids.length - 1] ?? lastSeenId; |
| 130 | + |
| 131 | + const result = await prismaCore.missionEvent.updateMany({ |
| 132 | + where: { id: { in: ids } }, |
| 133 | + data: { type: "update" }, |
| 134 | + }); |
| 135 | + updatedTotal += result.count; |
| 136 | + |
| 137 | + console.log(`[MissionEventFix] Batch corrige: ${result.count} (total: ${updatedTotal})`); |
| 138 | + } |
| 139 | + |
| 140 | + console.log(`[MissionEventFix] Lignes corrigees: ${updatedTotal}`); |
| 141 | +}; |
| 142 | + |
| 143 | +const shutdown = async (exitCode: number) => { |
| 144 | + await prismaCore.$disconnect().catch(() => undefined); |
| 145 | + process.exit(exitCode); |
| 146 | +}; |
| 147 | + |
| 148 | +run() |
| 149 | + .then(async () => { |
| 150 | + await shutdown(0); |
| 151 | + }) |
| 152 | + .catch(async (error) => { |
| 153 | + console.error("[MissionEventFix] Failed:", error); |
| 154 | + await shutdown(1); |
| 155 | + }); |
0 commit comments