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
Changes from 1 commit
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,11 @@ public class CDILiquibase implements Extension {
@LiquibaseType
ResourceAccessor resourceAccessor;

@Inject @LiquibaseType
@Inject
@LiquibaseType
private CDILiquibaseConfig config;
@Inject @LiquibaseType
@Inject
@LiquibaseType
private DataSource dataSource;
private boolean initialized;
private boolean updateSuccessful;
Expand All @@ -94,33 +96,38 @@ 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);
}
}
Expand Down Expand Up @@ -158,7 +165,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 +180,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