--
-- Migration script to fix account mappings table
-- Fixes the unique constraint to allow multiple accounts per CA-3 line
--

-- Drop the old unique constraint that prevents multiple accounts per line
ALTER TABLE `llx_declarationtva_account_mappings` 
DROP INDEX `uk_mapping_entity_line`;

-- Add new unique constraint that allows multiple accounts per line
-- but prevents duplicate account mappings
ALTER TABLE `llx_declarationtva_account_mappings` 
ADD UNIQUE KEY `uk_mapping_entity_line_account` (`entity`, `ca3_line`, `account_code`);

-- Add index on account_code for better performance
ALTER TABLE `llx_declarationtva_account_mappings` 
ADD INDEX `idx_account_code` (`account_code`);

-- Update the comment to reflect the new structure
ALTER TABLE `llx_declarationtva_account_mappings` 
MODIFY COLUMN `ca3_line` varchar(8) NOT NULL COMMENT 'A1, A2, A3, A4, A5, 08, 09, 9B, 17, 20, 21, 22, 25, 26, 28, 29';
