Skip to content

Commit

Permalink
Miscellaneous refactorings
Browse files Browse the repository at this point in the history
  • Loading branch information
ibacher committed Oct 8, 2024
1 parent eb2333a commit d7c80a6
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 98 deletions.
117 changes: 46 additions & 71 deletions api/src/main/java/org/openmrs/liquibase/ChangeLogDetective.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,38 +83,16 @@ public String getInitialLiquibaseSnapshotVersion(String context, LiquibaseProvid
log.info("looking for un-run change sets in snapshot version '{}'", version);
List<String> changeSets = snapshotCombinations.get(version);

Liquibase liquibase = null;
try {
for (String filename : changeSets) {
String scopeId = LiquibaseScopeHandling.enterLiquibaseUILoggingService();

liquibase = liquibaseProvider.getLiquibase(filename);

List<ChangeSet> rawUnrunChangeSets = new StatusCommandStep()
.listUnrunChangeSets(new Contexts(context),
new LabelExpression(), liquibase.getDatabaseChangeLog(), liquibase.getDatabase());


LiquibaseScopeHandling.exitLiquibaseScope(scopeId);
liquibase.close();

List<ChangeSet> refinedUnrunChangeSets = excludeVintageChangeSets(filename, rawUnrunChangeSets);

log.info("file '{}' contains {} un-run change sets", filename, refinedUnrunChangeSets.size());
logUnRunChangeSetDetails(filename, refinedUnrunChangeSets);

unrunChangeSetsCount += refinedUnrunChangeSets.size();
}
}
finally {
if (liquibase != null) {
try {
liquibase.close();
}
catch (Exception e) {
// ignore exceptions triggered by closing liquibase a second time
}
}
Contexts contexts = new Contexts(context);
for (String filename : changeSets) {
List<ChangeSet> rawUnrunChangeSets = getUnrunChangeSets(filename, contexts, liquibaseProvider);

List<ChangeSet> refinedUnrunChangeSets = excludeVintageChangeSets(filename, rawUnrunChangeSets);

log.info("file '{}' contains {} un-run change sets", filename, refinedUnrunChangeSets.size());
logUnRunChangeSetDetails(filename, refinedUnrunChangeSets);

unrunChangeSetsCount += refinedUnrunChangeSets.size();
}

if (unrunChangeSetsCount == 0) {
Expand Down Expand Up @@ -146,35 +124,15 @@ public List<String> getUnrunLiquibaseUpdateFileNames(String snapshotVersion, Str
List<String> updateVersions = changeLogVersionFinder.getUpdateVersionsGreaterThan(snapshotVersion);
List<String> updateFileNames = changeLogVersionFinder.getUpdateFileNames(updateVersions);

Liquibase liquibase = null;
try {
for (String filename : updateFileNames) {
String scopeId = LiquibaseScopeHandling.enterLiquibaseUILoggingService();
liquibase = liquibaseProvider.getLiquibase(filename);
Contexts contexts = new Contexts(context);
for (String filename : updateFileNames) {
List<ChangeSet> unrunChangeSets = getUnrunChangeSets(filename, contexts, liquibaseProvider);

List<ChangeSet> unrunChangeSets = new StatusCommandStep()
.listUnrunChangeSets(new Contexts(context),
new LabelExpression(), liquibase.getDatabaseChangeLog(), liquibase.getDatabase());

LiquibaseScopeHandling.exitLiquibaseScope(scopeId);
liquibase.close();

log.info("file '{}' contains {} un-run change sets", filename, unrunChangeSets.size());
logUnRunChangeSetDetails(filename, unrunChangeSets);

if (!unrunChangeSets.isEmpty()) {
unrunLiquibaseUpdates.add(filename);
}
}
}
finally {
if (liquibase != null) {
try {
liquibase.close();
}
catch (Exception e) {
// ignore exceptions triggered by closing liquibase a second time
}
log.info("file '{}' contains {} un-run change sets", filename, unrunChangeSets.size());
logUnRunChangeSetDetails(filename, unrunChangeSets);

if (!unrunChangeSets.isEmpty()) {
unrunLiquibaseUpdates.add(filename);
}
}

Expand All @@ -183,7 +141,7 @@ public List<String> getUnrunLiquibaseUpdateFileNames(String snapshotVersion, Str

List<String> getSnapshotVersionsInDescendingOrder(Map<String, List<String>> snapshotCombinations) {
List<String> versions = new ArrayList<>(snapshotCombinations.keySet());
Collections.sort(versions, Collections.reverseOrder());
versions.sort(Collections.reverseOrder());
return versions;
}

Expand All @@ -197,15 +155,29 @@ List<ChangeSet> excludeVintageChangeSets(String filename, List<ChangeSet> change
return result;
}

boolean isVintageChangeSet(String filename, ChangeSet changeSet) {
if (filename != null && filename.contains(LIQUIBASE_CORE_DATA_1_9_X_FILENAME)
&& changeSet.getId().equals(DISABLE_FOREIGN_KEY_CHECKS) && changeSet.getAuthor().equals(BEN)) {
return true;
List<ChangeSet> getUnrunChangeSets(String filename, Contexts context, LiquibaseProvider liquibaseProvider) throws Exception {
String scopeId = LiquibaseScopeHandling.enterLiquibaseUILoggingService();
Liquibase liquibase = liquibaseProvider.getLiquibase(filename);

List<ChangeSet> unrunChangeSets;
try {
unrunChangeSets = new StatusCommandStep()
.listUnrunChangeSets(context,
new LabelExpression(), liquibase.getDatabaseChangeLog(), liquibase.getDatabase());

} finally {
LiquibaseScopeHandling.exitLiquibaseScope(scopeId);
liquibase.close();
}
if (filename != null && filename.contains(LIQUIBASE_CORE_DATA_1_9_X_FILENAME)
&& changeSet.getId().equals(ENABLE_FOREIGN_KEY_CHECKS) && changeSet.getAuthor().equals(BEN)) {
return true;

return unrunChangeSets;
}

boolean isVintageChangeSet(String filename, ChangeSet changeSet) {
if (filename != null && filename.contains(LIQUIBASE_CORE_DATA_1_9_X_FILENAME) && changeSet.getAuthor().equals(BEN)) {
return changeSet.getId().equals(DISABLE_FOREIGN_KEY_CHECKS) || changeSet.getId().equals(ENABLE_FOREIGN_KEY_CHECKS);
}

return false;
}

Expand All @@ -218,10 +190,13 @@ boolean isVintageChangeSet(String filename, ChangeSet changeSet) {
boolean logUnRunChangeSetDetails(String filename, List<ChangeSet> changeSets) {
if (changeSets.size() < MAX_NUMBER_OF_CHANGE_SETS_TO_LOG && (filename.contains(LIQUIBASE_CORE_DATA_1_9_X_FILENAME)
|| filename.contains(LIQUIBASE_SCHEMA_ONLY_1_9_X_FILENAME))) {
for (ChangeSet changeSet : changeSets) {
log.info("file '{}' contains un-run change set with id '{}' by author '{}'", filename, changeSet.getId(),
changeSet.getAuthor());
if (log.isInfoEnabled()) {
for (ChangeSet changeSet : changeSets) {
log.info("file '{}' contains un-run change set with id '{}' by author '{}'", filename, changeSet.getId(),
changeSet.getAuthor());
}
}

return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static String enterLiquibaseUILoggingService() throws Exception {
// Temp workaround to silence liquibase writing to stdout - https://github.com/liquibase/liquibase/issues/2396,
// https://github.com/liquibase/liquibase/issues/3651
LoggerUIService loggerUIService = new LoggerUIService();
loggerUIService.setStandardLogLevel(Level.FINE);
loggerUIService.setStandardLogLevel(Level.INFO);
Map<String, Object> m = Collections.singletonMap(Scope.Attr.ui.name(), loggerUIService);
return Scope.enter(m);
}
Expand Down
2 changes: 1 addition & 1 deletion api/src/main/java/org/openmrs/module/ModuleUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public static void startup(Properties props) throws ModuleMustStartException {

String moduleListString = props.getProperty(ModuleConstants.RUNTIMEPROPERTY_MODULE_LIST_TO_LOAD);

if (moduleListString == null || moduleListString.length() == 0) {
if (moduleListString == null || moduleListString.isEmpty()) {
// Attempt to get all of the modules from the modules folder
// and store them in the modules list
log.debug("Starting all modules");
Expand Down
54 changes: 29 additions & 25 deletions api/src/main/java/org/openmrs/util/LocaleUtility.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class LocaleUtility implements GlobalPropertyListener {
* Cached version of the default locale. This is cached so that we don't have to look it up in
* the global property table every page load
*/
private static Locale defaultLocaleCache = null;
private static volatile Locale defaultLocaleCache = null;

/**
* Cached version of the localeAllowedList. This is cached so that we don't have to look it up
Expand All @@ -55,36 +55,40 @@ public class LocaleUtility implements GlobalPropertyListener {
*/
public static Locale getDefaultLocale() {
if (defaultLocaleCache == null) {
if (Context.isSessionOpen()) {
try {
String locale = Context.getAdministrationService().getGlobalProperty(
OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE);

if (StringUtils.hasLength(locale)) {
synchronized (LocaleUtility.class) {
if (defaultLocaleCache == null) {
if (Context.isSessionOpen()) {
try {
defaultLocaleCache = fromSpecification(locale);
Context.addProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
String locale = Context.getAdministrationService().getGlobalProperty(
OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE);

if (StringUtils.hasLength(locale)) {
try {
defaultLocaleCache = fromSpecification(locale);
} catch (Exception t) {
log.warn("Unable to parse default locale global property value: {}", locale, t);
}
}
} catch (Exception e) {
// don't print the full stack-trace by default
log.warn("Unable to get locale global property value. {}", e.getMessage());
log.debug("Exception caught while capturing locale global property value.", e);
} finally {
Context.removeProxyPrivilege(PrivilegeConstants.GET_GLOBAL_PROPERTIES);
}
catch (Exception t) {
log.warn("Unable to parse default locale global property value: " + locale, t);

// if we weren't able to load the locale from the global property,
// use the default one
if (defaultLocaleCache == null) {
defaultLocaleCache = fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE);
}
} else {
// if session is not open, return the default locale without caching
return fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE);
}
}
catch (Exception e) {
// swallow most of the stack trace for most users
log.warn("Unable to get locale global property value. " + e.getMessage());
log.trace("Unable to get locale global property value", e);
}

// if we weren't able to load the locale from the global property,
// use the default one
if (defaultLocaleCache == null) {
defaultLocaleCache = fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE);
}
} else {
// if session is not open, return the default locale without caching
return fromSpecification(OpenmrsConstants.GLOBAL_PROPERTY_DEFAULT_LOCALE_DEFAULT_VALUE);
}

}

return defaultLocaleCache;
Expand Down

0 comments on commit d7c80a6

Please sign in to comment.