-- Banner keeps its historical h5 payload for App compatibility while these
-- columns retain the owning template relation for validation and lifecycle cleanup.
ALTER TABLE admin_app_banners
  ADD COLUMN target_type VARCHAR(32) NOT NULL DEFAULT '' AFTER banner_type,
  ADD COLUMN activity_template_id VARCHAR(64) NOT NULL DEFAULT '' AFTER target_type,
  ADD COLUMN activity_template_code VARCHAR(64) NOT NULL DEFAULT '' AFTER activity_template_id;

UPDATE admin_app_banners
SET target_type = banner_type
WHERE target_type = '';

-- Template disable/archive uses this bounded equality lookup; app_code first
-- preserves tenant isolation and prevents a full banner scan.
CREATE INDEX idx_admin_app_banner_activity_target
  ON admin_app_banners (app_code, target_type, activity_template_id, status);
