#!/usr/bin/env node /** * Migrate from JSON page cache to SQLite * This script reads the existing page-cache.json and imports it into SQLite */ import { CacheService } from '../services/cache-service.js'; import fs from 'fs/promises'; import path from 'path'; const JSON_CACHE_PATH = './data/page-cache.json'; const SQLITE_CACHE_PATH = './data/page-cache.db'; console.log('\nšŸ”„ Migrating page cache from JSON to SQLite...\n'); try { // Check if JSON cache exists try { await fs.access(JSON_CACHE_PATH); } catch (error) { console.log('ā„¹ļø No existing JSON cache found. Starting with empty SQLite database.'); // Initialize empty SQLite database const cacheService = new CacheService(SQLITE_CACHE_PATH); await cacheService.initialize(); cacheService.close(); console.log('\nāœ… Empty SQLite cache created!\n'); process.exit(0); } // Read JSON cache console.log('šŸ“– Reading JSON cache...'); const jsonData = await fs.readFile(JSON_CACHE_PATH, 'utf-8'); const jsonCache = JSON.parse(jsonData); const entries = Object.entries(jsonCache); console.log(` Found ${entries.length} entries\n`); // Check if SQLite database already exists let existingDb = false; try { await fs.access(SQLITE_CACHE_PATH); existingDb = true; console.log('āš ļø SQLite database already exists!'); console.log(' The existing database will be backed up and replaced.\n'); // Backup existing database const backupPath = `${SQLITE_CACHE_PATH}.backup.${Date.now()}`; await fs.copyFile(SQLITE_CACHE_PATH, backupPath); console.log(`āœ“ Backed up to: ${backupPath}\n`); // Delete existing database await fs.unlink(SQLITE_CACHE_PATH); } catch (error) { // Database doesn't exist, that's fine } // Initialize SQLite cache console.log('šŸ—„ļø Initializing SQLite database...'); const cacheService = new CacheService(SQLITE_CACHE_PATH); await cacheService.initialize(); // Bulk import entries console.log('šŸ“„ Importing entries...'); cacheService.bulkSet(entries); // Verify import const stats = cacheService.getStats(); console.log(`\nāœ… Migration complete!`); console.log(`\nšŸ“Š Statistics:`); console.log(` Total pages: ${stats.total}`); console.log(` Indexed pages: ${stats.indexed}`); console.log(`\n By Source:`); for (const source of stats.bySource) { console.log(` ${source.source.padEnd(10)} ${source.indexed} indexed / ${source.total} total`); } cacheService.close(); // Rename old JSON cache for safety const jsonBackupPath = `${JSON_CACHE_PATH}.migrated.${Date.now()}`; await fs.rename(JSON_CACHE_PATH, jsonBackupPath); console.log(`\nāœ“ Original JSON cache backed up to: ${jsonBackupPath}`); console.log(`\nšŸ’” Tip: You can safely delete the backup files once you verify everything works.\n`); } catch (error) { console.error('\nāŒ Migration failed:', error.message); console.error(error.stack); process.exit(1); }