-- =====================================================================
--  Migration: Lien bank -> journal_entries
--  Date: 2025-08-26 (Phase 1.1)
--  Ajoute colonne de liaison optionnelle pour traçabilité.
-- =====================================================================
-- Ajout colonne journal_entry_id si absente
SET @col := (SELECT COUNT(1) FROM information_schema.columns WHERE table_schema = DATABASE() AND table_name='bank' AND column_name='journal_entry_id');
SET @sql := IF(@col=0,'ALTER TABLE bank ADD COLUMN journal_entry_id BIGINT NULL AFTER id_bank','SELECT 1');
PREPARE st FROM @sql; EXECUTE st; DEALLOCATE PREPARE st;

-- Ajout index si absent
SET @idx := (SELECT COUNT(1) FROM information_schema.statistics WHERE table_schema = DATABASE() AND table_name='bank' AND index_name='idx_bank_journal');
SET @sql2 := IF(@idx=0,'ALTER TABLE bank ADD INDEX idx_bank_journal (journal_entry_id)','SELECT 1');
PREPARE st2 FROM @sql2; EXECUTE st2; DEALLOCATE PREPARE st2;
