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:
commit
6aab4ae8a3
@ -45,15 +45,7 @@ public class MigrationStarterService {
|
|||||||
|
|
||||||
tenantRepository.findAll().forEach(tenant -> {
|
tenantRepository.findAll().forEach(tenant -> {
|
||||||
|
|
||||||
TenantContext.setTenantId(tenant.getTenantId());
|
runForTenant(tenant.getTenantId());
|
||||||
|
|
||||||
log.info("Start migration for tentant: " + tenant);
|
|
||||||
|
|
||||||
migrations.sort(Comparator.comparing(Migration::getVersion));
|
|
||||||
|
|
||||||
for (var migration : migrations) {
|
|
||||||
migration.run();
|
|
||||||
}
|
|
||||||
});
|
});
|
||||||
|
|
||||||
log.info("Migration is finished");
|
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() {
|
private void seedMigration() {
|
||||||
|
|
||||||
if (migrationPersistenceService.getLatestProcessedVersion() == null) {
|
if (migrationPersistenceService.getLatestProcessedVersion() == null) {
|
||||||
|
|||||||
@ -1,6 +1,7 @@
|
|||||||
package com.iqser.red.service.peristence.v1.server.migration.migrations;
|
package com.iqser.red.service.peristence.v1.server.migration.migrations;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.core.io.InputStreamResource;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import com.iqser.red.service.peristence.v1.server.migration.Migration;
|
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) {
|
public void migrateFile(String dossierId, String fileId) {
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|
||||||
|
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);
|
var text = storageService.readJSONObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT), Text.class);
|
||||||
storageService.storeJSONObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT), text);
|
storageService.storeJSONObject(StorageIdUtils.getStorageId(dossierId, fileId, FileType.TEXT), text);
|
||||||
|
}
|
||||||
} catch (StorageObjectDoesNotExist e) {
|
} catch (StorageObjectDoesNotExist e) {
|
||||||
log.warn("Text not found for dossier {} and file {}, ignoring....", dossierId, fileId);
|
log.warn("Text not found for dossier {} and file {}, ignoring....", dossierId, fileId);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -19,6 +19,8 @@ import org.springframework.core.io.ResourceLoader;
|
|||||||
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
import org.springframework.jdbc.datasource.SingleConnectionDataSource;
|
||||||
import org.springframework.stereotype.Service;
|
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.ConflictException;
|
||||||
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
|
import com.iqser.red.service.persistence.management.v1.processor.exception.NotFoundException;
|
||||||
import com.iqser.red.service.persistence.management.v1.processor.multitenancy.entity.TenantEntity;
|
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 LiquibaseProperties liquibaseProperties;
|
||||||
private final ResourceLoader resourceLoader;
|
private final ResourceLoader resourceLoader;
|
||||||
private final TenantRepository tenantRepository;
|
private final TenantRepository tenantRepository;
|
||||||
|
private final AutomaticAnalysisJob automaticAnalysisJob;
|
||||||
|
private final MigrationStarterService migrationStarterService;
|
||||||
|
|
||||||
|
|
||||||
public TenantManagementService(EncryptionDecryptionService encryptionService,
|
public TenantManagementService(EncryptionDecryptionService encryptionService,
|
||||||
@Qualifier("tenantLiquibaseProperties") LiquibaseProperties liquibaseProperties,
|
@Qualifier("tenantLiquibaseProperties") LiquibaseProperties liquibaseProperties,
|
||||||
ResourceLoader resourceLoader,
|
ResourceLoader resourceLoader,
|
||||||
TenantRepository tenantRepository) {
|
TenantRepository tenantRepository, AutomaticAnalysisJob automaticAnalysisJob, MigrationStarterService migrationStarterService) {
|
||||||
|
|
||||||
this.encryptionService = encryptionService;
|
this.encryptionService = encryptionService;
|
||||||
this.liquibaseProperties = liquibaseProperties;
|
this.liquibaseProperties = liquibaseProperties;
|
||||||
this.resourceLoader = resourceLoader;
|
this.resourceLoader = resourceLoader;
|
||||||
this.tenantRepository = tenantRepository;
|
this.tenantRepository = tenantRepository;
|
||||||
|
this.automaticAnalysisJob = automaticAnalysisJob;
|
||||||
|
this.migrationStarterService = migrationStarterService;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,6 +100,17 @@ public class TenantManagementService {
|
|||||||
.password(encryptedPassword)
|
.password(encryptedPassword)
|
||||||
.build();
|
.build();
|
||||||
tenantRepository.save(tenantEntity);
|
tenantRepository.save(tenantEntity);
|
||||||
|
|
||||||
|
try{
|
||||||
|
automaticAnalysisJob.setSchedulingStopped(true);
|
||||||
|
migrationStarterService.runForTenant(tenantRequest.getTenantId());
|
||||||
|
automaticAnalysisJob.setSchedulingStopped(false);
|
||||||
|
} catch (Exception e){
|
||||||
|
automaticAnalysisJob.setSchedulingStopped(false);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw ConflictException.withObjectName("tenant");
|
throw ConflictException.withObjectName("tenant");
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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 com.iqser.red.service.persistence.service.v1.api.model.dossiertemplate.dossier.file.FileModel;
|
||||||
|
|
||||||
import lombok.RequiredArgsConstructor;
|
import lombok.RequiredArgsConstructor;
|
||||||
|
import lombok.Setter;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@ -28,11 +29,14 @@ public class AutomaticAnalysisJob implements Job {
|
|||||||
private final FileStatusService fileStatusService;
|
private final FileStatusService fileStatusService;
|
||||||
private final TenantRepository tenantRepository;
|
private final TenantRepository tenantRepository;
|
||||||
|
|
||||||
|
@Setter
|
||||||
|
private boolean schedulingStopped;
|
||||||
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void execute(JobExecutionContext jobExecutionContext) {
|
public void execute(JobExecutionContext jobExecutionContext) {
|
||||||
|
|
||||||
if (fileManagementServiceSettings.isMigrateOnly()) {
|
if (fileManagementServiceSettings.isMigrateOnly() || schedulingStopped) {
|
||||||
log.info("Skipping scheduling during migration");
|
log.info("Skipping scheduling during migration");
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user