#!/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);