Skip to content

Commit

Permalink
Merge pull request #2397 from liquibase/log-exceptions-in-cdi
Browse files Browse the repository at this point in the history
Log exceptions in CDILiquibase before rethrowing them
  • Loading branch information
nvoxland committed Feb 7, 2022
2 parents 5f00c17 + 2d93abf commit 7f807d0
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 30 deletions.
Expand Up @@ -10,8 +10,8 @@
import liquibase.exception.DatabaseException;
import liquibase.exception.LiquibaseException;
import liquibase.exception.UnexpectedLiquibaseException;
import liquibase.integration.commandline.LiquibaseCommandLineConfiguration;
import liquibase.integration.cdi.annotations.LiquibaseType;
import liquibase.integration.commandline.LiquibaseCommandLineConfiguration;
import liquibase.logging.Logger;
import liquibase.resource.ResourceAccessor;
import liquibase.util.LiquibaseUtil;
Expand Down Expand Up @@ -77,9 +77,12 @@ public class CDILiquibase implements Extension {
@LiquibaseType
ResourceAccessor resourceAccessor;

@Inject @LiquibaseType
private CDILiquibaseConfig config;
@Inject @LiquibaseType
@Inject
@LiquibaseType
protected CDILiquibaseConfig config;

@Inject
@LiquibaseType
private DataSource dataSource;
private boolean initialized;
private boolean updateSuccessful;
Expand All @@ -94,38 +97,43 @@ public boolean isUpdateSuccessful() {

@PostConstruct
public void onStartup() {
Logger log = Scope.getCurrentScope().getLog(getClass());

log.info("Booting Liquibase " + LiquibaseUtil.getBuildVersionInfo());
String hostName;
try {
hostName = NetUtil.getLocalHostName();
} catch (Exception e) {
log.warning("Cannot find hostname: " + e.getMessage());
log.fine("", e);
return;
}
Logger log = Scope.getCurrentScope().getLog(getClass());

log.info("Booting Liquibase " + LiquibaseUtil.getBuildVersionInfo());
String hostName;
try {
hostName = NetUtil.getLocalHostName();
} catch (Exception e) {
log.warning("Cannot find hostname: " + e.getMessage());
log.fine("", e);
return;
}

if (!LiquibaseCommandLineConfiguration.SHOULD_RUN.getCurrentValue()) {
log.info(String.format("Liquibase did not run on %s because %s was set to false.",
hostName,
LiquibaseCommandLineConfiguration.SHOULD_RUN.getKey()
));
return;
}
if (!config.getShouldRun()) {
log.info(String.format("Liquibase did not run on %s because CDILiquibaseConfig.shouldRun was set to false.", hostName));
return;
}
initialized = true;
try {
if (!LiquibaseCommandLineConfiguration.SHOULD_RUN.getCurrentValue()) {
log.info(String.format("Liquibase did not run on %s because %s was set to false.",
hostName,
LiquibaseCommandLineConfiguration.SHOULD_RUN.getKey()
));
return;
}
if (!config.getShouldRun()) {
log.info(String.format("Liquibase did not run on %s because CDILiquibaseConfig.shouldRun was set to false.", hostName));
return;
}
initialized = true;
performUpdate();
} catch (LiquibaseException e) {
} catch (Throwable e) {
Scope.getCurrentScope().getLog(getClass()).severe(e.getMessage(), e);
if (e instanceof RuntimeException) {
throw (RuntimeException) e;
}

throw new UnexpectedLiquibaseException(e);
}
}

private void performUpdate() throws LiquibaseException {
protected void performUpdate() throws LiquibaseException {
Connection c = null;
Liquibase liquibase = null;
try {
Expand Down Expand Up @@ -158,7 +166,7 @@ private void performUpdate() throws LiquibaseException {
protected Liquibase createLiquibase(Connection c) throws LiquibaseException {
Liquibase liquibase = new Liquibase(config.getChangeLog(), resourceAccessor, createDatabase(c));
if (config.getParameters() != null) {
for(Map.Entry<String, String> entry: config.getParameters().entrySet()) {
for (Map.Entry<String, String> entry : config.getParameters().entrySet()) {
liquibase.setChangeLogParameter(entry.getKey(), entry.getValue());
}
}
Expand All @@ -173,6 +181,7 @@ protected Liquibase createLiquibase(Connection c) throws LiquibaseException {
/**
* Subclasses may override this method add change some database settings such as
* default schema before returning the database object.
*
* @param c
* @return a Database implementation retrieved from the {@link liquibase.database.DatabaseFactory}.
* @throws DatabaseException
Expand Down
@@ -1,12 +1,19 @@
package liquibase.integration.cdi;

import liquibase.Scope;
import liquibase.configuration.LiquibaseConfiguration;
import liquibase.exception.LiquibaseException;
import liquibase.logging.core.BufferedLogService;
import liquibase.logging.core.CompositeLogService;
import liquibase.logging.core.CompositeLogger;
import org.jboss.weld.environment.se.Weld;
import org.jboss.weld.environment.se.WeldContainer;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.util.logging.Level;

import static org.junit.Assert.*;

/**
Expand Down Expand Up @@ -54,4 +61,27 @@ public void shouldRunWhenConfigShouldRunIsTrue() {
System.setProperty("liquibase.config.shouldRun", "true");
validateRunningState(true);
}

@Test
public void onStartupExceptionsAreCorrectlyHandled() throws Exception {
System.setProperty("liquibase.config.shouldRun", "true");
final CDILiquibase cdi = new CDILiquibase() {
@Override
protected void performUpdate() {
throw new IllegalArgumentException("Tested Exception");
}
};
cdi.config = new CDILiquibaseConfig();

BufferedLogService bufferLog = new BufferedLogService();
Scope.child(Scope.Attr.logService.name(), bufferLog, () -> {
try {
cdi.onStartup();
fail("Did not throw exception");
} catch (IllegalArgumentException e) {
assert e.getMessage().equals("Tested Exception");
assert bufferLog.getLogAsString(Level.SEVERE).contains("SEVERE Tested Exception");
}
});
}
}

0 comments on commit 7f807d0

Please sign in to comment.