Skip to content

Commit

Permalink
TRUNK-6208: Upgrade 2.7 Platform to Liquibase 4.27.0
Browse files Browse the repository at this point in the history
  • Loading branch information
k4pran committed May 19, 2024
1 parent e962f22 commit ccd32e4
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 31 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
15 changes: 11 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 @@ -86,8 +87,11 @@ public String getInitialLiquibaseSnapshotVersion(String context, LiquibaseProvid
try {
for (String filename : changeSets) {
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());

liquibase.close();

List<ChangeSet> refinedUnrunChangeSets = excludeVintageChangeSets(filename, rawUnrunChangeSets);
Expand Down Expand Up @@ -142,8 +146,11 @@ public List<String> getUnrunLiquibaseUpdateFileNames(String snapshotVersion, Str
try {
for (String filename : updateFileNames) {
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());

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
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;
}
}
33 changes: 28 additions & 5 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 Down Expand Up @@ -215,7 +217,10 @@ 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();
int numChangeSetsToRun = new StatusCommandStep()
.listUnrunChangeSets(contexts,
new LabelExpression(), liquibase.getDatabaseChangeLog(), liquibase.getDatabase()).size();

Database database = null;
LockService lockHandler = null;

Expand All @@ -233,7 +238,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 +431,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 @@ -673,8 +683,10 @@ public static List<OpenMRSChangeSet> getUnrunDatabaseChanges(String... changeLog
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);
Expand Down Expand Up @@ -884,4 +896,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 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 @@ -11,7 +11,7 @@
graphic logo is a trademark of OpenMRS Inc.
-->
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.6.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd">
<databaseChangeLog logicalFilePath="liquibase-core-data.xml" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.6.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd">
<changeSet author="manoj (generated)" id="1698595908058-17">
<insert tableName="person">
<column name="person_id" valueNumeric="1"/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
graphic logo is a trademark of OpenMRS Inc.
-->
<databaseChangeLog xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.6.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd">
<databaseChangeLog logicalFilePath="liquibase-schema-only.xml" xmlns="http://www.liquibase.org/xml/ns/dbchangelog" xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext" xmlns:pro="http://www.liquibase.org/xml/ns/pro" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog-ext http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-ext.xsd http://www.liquibase.org/xml/ns/pro http://www.liquibase.org/xml/ns/pro/liquibase-pro-4.6.xsd http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-4.6.xsd">
<changeSet author="manoj (generated)" id="1698595905189-1">
<createTable tableName="allergy">
<column autoIncrement="true" name="allergy_id" type="INT">
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 @@ -11,6 +11,7 @@
-->
<databaseChangeLog
logicalFilePath="liquibase-update-to-latest.xml"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
-->
<databaseChangeLog
logicalFilePath="liquibase-update-to-latest.xml"
xmlns="http://www.liquibase.org/xml/ns/dbchangelog"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:ext="http://www.liquibase.org/xml/ns/dbchangelog-ext"
Expand Down Expand Up @@ -99,5 +100,14 @@
CREATE FUNCTION UUID() RETURNS UUID LANGUAGE SQL AS $$ SELECT uuid_generate_v1() $$;
</sql>
</changeSet>

<changeSet id="TRUNK-6208-2024-05-18-1004" author="k4pran" dbms="h2">
<!--
Changing the column type to int(10) due to Liquibase change where integer precision
greater than 10 is now interpreted as BIGINT in H2 databases.
See: https://github.com/liquibase/liquibase/pull/4008/files
-->
<modifyDataType tableName="program_attribute_type" columnName="retired_by" newDataType="int(10)"/>
</changeSet>

</databaseChangeLog>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class DatabaseUpdaterDatabaseIT extends H2DatabaseIT {
* This constant needs to be updated when adding new Liquibase update files to openmrs-core.
*/

private static final int CHANGE_SET_COUNT_FOR_GREATER_THAN_2_1_X = 897;
private static final int CHANGE_SET_COUNT_FOR_GREATER_THAN_2_1_X = 898;

private static final int CHANGE_SET_COUNT_FOR_2_1_X = 870;

Expand Down
Loading

0 comments on commit ccd32e4

Please # to comment.