Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Log exceptions in CDILiquibase before rethrowing them #2397

Merged
merged 3 commits into from Feb 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
}
});
}
}