forked from mekomsolutions/openmrs-module-initializer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
mekomsolutions#255 Liquibase loading fails when trying to execute sql…
…Files relative to changeset
- Loading branch information
Showing
14 changed files
with
416 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<project xmlns="http://maven.apache.org/POM/4.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> | ||
<parent> | ||
<artifactId>initializer</artifactId> | ||
<groupId>org.openmrs.module</groupId> | ||
<version>2.7.0-SNAPSHOT</version> | ||
</parent> | ||
<modelVersion>4.0.0</modelVersion> | ||
|
||
<artifactId>initializer-api-2.5</artifactId> | ||
<packaging>jar</packaging> | ||
<name>Initializer API 2.5</name> | ||
<description>API 2.5 project for Initializer</description> | ||
|
||
<properties> | ||
<openmrsPlatformVersion>${openmrsVersion2.5}</openmrsPlatformVersion> | ||
<datafilterVersion>2.2.0</datafilterVersion> | ||
</properties> | ||
|
||
<dependencies> | ||
|
||
<dependency> | ||
<groupId>org.openmrs.test</groupId> | ||
<artifactId>openmrs-test</artifactId> | ||
<type>pom</type> | ||
<version>${openmrsPlatformVersion}</version> | ||
<scope>test</scope> | ||
<exclusions> | ||
<exclusion> | ||
<groupId>org.powermock</groupId> | ||
<artifactId>powermock-api-mockito2</artifactId> | ||
</exclusion> | ||
</exclusions> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.parent.groupId}</groupId> | ||
<artifactId>${project.parent.artifactId}-api</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.parent.groupId}</groupId> | ||
<artifactId>${project.parent.artifactId}-api</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>test</scope> | ||
<type>test-jar</type> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.parent.groupId}</groupId> | ||
<artifactId>${project.parent.artifactId}-api-2.4</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.parent.groupId}</groupId> | ||
<artifactId>${project.parent.artifactId}-api-2.3</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.parent.groupId}</groupId> | ||
<artifactId>${project.parent.artifactId}-api-2.3</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>test</scope> | ||
<type>test-jar</type> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.parent.groupId}</groupId> | ||
<artifactId>${project.parent.artifactId}-api-2.2</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>${project.parent.groupId}</groupId> | ||
<artifactId>${project.parent.artifactId}-api-2.2</artifactId> | ||
<version>${project.parent.version}</version> | ||
<scope>test</scope> | ||
<type>test-jar</type> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.openmrs.module</groupId> | ||
<artifactId>fhir2-api-2.5</artifactId> | ||
<version>${fhir2Version}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>org.openmrs.module</groupId> | ||
<artifactId>datafilter-api</artifactId> | ||
<version>${datafilterVersion}</version> | ||
<scope>provided</scope> | ||
</dependency> | ||
|
||
</dependencies> | ||
|
||
</project> |
96 changes: 96 additions & 0 deletions
96
api-2.5/src/main/java/org/openmrs/module/initializer/api/loaders/LiquibaseLoader2_5.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.openmrs.module.initializer.api.loaders; | ||
|
||
import org.openmrs.annotation.OpenmrsProfile; | ||
import org.openmrs.api.context.Context; | ||
import org.openmrs.liquibase.ChangeSetExecutorCallback; | ||
import org.openmrs.module.initializer.Domain; | ||
import org.openmrs.module.initializer.api.ConfigDirUtil; | ||
import org.openmrs.util.DatabaseUpdater; | ||
import org.openmrs.util.OpenmrsUtil; | ||
|
||
import java.io.File; | ||
import java.nio.file.Path; | ||
import java.nio.file.Paths; | ||
import java.util.List; | ||
|
||
@OpenmrsProfile(openmrsPlatformVersion = "2.5") | ||
public class LiquibaseLoader2_5 extends BaseFileLoader { | ||
|
||
public static final String LIQUIBASE_FILE_NAME = "liquibase"; | ||
|
||
@Override | ||
protected Domain getDomain() { | ||
return Domain.LIQUIBASE; | ||
} | ||
|
||
@Override | ||
protected String getFileExtension() { | ||
return "xml"; | ||
} | ||
|
||
@Override | ||
protected void load(File file) throws Exception { | ||
if (file.getName().equalsIgnoreCase(LIQUIBASE_FILE_NAME + "." + getFileExtension())) { | ||
// Because liquibase uses the path provided as an identifier of the changeset, we need to relativize it | ||
// This ensures that liquibase changesets are not re-executed if the absolute path on the server changes | ||
String absolutePath = file.getAbsolutePath(); | ||
String relativePath = getPathRelativeToApplicationDataDirectory(file); | ||
updateExistingLiquibaseChangeLogPathsIfNeeded(absolutePath, relativePath); | ||
try { | ||
DatabaseUpdater.executeChangelog(relativePath, (ChangeSetExecutorCallback) null); | ||
} | ||
catch (Exception e) { | ||
log.error("An error occurred executing liquibase file: " + file, e); | ||
throw e; | ||
} | ||
} | ||
} | ||
|
||
/** | ||
* If a particular changeset file is being executed, and this is referenced by the given oldPath, | ||
* then update that changeset to reference the given newPath | ||
*/ | ||
protected void updateExistingLiquibaseChangeLogPathsIfNeeded(String oldPath, String newPath) { | ||
log.debug("Checking if liquibase filenames need to be updated for " + oldPath); | ||
int numRows = sqlCount("select count(*) from liquibasechangelog where filename = '" + oldPath + "'"); | ||
if (numRows > 0) { | ||
log.warn("Liquibase filename update is needed for " + oldPath + ". Updating to " + newPath); | ||
sqlUpdate("update liquibasechangelog set filename = '" + newPath + "' where filename = '" + oldPath + "'"); | ||
} else { | ||
log.debug("Liquibase filename update is not required"); | ||
} | ||
} | ||
|
||
/** | ||
* @param file the file to relativize | ||
* @return the path of the given file, relativized to the OpenMRS application data directory | ||
*/ | ||
protected String getPathRelativeToApplicationDataDirectory(File file) { | ||
Path appDataDirPath = Paths.get(OpenmrsUtil.getApplicationDataDirectory()); | ||
Path liquibaseFilePath = file.toPath(); | ||
return appDataDirPath.relativize(liquibaseFilePath).toString(); | ||
} | ||
|
||
/** | ||
* @param sql the sql to execute | ||
* @return the results of that execution, where the first column of the first row is returned as an | ||
* integer count | ||
*/ | ||
private int sqlCount(String sql) { | ||
List<List<Object>> results = Context.getAdministrationService().executeSQL(sql, true); | ||
Object singleResult = results.get(0).get(0); | ||
return Integer.parseInt(singleResult.toString()); | ||
} | ||
|
||
/** | ||
* @param sql the sql to execute as a database update operation | ||
*/ | ||
private void sqlUpdate(String sql) { | ||
Context.getAdministrationService().executeSQL(sql, false); | ||
} | ||
|
||
@Override | ||
public ConfigDirUtil getDirUtil() { | ||
return new ConfigDirUtil(iniz.getConfigDirPath(), iniz.getChecksumsDirPath(), getDomainName(), true); | ||
} | ||
} |
110 changes: 110 additions & 0 deletions
110
...est/java/org/openmrs/module/initializer/api/loaders/LiquibaseLoader25IntegrationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
/** | ||
* 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.module.initializer.api.loaders; | ||
|
||
import org.apache.commons.io.IOUtils; | ||
import org.junit.After; | ||
import org.junit.Assert; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.openmrs.api.AdministrationService; | ||
import org.openmrs.module.initializer.DomainBaseModuleContextSensitiveTest; | ||
import org.openmrs.util.OpenmrsClassLoader; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Qualifier; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.List; | ||
|
||
public class LiquibaseLoader25IntegrationTest extends DomainBaseModuleContextSensitiveTest { | ||
|
||
@Autowired | ||
private LiquibaseLoader2_5 loader; | ||
|
||
@Autowired | ||
@Qualifier("adminService") | ||
private AdministrationService adminService; | ||
|
||
@Before | ||
public void setup() throws Exception { | ||
System.setProperty("useInMemoryDatabase", "true"); | ||
ClassLoader cl = OpenmrsClassLoader.getInstance(); | ||
String schemaSql = IOUtils.resourceToString("liquibase-schema.sql", StandardCharsets.UTF_8, cl); | ||
adminService.executeSQL(schemaSql, false); | ||
} | ||
|
||
@After | ||
public void teardown() { | ||
adminService.executeSQL("drop table LIQUIBASECHANGELOG", false); | ||
adminService.executeSQL("drop table LIQUIBASECHANGELOGLOCK", false); | ||
} | ||
|
||
@Test | ||
public void load_shouldExecuteNewChangeSet() { | ||
String relativePath = "configuration/liquibase/liquibase.xml"; | ||
String absolutePath = getAppDataDirPath() + relativePath; | ||
Assert.assertEquals(0, numChangeLogEntries(absolutePath)); | ||
Assert.assertEquals(0, numChangeLogEntries(relativePath)); | ||
Assert.assertNull(adminService.getGlobalProperty("test_changes_1")); | ||
Assert.assertNull(adminService.getGlobalProperty("test_changes_2")); | ||
loader.load(); | ||
Assert.assertEquals(0, numChangeLogEntries(absolutePath)); | ||
Assert.assertEquals(2, numChangeLogEntries(relativePath)); | ||
Assert.assertEquals("true", adminService.getGlobalProperty("test_changes_1")); | ||
Assert.assertEquals("true", adminService.getGlobalProperty("test_changes_2")); | ||
} | ||
|
||
@Test | ||
public void load_shouldUpdateAbsolutePathToRelativePathIfNeeded() { | ||
String relativePath = "configuration/liquibase/liquibase.xml"; | ||
String absolutePath = getAppDataDirPath() + relativePath; | ||
insertExistingChangeLogEntry(absolutePath); | ||
Assert.assertEquals(1, numChangeLogEntries(absolutePath)); | ||
Assert.assertEquals(0, numChangeLogEntries(relativePath)); | ||
loader.updateExistingLiquibaseChangeLogPathsIfNeeded(absolutePath, relativePath); | ||
Assert.assertEquals(0, numChangeLogEntries(absolutePath)); | ||
Assert.assertEquals(1, numChangeLogEntries(relativePath)); | ||
} | ||
|
||
private int numChangeLogEntries(String filename) { | ||
List<List<Object>> ret = adminService | ||
.executeSQL("select count(*) from liquibasechangelog where filename = '" + filename + "'", true); | ||
return Integer.parseInt(ret.get(0).get(0).toString()); | ||
} | ||
|
||
private void insertExistingChangeLogEntry(String filename) { | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append("INSERT INTO LIQUIBASECHANGELOG ("); | ||
sb.append(" ID, "); | ||
sb.append(" AUTHOR, "); | ||
sb.append(" FILENAME, "); | ||
sb.append(" DATEEXECUTED, "); | ||
sb.append(" ORDEREXECUTED, "); | ||
sb.append(" EXECTYPE, "); | ||
sb.append(" MD5SUM, "); | ||
sb.append(" DESCRIPTION, "); | ||
sb.append(" COMMENTS, "); | ||
sb.append(" LIQUIBASE "); | ||
sb.append(") "); | ||
sb.append("values ("); | ||
sb.append(" 'previousSqlFileChangeset', "); | ||
sb.append(" 'iniz', "); | ||
sb.append(" '").append(filename).append("', "); | ||
sb.append(" '2023-11-01 00:00:00', "); | ||
sb.append(" 1, "); | ||
sb.append(" 'EXECUTED', "); | ||
sb.append(" '8:4019e34234869ff55c81acf9d779f2a7', "); | ||
sb.append(" 'sqlFile', "); | ||
sb.append(" '', "); | ||
sb.append(" '4.4.1'"); | ||
sb.append(")"); | ||
adminService.executeSQL(sb.toString(), false); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
CREATE TABLE LIQUIBASECHANGELOG | ||
( | ||
ID varchar(255) NOT NULL, | ||
AUTHOR varchar(255) NOT NULL, | ||
FILENAME varchar(255) NOT NULL, | ||
DATEEXECUTED datetime NOT NULL, | ||
ORDEREXECUTED int(11) NOT NULL, | ||
EXECTYPE varchar(10) NOT NULL, | ||
MD5SUM varchar(35), | ||
DESCRIPTION varchar(255), | ||
COMMENTS varchar(255), | ||
TAG varchar(255), | ||
LIQUIBASE varchar(20), | ||
CONTEXTS varchar(255), | ||
LABELS varchar(255), | ||
DEPLOYMENT_ID varchar(10), | ||
PRIMARY KEY (ID, AUTHOR, FILENAME) | ||
); | ||
|
||
CREATE TABLE LIQUIBASECHANGELOGLOCK | ||
( | ||
ID int(11) NOT NULL, | ||
LOCKED tinyint(1) NOT NULL, | ||
LOCKGRANTED datetime, | ||
LOCKEDBY varchar(255), | ||
PRIMARY KEY (ID) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
<?xml version="1.0" encoding="UTF-8" ?> | ||
<!-- | ||
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. | ||
--> | ||
|
||
<Configuration xmlns="http://logging.apache.org/log4j/2.0/config"> | ||
<Properties> | ||
<!-- The default pattern is stored as a property so that it's only defined once. | ||
It's also quite challenging to escape using Log4j2's variable substitution. --> | ||
<Property name="defaultPattern">%p - %C{1}.%M(%L) |%d{ISO8601}| %m%n</Property> | ||
</Properties> | ||
<Appenders> | ||
<!-- the console appender is not required but usually a good idea --> | ||
<Console name="CONSOLE" target="SYSTEM_OUT"> | ||
<PatternLayout pattern="${openmrs:logLayout:-${defaultPattern}}" /> | ||
</Console> | ||
</Appenders> | ||
<Loggers> | ||
<Logger name="org.openmrs.module.initializer" level="INFO" /> | ||
<Logger name="org.springframework" additivity="false" level="WARN" /> | ||
<Logger name="org.hibernate" additivity="false" level="ERROR" /> | ||
<Root level="WARN"> | ||
<AppenderRef ref="CONSOLE" /> | ||
</Root> | ||
</Loggers> | ||
</Configuration> |
16 changes: 16 additions & 0 deletions
16
api-2.5/src/test/resources/testAppDataDir/configuration/liquibase/liquibase.xml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
|
||
<databaseChangeLog | ||
xmlns="http://www.liquibase.org/xml/ns/dbchangelog" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation="http://www.liquibase.org/xml/ns/dbchangelog http://www.liquibase.org/xml/ns/dbchangelog/dbchangelog-2.0.xsd"> | ||
|
||
<changeSet id="previousSqlFileChangeset" author="iniz" runAlways="true" runOnChange="true"> | ||
<sqlFile endDelimiter="#" path="sql/test_changes_1.sql" relativeToChangelogFile="true" stripComments="true" /> | ||
</changeSet> | ||
|
||
<changeSet id="newSqlFileChangeset" author="iniz" runAlways="true" runOnChange="true"> | ||
<sqlFile endDelimiter="#" path="sql/test_changes_2.sql" relativeToChangelogFile="true" stripComments="true" /> | ||
</changeSet> | ||
|
||
</databaseChangeLog> |
1 change: 1 addition & 0 deletions
1
api-2.5/src/test/resources/testAppDataDir/configuration/liquibase/sql/test_changes_1.sql
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
insert into global_property (uuid, property, property_value) values (uuid(), 'test_changes_1', 'true'); |
Oops, something went wrong.