| 1 | import Database from 'better-sqlite3';
|
| 2 | import fs from 'fs';
|
| 3 | import path from 'path';
|
| 4 | import { fileURLToPath } from 'url';
|
| 5 |
|
| 6 | const __filename = fileURLToPath(import.meta.url);
|
| 7 | const __dirname = path.dirname(__filename);
|
| 8 |
|
| 9 | export function setupDatabase(dbPath = 'orbit.db') {
|
| 10 | const db = new Database(dbPath);
|
| 11 |
|
| 12 | // Read and execute migrations in order
|
| 13 | const migrationsDir = path.join(__dirname, '..', 'migrations');
|
| 14 | const migrationFiles = [
|
| 15 | '001_initial_schema.sql',
|
| 16 | '002_mood_enhancements.sql'
|
| 17 | ];
|
| 18 |
|
| 19 | for (const file of migrationFiles) {
|
| 20 | const migrationPath = path.join(migrationsDir, file);
|
| 21 | if (fs.existsSync(migrationPath)) {
|
| 22 | const migration = fs.readFileSync(migrationPath, 'utf8');
|
| 23 | db.exec(migration);
|
| 24 | }
|
| 25 | }
|
| 26 |
|
| 27 | return db;
|
| 28 | }
|
| 29 |
|
| 30 | export function setupTestDatabase() {
|
| 31 | // Use in-memory database for tests
|
| 32 | const db = new Database(':memory:');
|
| 33 |
|
| 34 | // Read and execute migrations in order
|
| 35 | const migrationsDir = path.join(__dirname, '..', 'migrations');
|
| 36 | const migrationFiles = [
|
| 37 | '001_initial_schema.sql',
|
| 38 | '002_mood_enhancements.sql'
|
| 39 | ];
|
| 40 |
|
| 41 | for (const file of migrationFiles) {
|
| 42 | const migrationPath = path.join(migrationsDir, file);
|
| 43 | if (fs.existsSync(migrationPath)) {
|
| 44 | const migration = fs.readFileSync(migrationPath, 'utf8');
|
| 45 | db.exec(migration);
|
| 46 | }
|
| 47 | }
|
| 48 |
|
| 49 | return db;
|
| 50 | }
|