ALTER TABLE custodies
  ADD COLUMN custody_name VARCHAR(180) NULL AFTER custody_number,
  ADD COLUMN last_edited_by_user_id BIGINT UNSIGNED NULL AFTER created_by_category,
  ADD COLUMN last_edited_by_name VARCHAR(80) NULL AFTER last_edited_by_user_id,
  ADD COLUMN last_edited_at DATETIME NULL AFTER last_edited_by_name,
  ADD KEY idx_custodies_name (custody_name),
  ADD CONSTRAINT fk_custodies_last_edited_user
    FOREIGN KEY (last_edited_by_user_id)
    REFERENCES users(id)
    ON DELETE SET NULL;

UPDATE custodies
SET custody_name = CONCAT('عهدة ', DATE_FORMAT(custody_date, '%Y-%m-%d'))
WHERE custody_name IS NULL OR TRIM(custody_name) = '';

ALTER TABLE custodies
  MODIFY custody_name VARCHAR(180) NOT NULL;
