{"version":3,"file":"db.mjs","names":[],"sources":["../../src/commands/db.ts"],"sourcesContent":["import { Context, Effect } from \"effect\";\nimport { findConfigPath, MISSING_CONFIG_MESSAGE } from \"../config\";\nimport { type BosBuilder, BosDepsTag } from \"./shared\";\n\nexport function registerDb(builder: BosBuilder) {\n  return {\n    dbStudio: builder.dbStudio.handler(async ({ input, context }) => {\n      const deps = Context.get(context[\"effect/context\"], BosDepsTag);\n      const configPath = findConfigPath();\n      if (!configPath) {\n        return {\n          status: \"error\" as const,\n          plugin: input.plugin,\n          source: \"remote\" as const,\n          section: \"\",\n          error: `${MISSING_CONFIG_MESSAGE} in current directory`,\n        };\n      }\n\n      try {\n        const binding = await Effect.runPromise(deps.databaseBindings.forPluginKey(input.plugin));\n        await Effect.runPromise(deps.drizzleKit.studio(binding));\n\n        return {\n          status: \"success\" as const,\n          plugin: binding.key,\n          source: binding.source,\n          section: binding.section,\n          databaseSecret: binding.identity.secretName,\n          databaseUrl: binding.url,\n          workspaceDir: binding.identity.workspaceDir,\n        };\n      } catch (error) {\n        return {\n          status: \"error\" as const,\n          plugin: input.plugin,\n          source: \"remote\" as const,\n          section: \"\",\n          error: error instanceof Error ? error.message : \"Unknown error\",\n        };\n      }\n    }),\n\n    dbDoctor: builder.dbDoctor.handler(async ({ input, context }) => {\n      const deps = Context.get(context[\"effect/context\"], BosDepsTag);\n      try {\n        const configPath = findConfigPath();\n        if (!configPath) {\n          return {\n            status: \"error\" as const,\n            plugin: input.plugin,\n            slug: \"\",\n            journalTable: \"\",\n            journalSchema: \"\",\n            diagnosis: \"error\",\n            localMigrationCount: 0,\n            appliedHashCount: 0,\n            expectedTables: [],\n            missingTables: [],\n            error: `${MISSING_CONFIG_MESSAGE} in current directory`,\n          };\n        }\n\n        const binding = await Effect.runPromise(deps.databaseBindings.forPluginKey(input.plugin));\n\n        const { diagnosePlugin } = await import(\"../cli/db-doctor\");\n        const report = await diagnosePlugin(binding);\n\n        return {\n          status: \"success\" as const,\n          ...report,\n        };\n      } catch (error) {\n        return {\n          status: \"error\" as const,\n          plugin: input.plugin,\n          slug: \"\",\n          journalTable: \"\",\n          journalSchema: \"\",\n          diagnosis: \"error\",\n          localMigrationCount: 0,\n          appliedHashCount: 0,\n          expectedTables: [],\n          missingTables: [],\n          error: error instanceof Error ? error.message : \"Unknown error\",\n        };\n      }\n    }),\n\n    dbRepair: builder.dbRepair.handler(async ({ input, context }) => {\n      const deps = Context.get(context[\"effect/context\"], BosDepsTag);\n      try {\n        const configPath = findConfigPath();\n        if (!configPath) {\n          return {\n            status: \"error\" as const,\n            message: MISSING_CONFIG_MESSAGE,\n            diagnosis: null,\n            error: \"No config\",\n          };\n        }\n\n        const binding = await Effect.runPromise(deps.databaseBindings.forPluginKey(input.plugin));\n\n        const { repairPlugin } = await import(\"../cli/db-repair\");\n        const result = await repairPlugin(binding, input.mode ?? \"history-reset\", deps.drizzleKit);\n\n        return {\n          ...result,\n          error: result.status === \"error\" ? result.message : undefined,\n        };\n      } catch (error) {\n        return {\n          status: \"error\" as const,\n          message: error instanceof Error ? error.message : \"Unknown error\",\n          diagnosis: null,\n          error: error instanceof Error ? error.message : \"Unknown error\",\n        };\n      }\n    }),\n  };\n}\n"],"mappings":";;;;;AAIA,SAAgB,WAAW,SAAqB;CAC9C,OAAO;EACL,UAAU,QAAQ,SAAS,QAAQ,OAAO,EAAE,OAAO,cAAc;GAC/D,MAAM,OAAO,QAAQ,IAAI,QAAQ,mBAAmB,UAAU;GAE9D,IAAI,CADe,eACL,GACZ,OAAO;IACL,QAAQ;IACR,QAAQ,MAAM;IACd,QAAQ;IACR,SAAS;IACT,OAAO,GAAG,uBAAuB;GACnC;GAGF,IAAI;IACF,MAAM,UAAU,MAAM,OAAO,WAAW,KAAK,iBAAiB,aAAa,MAAM,MAAM,CAAC;IACxF,MAAM,OAAO,WAAW,KAAK,WAAW,OAAO,OAAO,CAAC;IAEvD,OAAO;KACL,QAAQ;KACR,QAAQ,QAAQ;KAChB,QAAQ,QAAQ;KA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