feat: Add function to configure SQLite database

This commit is contained in:
Peifan Li
2025-12-27 13:17:17 -05:00
parent 0ec84785e6
commit 632ac19cc0
2 changed files with 34 additions and 23 deletions

View File

@@ -1,5 +1,11 @@
# Change Log
## v1.7.3 (2025-12-27)
### Feat
- feat: Add function to configure SQLite database (0ec8478)
## v1.7.2 (2025-12-27)
### Feat

View File

@@ -1,50 +1,55 @@
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
import path from 'path';
import { ROOT_DIR } from '../config/paths';
import { db, sqlite, configureDatabase } from './index';
import { migrate } from "drizzle-orm/better-sqlite3/migrator";
import path from "path";
import { ROOT_DIR } from "../config/paths";
import { configureDatabase, db, sqlite } from "./index";
export async function runMigrations() {
try {
console.log('Running database migrations...');
console.log("Running database migrations...");
// In production/docker, the drizzle folder is copied to the root or src/drizzle
// We need to find where it is.
// Based on Dockerfile: COPY . . -> it should be at /app/drizzle
const migrationsFolder = path.join(ROOT_DIR, 'drizzle');
const migrationsFolder = path.join(ROOT_DIR, "drizzle");
migrate(db, { migrationsFolder });
console.log('Database migrations completed successfully.');
console.log("Database migrations completed successfully.");
// Re-apply database configuration after migration
// This ensures journal_mode is set to DELETE even if migration changed it
// or if the database file already existed with WAL mode
// This is critical for NTFS/FUSE filesystem compatibility
configureDatabase(sqlite);
console.log('Database configuration applied (NTFS/FUSE compatible mode).');
console.log("Database configuration applied (NTFS/FUSE compatible mode).");
// Check for legacy data files and run data migration if found
const { runMigration: runDataMigration } = await import('../services/migrationService');
const { VIDEOS_DATA_PATH, COLLECTIONS_DATA_PATH, STATUS_DATA_PATH } = await import('../config/paths');
const fs = await import('fs-extra');
const { runMigration: runDataMigration } = await import(
"../services/migrationService"
);
const { VIDEOS_DATA_PATH, COLLECTIONS_DATA_PATH, STATUS_DATA_PATH } =
await import("../config/paths");
const fs = await import("fs-extra");
// Hardcoded path for settings as in migrationService
const SETTINGS_DATA_PATH = path.join(path.dirname(VIDEOS_DATA_PATH), 'settings.json');
const SETTINGS_DATA_PATH = path.join(
path.dirname(VIDEOS_DATA_PATH),
"settings.json"
);
const hasLegacyData =
fs.existsSync(VIDEOS_DATA_PATH) ||
fs.existsSync(COLLECTIONS_DATA_PATH) ||
fs.existsSync(STATUS_DATA_PATH) ||
const hasLegacyData =
fs.existsSync(VIDEOS_DATA_PATH) ||
fs.existsSync(COLLECTIONS_DATA_PATH) ||
fs.existsSync(STATUS_DATA_PATH) ||
fs.existsSync(SETTINGS_DATA_PATH);
if (hasLegacyData) {
console.log('Legacy data files found. Running data migration...');
console.log("Legacy data files found. Running data migration...");
await runDataMigration();
} else {
console.log('No legacy data files found. Skipping data migration.');
console.log("No legacy data files found. Skipping data migration.");
}
} catch (error) {
console.error('Error running database migrations:', error);
console.error("Error running database migrations:", error);
// Don't throw, as we might want the app to start even if migration fails (though it might be broken)
// But for initial setup, it's critical.
throw error;