chore(settings): drop installation service / migrations (#2528)
This commit is contained in:
@@ -1,91 +0,0 @@
|
||||
package org.booklore.service;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.model.dto.Installation;
|
||||
import org.booklore.model.entity.AppSettingEntity;
|
||||
import org.booklore.repository.AppSettingsRepository;
|
||||
import org.springframework.stereotype.Service;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.time.Instant;
|
||||
import java.util.UUID;
|
||||
|
||||
@Service
|
||||
@Slf4j
|
||||
public class InstallationService {
|
||||
|
||||
private static final String INSTALLATION_ID_KEY = "installation_id";
|
||||
|
||||
private final AppSettingsRepository appSettingsRepository;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
public InstallationService(AppSettingsRepository appSettingsRepository, ObjectMapper objectMapper) {
|
||||
this.appSettingsRepository = appSettingsRepository;
|
||||
this.objectMapper = objectMapper;
|
||||
}
|
||||
|
||||
public Installation getOrCreateInstallation() {
|
||||
AppSettingEntity setting = appSettingsRepository.findByName(INSTALLATION_ID_KEY);
|
||||
|
||||
if (setting == null) {
|
||||
return createNewInstallation();
|
||||
}
|
||||
|
||||
try {
|
||||
return objectMapper.readValue(setting.getVal(), Installation.class);
|
||||
} catch (Exception e) {
|
||||
log.warn("Failed to parse installation ID, creating new one", e);
|
||||
return createNewInstallation();
|
||||
}
|
||||
}
|
||||
|
||||
private Installation createNewInstallation() {
|
||||
Instant now = Instant.now();
|
||||
String uuid = UUID.randomUUID().toString();
|
||||
|
||||
String combined = now.toString() + "_" + uuid;
|
||||
String installationId = hashToSha256(combined).substring(0, 24);
|
||||
|
||||
Installation installation = new Installation(installationId, now);
|
||||
saveInstallation(installation);
|
||||
|
||||
log.info("Generated new installation ID");
|
||||
return installation;
|
||||
}
|
||||
|
||||
private void saveInstallation(Installation installation) {
|
||||
try {
|
||||
String json = objectMapper.writeValueAsString(installation);
|
||||
AppSettingEntity setting = appSettingsRepository.findByName(INSTALLATION_ID_KEY);
|
||||
|
||||
if (setting == null) {
|
||||
setting = new AppSettingEntity();
|
||||
setting.setName(INSTALLATION_ID_KEY);
|
||||
}
|
||||
|
||||
setting.setVal(json);
|
||||
appSettingsRepository.save(setting);
|
||||
} catch (Exception e) {
|
||||
throw new RuntimeException("Failed to save installation ID", e);
|
||||
}
|
||||
}
|
||||
|
||||
private String hashToSha256(String input) {
|
||||
try {
|
||||
MessageDigest digest = MessageDigest.getInstance("SHA-256");
|
||||
byte[] hash = digest.digest(input.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder hexString = new StringBuilder();
|
||||
for (byte b : hash) {
|
||||
String hex = Integer.toHexString(0xff & b);
|
||||
if (hex.length() == 1) hexString.append('0');
|
||||
hexString.append(hex);
|
||||
}
|
||||
return hexString.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("SHA-256 algorithm not found", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -11,8 +11,6 @@ import org.springframework.stereotype.Component;
|
||||
public class AppMigrationStartup {
|
||||
|
||||
private final AppMigrationService appMigrationService;
|
||||
private final GenerateInstallationIdMigration generateInstallationIdMigration;
|
||||
private final MigrateInstallationIdToJsonMigration migrateInstallationIdToJsonMigration;
|
||||
private final PopulateMissingFileSizesMigration populateMissingFileSizesMigration;
|
||||
private final PopulateMetadataScoresMigration populateMetadataScoresMigration;
|
||||
private final PopulateFileHashesMigration populateFileHashesMigration;
|
||||
@@ -25,8 +23,6 @@ public class AppMigrationStartup {
|
||||
|
||||
@EventListener(ApplicationReadyEvent.class)
|
||||
public void runMigrationsOnce() {
|
||||
appMigrationService.executeMigration(generateInstallationIdMigration);
|
||||
appMigrationService.executeMigration(migrateInstallationIdToJsonMigration);
|
||||
appMigrationService.executeMigration(populateMissingFileSizesMigration);
|
||||
appMigrationService.executeMigration(populateMetadataScoresMigration);
|
||||
appMigrationService.executeMigration(populateFileHashesMigration);
|
||||
|
||||
-33
@@ -1,33 +0,0 @@
|
||||
package org.booklore.service.migration.migrations;
|
||||
|
||||
import org.booklore.service.InstallationService;
|
||||
import org.booklore.service.migration.Migration;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class GenerateInstallationIdMigration implements Migration {
|
||||
|
||||
private final InstallationService installationService;
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return "generateInstallationId";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Generate unique installation ID using timestamp and UUID";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
log.info("Executing migration: {}", getKey());
|
||||
installationService.getOrCreateInstallation();
|
||||
log.info("Completed migration: {}", getKey());
|
||||
}
|
||||
}
|
||||
|
||||
-56
@@ -1,56 +0,0 @@
|
||||
package org.booklore.service.migration.migrations;
|
||||
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.booklore.model.entity.AppSettingEntity;
|
||||
import org.booklore.repository.AppSettingsRepository;
|
||||
import org.booklore.service.migration.Migration;
|
||||
import org.springframework.stereotype.Component;
|
||||
import tools.jackson.databind.ObjectMapper;
|
||||
|
||||
import java.time.Instant;
|
||||
|
||||
@Slf4j
|
||||
@Component
|
||||
@RequiredArgsConstructor
|
||||
public class MigrateInstallationIdToJsonMigration implements Migration {
|
||||
|
||||
private static final String INSTALLATION_ID_KEY = "installation_id";
|
||||
|
||||
private final AppSettingsRepository appSettingsRepository;
|
||||
private final ObjectMapper objectMapper;
|
||||
|
||||
@Override
|
||||
public String getKey() {
|
||||
return "migrateInstallationIdToJson";
|
||||
}
|
||||
|
||||
@Override
|
||||
public String getDescription() {
|
||||
return "Migrate existing installation_id from plain string to JSON format with date";
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() {
|
||||
log.info("Executing migration: {}", getKey());
|
||||
|
||||
AppSettingEntity setting = appSettingsRepository.findByName(INSTALLATION_ID_KEY);
|
||||
|
||||
if (setting != null) {
|
||||
String value = setting.getVal();
|
||||
try {
|
||||
objectMapper.readTree(value);
|
||||
log.info("Installation ID is already in JSON format, skipping migration");
|
||||
} catch (Exception e) {
|
||||
Instant now = Instant.now();
|
||||
String json = String.format("{\"id\":\"%s\",\"date\":\"%s\"}", value, now);
|
||||
setting.setVal(json);
|
||||
appSettingsRepository.save(setting);
|
||||
log.info("Migrated installation ID to JSON format with current date");
|
||||
}
|
||||
}
|
||||
|
||||
log.info("Completed migration: {}", getKey());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user