do $$ 
declare
    user_id uuid;
    changeset_id int4;
begin
    select tu.userid
    into user_id
    from vclib.t_users tu 
    join vclib.t_identity ti on tu.identity = ti.identityid
    where ti.identityname = 'Administrator'
    limit 1;
    if user_id is null then
        select userid into user_id
        from vclib.t_users
        limit 1;
    end if;
    call public.sp_generateidforentity('VCLib.CHANGESET', 'PrimaryKey', 1, changeset_id);
    insert into vclib.t_changesets (id, timestamp, userid)
    values (changeset_id, now(), user_id);
    perform fn_copydatatohistory('t_ehr_type_medical_devices', 't_ehr_type_medical_devices_history', changeset_id);
end $$;