Version control - not rollback on error option

This commit is contained in:
ViacheslavKlimov 2024-03-15 11:28:04 +02:00
parent 92cd231034
commit eb50685108
6 changed files with 35 additions and 15 deletions

View File

@ -101,7 +101,11 @@ public class DefaultEntitiesExportImportService implements EntitiesExportImportS
ctx.putInternalId(exportData.getExternalId(), importResult.getSavedEntity().getId()); ctx.putInternalId(exportData.getExternalId(), importResult.getSavedEntity().getId());
ctx.addReferenceCallback(exportData.getExternalId(), importResult.getSaveReferencesCallback()); ctx.addReferenceCallback(exportData.getExternalId(), importResult.getSaveReferencesCallback());
if (ctx.isRollbackOnError()) {
ctx.addEventCallback(importResult.getSendEventsCallback()); ctx.addEventCallback(importResult.getSendEventsCallback());
} else {
importResult.getSendEventsCallback().run();
}
return importResult; return importResult;
} }

View File

@ -246,16 +246,18 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont
switch (request.getType()) { switch (request.getType()) {
case SINGLE_ENTITY: { case SINGLE_ENTITY: {
SingleEntityVersionLoadRequest versionLoadRequest = (SingleEntityVersionLoadRequest) request; SingleEntityVersionLoadRequest versionLoadRequest = (SingleEntityVersionLoadRequest) request;
ctx.setRollbackOnError(true);
VersionLoadConfig config = versionLoadRequest.getConfig(); VersionLoadConfig config = versionLoadRequest.getConfig();
ListenableFuture<EntityExportData> future = gitServiceQueue.getEntity(user.getTenantId(), request.getVersionId(), versionLoadRequest.getExternalEntityId()); ListenableFuture<EntityExportData> future = gitServiceQueue.getEntity(user.getTenantId(), request.getVersionId(), versionLoadRequest.getExternalEntityId());
DonAsynchron.withCallback(future, DonAsynchron.withCallback(future,
entityData -> doInTemplate(ctx, request, c -> loadSingleEntity(c, config, entityData)), entityData -> load(ctx, request, c -> loadSingleEntity(c, config, entityData)),
e -> processLoadError(ctx, e), executor); e -> processLoadError(ctx, e), executor);
break; break;
} }
case ENTITY_TYPE: { case ENTITY_TYPE: {
EntityTypeVersionLoadRequest versionLoadRequest = (EntityTypeVersionLoadRequest) request; EntityTypeVersionLoadRequest versionLoadRequest = (EntityTypeVersionLoadRequest) request;
executor.submit(() -> doInTemplate(ctx, request, c -> loadMultipleEntities(c, versionLoadRequest))); ctx.setRollbackOnError(versionLoadRequest.isRollbackOnError());
executor.submit(() -> load(ctx, request, c -> loadMultipleEntities(c, versionLoadRequest)));
break; break;
} }
default: default:
@ -265,19 +267,24 @@ public class DefaultEntitiesVersionControlService implements EntitiesVersionCont
return ctx.getRequestId(); return ctx.getRequestId();
} }
private <R> VersionLoadResult doInTemplate(EntitiesImportCtx ctx, VersionLoadRequest request, Function<EntitiesImportCtx, VersionLoadResult> function) { private <R> VersionLoadResult load(EntitiesImportCtx ctx, VersionLoadRequest request, Function<EntitiesImportCtx, VersionLoadResult> loadFunction) {
try { try {
VersionLoadResult result = transactionTemplate.execute(status -> { VersionLoadResult result;
if (ctx.isRollbackOnError()) {
result = transactionTemplate.execute(status -> {
try { try {
return function.apply(ctx); return loadFunction.apply(ctx);
} catch (RuntimeException e) { } catch (RuntimeException e) {
throw e; throw e;
} catch (Exception e) { } catch (Exception e) {
throw new RuntimeException(e); // to prevent UndeclaredThrowableException throw new RuntimeException(e); // to prevent UndeclaredThrowableException
} }
}); });
for (ThrowingRunnable throwingRunnable : ctx.getEventCallbacks()) { for (ThrowingRunnable eventCallback : ctx.getEventCallbacks()) {
throwingRunnable.run(); eventCallback.run();
}
} else {
result = loadFunction.apply(ctx);
} }
result.setDone(true); result.setDone(true);
return cachePut(ctx.getRequestId(), result); return cachePut(ctx.getRequestId(), result);

View File

@ -58,6 +58,7 @@ public class EntitiesImportCtx {
private boolean finalImportAttempt = false; private boolean finalImportAttempt = false;
private EntityImportSettings settings; private EntityImportSettings settings;
private EntityImportResult<?> currentImportResult; private EntityImportResult<?> currentImportResult;
private boolean rollbackOnError;
public EntitiesImportCtx(UUID requestId, User user, String versionId) { public EntitiesImportCtx(UUID requestId, User user, String versionId) {
this(requestId, user, versionId, null); this(requestId, user, versionId, null);

View File

@ -26,6 +26,7 @@ import java.util.Map;
public class EntityTypeVersionLoadRequest extends VersionLoadRequest { public class EntityTypeVersionLoadRequest extends VersionLoadRequest {
private Map<EntityType, EntityTypeVersionLoadConfig> entityTypes; private Map<EntityType, EntityTypeVersionLoadConfig> entityTypes;
private boolean rollbackOnError;
@Override @Override
public VersionLoadRequestType getType() { public VersionLoadRequestType getType() {

View File

@ -99,3 +99,8 @@
</button> </button>
</div> </div>
</fieldset> </fieldset>
<div [formGroup]="entityTypesVersionLoadFormGroup" fxLayout="row">
<mat-checkbox formControlName="rollbackOnError">
{{ 'version-control.rollback-on-error' | translate }}
</mat-checkbox>
</div>

View File

@ -4915,7 +4915,9 @@
"device-credentials-conflict": "Failed to load the device with external id <b>{{entityId}}</b><br/>due to the same credentials are already present in the database for another device.<br/>Please consider disabling the <b>load credentials</b> setting in the restore form.", "device-credentials-conflict": "Failed to load the device with external id <b>{{entityId}}</b><br/>due to the same credentials are already present in the database for another device.<br/>Please consider disabling the <b>load credentials</b> setting in the restore form.",
"missing-referenced-entity": "Failed to load the <b>{{sourceEntityTypeName}}</b> with external id <b>{{sourceEntityId}}</b><br/>because it references missing <b>{{targetEntityTypeName}}</b> with id <b>{{targetEntityId}}</b>.", "missing-referenced-entity": "Failed to load the <b>{{sourceEntityTypeName}}</b> with external id <b>{{sourceEntityId}}</b><br/>because it references missing <b>{{targetEntityTypeName}}</b> with id <b>{{targetEntityId}}</b>.",
"runtime-failed": "<b>Failed:</b> {{message}}", "runtime-failed": "<b>Failed:</b> {{message}}",
"auto-commit-settings-read-only-hint": "Auto-commit feature doesn't work with enabled read-only option in Repository settings." "auto-commit-settings-read-only-hint": "Auto-commit feature doesn't work with enabled read-only option in Repository settings.",
"rollback-on-error": "Rollback on error",
"rollback-on-error-hint": "If you have a large amount of entities to restore, consider disabling this option to increase performance.<br><b>Note</b>, if an error occurs over the course of version loading, already persisted entities (with relations, attributes, etc.) will stay as is"
}, },
"widget": { "widget": {
"widget-library": "Widgets library", "widget-library": "Widgets library",