Skip to content

Commit

Permalink
Accommodate Liquibase update
Browse files Browse the repository at this point in the history
  • Loading branch information
greenc-FNAL committed Oct 2, 2024
1 parent a64c5aa commit b4adac4
Showing 1 changed file with 2 additions and 110 deletions.
112 changes: 2 additions & 110 deletions modules/dcache/src/main/java/org/dcache/util/SpringLiquibase.java
Original file line number Diff line number Diff line change
@@ -1,41 +1,22 @@
package org.dcache.util;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLClassLoader;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.Vector;
import java.util.jar.Manifest;
import liquibase.Contexts;
import liquibase.LabelExpression;
import liquibase.Liquibase;
import liquibase.changelog.ChangeSet;
import liquibase.exception.LiquibaseException;
import liquibase.exception.MigrationFailedException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.logging.LogFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.ResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternUtils;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ConnectionCallback;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.lang.Nullable;

public class SpringLiquibase
extends liquibase.integration.spring.SpringLiquibase {

private static final Logger LOGGER =
LoggerFactory.getLogger(SpringLiquibase.class);

private boolean _shouldUpdate = true;

public void setShouldUpdate(boolean shouldUpdate) {
Expand All @@ -55,98 +36,9 @@ protected JdbcTemplate getJdbcTemplate() {
return new JdbcTemplate(getDataSource());
}

protected SpringResourceOpener createResourceOpener() {
return new DcacheSpringResourceOpener(getChangeLog());
}

protected class DcacheSpringResourceOpener extends SpringResourceOpener {

public DcacheSpringResourceOpener(String parentFile) {
super(parentFile);
}

// Workaround for https://liquibase.jira.com/browse/CORE-3001
@Override
protected void init() {
try {
Enumeration<URL> baseUrls;
ClassLoader classLoader = toClassLoader();
if (classLoader != null) {
if (classLoader instanceof URLClassLoader) {
baseUrls = new Vector<URL>(
Arrays.asList(((URLClassLoader) classLoader).getURLs())).elements();

while (baseUrls.hasMoreElements()) {
addRootPath(baseUrls.nextElement());
}
}

baseUrls = classLoader.getResources("");

while (baseUrls.hasMoreElements()) {
addRootPath(baseUrls.nextElement());
}
}
} catch (IOException e) {
throw new UnexpectedLiquibaseException(e);
}
try {
Resource[] resources = ResourcePatternUtils.getResourcePatternResolver(
getResourceLoader()).getResources("");
if (resources.length == 0 || resources.length == 1
&& !resources[0].exists()) { //sometimes not able to look up by empty string, try all the liquibase packages
Set<String> liquibasePackages = new HashSet<>();
for (Resource manifest : ResourcePatternUtils.getResourcePatternResolver(
getResourceLoader()).getResources(
ResourcePatternResolver.CLASSPATH_ALL_URL_PREFIX
+ "META-INF/MANIFEST.MF")) {
if (manifest.exists()) {
InputStream inputStream = null;
try {
inputStream = manifest.getInputStream();
Manifest manifestObj = new Manifest(inputStream);
String attr = manifestObj.getMainAttributes()
.getValue("Liquibase-Package");
if (attr != null) {
String packages = "\\s*,\\s*";
for (String fullPackage : attr.split(packages)) {
liquibasePackages.add(fullPackage.split("\\.")[0]);
}
}
} finally {
if (inputStream != null) {
inputStream.close();
}
}
}
}

if (liquibasePackages.size() == 0) {
LogFactory.getInstance().getLog().warning(
"No Liquibase-Packages entry found in MANIFEST.MF. Using fallback of entire 'liquibase' package");
liquibasePackages.add("liquibase");
}

for (String foundPackage : liquibasePackages) {
resources = ResourcePatternUtils.getResourcePatternResolver(
getResourceLoader()).getResources(foundPackage);
for (Resource res : resources) {
addRootPath(res.getURL());
}
}
} else {
for (Resource res : resources) {
addRootPath(res.getURL());
}
}
} catch (IOException e) {
LogFactory.getInstance().getLog().warning("Error initializing SpringLiquibase", e);
}
}
}

private class SchemaMigrator implements ConnectionCallback<Void> {

@Nullable
@Override
public Void doInConnection(Connection c)
throws SQLException, DataAccessException {
Expand Down

0 comments on commit b4adac4

Please sign in to comment.