delete from VCLib.T_CLIENT_MODULE_FORM cmf
using VCLib.T_CLIENT_MODULES cm
where cm.ClientModuleID = cmf.ClientModules
        and
    cm.ModuleType in ('Custom_DnDoctorClientModule', 'Custom_DispDoctorClientModule');

insert into VCLib.T_ROLE_CLIENT_MODULES(ClientModuleID,LoadOrder,RoleClientModuleID,RoleID)
select cmn.ClientModuleID, rcm.LoadOrder, gen_random_uuid(), rcm.RoleID
from VCLib.T_ROLE_CLIENT_MODULES rcm
join VCLib.T_CLIENT_MODULES cm on cm.ClientModuleID = rcm.ClientModuleID
join VCLib.T_CLIENT_MODULES cmn on cmn.ModuleType = 'Custom_' || cm.ModuleType
where cm.ModuleType in ( 'RecommendationClientModule', 'DispBaseClientModule', 'DnBaseClientModule', 'FondClientModule' );

delete from VCLib.T_ROLE_CLIENT_MODULES rcm1
where exists
	(
		select *
		from VCLib.T_ROLE_CLIENT_MODULES rcm2
		where rcm1.ClientModuleID = rcm2.ClientModuleID
				and
			rcm1.RoleID = rcm2.RoleID
				and
			rcm1.RoleClientModuleID < rcm2.RoleClientModuleID
	);
