Skip to content

Commit

Permalink
TRUNK-6208: Upgrade 2.7 Platform to Liquibase 4.27.0 (openmrs#4637)
Browse files Browse the repository at this point in the history
* TRUNK-6208: Upgrade 2.7 Platform to Liquibase 4.27.0

* TRUNK-6208 remove h2 changeset

* TRUNK-6208 update changeset count after remove h2 changeset

* TRUNK-6208 change type from int(11) to int

---------

Co-authored-by: dkayiwa <[email protected]>
  • Loading branch information
2 people authored and Wandji69 committed Jun 27, 2024
1 parent 55ac37f commit 834c0bd
Show file tree
Hide file tree
Showing 15 changed files with 126 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,18 @@
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import liquibase.pro.packaged.Q;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.FlushMode;
import org.hibernate.SQLQuery;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.openmrs.Cohort;
import org.openmrs.Encounter;
import org.openmrs.EncounterProvider;
Expand Down
21 changes: 17 additions & 4 deletions api/src/main/java/org/openmrs/liquibase/ChangeLogDetective.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.changelog.ChangeSet;
import liquibase.command.core.StatusCommandStep;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -85,9 +86,16 @@ public String getInitialLiquibaseSnapshotVersion(String context, LiquibaseProvid
Liquibase liquibase = null;
try {
for (String filename : changeSets) {
String scopeId = LiquibaseScopeHandling.enterLiquibaseUILoggingService();

liquibase = liquibaseProvider.getLiquibase(filename);
List<ChangeSet> rawUnrunChangeSets = liquibase.listUnrunChangeSets(new Contexts(context),
new LabelExpression());

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);
Expand Down Expand Up @@ -141,9 +149,14 @@ public List<String> getUnrunLiquibaseUpdateFileNames(String snapshotVersion, Str
Liquibase liquibase = null;
try {
for (String filename : updateFileNames) {
String scopeId = LiquibaseScopeHandling.enterLiquibaseUILoggingService();
liquibase = liquibaseProvider.getLiquibase(filename);
List<ChangeSet> unrunChangeSets = liquibase.listUnrunChangeSets(new Contexts(context),
new LabelExpression());

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());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/**
* This Source Code Form is subject to the terms of the Mozilla Public License,
* v. 2.0. If a copy of the MPL was not distributed with this file, You can
* obtain one at http://mozilla.org/MPL/2.0/. OpenMRS is also distributed under
* the terms of the Healthcare Disclaimer located at http://openmrs.org/license.
*
* Copyright (C) OpenMRS Inc. OpenMRS is a registered trademark and the OpenMRS
* graphic logo is a trademark of OpenMRS Inc.
*/
package org.openmrs.liquibase;

import java.util.Collections;
import java.util.Map;
import java.util.logging.Level;

import liquibase.Scope;
import liquibase.ui.LoggerUIService;

public class LiquibaseScopeHandling {

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);
Map<String, Object> m = Collections.singletonMap(Scope.Attr.ui.name(), loggerUIService);
return Scope.enter(m);
}

public static void exitLiquibaseScope(String scopeId) throws Exception {
Scope.exit(scopeId);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,21 @@

import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.InputStreamList;
import liquibase.resource.Resource;
import org.openmrs.util.OpenmrsClassLoader;

import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
import java.util.List;

/**
* A customization of Liquibase's {@link ClassLoaderResourceAccessor} which defaults to the OpenMRS ClassLoader and has
* special handling for our liquibase.xml files, which occur multiple times on the classpath.
* @deprecated As of 2.7.0, replaced by the usage of {@link #search(String, boolean)} or {@link #getAll(String)},
* as this provides a better handling of paths that map to multiple resources using Liquibase's DUPLICATE_FILE_MODE.
* Refer to {@link liquibase.GlobalConfiguration#DUPLICATE_FILE_MODE} for the configuration and usage details.
*/
@Deprecated
public class OpenmrsClassLoaderResourceAccessor extends ClassLoaderResourceAccessor {

public OpenmrsClassLoaderResourceAccessor() {
Expand All @@ -33,15 +38,23 @@ public OpenmrsClassLoaderResourceAccessor(ClassLoader classLoader) {

@Override
public InputStreamList openStreams(String relativeTo, String streamPath) throws IOException {
InputStreamList result = super.openStreams(relativeTo, streamPath);
if (result != null && !result.isEmpty() && result.size() > 1) {
List<Resource> resources = super.getAll(streamPath);
InputStreamList result = new InputStreamList();
if (resources == null || resources.isEmpty()) {
return result;
}

for (Resource resource : resources) {
result.add(resource.getUri(), resource.openInputStream());
}
if (!result.isEmpty() && result.size() > 1) {
try (InputStreamList oldResult = result) {
URI uri = oldResult.getURIs().get(0);
result = new InputStreamList(uri, uri.toURL().openStream());
}

}

return result;
}
}
44 changes: 38 additions & 6 deletions api/src/main/java/org/openmrs/util/DatabaseUpdater.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
package org.openmrs.util;

import liquibase.Contexts;
import liquibase.GlobalConfiguration;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.RuntimeEnvironment;
Expand All @@ -23,6 +24,7 @@
import liquibase.changelog.filter.DbmsChangeSetFilter;
import liquibase.changelog.filter.ShouldRunChangeSetFilter;
import liquibase.changelog.visitor.UpdateVisitor;
import liquibase.command.core.StatusCommandStep;
import liquibase.database.Database;
import liquibase.database.DatabaseFactory;
import liquibase.database.jvm.JdbcConnection;
Expand All @@ -33,13 +35,15 @@
import liquibase.resource.CompositeResourceAccessor;
import liquibase.resource.FileSystemResourceAccessor;
import liquibase.resource.ResourceAccessor;
import liquibase.ui.LoggerUIService;
import org.apache.commons.io.IOUtils;
import org.openmrs.annotation.Authorized;
import org.openmrs.api.context.Context;
import org.openmrs.liquibase.ChangeLogDetective;
import org.openmrs.liquibase.ChangeLogVersionFinder;
import org.openmrs.liquibase.ChangeSetExecutorCallback;
import org.openmrs.liquibase.LiquibaseProvider;
import org.openmrs.liquibase.LiquibaseScopeHandling;
import org.openmrs.liquibase.OpenmrsClassLoaderResourceAccessor;
import org.openmrs.module.ModuleClassLoader;
import org.slf4j.Logger;
Expand Down Expand Up @@ -68,6 +72,7 @@
import java.util.Map;
import java.util.Properties;
import java.util.Set;
import java.util.logging.Level;

/**
* This class uses Liquibase to update the database. <br>
Expand Down Expand Up @@ -214,8 +219,13 @@ public static List<String> executeChangelog(String changeLogFile, Contexts conte

log.debug("Setting up liquibase object to run changelog: {}", changeLogFile);
Liquibase liquibase = getLiquibase(changeLogFile, cl);

int numChangeSetsToRun = liquibase.listUnrunChangeSets(contexts, new LabelExpression()).size();

String scopeId = LiquibaseScopeHandling.enterLiquibaseUILoggingService();
int numChangeSetsToRun = new StatusCommandStep()
.listUnrunChangeSets(contexts,
new LabelExpression(), liquibase.getDatabaseChangeLog(), liquibase.getDatabase()).size();
LiquibaseScopeHandling.exitLiquibaseScope(scopeId);

Database database = null;
LockService lockHandler = null;

Expand All @@ -233,7 +243,7 @@ public static List<String> executeChangelog(String changeLogFile, Contexts conte

// ensure that the change log history service is initialised
//
ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).init();
Scope.getCurrentScope().getSingleton(ChangeLogHistoryServiceFactory.class).getChangeLogService(database).init();

logIterator.run(new OpenmrsUpdateVisitor(database, callback, numChangeSetsToRun),
new RuntimeEnvironment(database, contexts, new LabelExpression()));
Expand Down Expand Up @@ -426,8 +436,13 @@ private static Liquibase getLiquibase(String changeLogFile, ClassLoader cl) thro
changeLogFile = EMPTY_CHANGE_LOG_FILE;
}

configureLiquibaseDuplicateFileMode();

// ensure that the change log history service is initialised
ChangeLogHistoryServiceFactory.getInstance().getChangeLogService(database).init();
Scope.getCurrentScope()
.getSingleton(ChangeLogHistoryServiceFactory.class)
.getChangeLogService(database)
.init();
return new Liquibase(changeLogFile, getCompositeResourceAccessor(cl), database);
}
catch (Exception e) {
Expand Down Expand Up @@ -604,13 +619,15 @@ public static List<OpenMRSChangeSet> getDatabaseChanges() throws Exception {
Liquibase liquibase = null;
try {
for (String filename : changeLogFileNames) {
String scopeId = LiquibaseScopeHandling.enterLiquibaseUILoggingService();
liquibase = getLiquibase(filename);
List<ChangeSet> changeSets = liquibase.getDatabaseChangeLog().getChangeSets();

for (ChangeSet changeSet : changeSets) {
OpenMRSChangeSet openMRSChangeSet = new OpenMRSChangeSet(changeSet, liquibase.getDatabase());
result.add(openMRSChangeSet);
}
LiquibaseScopeHandling.exitLiquibaseScope(scopeId);
liquibase.close();
}
}
Expand Down Expand Up @@ -669,18 +686,22 @@ public static List<OpenMRSChangeSet> getUnrunDatabaseChanges(String... changeLog

List<OpenMRSChangeSet> results = new ArrayList<>();

String scopeId = LiquibaseScopeHandling.enterLiquibaseUILoggingService();
for (String changelogFile : changeLogFilenames) {
Liquibase liquibase = getLiquibase(changelogFile, null);
database = liquibase.getDatabase();

List<ChangeSet> changeSets = liquibase.listUnrunChangeSets(new Contexts(CONTEXT), new LabelExpression());

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

for (ChangeSet changeSet : changeSets) {
OpenMRSChangeSet omrschangeset = new OpenMRSChangeSet(changeSet, database);
results.add(omrschangeset);
}
}

LiquibaseScopeHandling.exitLiquibaseScope(scopeId);
return results;

}
Expand Down Expand Up @@ -884,4 +905,15 @@ private static CompositeResourceAccessor getCompositeResourceAccessor(ClassLoade
ResourceAccessor fsFO = new FileSystemResourceAccessor(OpenmrsUtil.getApplicationDataDirectoryAsFile());
return new CompositeResourceAccessor(openmrsFO, fsFO);
}

private static void configureLiquibaseDuplicateFileMode() {
final String dupFlagModeKey = GlobalConfiguration.DUPLICATE_FILE_MODE.getKey();
final String dupFlagMode = Context.getRuntimeProperties().getProperty(dupFlagModeKey);

if (dupFlagMode != null) {
System.setProperty(dupFlagModeKey, dupFlagMode);
} else if (System.getProperty(dupFlagModeKey) == null) {
System.setProperty(dupFlagModeKey, OpenmrsConstants.LIQUIBASE_DUPLICATE_FILE_MODE_DEFAULT);
}
}
}
4 changes: 4 additions & 0 deletions api/src/main/java/org/openmrs/util/OpenmrsConstants.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.Properties;

import liquibase.GlobalConfiguration;
import org.apache.commons.io.IOUtils;
import org.openmrs.GlobalProperty;
import org.openmrs.api.context.Context;
Expand Down Expand Up @@ -1314,6 +1315,9 @@ public static enum PERSON_TYPE {
/** Value for the long person name format */
public static final String PERSON_NAME_FORMAT_LONG = "long";

// Liquibase Constants
public static final String LIQUIBASE_DUPLICATE_FILE_MODE_DEFAULT = GlobalConfiguration.DuplicateFileMode.WARN.name();

private OpenmrsConstants() {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1790,6 +1790,7 @@
<validCheckSum>1b6141be59a8a243b9e56d0aad952af</validCheckSum> <!-- checksum from before idExceptions added -->
<validCheckSum>89cc7a14b0582f157ea2dbb9de092fd</validCheckSum> <!-- current checksum with idExceptions param added -->
<validCheckSum><comment>Changes to new concept_reference tables</comment>3:4edd135921eb263d4811cf1c22ef4846</validCheckSum>
<validCheckSum><comment>Account for trailing whitespace</comment>9:5d41399f930d6de5c0030986b78f35e5</validCheckSum>
<preConditions onFail="MARK_RAN">
<not>
<or>
Expand Down Expand Up @@ -3521,6 +3522,7 @@
<validCheckSum><comment>After changing to boolean</comment>3:b57c0f651ed477457fd16e503eaf51a4</validCheckSum>
<validCheckSum>3:002108aacdf55731f4fd472d5308c5c8</validCheckSum>
<validCheckSum>3:6a72bbb390155596e52da4e8d065d1a2</validCheckSum>
<validCheckSum><comment>Account for trailing whitespace</comment>9:f41906e41efde3f7473abaa52fa161dc</validCheckSum>
<validCheckSum><comment>Fixing TRUNK-4040</comment>3:74026cd4543ebbf561999a81c276224d</validCheckSum>
<preConditions onFail="MARK_RAN">
<not>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@
<column name="retired" type="boolean" defaultValueBoolean="false">
<constraints nullable="false" />
</column>
<column name="retired_by" type="int(11)" />
<column name="retired_by" type="int" />
<column name="date_retired" type="datetime" />
<column name="retire_reason" type="varchar(255)" defaultValue="null" />
<column name="uuid" type="char(38)">
Expand Down
Loading

0 comments on commit 834c0bd

Please sign in to comment.