| 1 | namespace HybridCodebaseIndex.Core; |
| 2 | |
| 3 | internal static partial class SqliteFtsIndex |
| 4 | { |
| 5 | internal static string ResolveDatabasePath(string workspaceRoot, string indexDirectoryRelative) |
| 6 | { |
| 7 | // Back-compat note: |
| 8 | // - Read operations can fall back to legacy location. |
| 9 | // - Write operations (reindex) always go to the requested location. |
| 10 | // This helper keeps old callsites working by using the "read" resolution. |
| 11 | return ResolveDatabasePathForRead(workspaceRoot, indexDirectoryRelative); |
| 12 | } |
| 13 | |
| 14 | internal static string ResolveDatabasePathForRead(string workspaceRoot, string indexDirectoryRelative) |
| 15 | { |
| 16 | var root = Path.GetFullPath(workspaceRoot.TrimEnd(Path.DirectorySeparatorChar)); |
| 17 | var requestedDir = Path.Combine(root, indexDirectoryRelative.TrimStart(Path.DirectorySeparatorChar, '/')); |
| 18 | var requestedDb = Path.Combine(requestedDir, $"codebase-index-v{FormatVersion}.sqlite"); |
| 19 | if (File.Exists(requestedDb)) |
| 20 | return requestedDb; |
| 21 | |
| 22 | var legacyDb = GetLegacyDbPath(root); |
| 23 | if (File.Exists(legacyDb)) |
| 24 | return legacyDb; |
| 25 | |
| 26 | return requestedDb; |
| 27 | } |
| 28 | |
| 29 | internal static string ResolveDatabasePathForWrite(string workspaceRoot, string indexDirectoryRelative) |
| 30 | { |
| 31 | var root = Path.GetFullPath(workspaceRoot.TrimEnd(Path.DirectorySeparatorChar)); |
| 32 | var requestedDir = Path.Combine(root, indexDirectoryRelative.TrimStart(Path.DirectorySeparatorChar, '/')); |
| 33 | Directory.CreateDirectory(requestedDir); |
| 34 | |
| 35 | // Best-effort migrate settings.toml if legacy exists and new doesn't. |
| 36 | TryMigrateSettingsToml(root, requestedDir); |
| 37 | |
| 38 | return Path.Combine(requestedDir, $"codebase-index-v{FormatVersion}.sqlite"); |
| 39 | } |
| 40 | |
| 41 | private static string GetLegacyDbPath(string workspaceRootNormalized) |
| 42 | { |
| 43 | var legacyDir = Path.Combine(workspaceRootNormalized, ".cascade-ide", "hybrid-codebase-index"); |
| 44 | return Path.Combine(legacyDir, $"codebase-index-v{FormatVersion}.sqlite"); |
| 45 | } |
| 46 | |
| 47 | private static void TryMigrateSettingsToml(string workspaceRootNormalized, string requestedDir) |
| 48 | { |
| 49 | try |
| 50 | { |
| 51 | var newSettings = Path.Combine(requestedDir, "settings.toml"); |
| 52 | if (File.Exists(newSettings)) |
| 53 | return; |
| 54 | |
| 55 | var legacySettings = Path.Combine(workspaceRootNormalized, ".cascade-ide", "hybrid-codebase-index", "settings.toml"); |
| 56 | if (!File.Exists(legacySettings)) |
| 57 | return; |
| 58 | |
| 59 | File.Copy(legacySettings, newSettings); |
| 60 | } |
| 61 | catch |
| 62 | { |
| 63 | // best-effort |
| 64 | } |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | |