#!/usr/bin/env node
/**
* Document Management System Migration
* Adds document tables to existing WorkflowMCP SQLite database
* Usage: node migrate-documents.js
*/
import sqlite3 from 'sqlite3';
import { open } from 'sqlite';
import fs from 'fs';
import path from 'path';
import { fileURLToPath } from 'url';
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
// Database path
const DB_PATH = path.resolve(__dirname, '../../data/workflow.db');
const SCHEMA_PATH = path.resolve(__dirname, 'schema.sql');
async function migrateDocuments() {
console.log('π Starting Document Management System Migration...');
if (!fs.existsSync(DB_PATH)) {
console.error('β Database not found:', DB_PATH);
process.exit(1);
}
const db = await open({
filename: DB_PATH,
driver: sqlite3.Database
});
try {
// Enable foreign keys
await db.exec('PRAGMA foreign_keys = ON');
console.log('π Reading schema file...');
const schema = fs.readFileSync(SCHEMA_PATH, 'utf8');
// Extract document-related DDL from schema
const documentSchemaStart = schema.indexOf('-- Document Management System');
const documentSchema = schema.substring(documentSchemaStart);
// Split into individual statements
const statements = documentSchema
.split(';')
.map(stmt => stmt.trim())
.filter(stmt => stmt && !stmt.startsWith('--'));
console.log('π§ Creating document management tables...');
// Execute each statement
for (const [index, stmt] of statements.entries()) {
try {
if (stmt.trim()) {
await db.exec(stmt + ';');
console.log(`β
Statement ${index + 1} executed successfully`);
}
} catch (error) {
console.warn(`β οΈ Statement ${index + 1} skipped (might already exist):`, error.message);
}
}
console.log('π Verifying table creation...');
// Check if tables were created
const tables = await db.all(`
SELECT name FROM sqlite_master
WHERE type='table' AND name LIKE '%document%'
`);
console.log('π Document tables created:');
tables.forEach(table => {
console.log(` β
${table.name}`);
});
// Check if FTS table exists
const ftsCheck = await db.get(`
SELECT name FROM sqlite_master
WHERE type='table' AND name='documents_fts'
`);
if (ftsCheck) {
console.log('π Full-text search index created successfully');
}
console.log('π Document Management System migration completed successfully!');
return true;
} catch (error) {
console.error('β Migration failed:', error);
return false;
} finally {
db.close();
}
}
// Import existing test documents
async function importExistingDocs() {
console.log('π Importing existing test documents...');
const db = await open({
filename: DB_PATH,
driver: sqlite3.Database
});
try {
const insertDoc = await db.prepare(`
INSERT INTO documents (title, content, doc_type, category, file_path, tags, summary, status)
VALUES (?, ?, ?, ?, ?, ?, ?, ?)
`);
// Import Phase 2.6 test guide
const testGuidePath = path.resolve(__dirname, '../../docs/PHASE_2.6_TEST_GUIDE.md');
if (fs.existsSync(testGuidePath)) {
const testGuideContent = fs.readFileSync(testGuidePath, 'utf8');
await insertDoc.run(
'Phase 2.6 SvelteKit Dashboard ν
μ€νΈ κ°μ΄λ',
testGuideContent,
'test_guide',
'phase_2.6',
testGuidePath,
JSON.stringify(['testing', 'sveltekit', 'phase_2.6', 'dashboard']),
'SvelteKit μΉ λμ보λμ λͺ¨λ κΈ°λ₯μ κ²μ¦νκ³ SQLite λ°μ΄ν°λ² μ΄μ€μμ μ°λ ν
μ€νΈ',
'approved'
);
console.log('β
Imported: Phase 2.6 Test Guide');
}
// Import test results
const testResultsPath = path.resolve(__dirname, '../../docs/PHASE_2.6_TEST_RESULTS.md');
if (fs.existsSync(testResultsPath)) {
const testResultsContent = fs.readFileSync(testResultsPath, 'utf8');
await insertDoc.run(
'Phase 2.6 SvelteKit Dashboard ν
μ€νΈ κ²°κ³Ό λ³΄κ³ μ',
testResultsContent,
'test_results',
'phase_2.6',
testResultsPath,
JSON.stringify(['testing', 'results', 'phase_2.6', 'sveltekit']),
'ν΅κ³Όμ¨ 85.7%λ‘ ν΅μ¬ κΈ°λ₯μ΄ λͺ¨λ μ μ μλνλ κ²μ νμΈ',
'approved'
);
console.log('β
Imported: Phase 2.6 Test Results');
}
// Import quick checklist
const checklistPath = path.resolve(__dirname, '../../docs/QUICK_TEST_CHECKLIST.md');
if (fs.existsSync(checklistPath)) {
const checklistContent = fs.readFileSync(checklistPath, 'utf8');
await insertDoc.run(
'Phase 2.6 λΉ λ₯Έ ν
μ€νΈ 체ν¬λ¦¬μ€νΈ',
checklistContent,
'checklist',
'phase_2.6',
checklistPath,
JSON.stringify(['testing', 'checklist', 'quick_test']),
'5λΆ λ΄ ν΅μ¬ κΈ°λ₯ νμΈμ μν 10κ° μ²΄ν¬ν¬μΈνΈ',
'approved'
);
console.log('β
Imported: Quick Test Checklist');
}
// Show import results
const docCount = await db.get('SELECT COUNT(*) as count FROM documents');
console.log(`π Total documents in database: ${docCount.count}`);
return true;
} catch (error) {
console.error('β Document import failed:', error);
return false;
} finally {
db.close();
}
}
// Main execution
async function main() {
const migrationSuccess = await migrateDocuments();
if (migrationSuccess) {
await importExistingDocs();
console.log('\nπ― Next steps:');
console.log('1. Update sqlite-server.js to include document management tools');
console.log('2. Add document management UI to SvelteKit dashboard');
console.log('3. Test document CRUD operations');
}
}
main().catch(console.error);