-- Add can_export column to role_permissions table if it doesn't exist
-- Run this migration if you get errors about can_export column

-- Check if column exists before adding (MySQL doesn't have IF NOT EXISTS for columns)
SET @dbname = DATABASE();
SET @tablename = 'role_permissions';
SET @columnname = 'can_export';
SET @preparedStatement = (SELECT IF(
  (
    SELECT COUNT(*) FROM INFORMATION_SCHEMA.COLUMNS
    WHERE
      TABLE_SCHEMA = @dbname
      AND TABLE_NAME = @tablename
      AND COLUMN_NAME = @columnname
  ) > 0,
  'SELECT 1',
  'ALTER TABLE role_permissions ADD COLUMN can_export TINYINT(1) DEFAULT 0 AFTER can_delete'
));

PREPARE alterIfNotExists FROM @preparedStatement;
EXECUTE alterIfNotExists;
DEALLOCATE PREPARE alterIfNotExists;

-- Update any NULL values to 0
UPDATE role_permissions SET can_export = 0 WHERE can_export IS NULL;
