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

Handle AutoCloseable connexion and database #9

Closed
Closed
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
12 changes: 11 additions & 1 deletion autoconfigure/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ limitations under the License.
-->
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<parent>
<groupId>net.lbruun.springboot</groupId>
<artifactId>preliquibase-spring-boot-parent</artifactId>
<version>1.3.1-SNAPSHOT</version>
</parent>

<artifactId>preliquibase-spring-boot-autoconfigure</artifactId>
<packaging>jar</packaging>
<name>Spring Boot AutoConfiguration :: Pre-Liquibase AutoConfiguration</name>

<description>Autoconfiguration for Pre-Liquibase</description>

<dependencies>
Expand Down Expand Up @@ -53,13 +56,20 @@ limitations under the License.
<artifactId>hsqldb</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>

<build>
<plugins>

<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.2</version> <!-- Must be at least 2.22 for Junit 5 -->
<version>2.22.2</version> <!-- Must be at least 2.22 for Junit 5 -->
</plugin>

<!-- Do deploy current project -->
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,20 @@
*/
package net.lbruun.springboot.preliquibase;

import liquibase.change.DatabaseChange;
import liquibase.integration.spring.SpringLiquibase;
import net.lbruun.springboot.preliquibase.PreLiquibaseAutoConfiguration.EnabledCondition;
import net.lbruun.springboot.preliquibase.PreLiquibaseAutoConfiguration.LiquibaseDataSourceCondition;
import java.util.Objects;
import javax.sql.DataSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.AbstractDependsOnBeanFactoryPostProcessor;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.*;
import org.springframework.boot.autoconfigure.condition.AllNestedConditions;
import org.springframework.boot.autoconfigure.condition.AnyNestedCondition;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.autoconfigure.liquibase.LiquibaseAutoConfiguration;
Expand All @@ -38,9 +41,10 @@
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.lang.NonNull;

import javax.sql.DataSource;
import java.util.Objects;
import liquibase.change.DatabaseChange;
import liquibase.integration.spring.SpringLiquibase;
import net.lbruun.springboot.preliquibase.PreLiquibaseAutoConfiguration.EnabledCondition;
import net.lbruun.springboot.preliquibase.PreLiquibaseAutoConfiguration.LiquibaseDataSourceCondition;

/**
* Auto-configuration for Pre-Liquibase.
Expand All @@ -56,13 +60,13 @@ public class PreLiquibaseAutoConfiguration {

Logger logger = LoggerFactory.getLogger(PreLiquibaseAutoConfiguration.class);

/**
* Returns provider which will tell which {@code DataSource} to use for
* Pre-Liquibase. This will return a provider which will resolve to the same
* DataSource as used by Liquibase itself, however an application can
* configure its own bean of type {@code PreLiquibaseDataSourceProvider} and
* thereby override which DataSource to use for Pre-Liquibase.
*/
/**
* Returns provider which will tell which {@code DataSource} to use for Pre-Liquibase. This will
* return a provider which will resolve to the same DataSource as used by Liquibase itself,
* however an application can configure its own bean of type
* {@code PreLiquibaseDataSourceProvider} and thereby override which DataSource to use for
* Pre-Liquibase.
*/
@ConditionalOnMissingBean({PreLiquibaseDataSourceProvider.class})
@Bean
public PreLiquibaseDataSourceProvider preLiquibaseDataSourceProvider(
Expand All @@ -89,7 +93,7 @@ public PreLiquibase preLiquibase(
ApplicationContext applicationContext) {
logger.debug("Instantiation of PreLiquibase");

PreLiquibase preLiquibase = new PreLiquibase(
final PreLiquibase preLiquibase = new PreLiquibase(
environment,
dataSourceProvider.getDataSource(),
properties,
Expand All @@ -106,10 +110,9 @@ public PreLiquibase preLiquibase(
* declaring that Pre-Liquibase must execute before Liquibase, we declare
* the opposite: that Liquibase must execute after Pre-Liquibase.
*/
@Configuration()
@Configuration
@ConditionalOnClass(SpringLiquibase.class)
static class LiquibaseOnPreLiquibaseDependencyPostProcessor
extends AbstractDependsOnBeanFactoryPostProcessor {
static class LiquibaseOnPreLiquibaseDependencyPostProcessor extends AbstractDependsOnBeanFactoryPostProcessor {

Logger logger = LoggerFactory.getLogger(LiquibaseOnPreLiquibaseDependencyPostProcessor.class);

Expand All @@ -125,7 +128,6 @@ static class LiquibaseOnPreLiquibaseDependencyPostProcessor
* Liquibase.
*/
static final class LiquibaseDataSourceCondition extends AnyNestedCondition {

LiquibaseDataSourceCondition() {
super(ConfigurationPhase.REGISTER_BEAN);
}
Expand Down Expand Up @@ -197,19 +199,17 @@ public DefaultPreLiquibaseDataSourceProvider(
@NonNull ObjectProvider<DataSource> dataSource,
@NonNull ObjectProvider<DataSource> liquibaseDataSource) {

// Here we re-use Spring Boot's own LiquibaseAutoConfiguration
// so that we figure out which DataSource will (later) be used
// by LiquibaseAutoConfiguration. This ensures that we use the same
// logic for figuring out which DataSource to use.
// Note that SpringLiquibase object below gets instantiated OUTSIDE
// of the IoC container, meaning it is just normal "new" instantiaion.
// This is important as we do not want the SpringLiquibase's
// afterPropertiesSet method to kick in. All we are interested in
// is to figure out which datasource Liquibase would be using.
LiquibaseAutoConfiguration.LiquibaseConfiguration liquibaseConfiguration
= new LiquibaseAutoConfiguration.LiquibaseConfiguration(liquibaseProperties);
SpringLiquibase liquibase = liquibaseConfiguration.liquibase(
dataSource, liquibaseDataSource);
// Here we re-use Spring Boot's own LiquibaseAutoConfiguration
// so that we figure out which DataSource will (later) be used
// by LiquibaseAutoConfiguration. This ensures that we use the same
// logic for figuring out which DataSource to use.
// Note that SpringLiquibase object below gets instantiated OUTSIDE
// of the IoC container, meaning it is just normal "new" instantiaion.
// This is important as we do not want the SpringLiquibase's
// afterPropertiesSet method to kick in. All we are interested in
// is to figure out which datasource Liquibase would be using.
final LiquibaseAutoConfiguration.LiquibaseConfiguration liquibaseConfiguration = new LiquibaseAutoConfiguration.LiquibaseConfiguration(liquibaseProperties);
final SpringLiquibase liquibase = liquibaseConfiguration.liquibase(dataSource, liquibaseDataSource);

// Sanity check
Objects.requireNonNull(liquibase.getDataSource(), "Unexpected: null value for DataSource returned from SpringLiquibase class");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public PreLiquibaseException(String message) {

public PreLiquibaseException(String message, Throwable cause) {
super(message, cause);

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
Expand All @@ -15,16 +15,14 @@
*/
package net.lbruun.springboot.preliquibase;

import jakarta.validation.constraints.NotEmpty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;

import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Collections.singletonList;
import static net.lbruun.springboot.preliquibase.PreLiquibaseProperties.PROPERTIES_PREFIX;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

import static net.lbruun.springboot.preliquibase.PreLiquibaseProperties.PROPERTIES_PREFIX;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.validation.annotation.Validated;
import jakarta.validation.constraints.NotEmpty;

/**
* Properties for Pre-Liquibase module.
Expand All @@ -36,22 +34,21 @@
public class PreLiquibaseProperties {

public static final String PROPERTIES_PREFIX = "preliquibase";
public static final String DEFAULT_SCRIPT_LOCATION
= "classpath:preliquibase/";
public static final String DEFAULT_SCRIPT_LOCATION = "classpath:preliquibase/";

private boolean enabled = true;

/**
* Database platform code to use when choosing which SQL script files
* to execute (such as {@code preliquibase/${dbPlatformCode}.sql}).
* Database platform code to use when choosing which SQL script files to execute (such as
* {@code preliquibase/${dbPlatformCode}.sql}).
*/
private String dbPlatformCode;

/**
* SQL script resource references.
*/
@NotEmpty(message = "sqlScriptReferences must not be empty")
private List<String> sqlScriptReferences = Collections.singletonList(DEFAULT_SCRIPT_LOCATION);
private List<String> sqlScriptReferences = singletonList(DEFAULT_SCRIPT_LOCATION);

/**
* Whether to stop if an error occurs while executing the SQL script.
Expand All @@ -66,8 +63,7 @@ public class PreLiquibaseProperties {
/**
* SQL scripts encoding.
*/
private Charset sqlScriptEncoding = StandardCharsets.UTF_8;

private Charset sqlScriptEncoding = UTF_8;

/**
* Get the 'enabled' setting (if the module is enabled or not).
Expand All @@ -88,38 +84,34 @@ public void setEnabled(boolean enabled) {
this.enabled = enabled;
}


/**
* Gets 'continueOnError' setting.
*
* @see #setContinueOnError(boolean)
*/
public boolean isContinueOnError() {
return this.continueOnError;
return continueOnError;
}


/**
* Sets whether to stop if an error occurs while executing the SQL script.
* Default value is: {@code false}.
* Sets whether to stop if an error occurs while executing the SQL script. Default value is:
* {@code false}.
*/
public void setContinueOnError(boolean continueOnError) {
this.continueOnError = continueOnError;
this.continueOnError = continueOnError;
}


/**
* Gets 'separator' setting.
*
* @see #setSeparator(java.lang.String)
*/
public String getSeparator() {
return this.separator;
return separator;
}

/**
* Sets statement separator in SQL scripts.
* Defaults to semi-colon if not set.
* Sets statement separator in SQL scripts. Defaults to semi-colon if not set.
*/
public void setSeparator(String separator) {
this.separator = separator;
Expand All @@ -132,12 +124,11 @@ public void setSeparator(String separator) {
* @see #setSqlScriptEncoding(java.nio.charset.Charset)
*/
public Charset getSqlScriptEncoding() {
return this.sqlScriptEncoding;
return sqlScriptEncoding;
}

/**
* Sets the file encoding for SQL script file.
* Defaults to {@code UTF-8} if not set.
* Sets the file encoding for SQL script file. Defaults to {@code UTF-8} if not set.
*
* @param sqlScriptEncoding
*/
Expand All @@ -159,12 +150,12 @@ public String getDbPlatformCode() {
}

/**
* Sets the db engine code to use when finding which SQL script to execute,
* as in {@code preliquibase/${dbEngineCode}.sql}}.
* Sets the db engine code to use when finding which SQL script to execute, as in
* {@code preliquibase/${dbEngineCode}.sql}}.
*
* <p>
* Setting this value explicitly overrides the database platform
* auto-detection. The value can be any value; it will not be validated.
* Setting this value explicitly overrides the database platform auto-detection. The value can be
* any value; it will not be validated.
*
* @param dbPlatformCode
*/
Expand All @@ -182,45 +173,43 @@ public List<String> getSqlScriptReferences() {
return sqlScriptReferences;
}

// @formatter:off
/**
* Sets location(s) of where to find the SQL script(s) to execute.
*
* <p>
* The value is interpreted slightly differently depending on its
* content:
* The value is interpreted slightly differently depending on its
* content:
* <ul>
* <li>If the value is a Spring Resource textual reference which ends with {@code "/"}:
* In this case, the reference is expected to be a folder reference
* where SQL scripts can be found. From this folder:
* If a file named "{@link #setDbPlatformCode(java.lang.String)
* DBPLATFORMCODE}{@code .sql}" (e.g. "{@code postgresql.sql}") exists then
* that will be used. If no such file is found then a file named
* "{@code default.sql}" is used.
* If neither file is found then no action will be taken, similarly
* to {@link #setEnabled(boolean) disabling} the module.
* <br>
* Example values:<br>
* {@code "classpath:my-folder/"} (load from classpath folder).<br>
* {@code "file:c:/config/sql-scripts/"} (load from file system folder, Windows).<br>
* {@code "file:/app/etc/sql-scripts/"} (load from file system folder, Linux).<br>
* <br>
* </li>
* <li>Otherwise: The value is interpreted as a comma-separated list of
* Spring Resource textual references to <i>specific</i> SQL files. Each
* script file will be executed in the order they are listed. Before
* execution of any of of the script files it is checked if all files
* mentioned in the list actually exists. If not, an
* {@link PreLiquibaseException.SqlScriptRefError} exception is thrown.
* <br>
* Example value: {@code "file:/foo/bar/myscript1.sql,file:/foo/bar/myscript2.sql"}.
* </li>
* <li>If the value is a Spring Resource textual reference which ends with {@code "/"}:
* In this case, the reference is expected to be a folder reference
* where SQL scripts can be found. From this folder:
@@ -190,13 +190,13 @@ public List<String> getSqlScriptReferences() {
* "{@code default.sql}" is used.
* If neither file is found then no action will be taken, similarly
* to {@link #setEnabled(boolean) disabling} the module.
* <br>
* Example values:<br>
* {@code "classpath:my-folder/"} (load from classpath folder).<br>
* {@code "file:c:/config/sql-scripts/"} (load from file system folder, Windows).<br>
* {@code "file:/app/etc/sql-scripts/"} (load from file system folder, Linux).<br>
* <br>
* </li>
* <li>Otherwise: The value is interpreted as a comma-separated list of
* Spring Resource textual references to <i>specific</i> SQL files. Each
* script file will be executed in the order they are listed. Before
@@ -206,14 +206,14 @@ public List<String> getSqlScriptReferences() {
* <br>
* Example value: {@code "file:/foo/bar/myscript1.sql,file:/foo/bar/myscript2.sql"}.
* </li>
* </ul>
*
* <p>
* Default value: {@link #DEFAULT_SCRIPT_LOCATION}.
*
* @param sqlScriptReferences list of Spring Resource references.
*/
//@formatter:on
public void setSqlScriptReferences(List<String> sqlScriptReferences) {
this.sqlScriptReferences = sqlScriptReferences;
}
Expand Down