Run Liquibase As Part of A Java SE Application: Integrate Liquibase Into Hibernate's Bootstrapping Process
Run Liquibase As Part of A Java SE Application: Integrate Liquibase Into Hibernate's Bootstrapping Process
www.thoughts-on-java.org
Integrate Liquibase For Automatic Database Upgrades
In the next step, you can use the MetadataSources object to get an
instance of a ConnectionProvider service and retrieve a
java.sql.Connection. Hibernate created this connection based on the
configuration data in the hibernate.cfg.xml file. You can then use the
Connection object to create a Liquibase-specific JdbcConnection.
Now you’ve everything you need to initialize Liquibase. You first need
to create a Database object and provide the name of the changelog
file, a ClassLoaderResourceAccessor instance and the Database
object. Then, you can call the update method with a reference to the
context you want to use for your database update.
// Initialize Liquibase and run the update
Database database = DatabaseFactory.getInstance()
.findCorrectDatabaseImplementation(jdbcCon);
Liquibase liquibase = new Liquibase("db.changelog.xml",
new ClassLoaderResourceAccessor(), database);
liquibase.update("test");
www.thoughts-on-java.org
Integrate Liquibase For Automatic Database Upgrades
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>3.5.3</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-cdi</artifactId>
<version>3.5.3</version>
</dependency>
www.thoughts-on-java.org
Integrate Liquibase For Automatic Database Upgrades
You then need to implement a CDI bean that produces a
CDILiquibaseConfig, a DataSource and a ResourceAccessor.
@Dependent
public class LiquibaseCdiIntegration {
@Resource
private DataSource myDataSource;
@Produces
@LiquibaseType
public CDILiquibaseConfig createConfig() {
CDILiquibaseConfig config = new CDILiquibaseConfig();
config.setChangeLog("//c:/tmp/db.changelog.xml");
return config;
}
@Produces
@LiquibaseType
public DataSource createDataSource() throws SQLException {
return myDataSource;
}
@Produces
@LiquibaseType
public ResourceAccessor create() {
return new
ClassLoaderResourceAccessor(getClass().getClassLoader());
}
}
www.thoughts-on-java.org