Commit ae5cd53a authored by Matija Obreza's avatar Matija Obreza

Using Liquibase for schema updates

parent b742b6e6
# Using Liquibase
Liquibase guide at <http://www.baeldung.com/liquibase-refactor-schema-of-java-app> best documents how to integrate Liquibase into a Spring application.
## Dependencies
```xml
<properties>
<liquibase.version>3.5.3</liquibase.version>
</properties>
<dependencies>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>
</dependencies>
```
## Running database migration
Database migration scripts must execute before JPA and Hibernate kick in:
```java
@Bean(name = "databaseMigration")
public SpringLiquibase databaseMigration() {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:liquibase/liquibase-changeLog.yml");
liquibase.setDataSource(dataSource());
return liquibase;
}
@Bean(name = "entityManagerFactory")
@DependsOn("databaseMigration")
public LocalContainerEntityManagerFactoryBean entityManagerFactory() {
// The factory depends on "databaseMigration" bean above
}
```
## Generating changeset
Integrate the `liquibase-hibernate4` plugin into the `pom.xml`:
```xml
<plugins>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate4</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data-jpa.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
```
Add `src/main/resources/liquibase.properties` configuration and configure the
"updated database" (new version) and the "current database" against which the changeset should be generated.
```properties
changeLogFile=src/main/resources/liquibase/liquibase-changeLog.yml
diffChangeLogFile=src/main/resources/liquibase/liquibase-diff-changeLog.yml
# Current database
url=jdbc:mysql://localhost/genesys201703?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
username=root
password=
driver=com.mysql.jdbc.Driver
# Updated database
referenceUrl=hibernate:spring:org.genesys2.server.model?dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# &hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
#referenceDriver=com.mysql.jdbc.Driver
#referenceUsername=root
#referencePassword=
```
Compile your code and run the plugin:
```bash
mvn clean compile liquibase:diff
```
Results will be generated to file specified in `diffChangeLogFile` above.
......@@ -72,6 +72,7 @@
<jsp-api.version>2.1</jsp-api.version>
<spring.framework.version>4.3.8.RELEASE</spring.framework.version>
<spring-data-jpa.version>1.9.4.RELEASE</spring-data-jpa.version>
<spring.data.release-train>Hopper-SR1</spring.data.release-train>
<spring.security.version>4.0.4.RELEASE</spring.security.version>
<spring.security.oauth2.version>1.0.5.RELEASE</spring.security.oauth2.version>
......@@ -93,6 +94,7 @@
<oval.version>1.81</oval.version>
<jackson.version>2.6.4</jackson.version>
<jaxb-api.version>2.2.12</jaxb-api.version>
<liquibase.version>3.5.3</liquibase.version>
<!--Container -->
<jetty.version>9.3.6.v20151106</jetty.version>
......@@ -284,7 +286,7 @@
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>1.9.4.RELEASE</version>
<version>${spring-data-jpa.version}</version>
</dependency>
<!-- App blocks -->
......@@ -298,7 +300,7 @@
<artifactId>application-blocks-security</artifactId>
<version>${application.blocks.version}</version>
</dependency>
<!-- Hibernate dependencies -->
......@@ -506,6 +508,11 @@
<artifactId>opencsv</artifactId>
<version>3.7</version>
</dependency>
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>${liquibase.version}</version>
</dependency>
</dependencies>
<build>
......@@ -762,6 +769,36 @@
<sortArtifactByName>true</sortArtifactByName>
</configuration>
</plugin>
<plugin>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-maven-plugin</artifactId>
<version>${liquibase.version}</version>
<configuration>
<propertyFile>src/main/resources/liquibase.properties</propertyFile>
</configuration>
<dependencies>
<dependency>
<groupId>org.liquibase.ext</groupId>
<artifactId>liquibase-hibernate4</artifactId>
<version>3.6</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>${spring.framework.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<version>${spring-data-jpa.version}</version>
</dependency>
</dependencies>
</plugin>
</plugins>
<resources>
<resource>
......
......@@ -26,6 +26,7 @@ import org.hibernate.jpa.HibernatePersistenceProvider;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;
import org.springframework.context.annotation.ImportResource;
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
import org.springframework.jdbc.core.JdbcTemplate;
......@@ -34,7 +35,10 @@ import org.springframework.orm.jpa.LocalContainerEntityManagerFactoryBean;
import org.springframework.orm.jpa.vendor.HibernateJpaVendorAdapter;
import org.springframework.transaction.annotation.EnableTransactionManagement;
@EnableJpaRepositories(basePackages = { "org.genesys2.server.persistence.domain", "org.genesys2.server.persistence.acl", "org.genesys2.server.filerepository.persistence" }, entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager", repositoryImplementationPostfix = "CustomImpl")
import liquibase.integration.spring.SpringLiquibase;
@EnableJpaRepositories(basePackages = { "org.genesys2.server.persistence.domain", "org.genesys2.server.persistence.acl",
"org.genesys2.server.filerepository.persistence" }, entityManagerFactoryRef = "entityManagerFactory", transactionManagerRef = "transactionManager", repositoryImplementationPostfix = "CustomImpl")
// @EnableJpaAuditing(auditorAwareRef = "auditorAware")
@EnableTransactionManagement
@Configuration
......@@ -67,7 +71,7 @@ public class SpringDataBaseConfig {
*/
@Value("${db.pool.initialSize}")
private int initialSize;
@Value("${db.pool.maxIdle}")
private int maxIdle;
......@@ -104,7 +108,16 @@ public class SpringDataBaseConfig {
return new JdbcTemplate(dataSource);
}
@Bean(name = "databaseMigration")
public SpringLiquibase databaseMigration() {
SpringLiquibase liquibase = new SpringLiquibase();
liquibase.setChangeLog("classpath:liquibase/liquibase-changeLog.yml");
liquibase.setDataSource(dataSource());
return liquibase;
}
@Bean(name = "entityManagerFactory")
@DependsOn("databaseMigration")
public LocalContainerEntityManagerFactoryBean entityManagerFactory(HazelcastCacheRegionFactoryWrapper wrp) throws Exception {
final LocalContainerEntityManagerFactoryBean entityManager = new LocalContainerEntityManagerFactoryBean();
entityManager.setDataSource(dataSource());
......
changeLogFile=src/main/resources/liquibase/liquibase-changeLog.yml
diffChangeLogFile=src/main/resources/liquibase/liquibase-diff-changeLog.yml
# Current database
url=jdbc:mysql://localhost/genesys201703?useUnicode=true&characterEncoding=UTF-8&useFastDateParsing=false
username=root
password=
driver=com.mysql.jdbc.Driver
# Updated database
referenceUrl=hibernate:spring:org.genesys2.server.model?dialect=org.hibernate.dialect.MySQL5InnoDBDialect
# &hibernate.ejb.naming_strategy=org.hibernate.cfg.ImprovedNamingStrategy
#referenceDriver=com.mysql.jdbc.Driver
#referenceUsername=root
#referencePassword=
databaseChangeLog:
# Application Blocks: upgrade user to BasicUser
- changeSet:
id: 1495959628118-1
author: matijaobreza (generated)
changes:
- renameTable:
newTableName: userrole
oldTableName: user_role
- changeSet:
id: 1495959628118-3
author: matijaobreza (generated)
changes:
- renameColumn:
columnDataType: varchar(255)
newColumnName: fullName
oldColumnName: name
remarks: Full name of user
tableName: user
- changeSet:
id: 1495959628118-5
author: matijaobreza (generated)
changes:
- addColumn:
tableName: user
columns:
- column:
constraints:
nullable: false
name: active
type: bit
- column:
name: createdBy
type: bigint
- column:
name: lastModifiedBy
type: bigint
- column:
name: lastModifiedDate
type: datetime
- column:
name: passwordExpires
type: datetime
- column:
name: shortName
type: varchar(20)
- column:
name: version
type: integer
- changeSet:
id: 1495959628118-6
author: mobreza
changes:
- sql:
comment: Migrate user.enabled to user.active = user.enabled;
sql: update user set active = enabled;
- changeSet:
id: 1495959628118-7
author: matijaobreza (generated)
changes:
- renameColumn:
columnDataType: varchar(20)
newColumnName: accountType
oldColumnName: loginType
remarks: Account type
tableName: user
- sql:
comment: loginType#PASSWORD is changed to accountType#LOCAL
sql: update user set accountType = 'LOCAL' where accountType = 'PASSWORD';
- changeSet:
id: 1495959628118-8
author: mobreza
changes:
- sql:
comment: Assign user.version
sql: update user set version = 1 where version is null;
- changeSet:
id: 1495959628118-9
author: matijaobreza (generated)
changes:
- addColumn:
tableName: user
columns:
- column:
name: createdDate
type: datetime
- column:
name: accountExpires
type: datetime
- changeSet:
id: 1495959628118-10
author: matijaobreza (generated)
changes:
- addUniqueConstraint:
columnNames: shortName
constraintName: UC_USERSHORTNAME_COL
tableName: user
- changeSet:
id: 1495959628118-11
author: matijaobreza (generated)
changes:
- renameColumn:
columnDataType: bigint
oldColumnName: user_id
newColumnName: userId
tableName: userrole
- renameColumn:
columnDataType: varchar(50)
oldColumnName: user_role
newColumnName: role
tableName: userrole
......@@ -28,6 +28,7 @@ log4j.rootLogger=error, stdout
log4j.category.org.genesys2=warn
log4j.category.org.genesys=warn
log4j.category.liquibase=debug
#log4j.category.org.genesys2.server.servlet.controller=debug
#log4j.category.org.hibernate.cfg.Configuration=debug
#log4j.category.org.hibernate.search=debug
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment