{"version":3,"file":"db-repair.cjs","names":["diagnosePlugin","join","existsSync","Effect"],"sources":["../../src/cli/db-repair.ts"],"sourcesContent":["import { existsSync } from \"node:fs\";\nimport { join } from \"node:path\";\nimport { Effect } from \"effect\";\nimport type { DatabaseBinding, DrizzleKitService } from \"../db\";\nimport { type DoctorReport, diagnosePlugin } from \"./db-doctor\";\n\nexport interface RepairResult {\n  status: \"repaired\" | \"refused\" | \"error\";\n  diagnosis: DoctorReport;\n  message: string;\n}\n\nexport async function repairPlugin(\n  binding: DatabaseBinding,\n  mode: \"history-reset\" | \"recreate\",\n  drizzleKit: DrizzleKitService,\n): Promise<RepairResult> {\n  const diagnosis = await diagnosePlugin(binding);\n\n  if (diagnosis.diagnosis === \"error\") {\n    return {\n      status: \"refused\",\n      diagnosis,\n      message: `Cannot repair — diagnosis failed: ${diagnosis.error}`,\n    };\n  }\n\n  if (mode === \"recreate\") {\n    return {\n      status: \"refused\",\n      diagnosis,\n      message:\n        \"Recreate mode is not supported without per-plugin database schemas. Use --mode history-reset instead.\",\n    };\n  }\n\n  if (diagnosis.diagnosis !== \"drift-safe-repair\") {\n    if (diagnosis.diagnosis === \"healthy\") {\n      return { status: \"refused\", diagnosis, message: \"Database is healthy — no repair needed.\" };\n    }\n    if (diagnosis.diagnosis === \"no-local-migrations\") {\n      return {\n        status: \"refused\",\n        diagnosis,\n        message: \"No local migrations found for this plugin.\",\n      };\n    }\n    if (diagnosis.diagnosis === \"drift-manual\") {\n      return {\n        status: \"refused\",\n        diagnosis,\n        message:\n          `Partial drift detected (${diagnosis.missingTables.length}/${diagnosis.expectedTables.length} tables missing). ` +\n          \"Manual intervention required — some tables exist but schema is incomplete.\",\n      };\n    }\n    if (diagnosis.diagnosis === \"unapplied\") {\n      return {\n        status: \"refused\",\n        diagnosis,\n        message: \"Migrations have not been applied yet. Start the dev server to apply them.\",\n      };\n    }\n    if (diagnosis.diagnosis === \"untracked-existing-schema\") {\n      return {\n        status: \"refused\",\n        diagnosis,\n        message:\n          \"Tables exist but no matching migration history was found. \" +\n          \"Run `drizzle-kit pull --init` in the plugin workspace to adopt the existing schema, \" +\n          \"then run migrations from that baseline.\",\n      };\n    }\n    return {\n      status: \"refused\",\n      diagnosis,\n      message: `Cannot repair — diagnosis: ${diagnosis.diagnosis}`,\n    };\n  }\n\n  // drift-safe-repair: drop the journal and replay\n  const { Pool } = await import(\"pg\");\n  const journalRef = `\"${diagnosis.journalSchema}\".\"${diagnosis.journalTable}\"`;\n  const pool = new Pool({\n    connectionString: binding.url,\n    ssl:\n      binding.url.includes(\"localhost\") || binding.url.includes(\"127.0.0.1\")\n        ? false\n        : { rejectUnauthorized: false },\n    max: 1,\n    connectionTimeoutMillis: 10_000,\n  });\n\n  try {\n    await pool.query(`DROP TABLE IF EXISTS ${journalRef}`);\n\n    if (binding.identity.workspaceDir) {\n      const configPath = join(binding.identity.workspaceDir, \"drizzle.config.ts\");\n      if (existsSync(configPath)) {\n        try {\n          await Effect.runPromise(drizzleKit.migrate(binding));\n          return {\n            status: \"repaired\",\n            diagnosis,\n            message:\n              `Migration history reset for ${diagnosis.plugin}. ` +\n              `Migrations reapplied via drizzle-kit. Restart the dev server to confirm.`,\n          };\n        } catch (error) {\n          return {\n            status: \"repaired\",\n            diagnosis,\n            message:\n              `Migration history reset for ${diagnosis.plugin}. ` +\n              `Automatic reapply failed: ${error instanceof Error ? error.message : String(error)}. ` +\n              `Run \\`pnpm --dir ${binding.identity.workspaceDir} run db:migrate\\` manually.`,\n          };\n        }\n      }\n    }\n  } catch (error) {\n    return {\n      status: \"error\",\n      diagnosis,\n      message: `Repair failed: ${error instanceof Error ? error.message : String(error)}`,\n    };\n  } finally {\n    await pool.end().catch(() => {});\n  }\n\n  return {\n    status: \"repaired\",\n    diagnosis,\n    message:\n      `Migration history reset for ${diagnosis.plugin}. ` +\n      \"No local drizzle.config.ts found — start the dev server to reapply migrations.\",\n  };\n}\n"],"mappings":";;;;;;AAYA,eAAsB,aACpB,SACA,MACA,YACuB;CAC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