ALTER TABLE sys_user
    ADD COLUMN effective_start_at DATETIME DEFAULT NULL AFTER status,
    ADD COLUMN effective_end_at DATETIME DEFAULT NULL AFTER effective_start_at;

CREATE TABLE IF NOT EXISTS user_role_history (
    id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
    tenant_id BIGINT UNSIGNED NOT NULL,
    user_id BIGINT UNSIGNED NOT NULL,
    old_role_id BIGINT UNSIGNED DEFAULT NULL,
    new_role_id BIGINT UNSIGNED NOT NULL,
    action_type VARCHAR(32) NOT NULL COMMENT 'ASSIGN/REMOVE/REPLACE',
    action_reason VARCHAR(255) DEFAULT NULL,
    created_by BIGINT UNSIGNED NOT NULL DEFAULT 0,
    created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
    KEY idx_user_time (user_id, created_at)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
