added ON DELETE CASCADE and improvements
This commit is contained in:
		
							parent
							
								
									ffa00bd94a
								
							
						
					
					
						commit
						15d53a95cc
					
				@ -130,15 +130,10 @@ ALTER TABLE notification_request ALTER COLUMN info SET DATA TYPE varchar(1000000
 | 
			
		||||
CREATE TABLE IF NOT EXISTS alarm_types (
 | 
			
		||||
    tenant_id uuid NOT NULL,
 | 
			
		||||
    type varchar(255) NOT NULL,
 | 
			
		||||
    CONSTRAINT tenant_id_type_unq_key UNIQUE (tenant_id, type)
 | 
			
		||||
    CONSTRAINT tenant_id_type_unq_key UNIQUE (tenant_id, type),
 | 
			
		||||
    CONSTRAINT fk_entity_tenant_id FOREIGN KEY (tenant_id) REFERENCES tenant(id) ON DELETE CASCADE
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
-- Activate the pg_trgm module for trigram-based searches.
 | 
			
		||||
CREATE EXTENSION IF NOT EXISTS pg_trgm;
 | 
			
		||||
 | 
			
		||||
-- Create a GIN index on the `type` column. (for optimizing `ILIKE` in search query)
 | 
			
		||||
CREATE INDEX IF NOT EXISTS idx_gin_alarm_types_type ON alarm_types USING GIN(type gin_trgm_ops);
 | 
			
		||||
 | 
			
		||||
INSERT INTO alarm_types (tenant_id, type) SELECT DISTINCT tenant_id, type FROM alarm ON CONFLICT (tenant_id, type) DO NOTHING;
 | 
			
		||||
 | 
			
		||||
ALTER TABLE widget_type
 | 
			
		||||
 | 
			
		||||
@ -77,7 +77,7 @@ public interface AlarmService extends EntityDaoService {
 | 
			
		||||
 | 
			
		||||
    AlarmApiCallResult delAlarm(TenantId tenantId, AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
    AlarmApiCallResult delAlarm(TenantId tenantId, AlarmId alarmId, boolean deleteAlarmType);
 | 
			
		||||
    AlarmApiCallResult delAlarm(TenantId tenantId, AlarmId alarmId, boolean checkAndDeleteAlarmType);
 | 
			
		||||
 | 
			
		||||
    void delAlarmTypes(TenantId tenantId, Set<String> types);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -100,5 +100,5 @@ public interface AlarmDao extends Dao<Alarm> {
 | 
			
		||||
 | 
			
		||||
    PageData<EntitySubtype> findTenantAlarmTypes(UUID tenantId, PageLink pageLink);
 | 
			
		||||
 | 
			
		||||
    boolean removeAlarmTypes(UUID tenantId, Set<String> types);
 | 
			
		||||
    boolean removeAlarmTypesIfNoAlarmsPresent(UUID tenantId, Set<String> types);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -223,7 +223,7 @@ public class BaseAlarmService extends AbstractCachedEntityService<TenantId, Page
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    @Transactional
 | 
			
		||||
    public AlarmApiCallResult delAlarm(TenantId tenantId, AlarmId alarmId, boolean deleteAlarmType) {
 | 
			
		||||
    public AlarmApiCallResult delAlarm(TenantId tenantId, AlarmId alarmId, boolean checkAndDeleteAlarmType) {
 | 
			
		||||
        log.debug("Deleting Alarm Id: {}", alarmId);
 | 
			
		||||
        AlarmInfo alarm = alarmDao.findAlarmInfoById(tenantId, alarmId.getId());
 | 
			
		||||
        if (alarm == null) {
 | 
			
		||||
@ -233,7 +233,7 @@ public class BaseAlarmService extends AbstractCachedEntityService<TenantId, Page
 | 
			
		||||
            alarmDao.removeById(tenantId, alarm.getUuidId());
 | 
			
		||||
            eventPublisher.publishEvent(DeleteEntityEvent.builder().tenantId(tenantId)
 | 
			
		||||
                    .entityId(alarmId).entity(alarm).build());
 | 
			
		||||
            if (deleteAlarmType) {
 | 
			
		||||
            if (checkAndDeleteAlarmType) {
 | 
			
		||||
                delAlarmTypes(tenantId, Collections.singleton(alarm.getType()));
 | 
			
		||||
            }
 | 
			
		||||
            return AlarmApiCallResult.builder().alarm(alarm).deleted(true).successful(true).build();
 | 
			
		||||
@ -243,7 +243,7 @@ public class BaseAlarmService extends AbstractCachedEntityService<TenantId, Page
 | 
			
		||||
    @Override
 | 
			
		||||
    @Transactional
 | 
			
		||||
    public void delAlarmTypes(TenantId tenantId, Set<String> types) {
 | 
			
		||||
        if (!types.isEmpty() && alarmDao.removeAlarmTypes(tenantId.getId(), types)) {
 | 
			
		||||
        if (!types.isEmpty() && alarmDao.removeAlarmTypesIfNoAlarmsPresent(tenantId.getId(), types)) {
 | 
			
		||||
            publishEvictEvent(new AlarmTypesCacheEvictEvent(tenantId));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -354,6 +354,6 @@ public interface AlarmRepository extends JpaRepository<AlarmEntity, UUID> {
 | 
			
		||||
    @Transactional
 | 
			
		||||
    @Modifying
 | 
			
		||||
    @Query(value = "DELETE FROM alarm_types AS at WHERE NOT EXISTS (SELECT 1 FROM alarm AS a WHERE a.tenant_id = at.tenant_id AND a.type = at.type) AND at.tenant_id = :tenantId AND at.type IN (:types)", nativeQuery = true)
 | 
			
		||||
    int deleteTypeIfNoOneAlarmExists(@Param("tenantId") UUID tenantId, @Param("types") Set<String> types);
 | 
			
		||||
    int deleteTypeIfNoAlarmsExist(@Param("tenantId") UUID tenantId, @Param("types") Set<String> types);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -383,8 +383,8 @@ public class JpaAlarmDao extends JpaAbstractDao<AlarmEntity, Alarm> implements A
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean removeAlarmTypes(UUID tenantId, Set<String> types) {
 | 
			
		||||
        return alarmRepository.deleteTypeIfNoOneAlarmExists(tenantId, types) > 0;
 | 
			
		||||
    public boolean removeAlarmTypesIfNoAlarmsPresent(UUID tenantId, Set<String> types) {
 | 
			
		||||
        return alarmRepository.deleteTypeIfNoAlarmsExist(tenantId, types) > 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static String getPropagationTypes(AlarmPropagationInfo ap) {
 | 
			
		||||
 | 
			
		||||
@ -115,9 +115,3 @@ CREATE INDEX IF NOT EXISTS idx_notification_id ON notification(id);
 | 
			
		||||
CREATE INDEX IF NOT EXISTS idx_notification_recipient_id_created_time ON notification(recipient_id, created_time DESC);
 | 
			
		||||
 | 
			
		||||
CREATE INDEX IF NOT EXISTS idx_notification_recipient_id_unread ON notification(recipient_id) WHERE status <> 'READ';
 | 
			
		||||
 | 
			
		||||
-- Activate the pg_trgm module for trigram-based searches.
 | 
			
		||||
CREATE EXTENSION IF NOT EXISTS pg_trgm;
 | 
			
		||||
 | 
			
		||||
-- Create a GIN index on the `type` column. (for optimizing `ILIKE` in search query)
 | 
			
		||||
CREATE INDEX IF NOT EXISTS idx_gin_alarm_types_type ON alarm_types USING GIN(type gin_trgm_ops);
 | 
			
		||||
 | 
			
		||||
@ -64,12 +64,6 @@ CREATE TABLE IF NOT EXISTS alarm (
 | 
			
		||||
    cleared boolean
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE IF NOT EXISTS alarm_types (
 | 
			
		||||
    tenant_id uuid NOT NULL,
 | 
			
		||||
    type varchar(255) NOT NULL,
 | 
			
		||||
    CONSTRAINT tenant_id_type_unq_key UNIQUE (tenant_id, type)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE IF NOT EXISTS alarm_comment (
 | 
			
		||||
    id uuid NOT NULL,
 | 
			
		||||
    created_time bigint NOT NULL,
 | 
			
		||||
@ -862,3 +856,10 @@ CREATE TABLE IF NOT EXISTS user_settings (
 | 
			
		||||
    CONSTRAINT fk_user_id FOREIGN KEY (user_id) REFERENCES tb_user(id) ON DELETE CASCADE,
 | 
			
		||||
    CONSTRAINT user_settings_pkey PRIMARY KEY (user_id, type)
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
CREATE TABLE IF NOT EXISTS alarm_types (
 | 
			
		||||
    tenant_id uuid NOT NULL,
 | 
			
		||||
    type varchar(255) NOT NULL,
 | 
			
		||||
    CONSTRAINT tenant_id_type_unq_key UNIQUE (tenant_id, type),
 | 
			
		||||
    CONSTRAINT fk_entity_tenant_id FOREIGN KEY (tenant_id) REFERENCES tenant(id) ON DELETE CASCADE
 | 
			
		||||
);
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user