Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: new defaultAuthor(replace defaultMigrationAuthor) #634

Merged
merged 1 commit into from
Aug 29, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,12 @@

public class MongockConfiguration implements ExecutorConfiguration {


/**
* @deprecated not working as expected(https://github.com/mongock/mongock/issues/631)
*/
@Deprecated
public static final String DEFAULT_MIGRATION_AUTHOR = "default_author";
public static final String DEFAULT_AUTHOR = "default_author";
private static final Logger logger = LoggerFactory.getLogger(MongockConfiguration.class);
private final static String LEGACY_DEFAULT_MIGRATION_REPOSITORY_NAME = "mongockChangeLog";
private final static String LEGACY_DEFAULT_LOCK_REPOSITORY_NAME = "mongockLock";
Expand Down Expand Up @@ -125,8 +129,17 @@ public class MongockConfiguration implements ExecutorConfiguration {
*
* Default value: default_author
*/
@Deprecated
private String defaultMigrationAuthor = DEFAULT_MIGRATION_AUTHOR;

/**
* Set a default author that will be applied on all changelogs if no Author set
* Fix the buggy behaviour of defaultMigrationAuthor(always default_author)
*
* Default value: default_author
*/
private String defaultAuthor = DEFAULT_AUTHOR;

/**
* With the introduction of ChangeUnit in version 5, Mongock provides two strategies to approach the transactions(automatic and manually):
* - CHANGE_UNIT: Each change unit is wrapped in an independent transaction. This is the default and recommended way for two main reasons:
Expand Down Expand Up @@ -170,6 +183,7 @@ public void updateFrom(MongockConfiguration from) {
transactionStrategy = from.getTransactionStrategy();
maxTries = from.getMaxTries();
maxWaitingForLockMillis = from.getMaxWaitingForLockMillis();
defaultAuthor=from.getDefaultAuthor();
}

public long getLockAcquiredForMillis() {
Expand Down Expand Up @@ -313,10 +327,20 @@ public void setTransactionStrategy(TransactionStrategy transactionStrategy) {
this.transactionStrategy = transactionStrategy;
}

public String getDefaultAuthor() {
return defaultAuthor;
}

public void setDefaultAuthor(String defaultAuthor) {
this.defaultAuthor = defaultAuthor;
}

@Deprecated
public String getDefaultMigrationAuthor() {
return defaultMigrationAuthor;
}

@Deprecated
public void setDefaultMigrationAuthor(String defaultMigrationAuthor) {
this.defaultMigrationAuthor = defaultMigrationAuthor;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ public interface ChangeExecutorConfiguration {

TransactionStrategy getTransactionStrategy();

@Deprecated
String getDefaultMigrationAuthor();

String getDefaultAuthor();

List<String> getMigrationScanPackage();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,48 @@

public class MongockConfigurationTest {

@Test
public void shouldKeepBuggyCopyDefaultMigrationAuthorFromExisitingConfigurationDefault() {
MongockConfiguration conf = new MongockConfiguration();

MongockConfiguration newConf = new MongockConfiguration();
conf.setDefaultMigrationAuthor("another author");
newConf.updateFrom(conf);
assertEquals(MongockConfiguration.DEFAULT_MIGRATION_AUTHOR, newConf.getDefaultMigrationAuthor());
}

@Test
public void shouldCopyDefaultAuthorFromExisitingConfigurationIfSet() {
MongockConfiguration conf = new MongockConfiguration();
conf.setDefaultAuthor("joey ramone");

MongockConfiguration newConf = new MongockConfiguration();
newConf.updateFrom(conf);
assertEquals("joey ramone", newConf.getDefaultAuthor());
}

@Test
public void shouldCopyDefaultAuthorFromExisitingConfigurationIfNotSet() {
MongockConfiguration conf = new MongockConfiguration();

MongockConfiguration newConf = new MongockConfiguration();
newConf.updateFrom(conf);
assertEquals(MongockConfiguration.DEFAULT_AUTHOR, newConf.getDefaultAuthor());
}

@Test
public void shouldReturnDefaultMigrationAuthor_IfNotSet() {
MongockConfiguration conf = new MongockConfiguration();
assertEquals(MongockConfiguration.DEFAULT_AUTHOR, conf.getDefaultAuthor());
}

@Test
public void shouldReturnNewDefaultMigrationAuthor_IfNewProperty() {
MongockConfiguration conf = new MongockConfiguration();
conf.setDefaultAuthor("joey ramone");
assertEquals("joey ramone", conf.getDefaultAuthor());
}

@Test
public void shouldReturnNewQuitTryingAfterMillis_IfNewProperty() {
MongockConfiguration conf = new MongockConfiguration();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public interface ChangeLogScanner<SELF extends ChangeLogScanner<SELF, CONFIG>, C
* @param defaultMigrationAuthor the default author
* @return builder for fluent interface
*/
default SELF setDefaultMigrationAuthor(String defaultMigrationAuthor) {
getConfig().setDefaultMigrationAuthor(defaultMigrationAuthor);
default SELF setDefaultAuthor(String defaultMigrationAuthor) {
getConfig().setDefaultAuthor(defaultMigrationAuthor);
return getInstance();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ protected void prepareChangeLogService() {
}
}
changeLogService.reset();
changeLogService.setDefaultMigrationAuthor(config.getDefaultMigrationAuthor());
changeLogService.setDefaultAuthor(config.getDefaultAuthor());
changeLogService.setChangeLogsBasePackageList(changeLogsScanPackage);
changeLogService.setChangeLogsBaseClassList(changeLogsScanClasses);
changeLogService.setStartSystemVersion(config.getStartSystemVersion());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ protected ChangeLogItem buildChangeLogInstance(Class<?> changeUnitClass) throws
return ChangeLogItem.getFromAnnotation(
changeUnitClass,
changeUnit.id(),
StringUtils.hasText(changeUnit.author()) ? changeUnit.author() : getDefaultMigrationAuthor(),
StringUtils.hasText(changeUnit.author()) ? changeUnit.author() : getDefaultAuthor(),
changeUnit.order(),
changeUnit.failFast(),
changeUnit.transactional(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class ChangeLogServiceBase implements Validable {
private List<Class<?>> changeLogsBaseClassList;
private ArtifactVersion startSystemVersion;
private ArtifactVersion endSystemVersion;
private String defaultMigrationAuthor;
private String defaultAuthor;

public ChangeLogServiceBase(AnnotationProcessor annotationProcessor, LegacyAnnotationProcessor legacyAnnotationProcessor) {
this.legacyAnnotationProcessor = legacyAnnotationProcessor;
Expand All @@ -62,7 +62,7 @@ public final void reset() {
this.changeLogsBaseClassList = Collections.emptyList();
this.startSystemVersion = new DefaultArtifactVersion("0");
this.endSystemVersion = new DefaultArtifactVersion(String.valueOf(Integer.MAX_VALUE));
this.defaultMigrationAuthor = null;
this.defaultAuthor = null;
}

protected LegacyAnnotationProcessor getLegacyAnnotationProcessor() {
Expand Down Expand Up @@ -117,12 +117,12 @@ protected Optional<Function<Class<?>, Object>> getChangeLogInstantiator() {
return Optional.ofNullable(changeLogInstantiator);
}

public String getDefaultMigrationAuthor() {
return defaultMigrationAuthor;
public String getDefaultAuthor() {
return defaultAuthor;
}

public void setDefaultMigrationAuthor(String defaultMigrationAuthor) {
this.defaultMigrationAuthor = defaultMigrationAuthor;
public void setDefaultAuthor(String defaultAuthor) {
this.defaultAuthor = defaultAuthor;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ protected void prepareChangeLogService() {
}
changeLogService.reset();
changeLogService.setChangeLogsBasePackageList(changeLogsScanPackage);
changeLogService.setDefaultMigrationAuthor(SYSTEM_CHANGES_DEFAULT_AUTHOR);
changeLogService.setDefaultAuthor(SYSTEM_CHANGES_DEFAULT_AUTHOR);
}

private DriverSystemUpdatable getDriverSystemUpdatable(ConnectionDriver driver) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,18 @@ public void shouldThrowException_whenAuthorIsEmpty_IfChangeUnit() {
assertEquals("changeUnit[" + ChangeUnitWithAuthorEmpty.class.getName() + "] author cannot be null or empty.", ex.getMessage());
}

@Test
public void shouldNotThrowException_whenChangeUnitAuthorIsEmpty_IfSetDefaultAuthor() {
ChangeLogService changeLogService = new ChangeLogService(
Collections.emptyList(),
Collections.singletonList(ChangeUnitWithAuthorEmpty.class),
"0",
"9999"
);
changeLogService.setDefaultMigrationAuthor("default_author");
changeLogService.setDefaultAuthor("joey ramone");
List<ChangeLogItem> changeLogItemList = new ArrayList<>(changeLogService.fetchChangeLogs());
assertEquals(1, changeLogItemList.size());
ChangeSetItem changeSetItem = changeLogItemList.get(0).getChangeSetItems().get(0);
assertEquals("default_author", changeSetItem.getAuthor());
assertEquals("joey ramone", changeSetItem.getAuthor());
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ public void shouldPassDefaultAuthorToChangeLogService() {

builder.buildRunner().execute();

Mockito.verify(changeLogServiceSpy).setDefaultMigrationAuthor("default_author");
Mockito.verify(changeLogServiceSpy).setDefaultAuthor("default_author");


}
Expand Down
Loading