Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,23 @@ void GIVEN_allMongockChangeUnitsAlreadyExecuted_WHEN_migratingToFlamingockCommun

flamingock.run();

List<String> persistedSnapshot = auditHelper.getAuditEntriesSorted().stream()
.map(entry -> entry.getChangeId() + "|" + entry.getState() + "|"
+ entry.getType() + "|" + entry.getStageId())
.collect(java.util.stream.Collectors.toList());
assertEquals(java.util.Arrays.asList(
"system-change-00001_before|APPLIED|MONGOCK_BEFORE|flamingock-legacy-stage",
"system-change-00001|APPLIED|MONGOCK_EXECUTION|flamingock-legacy-stage",
"mongock-change-1_before|APPLIED|MONGOCK_BEFORE|flamingock-legacy-stage",
"mongock-change-1|APPLIED|MONGOCK_EXECUTION|flamingock-legacy-stage",
"mongock-change-2|APPLIED|MONGOCK_EXECUTION|flamingock-legacy-stage",
"migration-mongock-to-flamingock-community|STARTED|STANDARD_CODE|flamingock-system-stage",
"migration-mongock-to-flamingock-community|APPLIED|STANDARD_CODE|flamingock-system-stage",
"create-users-collection-with-index|STARTED|STANDARD_CODE|changes",
"create-users-collection-with-index|APPLIED|STANDARD_CODE|changes",
"seed-users|STARTED|STANDARD_CODE|changes",
"seed-users|APPLIED|STANDARD_CODE|changes"), persistedSnapshot);

// Verify audit sequence: 11 total entries as shown in actual execution
// Final state only: imported and freshly-executed changes alike, once STARTED is filtered out
auditHelper.verifyAuditFinalStateSequence(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@
import io.flamingock.internal.common.core.pipeline.PipelineHelper;
import io.flamingock.internal.common.core.audit.AuditEntry;
import io.flamingock.internal.common.core.audit.AuditHistoryReader;
import io.flamingock.internal.common.core.audit.AuditWriter;
import io.flamingock.internal.common.core.error.FlamingockException;
import io.flamingock.internal.common.core.pipeline.PipelineDescriptor;
import io.flamingock.internal.core.external.store.AuditHistoryAppender;
import io.flamingock.internal.core.external.targets.TargetSystemManager;
import io.flamingock.internal.core.external.targets.operations.TargetSystemOps;
import io.flamingock.internal.core.external.targets.operations.TransactionalTargetSystemOps;
import io.flamingock.internal.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -65,7 +66,8 @@ public void importHistory(@Named("change.targetSystem.id") String targetSystemId
List<AuditEntry> legacyHistory = legacyHistoryReader.getAuditHistory();
boolean ignoreUnknownEntries = resolveIgnoreUnknownEntries(ignoreUnknownEntriesPropertyValue);
validate(legacyHistory, targetSystemId, emptyOriginAllowedPropertyValue);
AuditWriter auditWriter = auditPersistenceFactory.get(PipelineHelper.LEGACY_STAGE_ID);
AuditHistoryAppender auditHistoryAppender =
(AuditHistoryAppender) auditPersistenceFactory.get(PipelineHelper.LEGACY_STAGE_ID);
legacyHistory.forEach(auditEntryFromOrigin -> {
Optional<String> stageId = pipelineHelper.findStageId(auditEntryFromOrigin);
if (!stageId.isPresent()) {
Expand All @@ -84,7 +86,13 @@ public void importHistory(@Named("change.targetSystem.id") String targetSystemId
AuditEntry auditEntryWithStageId = auditEntryFromOrigin.copyWithNewIdAndStageId(
pipelineHelper.getStorableChangeId(auditEntryFromOrigin),
stageId.get());
auditWriter.writeEntry(auditEntryWithStageId);
Result result = auditHistoryAppender.append(auditEntryWithStageId);
if (result instanceof Result.Error) {
throw new FlamingockException(String.format(
"Error importing audit entry with changeId[%s] into stage[%s] for targetSystem[%s].",
auditEntryWithStageId.getChangeId(), stageId.get(), targetSystemId),
((Result.Error) result).getError());
}
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* Copyright 2026 Flamingock (https://www.flamingock.io)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.flamingock.support.mongock;

import io.flamingock.internal.common.core.audit.AuditEntry;
import io.flamingock.internal.common.core.audit.AuditPersistenceFactory;
import io.flamingock.internal.common.core.audit.AuditHistoryReader;
import io.flamingock.internal.common.core.error.FlamingockException;
import io.flamingock.internal.common.core.pipeline.PipelineDescriptor;
import io.flamingock.internal.core.external.store.audit.community.CommunityAuditPersistence;
import io.flamingock.internal.core.external.targets.TargetSystemManager;
import io.flamingock.internal.core.external.targets.operations.TransactionalTargetSystemOps;
import io.flamingock.internal.util.Result;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.Optional;

import static io.flamingock.internal.common.core.audit.AuditReaderType.MONGOCK;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

class MongockImportChangeTest {
@Test
void failedAppendIncludesEntryIdentityAndOriginalCause() {
TargetSystemManager manager = mock(TargetSystemManager.class);
TransactionalTargetSystemOps ops = mock(TransactionalTargetSystemOps.class);
AuditHistoryReader reader = mock(AuditHistoryReader.class);
PipelineDescriptor pipeline = mock(PipelineDescriptor.class);
CommunityAuditPersistence persistence = mock(CommunityAuditPersistence.class);
AuditPersistenceFactory<CommunityAuditPersistence> factory = stageId -> {
assertEquals("flamingock-legacy-stage", stageId);
return persistence;
};
AuditEntry entry = mock(AuditEntry.class);
RuntimeException cause = new RuntimeException("write failed");
when(manager.getTargetSystem("mongo")).thenReturn(ops);
when(ops.getAuditAuditReader(MONGOCK)).thenReturn(Optional.of(reader));
when(reader.getAuditHistory()).thenReturn(Collections.singletonList(entry));
when(entry.getChangeId()).thenReturn("change-1");
when(entry.copyWithNewIdAndStageId("change-1", "stage-1")).thenReturn(entry);
when(pipeline.getStageByChange("change-1")).thenReturn(Optional.of("stage-1"));
when(persistence.append(any(AuditEntry.class))).thenReturn(new Result.Error(cause));

FlamingockException failure = assertThrows(FlamingockException.class,
() -> new MongockImportChange().importHistory("mongo", manager, factory, pipeline, null, null, null));
assertSame(cause, failure.getCause());
assertEquals("Error importing audit entry with changeId[change-1] into stage[stage-1] for targetSystem[mongo].",
failure.getMessage());
}
}
Loading