ALTER TABLE `diary_date_activities`
    ADD COLUMN `group_id` INT NULL AFTER `predefined_activity_id`,
    ADD CONSTRAINT `fk_diary_date_activities_group_id`
        FOREIGN KEY (`group_id`) REFERENCES `group`(`id`)
        ON DELETE SET NULL
        ON UPDATE CASCADE;

INSERT INTO `diary_date_activities` (
    `diary_date_id`,
    `is_timeblock`,
    `predefined_activity_id`,
    `group_id`,
    `duration`,
    `duration_text`,
    `order_id`,
    `created_at`,
    `updated_at`
)
SELECT
    d.`diary_date_id`,
    0 AS `is_timeblock`,
    ga.`custom_activity` AS `predefined_activity_id`,
    ga.`group_id`,
    ga.`duration`,
    ga.`duration_text`,
    (d.`order_id` * 100) + COALESCE(ga.`order_id`, 1) AS `order_id`,
    COALESCE(ga.`created_at`, NOW()) AS `created_at`,
    COALESCE(ga.`updated_at`, NOW()) AS `updated_at`
FROM `group_activity` ga
INNER JOIN `diary_date_activities` d ON d.`id` = ga.`diary_date_activity`;
