Pull request #578: RED-5548: Handle data migration on tenant creation, do not migrate already migrated files

Merge in RED/persistence-service from RED-5548 to master

* commit '119840f3c3bb5608002cfc266fcd5724105e37da':
  RED-5548: Handle data migration on tenant creation, do not migrate already migrated files
This commit is contained in:
Dominique Eiflaender 2022-12-12 10:01:31 +01:00
commit 6aab4ae8a3
4 changed files with 68 additions and 13 deletions

View File

@ -45,15 +45,7 @@ public class MigrationStarterService {
tenantRepository.findAll().forEach(tenant -> {
TenantContext.setTenantId(tenant.getTenantId());
log.info("Start migration for tentant: " + tenant);
migrations.sort(Comparator.comparing(Migration::getVersion));
for (var migration : migrations) {
migration.run();
}
runForTenant(tenant.getTenantId());
});
log.info("Migration is finished");
@ -63,6 +55,23 @@ public class MigrationStarterService {
}
public void runForTenant(String tenantId) {
TenantContext.setTenantId(tenantId);
seedMigration(); // Needed if tenant is created that is not migrated.
log.info("Start migration for tentant: " + tenantId);
migrations.sort(Comparator.comparing(Migration::getVersion));
for (var migration : migrations) {
migration.run();
}
log.info("Migration is finished for tenant: " + tenantId);
}
private void seedMigration() {
if (migrationPersistenceService.getLatestProcessedVersion() == null) {

View File

@ -1,6 +1,7 @@
package com.iqser.red.service.peristence.v1.server.migration.migrations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.stereotype.Service;
import com.iqser.red.service.peristence.v1.server.migration.Migration;
@ -59,6 +60,25 @@ public class ReduceTextFileSizeMigration10 extends Migration {
}
});
log.info("All files are migrated, removing bak files");
dossiers.forEach(dossier -> {
if (dossier.getHardDeletedTime() == null) {
var files = fileStatusPersistenceService.getStatusesForDossier(dossier.getId());
log.info("Start removing bak files of dossier {}", dossier.getId());
files.forEach(file -> {
if (file.getHardDeletedTime() == null) {
log.info("Start removing bak of file {}", file.getId());
storageService.deleteObject(StorageIdUtils.getStorageId(dossier.getId(), file.getId(), FileType.TEXT) + ".bak");
log.info("Finished removing bak of file {}", file.getId());
}
});
log.info("Finished removing bak files of dossier {}", dossier.getId());
}
});
log.info("All bak files removed");
}
@ -66,8 +86,13 @@ public class ReduceTextFileSizeMigration10 extends Migration {
public void migrateFile(String dossierId, String fileId) {
try {
var text = storageService.readJSONObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT), Text.class);
storageService.storeJSONObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT), text);
if(!storageService.objectExists(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT) + ".bak")) {
InputStreamResource textInputStreamResource = storageService.getObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT));
storageService.storeObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT) + ".bak", textInputStreamResource.getInputStream());
var text = storageService.readJSONObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT), Text.class);
storageService.storeJSONObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT), text);
}
} catch (StorageObjectDoesNotExist e) {
log.warn("Text not found for dossier {} and file {}, ignoring....", dossierId, fileId);
}

View File

@ -19,6 +19,8 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
import org.springframework.stereotype.Service;
import com.iqser.red.service.peristence.v1.server.migration.MigrationStarterService;
import com.iqser.red.service.peristence.v1.server.service.job.AutomaticAnalysisJob;
import com.iqser.red.service.persistence.management.v1.processor.exception.ConflictException;
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
import com.iqser.red.service.persistence.management.v1.processor.multitenancy.entity.TenantEntity;
@ -53,17 +55,21 @@ public class TenantManagementService {
private final LiquibaseProperties liquibaseProperties;
private final ResourceLoader resourceLoader;
private final TenantRepository tenantRepository;
private final AutomaticAnalysisJob automaticAnalysisJob;
private final MigrationStarterService migrationStarterService;
public TenantManagementService(EncryptionDecryptionService encryptionService,
@Qualifier("tenantLiquibaseProperties") LiquibaseProperties liquibaseProperties,
ResourceLoader resourceLoader,
TenantRepository tenantRepository) {
TenantRepository tenantRepository, AutomaticAnalysisJob automaticAnalysisJob, MigrationStarterService migrationStarterService) {
this.encryptionService = encryptionService;
this.liquibaseProperties = liquibaseProperties;
this.resourceLoader = resourceLoader;
this.tenantRepository = tenantRepository;
this.automaticAnalysisJob = automaticAnalysisJob;
this.migrationStarterService = migrationStarterService;
}
@ -94,6 +100,17 @@ public class TenantManagementService {
.password(encryptedPassword)
.build();
tenantRepository.save(tenantEntity);
try{
automaticAnalysisJob.setSchedulingStopped(true);
migrationStarterService.runForTenant(tenantRequest.getTenantId());
automaticAnalysisJob.setSchedulingStopped(false);
} catch (Exception e){
automaticAnalysisJob.setSchedulingStopped(false);
throw e;
}
} else {
throw ConflictException.withObjectName("tenant");
}

View File

@ -16,6 +16,7 @@ import com.iqser.red.service.persistence.management.v1.processor.utils.multitena
import com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileModel;
import lombok.RequiredArgsConstructor;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@ -28,11 +29,14 @@ public class AutomaticAnalysisJob implements Job {
private final FileStatusService fileStatusService;
private final TenantRepository tenantRepository;
@Setter
private boolean schedulingStopped;
@Override
public void execute(JobExecutionContext jobExecutionContext) {
if (fileManagementServiceSettings.isMigrateOnly()) {
if (fileManagementServiceSettings.isMigrateOnly() || schedulingStopped) {
log.info("Skipping scheduling during migration");
return;
}