/**
 * Migration: 031 — memory_item soft delete columns
 * Version: 31.0
 */
export class SoftDeleteFieldsMigration {
    version = '31.0';
    name = 'soft-delete-fields';
    description = 'Add is_deleted, deleted_at to memory_item for TTL soft delete (FR-006/007)';
    tableExists(db, tableName) {
        const result = db
            .prepare(`SELECT name FROM sqlite_master WHERE type='table' AND name=?`)
            .get(tableName);
        return !!result;
    }
    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.tableExists(db, 'memory_item')) {
            return;
        }
        if (!this.columnExists(db, 'memory_item', 'is_deleted')) {
            db.exec(`ALTER TABLE memory_item ADD COLUMN is_deleted BOOLEAN DEFAULT FALSE NOT NULL`);
        }
        if (!this.columnExists(db, 'memory_item', 'deleted_at')) {
            db.exec(`ALTER TABLE memory_item ADD COLUMN deleted_at TEXT`);
        }
        db.exec(`
      CREATE INDEX IF NOT EXISTS idx_memory_item_is_deleted_active
        ON memory_item(is_deleted)
        WHERE COALESCE(is_deleted, 0) = 0;
    `);
    }
    async down(db) {
        db.exec('DROP INDEX IF EXISTS idx_memory_item_is_deleted_active');
        if (this.tableExists(db, 'memento_schema_version')) {
            db.prepare('DELETE FROM memento_schema_version WHERE version = ?').run('31.0');
        }
    }
    async validateAfter(db) {
        if (!this.tableExists(db, 'memory_item')) {
            return;
        }
        if (!this.columnExists(db, 'memory_item', 'is_deleted')) {
            throw new Error('is_deleted column was not created');
        }
        if (!this.columnExists(db, 'memory_item', 'deleted_at')) {
            throw new Error('deleted_at column was not created');
        }
        if (!this.indexExists(db, 'idx_memory_item_is_deleted_active')) {
            throw new Error('idx_memory_item_is_deleted_active was not created');
        }
    }
}
export default SoftDeleteFieldsMigration;
//# sourceMappingURL=031-soft-delete-fields.js.map