* Initialize a new database at the given path
(dbPath: string)
| 65 | * Initialize a new database at the given path |
| 66 | */ |
| 67 | static initialize(dbPath: string): DatabaseConnection { |
| 68 | // Ensure parent directory exists |
| 69 | const dir = path.dirname(dbPath); |
| 70 | if (!fs.existsSync(dir)) { |
| 71 | fs.mkdirSync(dir, { recursive: true }); |
| 72 | } |
| 73 | |
| 74 | // Create and configure database |
| 75 | const { db, backend } = createDatabase(dbPath); |
| 76 | |
| 77 | configureConnection(db); |
| 78 | |
| 79 | // Run schema initialization |
| 80 | const schemaPath = path.join(__dirname, 'schema.sql'); |
| 81 | const schema = fs.readFileSync(schemaPath, 'utf-8'); |
| 82 | db.exec(schema); |
| 83 | |
| 84 | // Record current schema version so migrations aren't re-applied on open |
| 85 | const currentVersion = getCurrentVersion(db); |
| 86 | if (currentVersion < CURRENT_SCHEMA_VERSION) { |
| 87 | db.prepare( |
| 88 | 'INSERT OR IGNORE INTO schema_versions (version, applied_at, description) VALUES (?, ?, ?)' |
| 89 | ).run(CURRENT_SCHEMA_VERSION, Date.now(), 'Initial schema includes all migrations'); |
| 90 | } |
| 91 | |
| 92 | return new DatabaseConnection(db, dbPath, backend); |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Open an existing database |
no test coverage detected