fix: submitConfig 处理同版本 PENDING 记录的唯一约束冲突,已存在则更新而非新增
This commit is contained in:
@@ -70,7 +70,7 @@ public class ConfigServiceImpl implements ConfigService {
|
||||
return sameHash;
|
||||
}
|
||||
|
||||
// 存储到抽象存储层
|
||||
// 写入存储
|
||||
try {
|
||||
storageService.store(storagePath, configJson);
|
||||
} catch (Exception e) {
|
||||
@@ -78,12 +78,34 @@ public class ConfigServiceImpl implements ConfigService {
|
||||
throw new RuntimeException("存储配置文件失败: " + e.getMessage(), e);
|
||||
}
|
||||
|
||||
long fileSize = configJson.getBytes(StandardCharsets.UTF_8).length;
|
||||
|
||||
// 同一 (site_id, version) 已有 PENDING 记录 → 更新而非新增(避免唯一约束冲突)
|
||||
SiteConfig existingPending = siteConfigMapper.selectOne(
|
||||
new com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper<SiteConfig>()
|
||||
.eq(SiteConfig::getSiteId, siteId)
|
||||
.eq(SiteConfig::getVersion, version)
|
||||
.eq(SiteConfig::getStatus, ConfigStatus.PENDING)
|
||||
.eq(SiteConfig::getDeleted, 0));
|
||||
if (existingPending != null) {
|
||||
// 删除旧存储文件
|
||||
storageService.delete(existingPending.getStoragePath());
|
||||
existingPending.setConfigHash(configHash);
|
||||
existingPending.setStoragePath(storagePath);
|
||||
existingPending.setFileSize(fileSize);
|
||||
existingPending.setSchemaVersion(schemaVersion != null ? schemaVersion : existingPending.getSchemaVersion());
|
||||
existingPending.setSubmitterId(submitterId);
|
||||
siteConfigMapper.updateById(existingPending);
|
||||
log.info("Config updated: siteId={}, version={}, id={}, hash={}", siteId, version, existingPending.getId(), configHash);
|
||||
return existingPending;
|
||||
}
|
||||
|
||||
SiteConfig config = new SiteConfig();
|
||||
config.setSiteId(siteId);
|
||||
config.setVersion(version); // 客户端提交的版本号,仅供参考
|
||||
config.setVersion(version);
|
||||
config.setConfigHash(configHash);
|
||||
config.setStoragePath(storagePath);
|
||||
config.setFileSize((long) configJson.getBytes(StandardCharsets.UTF_8).length);
|
||||
config.setFileSize(fileSize);
|
||||
config.setSchemaVersion(schemaVersion != null ? schemaVersion : "1.0.0");
|
||||
config.setStatus(ConfigStatus.PENDING);
|
||||
config.setSubmitterId(submitterId);
|
||||
|
||||
Reference in New Issue
Block a user