[
  {
    "type": "alter_table",
    "description": "Add typeNum column to staff_chat_attachments for store scoping",
    "database": "{{store}}",
    "check_query": "SHOW COLUMNS FROM staff_chat_attachments LIKE 'typeNum'",
    "sql": "ALTER TABLE `staff_chat_attachments` ADD COLUMN `typeNum` varchar(10) DEFAULT NULL COMMENT 'Store identifier' AFTER `messageId`"
  },
  {
    "type": "alter_table",
    "description": "Add channelId column to staff_chat_attachments for ownership validation",
    "database": "{{store}}",
    "check_query": "SHOW COLUMNS FROM staff_chat_attachments LIKE 'channelId'",
    "sql": "ALTER TABLE `staff_chat_attachments` ADD COLUMN `channelId` int(10) unsigned DEFAULT NULL COMMENT 'Channel the attachment belongs to' AFTER `typeNum`"
  },
  {
    "type": "alter_table",
    "description": "Add uploadedByUserId column to staff_chat_attachments",
    "database": "{{store}}",
    "check_query": "SHOW COLUMNS FROM staff_chat_attachments LIKE 'uploadedByUserId'",
    "sql": "ALTER TABLE `staff_chat_attachments` ADD COLUMN `uploadedByUserId` int(10) unsigned DEFAULT NULL COMMENT 'kiosk_users.users.id - who uploaded' AFTER `channelId`"
  },
  {
    "type": "alter_table",
    "description": "Add index on channelId for staff_chat_attachments",
    "database": "{{store}}",
    "check_query": "SELECT COUNT(*) FROM information_schema.STATISTICS WHERE TABLE_SCHEMA = DATABASE() AND TABLE_NAME = 'staff_chat_attachments' AND INDEX_NAME = 'idx_channelId'",
    "check_value": "0",
    "sql": "ALTER TABLE `staff_chat_attachments` ADD INDEX `idx_channelId` (`channelId`)"
  },
  {
    "type": "alter_table",
    "description": "Backfill typeNum and channelId from staff_chat_messages for existing attachments",
    "database": "{{store}}",
    "check_query": "SELECT COUNT(*) FROM staff_chat_attachments WHERE typeNum IS NULL AND messageId IN (SELECT id FROM staff_chat_messages)",
    "check_value_gt": "0",
    "sql": "UPDATE staff_chat_attachments a INNER JOIN staff_chat_messages m ON a.messageId = m.id SET a.typeNum = m.typeNum, a.channelId = m.channelId, a.uploadedByUserId = m.senderUserId WHERE a.typeNum IS NULL"
  }
]
