From f8a6b85bb3804acc1117e92b9c24fc9beb72c285 Mon Sep 17 00:00:00 2001 From: Nathan Voxland Date: Mon, 24 Jan 2022 09:58:44 -0600 Subject: [PATCH 1/2] Log exceptions in CDILiquibase before rethrowing them --- .../integration/cdi/CDILiquibase.java | 64 +++++++++++-------- 1 file changed, 36 insertions(+), 28 deletions(-) diff --git a/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java b/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java index 1b8462faf2d..584b96627f9 100644 --- a/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java +++ b/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java @@ -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; @@ -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; @@ -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); } } @@ -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 entry: config.getParameters().entrySet()) { + for (Map.Entry entry : config.getParameters().entrySet()) { liquibase.setChangeLogParameter(entry.getKey(), entry.getValue()); } } @@ -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 From 2d93abfd354e290a546a538b604c308e2357c816 Mon Sep 17 00:00:00 2001 From: Nathan Voxland Date: Mon, 7 Feb 2022 12:59:03 -0600 Subject: [PATCH 2/2] Added CDILiquibase exception test --- .../integration/cdi/CDILiquibase.java | 5 ++-- .../integration/cdi/CDILiquibaseTest.java | 30 +++++++++++++++++++ 2 files changed, 33 insertions(+), 2 deletions(-) diff --git a/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java b/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java index 584b96627f9..5be39a08a7e 100644 --- a/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java +++ b/liquibase-cdi/src/main/java/liquibase/integration/cdi/CDILiquibase.java @@ -79,7 +79,8 @@ public class CDILiquibase implements Extension { @Inject @LiquibaseType - private CDILiquibaseConfig config; + protected CDILiquibaseConfig config; + @Inject @LiquibaseType private DataSource dataSource; @@ -132,7 +133,7 @@ public void onStartup() { } } - private void performUpdate() throws LiquibaseException { + protected void performUpdate() throws LiquibaseException { Connection c = null; Liquibase liquibase = null; try { diff --git a/liquibase-cdi/src/test/java/liquibase/integration/cdi/CDILiquibaseTest.java b/liquibase-cdi/src/test/java/liquibase/integration/cdi/CDILiquibaseTest.java index c88d9d1bcf7..ea724699f47 100644 --- a/liquibase-cdi/src/test/java/liquibase/integration/cdi/CDILiquibaseTest.java +++ b/liquibase-cdi/src/test/java/liquibase/integration/cdi/CDILiquibaseTest.java @@ -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.*; /** @@ -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"); + } + }); + } }