-- set working set id
SET @workingset_id = 2450;

-- save oldest branch created_at value to @oldest_branch_created_at
select min(br.created_at)
into @oldest_branch_created_at
from object_reference ref
         join object br on br.id = ref.to_id
where ref.from_id = @workingset_id
  and br.type_id = 36;

-- update working set created_at
UPDATE object
SET created_at = @oldest_branch_created_at
WHERE object.id = @workingset_id;

UPDATE object_revision
SET created_at = @oldest_branch_created_at
WHERE object_id = @workingset_id;

-- update associations created at
UPDATE object assoc
    JOIN object_reference r on assoc.id = r.assoc_id
    JOIN object br_bl on r.to_id = br_bl.id
SET assoc.created_at = @oldest_branch_created_at
WHERE br_bl.type_id in (36, 12)
  and r.from_id = @workingset_id;

UPDATE object_revision rev
    JOIN object_reference r on rev.object_id = r.assoc_id
    JOIN object br_bl on r.to_id = br_bl.id
SET rev.created_at = @oldest_branch_created_at
WHERE br_bl.type_id in (36, 12)
  and r.from_id = @workingset_id;
