fix: pre-build FTS5 index for Docker compatibility

- Add FTS5 pre-creation in fetch-templates.ts before data import
- Create prebuild-fts5.ts script to ensure FTS5 tables exist
- Improve logging in template-repository.ts for better debugging
- Add npm script 'prebuild:fts5' for manual FTS5 setup

This ensures template search works consistently in Docker mode
where runtime FTS5 table creation might fail due to permissions.

🤖 Generated with [Claude Code](https://claude.ai/code)

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
czlonkowski
2025-07-10 12:49:25 +02:00
parent e237669458
commit c9aadfcb30
5 changed files with 231 additions and 33 deletions

View File

@@ -29,6 +29,49 @@ async function fetchTemplates() {
const schema = fs.readFileSync(path.join(__dirname, '../../src/database/schema.sql'), 'utf8');
db.exec(schema);
// Pre-create FTS5 tables if supported
const hasFTS5 = db.checkFTS5Support();
if (hasFTS5) {
console.log('🔍 Creating FTS5 tables for template search...');
try {
// Create FTS5 virtual table
db.exec(`
CREATE VIRTUAL TABLE IF NOT EXISTS templates_fts USING fts5(
name, description, content=templates
);
`);
// Create triggers to keep FTS5 in sync
db.exec(`
CREATE TRIGGER IF NOT EXISTS templates_ai AFTER INSERT ON templates BEGIN
INSERT INTO templates_fts(rowid, name, description)
VALUES (new.id, new.name, new.description);
END;
`);
db.exec(`
CREATE TRIGGER IF NOT EXISTS templates_au AFTER UPDATE ON templates BEGIN
UPDATE templates_fts SET name = new.name, description = new.description
WHERE rowid = new.id;
END;
`);
db.exec(`
CREATE TRIGGER IF NOT EXISTS templates_ad AFTER DELETE ON templates BEGIN
DELETE FROM templates_fts WHERE rowid = old.id;
END;
`);
console.log('✅ FTS5 tables created successfully\n');
} catch (error) {
console.log('⚠️ Failed to create FTS5 tables:', error);
console.log(' Template search will use LIKE fallback\n');
}
} else {
console.log(' FTS5 not supported in this SQLite build');
console.log(' Template search will use LIKE queries\n');
}
// Create service
const service = new TemplateService(db);