|
22 | 22 | import com.mongodb.client.MongoDatabase; |
23 | 23 | import io.flamingock.api.annotations.EnableFlamingock; |
24 | 24 | import io.flamingock.api.annotations.Stage; |
| 25 | +import io.flamingock.common.test.mongock.MongockChangeEntry; |
| 26 | +import io.flamingock.common.test.mongock.MongockChangeState; |
| 27 | +import io.flamingock.common.test.mongock.MongockTestHelper; |
25 | 28 | import io.flamingock.store.mongodb.sync.MongoDBSyncAuditStore; |
26 | 29 | import io.flamingock.core.kit.TestKit; |
27 | 30 | import io.flamingock.core.kit.audit.AuditTestHelper; |
| 31 | +import io.flamingock.internal.common.core.audit.AuditEntry; |
28 | 32 | import io.flamingock.internal.common.core.response.data.ErrorInfo; |
29 | 33 | import io.flamingock.internal.core.operation.StagedExecuteOperationException; |
30 | 34 | import io.flamingock.internal.core.builder.runner.Runner; |
|
47 | 51 |
|
48 | 52 | import static io.flamingock.core.kit.audit.AuditEntryExpectation.APPLIED; |
49 | 53 | import static io.flamingock.core.kit.audit.AuditEntryExpectation.STARTED; |
| 54 | +import static io.flamingock.core.kit.audit.AuditEntryExpectation.auditEntry; |
50 | 55 | import static io.flamingock.internal.common.core.metadata.Constants.DEFAULT_MONGOCK_ORIGIN; |
51 | 56 | import static io.flamingock.internal.common.core.metadata.Constants.MONGOCK_IMPORT_EMPTY_ORIGIN_ALLOWED_PROPERTY_KEY; |
52 | 57 | import static io.flamingock.internal.common.core.metadata.Constants.MONGOCK_IMPORT_IGNORE_UNKNOWN_ENTRIES_PROPERTY_KEY; |
53 | 58 | import static io.flamingock.internal.common.core.metadata.Constants.MONGOCK_IMPORT_ORIGIN_PROPERTY_KEY; |
54 | 59 | import static io.flamingock.internal.common.core.metadata.Constants.MONGOCK_IMPORT_SKIP_PROPERTY_KEY; |
55 | 60 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 61 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 62 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 63 | +import static org.junit.jupiter.api.Assertions.assertNull; |
56 | 64 | import static org.junit.jupiter.api.Assertions.assertThrows; |
57 | 65 |
|
58 | 66 | @Testcontainers |
@@ -373,6 +381,52 @@ void GIVEN_allMongockChangeUnitsAlreadyExecutedAndCustomOriginProvidedByLiteralV |
373 | 381 | Assertions.assertEquals("readonly", users.get(1).getList("roles", String.class).get(0)); |
374 | 382 | } |
375 | 383 |
|
| 384 | + @Test |
| 385 | + @DisplayName("GIVEN Mongock v4 style audit entries without type, errorTrace and systemChange " + |
| 386 | + "WHEN migrating to Flamingock Community " + |
| 387 | + "THEN should import the history using v4 compatibility defaults") |
| 388 | + void GIVEN_mongockV4StyleAuditEntries_WHEN_migratingToFlamingockCommunity_THEN_shouldImportWithCompatibilityDefaults() { |
| 389 | + mongockTestHelper.writeAll(buildMongockV4ExecutedEntries()); |
| 390 | + |
| 391 | + MongoDBSyncTargetSystem mongodbTargetSystem = new MongoDBSyncTargetSystem("mongodb-target-system", mongoClient, DATABASE_NAME); |
| 392 | + |
| 393 | + Runner flamingock = testKit.createBuilder() |
| 394 | + .addTargetSystem(mongodbTargetSystem) |
| 395 | + .build(); |
| 396 | + |
| 397 | + flamingock.run(); |
| 398 | + |
| 399 | + auditHelper.verifyAuditSequenceStrict( |
| 400 | + auditEntry().withChangeId("mongock-change-1") |
| 401 | + .withState(AuditEntry.Status.APPLIED) |
| 402 | + .withType(AuditEntry.ChangeType.MONGOCK_EXECUTION) |
| 403 | + .withSystemChange(false), |
| 404 | + auditEntry().withChangeId("mongock-change-2") |
| 405 | + .withState(AuditEntry.Status.APPLIED) |
| 406 | + .withType(AuditEntry.ChangeType.MONGOCK_EXECUTION) |
| 407 | + .withSystemChange(false), |
| 408 | + STARTED("migration-mongock-to-flamingock-community"), |
| 409 | + APPLIED("migration-mongock-to-flamingock-community"), |
| 410 | + STARTED("create-users-collection-with-index"), |
| 411 | + APPLIED("create-users-collection-with-index"), |
| 412 | + STARTED("seed-users"), |
| 413 | + APPLIED("seed-users") |
| 414 | + ); |
| 415 | + |
| 416 | + AuditEntry importedChange1 = getAuditEntryByChangeId("mongock-change-1"); |
| 417 | + AuditEntry importedChange2 = getAuditEntryByChangeId("mongock-change-2"); |
| 418 | + |
| 419 | + assertNotNull(importedChange1); |
| 420 | + assertEquals(AuditEntry.ChangeType.MONGOCK_EXECUTION, importedChange1.getType()); |
| 421 | + assertFalse(importedChange1.getSystemChange()); |
| 422 | + assertNull(importedChange1.getErrorTrace()); |
| 423 | + |
| 424 | + assertNotNull(importedChange2); |
| 425 | + assertEquals(AuditEntry.ChangeType.MONGOCK_EXECUTION, importedChange2.getType()); |
| 426 | + assertFalse(importedChange2.getSystemChange()); |
| 427 | + assertNull(importedChange2.getErrorTrace()); |
| 428 | + } |
| 429 | + |
376 | 430 | @Test |
377 | 431 | @DisplayName("GIVEN Mongock audit history contains unknown entries " + |
378 | 432 | "AND relaxed import flag is not provided " + |
@@ -496,6 +550,54 @@ private static String firstFailedStageErrorMessage(StagedExecuteOperationExcepti |
496 | 550 | .orElseThrow(() -> new AssertionError("Expected a failed stage with ErrorInfo")); |
497 | 551 | } |
498 | 552 |
|
| 553 | + private List<MongockChangeEntry> buildMongockV4ExecutedEntries() { |
| 554 | + try { |
| 555 | + List<MongockChangeEntry> entries = new ArrayList<>(); |
| 556 | + entries.add(new MongockChangeEntry( |
| 557 | + "v4-execution-1", |
| 558 | + "mongock-change-1", |
| 559 | + "mongock", |
| 560 | + MongockTestHelper.DEFAULT_DATE_FORMAT.parse("2025-06-19T05:43:57.132Z"), |
| 561 | + MongockChangeState.EXECUTED, |
| 562 | + null, |
| 563 | + "io.mongock.examples.mongodb.standalone.mondogb.sync.migration.initializer.ClientInitializerChangeUnit", |
| 564 | + "apply", |
| 565 | + null, |
| 566 | + 23L, |
| 567 | + MongockTestHelper.DEFAULT_HOSTNAME, |
| 568 | + null, |
| 569 | + null, |
| 570 | + null |
| 571 | + )); |
| 572 | + entries.add(new MongockChangeEntry( |
| 573 | + "v4-execution-1", |
| 574 | + "mongock-change-2", |
| 575 | + "mongock", |
| 576 | + MongockTestHelper.DEFAULT_DATE_FORMAT.parse("2025-06-19T05:43:57.169Z"), |
| 577 | + MongockChangeState.EXECUTED, |
| 578 | + null, |
| 579 | + "io.mongock.examples.mongodb.standalone.mondogb.sync.migration.updater.ClientUpdaterChangeUnit", |
| 580 | + "apply", |
| 581 | + null, |
| 582 | + 20L, |
| 583 | + MongockTestHelper.DEFAULT_HOSTNAME, |
| 584 | + null, |
| 585 | + null, |
| 586 | + null |
| 587 | + )); |
| 588 | + return entries; |
| 589 | + } catch (Exception e) { |
| 590 | + throw new RuntimeException("Failed to build Mongock v4 test entries", e); |
| 591 | + } |
| 592 | + } |
| 593 | + |
| 594 | + private AuditEntry getAuditEntryByChangeId(String changeId) { |
| 595 | + return auditHelper.getAuditEntriesSorted().stream() |
| 596 | + .filter(entry -> changeId.equals(entry.getChangeId())) |
| 597 | + .findFirst() |
| 598 | + .orElse(null); |
| 599 | + } |
| 600 | + |
499 | 601 |
|
500 | 602 | @Test |
501 | 603 | @DisplayName("GIVEN all Mongock changeUnits already executed " + |
|
0 commit comments