{"version":3,"file":"patch-db-topology.cjs","names":["Effect","fileExists","readText","LAYER_PATH","detectDbTopology","writeText"],"sources":["../../../../../src/services/scaffold/upgrade-audit/steps/patch-db-topology.ts"],"sourcesContent":["import { Effect } from \"effect\";\nimport { detectDbTopology, LAYER_PATH } from \"../checks/db-topology\";\nimport { fileExists, readText, writeText } from \"../fs\";\nimport type { StepResult } from \"./author-config\";\n\n/**\n * Patches the sync-owned api db layer back onto the public-schema topology\n * for the api workspace. The upstream template namespaces every workspace\n * into `plugin_<slug>`, but the api's own surfaces all assume public:\n * `bos db doctor` checks the public schema, the drizzle-kit config targets\n * public (no schemaNames), and every v1-upgraded database has its tables in\n * public with a full journal. Under the template, hash-edited migrations\n * re-run into plugin_api, drift goes drift-manual, and the api plugin\n * refuses to boot (\"Serving without the API: all /api/* routes return 503\").\n *\n * Maintenance step for post-upgrade trees and a bos sync regression guard:\n * `pnpm db-topology [--dry-run]`\n */\nexport const patchDbTopology = (\n  projectDir: string,\n  dryRun: boolean,\n): Effect.Effect<StepResult, Error> =>\n  Effect.gen(function* () {\n    const exists = yield* fileExists(projectDir, LAYER_PATH);\n    if (!exists) {\n      return {\n        writes: [],\n        deletes: [],\n        notes: [\"api/src/db/layer.ts absent — nothing to patch\"],\n      };\n    }\n\n    const source = yield* readText(projectDir, LAYER_PATH);\n    const topology = detectDbTopology(source);\n\n    if (topology === \"public-topology\") {\n      return {\n        writes: [],\n        deletes: [],\n        notes: [\"db layer already keeps the api on public-schema topology\"],\n      };\n    }\n    if (topology === \"unknown\") {\n      return {\n        writes: [],\n        deletes: [],\n        notes: [\"schemaName derivation not recognized — left untouched, inspect manually\"],\n      };\n    }\n\n    let next = source.replace(\n      /const\\s+schemaName\\s*=\\s*`plugin_\\$\\{slug\\}`\\s*;/,\n      [\n        \"// The api keeps the v1 public-schema topology (what `bos db doctor` checks\",\n        \"// and the drizzle-kit config targets); plugins scope into plugin_<slug>.\",\n        // biome-ignore lint/suspicious/noTemplateCurlyInString: literal source text spliced into layer.ts\n        'const schemaName = slug === \"api\" ? undefined : `plugin_${slug}`;',\n      ].join(\"\\n\"),\n    );\n    // biome-ignore lint/suspicious/noTemplateCurlyInString: literal source text spliced into layer.ts\n    next = next.replace(/schema:\\s*\\$\\{schemaName\\}/, 'schema: ${schemaName ?? \"public\"}');\n\n    if (!dryRun) yield* writeText(projectDir, LAYER_PATH, next);\n    return {\n      writes: [LAYER_PATH],\n      deletes: [],\n      notes: [\n        'api db layer patched to public-schema topology (slug === \"api\" → undefined schemaName)',\n        \"sync-owned file — bos sync will restore the plugin_<slug> template until the upstream fix lands; re-run this step after syncs\",\n      ],\n    };\n  });\n"],"mappings":";;;;;;;;;;;;;;;;;;AAkBA,MAAa,mBACX,YACA,WAEAA,cAAO,IAAI,aAAa;CAEtB,IAAI,EAAC,OADiBC,sBAAW,iCAAsB,IAErD,OAAO;EACL,QAAQ,CAAC;EACT,SAAS,CAAC;EACV,OAAO,CAAC,+CAA+C;CACzD;CAGF,MAAM,SAAS,OAAOC,oBAAS,YAAYC,8BAAU;CACrD,MAAM,WAAWC,qCAAiB,MAAM;CAExC,IAAI,aAAa,mBACf,OAAO;EACL,QAAQ,CAAC;EACT,SAAS,CAAC;EACV,OAAO,CAAC,0DAA0D;CACpE;CAEF,IAAI,aAAa,WACf,OAAO;EACL,QAAQ,CAAC;EACT,SAAS,CAAC;EACV,OAAO,CAAC,yEAAyE;CACnF;CAGF,IAAI,OAAO,OAAO,QAChB,oDACA;EACE;EACA;EAEA;CACF,CAAC,CAAC,KAAK,IAAI,CACb;CAEA,OAAO,KAAK,QAAQ,8BAA8B,qCAAmC;CAErF,IAAI,CAAC,QAAQ,OAAOC,qBAAU,YAAYF,gCAAY,IAAI;CAC1D,OAAO;EACL,QAAQ,CAACA,8BAAU;EACnB,SAAS,CAAC;EACV,OAAO,CACL,4FACA,+HACF;CACF;AACF,CAAC"}