-- +goose Up
UPDATE fm_reports fr
JOIN helicopters h ON h.id = fr.helicopter_id
SET fr.report_code = CONCAT(
  UPPER(TRIM(h.identifier)),
  '-',
  LPAD(CAST(SUBSTRING_INDEX(fr.report_code, '-', -1) AS UNSIGNED), 4, '0')
)
WHERE fr.deleted_at IS NULL
  AND fr.report_code IS NOT NULL
  AND fr.report_code <> ''
  AND h.identifier IS NOT NULL
  AND TRIM(h.identifier) <> ''
  AND SUBSTRING_INDEX(fr.report_code, '-', -1) REGEXP '^[0-9]+$'; 

-- +goose Down
UPDATE fm_reports fr
JOIN helicopters h ON h.id = fr.helicopter_id
SET fr.report_code = CONCAT(
  UPPER(TRIM(h.type)),
  '-',
  LPAD(CAST(SUBSTRING_INDEX(fr.report_code, '-', -1) AS UNSIGNED), 4, '0')
)
WHERE fr.deleted_at IS NULL
  AND fr.report_code IS NOT NULL
  AND fr.report_code <> ''
  AND h.type IS NOT NULL
  AND TRIM(h.type) <> ''
  AND SUBSTRING_INDEX(fr.report_code, '-', -1) REGEXP '^[0-9]+$';
