AI rule node: make name of the AI settings unique within the scope of the tenant

This commit is contained in:
Dmytro Skarzhynets 2025-05-16 15:26:43 +03:00
parent 18c75998ef
commit e695ce3283
No known key found for this signature in database
GPG Key ID: 2B51652F224037DF
4 changed files with 12 additions and 4 deletions

View File

@ -22,5 +22,6 @@ CREATE TABLE ai_settings (
name VARCHAR(255) NOT NULL,
provider VARCHAR(255) NOT NULL,
model VARCHAR(255) NOT NULL,
api_key VARCHAR(1000) NOT NULL
api_key VARCHAR(1000) NOT NULL,
CONSTRAINT ai_settings_name_unq_key UNIQUE (tenant_id, name)
);

View File

@ -58,7 +58,7 @@ public final class AiSettings extends BaseData<AiSettingsId> implements HasTenan
@Schema(
requiredMode = Schema.RequiredMode.REQUIRED,
accessMode = Schema.AccessMode.READ_WRITE,
description = "Human-readable name of the AI settings",
description = "Human-readable name of the AI settings; must be unique within the scope of the tenant",
example = "Default AI Settings"
)
String name;

View File

@ -28,6 +28,7 @@ import org.thingsboard.server.common.data.page.PageLink;
import java.util.Optional;
import static org.thingsboard.server.dao.entity.AbstractEntityService.checkConstraintViolation;
import static org.thingsboard.server.dao.service.Validator.validatePageLink;
@Service
@ -38,7 +39,12 @@ class AiSettingsServiceImpl implements AiSettingsService {
@Override
public AiSettings save(AiSettings aiSettings) {
return aiSettingsDao.saveAndFlush(aiSettings.getTenantId(), aiSettings);
try {
return aiSettingsDao.saveAndFlush(aiSettings.getTenantId(), aiSettings);
} catch (Exception e) {
checkConstraintViolation(e, "ai_settings_name_unq_key", "AI settings record with such name already exists!");
throw e;
}
}
@Override

View File

@ -957,5 +957,6 @@ CREATE TABLE IF NOT EXISTS ai_settings (
name VARCHAR(255) NOT NULL,
provider VARCHAR(255) NOT NULL,
model VARCHAR(255) NOT NULL,
api_key VARCHAR(1000) NOT NULL
api_key VARCHAR(1000) NOT NULL,
CONSTRAINT ai_settings_name_unq_key UNIQUE (tenant_id, name)
);