|
| 1 | +import * as vscode from "vscode"; |
| 2 | +import * as fs from "fs"; |
| 3 | +import * as path from "path"; |
| 4 | +import { Logger, LogLevel } from "../logger/logger"; |
| 5 | +import { FileManager } from "../../services/file-manager"; |
| 6 | +import { dbManager } from "./database-manager"; |
| 7 | + |
| 8 | +export class InitDatabaseManager implements vscode.Disposable { |
| 9 | + private static instance: InitDatabaseManager; |
| 10 | + private isConnected: boolean = false; |
| 11 | + private connectionType: "file" | "memory" = "file"; |
| 12 | + private readonly logger: Logger; |
| 13 | + |
| 14 | + private constructor() { |
| 15 | + this.logger = Logger.initialize("InitDatabaseManager", { |
| 16 | + minLevel: LogLevel.DEBUG, |
| 17 | + }); |
| 18 | + } |
| 19 | + |
| 20 | + /** |
| 21 | + * Returns a singleton instance of the DatabaseManager, lazy-loading it if necessary. |
| 22 | + * This ensures that only one instance of the DatabaseManager is created throughout the application. |
| 23 | + */ |
| 24 | + public static getInstance(): InitDatabaseManager { |
| 25 | + InitDatabaseManager.instance ??= new InitDatabaseManager(); |
| 26 | + |
| 27 | + return InitDatabaseManager.instance; |
| 28 | + } |
| 29 | + |
| 30 | + async initialize(context: vscode.ExtensionContext): Promise<boolean> { |
| 31 | + try { |
| 32 | + await this.createFileDB(context); |
| 33 | + await this.connectToDatabase(context); |
| 34 | + this.isConnected = true; |
| 35 | + return true; |
| 36 | + } catch (error) { |
| 37 | + this.logger.error( |
| 38 | + "Database initialization failed, continuing with limited functionality", |
| 39 | + error, |
| 40 | + ); |
| 41 | + vscode.window.showWarningMessage( |
| 42 | + "Some features requiring database access will be limited. The extension will continue to function.", |
| 43 | + ); |
| 44 | + return false; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + async createFileDB(context: vscode.ExtensionContext) { |
| 49 | + try { |
| 50 | + const fileUploader = new FileManager(context, "repo"); |
| 51 | + const files = await fileUploader.getFiles(); |
| 52 | + if (!files?.find((file) => file.includes("dev.db"))) { |
| 53 | + await fileUploader.createFile("dev.db"); |
| 54 | + } |
| 55 | + } catch (error: any) { |
| 56 | + this.logger.warn("Unable to create DB file, will try to continue", error); |
| 57 | + // Don't throw, just log and continue |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + async connectToDatabase(context: vscode.ExtensionContext) { |
| 62 | + const dbDir = path.join(context.extensionPath, "repo"); |
| 63 | + const dbPath = path.join(dbDir, "dev.db"); |
| 64 | + const isWindows = dbPath.includes("\\"); |
| 65 | + const filePath = isWindows ? `file:/${dbPath}` : `file:${dbPath}`; |
| 66 | + |
| 67 | + try { |
| 68 | + if (!fs.existsSync(dbDir)) { |
| 69 | + fs.mkdirSync(dbDir, { recursive: true }); |
| 70 | + } |
| 71 | + } catch (error) { |
| 72 | + this.logger.warn("Failed to create database directory", error); |
| 73 | + return this.setupInMemoryDatabase(); |
| 74 | + } |
| 75 | + |
| 76 | + const maxRetries = 3; |
| 77 | + for (let attempt = 1; attempt <= maxRetries; attempt++) { |
| 78 | + try { |
| 79 | + await dbManager.connect(filePath); |
| 80 | + this.connectionType = "file"; |
| 81 | + return; |
| 82 | + } catch (error: any) { |
| 83 | + this.logger.warn(`Attempt ${attempt} to connect to DB failed`, error); |
| 84 | + if (attempt === maxRetries) { |
| 85 | + this.logger.warn("Switching to in-memory database"); |
| 86 | + return this.setupInMemoryDatabase(); |
| 87 | + } |
| 88 | + await new Promise((resolve) => setTimeout(resolve, 500)); |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + async setupInMemoryDatabase() { |
| 94 | + try { |
| 95 | + await dbManager.connect(":memory:"); |
| 96 | + this.connectionType = "memory"; |
| 97 | + vscode.window.showInformationMessage( |
| 98 | + "Using in-memory database due to connection issues.", |
| 99 | + ); |
| 100 | + } catch (error) { |
| 101 | + this.logger.error("Failed to set up in-memory database", error); |
| 102 | + // Don't throw, just set as not connected |
| 103 | + this.isConnected = false; |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + isDbConnected(): boolean { |
| 108 | + return this.isConnected; |
| 109 | + } |
| 110 | + |
| 111 | + dispose() { |
| 112 | + try { |
| 113 | + if (this.isConnected) { |
| 114 | + dbManager.close(); |
| 115 | + this.isConnected = false; |
| 116 | + } |
| 117 | + } catch (error) { |
| 118 | + this.logger.error("Error closing database connection", error); |
| 119 | + } |
| 120 | + } |
| 121 | + |
| 122 | + public async close(): Promise<void> { |
| 123 | + try { |
| 124 | + if (this.isConnected) { |
| 125 | + await dbManager.close(); |
| 126 | + this.isConnected = false; |
| 127 | + } |
| 128 | + } catch (error) { |
| 129 | + this.logger.error("Error closing database connection", error); |
| 130 | + } |
| 131 | + } |
| 132 | +} |
| 133 | + |
| 134 | +export const dbManagerInstance = InitDatabaseManager.getInstance(); |
0 commit comments