export class AddProjectIdMigration {
    version = '32.0';
    name = 'add-project-id';
    description = 'Add project_id to memory_item for project-scoped memory (Issue #81)';
    columnExists(db, table, column) {
        const rows = db.prepare(`PRAGMA table_info(${table})`).all();
        return rows.some(r => r.name === column);
    }
    indexExists(db, indexName) {
        const row = db
            .prepare(`SELECT name FROM sqlite_master WHERE type='index' AND name=?`)
            .get(indexName);
        return !!row;
    }
    async validateBefore(_db) { }
    async up(db) {
        if (!this.columnExists(db, 'memory_item', 'project_id')) {
            db.exec(`ALTER TABLE memory_item ADD COLUMN project_id TEXT`);
        }
        if (!this.indexExists(db, 'idx_memory_item_project_id_type')) {
            db.exec(`
        CREATE INDEX idx_memory_item_project_id_type
          ON memory_item(project_id, type)
          WHERE project_id IS NOT NULL
      `);
        }
    }
    async down(db) {
        db.exec('DROP INDEX IF EXISTS idx_memory_item_project_id_type');
    }
    async validateAfter(db) {
        if (!this.columnExists(db, 'memory_item', 'project_id')) {
            throw new Error('project_id column was not created');
        }
        if (!this.indexExists(db, 'idx_memory_item_project_id_type')) {
            throw new Error('idx_memory_item_project_id_type was not created');
        }
    }
}
export default AddProjectIdMigration;
//# sourceMappingURL=032-add-project-id.js.map