Skip to content

Commit

Permalink
Upgrade Liquibase to 4.27.0 (#155)
Browse files Browse the repository at this point in the history
  • Loading branch information
takezoe authored May 18, 2024
1 parent b1ca6b0 commit 40900ef
Show file tree
Hide file tree
Showing 12 changed files with 77 additions and 70 deletions.
18 changes: 9 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ Add following dependency into your Maven `pom.xml`:
<dependency>
<groupId>io.github.gitbucket</groupId>
<artifactId>solidbase</artifactId>
<version>1.0.5</version>
<version>1.1.0</version>
</dependency>
</dependencies>
```
Expand All @@ -34,10 +34,10 @@ Create the Liquibase migration xml files under `src/main/resources`. For example
```xml
<changeSet>
<createTable tableName="person">
<column name="id" type="int" autoIncrement="true" primaryKey="true" nullable="false"/>
<column name="firstname" type="varchar(50)"/>
<column name="lastname" type="varchar(50)" constraints nullable="false"/>
<column name="state" type="char(2)"/>
<column name="id" type="int" autoIncrement="true" primaryKey="true" nullable="false"/>
<column name="firstname" type="varchar(50)"/>
<column name="lastname" type="varchar(50)" nullable="false"/>
<column name="state" type="char(2)"/>
</createTable>
</changeSet>
```
Expand Down Expand Up @@ -104,10 +104,10 @@ Differences between the current version and the latest version are applied.

Solidbase creates a following `VERSIONS` table to manage versions automatically:

Column name | Data type | Not Null
:--------------|:-------------|:---------
MODULE_ID (PK) | VARCHAR(100) | Yes
VERSION | VARCHAR(100) | Yes
| Column name | Data type | Not Null |
|:---------------|:-------------|:---------|
| MODULE_ID (PK) | VARCHAR(100) | Yes |
| VERSION | VARCHAR(100) | Yes |

Solidbase uses this table to know the current version. When migration of the new version is successful, it updates the version with the new version.

Expand Down
4 changes: 2 additions & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

<groupId>io.github.gitbucket</groupId>
<artifactId>solidbase</artifactId>
<version>1.0.5</version>
<version>1.1.0-SNAPSHOT</version>

<packaging>jar</packaging>
<name>solidbase</name>
Expand Down Expand Up @@ -43,7 +43,7 @@
<dependency>
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
<version>4.16.1</version>
<version>4.27.0</version>
</dependency>
<dependency>
<groupId>org.apache.ant</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
*/
public class JDBCVersionManager implements VersionManager {

private Connection conn;
private final Connection conn;

public JDBCVersionManager(Connection conn){
this.conn = conn;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,14 @@
import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;

import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Map;

public class AntMigration implements Migration {

private String path;
private final String path;

public AntMigration(){
this(null);
Expand All @@ -33,7 +34,7 @@ public void migrate(String moduleId, String version, Map<String, Object> context
try {
ClassLoader classLoader = (ClassLoader) context.get(Solidbase.CLASSLOADER);
String source = MigrationUtils.readResourceAsString(classLoader, path);
Files.write(tempFilePath, source.getBytes("UTF-8"));
Files.write(tempFilePath, source.getBytes(StandardCharsets.UTF_8));

Project project = new Project();
project.setProperty("ant.file", path);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package io.github.gitbucket.solidbase.migration;

import java.io.ByteArrayInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.URI;
Expand All @@ -19,8 +20,9 @@
import liquibase.changelog.ChangeSet;
import liquibase.changelog.DatabaseChangeLog;
import liquibase.database.Database;
import liquibase.resource.AbstractResource;
import liquibase.resource.ClassLoaderResourceAccessor;
import liquibase.resource.InputStreamList;
import liquibase.resource.Resource;
import liquibase.sql.Sql;
import liquibase.sqlgenerator.SqlGeneratorFactory;
import liquibase.statement.SqlStatement;
Expand All @@ -30,7 +32,7 @@
*/
public class LiquibaseMigration implements Migration {

private String path;
private final String path;

/**
* Creates <code>LiquibaseMigration</code> that migrates using <code>/$MODULE_ID_$VERSION.xml</code> on the classpath.
Expand Down Expand Up @@ -100,9 +102,9 @@ protected void migrate(Connection conn, Database database, ClassLoader classLoad
}

private static class StringResourceAccessor extends ClassLoaderResourceAccessor {
private String fileName;
private String source;
private ClassLoader classLoader;
private final String fileName;
private final String source;
private final ClassLoader classLoader;

public StringResourceAccessor(String fileName, String source, ClassLoader classLoader){
super(classLoader);
Expand All @@ -112,21 +114,49 @@ public StringResourceAccessor(String fileName, String source, ClassLoader classL
}

@Override
public InputStreamList openStreams(String relativeTo, String streamPath) throws IOException {
streamPath = this.getFinalPath(relativeTo, streamPath);
if(streamPath.equals(fileName)){
InputStreamList returnList = new InputStreamList();
public List<Resource> getAll(String path) throws IOException {
if(path.equals(fileName)){
List<Resource> returnList = new ArrayList<>();
try {
URI uri = classLoader.getResources(streamPath).nextElement().toURI();
returnList.add(uri, new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8)));
URI uri = classLoader.getResources(path).nextElement().toURI();
returnList.add(new ByteArrayResource(source, path, uri));
} catch (URISyntaxException e) {
throw new RuntimeException(e);
}
return returnList;
} else {
return super.openStreams(relativeTo, streamPath);
throw new FileNotFoundException(path);
}
}
}

private static class ByteArrayResource extends AbstractResource {
private final String source;

public ByteArrayResource(String source, String path, URI uri) {
super(path, uri);
this.source = source;
}

@Override
public InputStream openInputStream() throws IOException {
return new ByteArrayInputStream(source.getBytes(StandardCharsets.UTF_8));
}

@Override
public boolean exists() {
return true;
}

@Override
public Resource resolve(String other) {
return null;
}

@Override
public Resource resolveSibling(String other) {
return null;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import javax.xml.transform.stream.StreamResult;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

Expand Down Expand Up @@ -75,7 +76,7 @@ public String preProcess(String moduleId, String version, String source) throws

private static Document parseXml(String xml) throws Exception {
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes("UTF-8")));
Document doc = builder.parse(new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)));
return doc;
}

Expand All @@ -88,6 +89,6 @@ private static String printXml(Document doc) throws Exception {
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.transform(new DOMSource(doc), new StreamResult(out));

return new String(out.toByteArray(), "UTF-8");
return out.toString("UTF-8");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,59 +14,34 @@
public class MigrationUtils {

public static int updateDatabase(Connection conn, String sql, Object... params) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(sql);
try {
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
setParameters(stmt, params);
return stmt.executeUpdate();
} finally {
if(stmt != null){
stmt.close();
}
}
}

public static Integer selectIntFromDatabase(Connection conn, String sql, Object... params) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(sql);
try {
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
setParameters(stmt, params);
ResultSet rs = stmt.executeQuery();
try {
if(rs.next()){
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getInt(1);
} else {
return null;
}
} finally {
if(rs != null){
rs.close();
}
}
} finally {
if(stmt != null){
stmt.close();
}
}
}

public static String selectStringFromDatabase(Connection conn, String sql, Object... params) throws SQLException {
PreparedStatement stmt = conn.prepareStatement(sql);
try {
try (PreparedStatement stmt = conn.prepareStatement(sql)) {
setParameters(stmt, params);
ResultSet rs = stmt.executeQuery();
try {
if(rs.next()){
try (ResultSet rs = stmt.executeQuery()) {
if (rs.next()) {
return rs.getString(1);
} else {
return null;
}
} finally {
if(rs != null){
rs.close();
}
}
} finally {
if(stmt != null){
stmt.close();
}
}
}
Expand Down Expand Up @@ -95,11 +70,9 @@ public static String readStreamAsString(InputStream in) throws IOException {
while((length = in.read(buf)) != -1){
out.write(buf, 0, length);
}
return new String(out.toByteArray(), "UTF-8");
return out.toString("UTF-8");
} finally {
if(in != null){
in.close();
}
in.close();
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/
public class SqlMigration implements Migration {

private String path;
private final String path;

/**
* Creates <code>SqlMigration</code> that migrates using <code>/$MODULE_ID_$VERSION.sql</code> on the classpath.
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/io/github/gitbucket/solidbase/model/Module.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@

public class Module {

private String moduleId;
private List<Version> versions = new ArrayList<>();
private final String moduleId;
private final List<Version> versions = new ArrayList<>();

public Module(String moduleId){
this.moduleId = moduleId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@

public class Version {

private String version;
private List<Migration> migrations = new ArrayList<>();
private final String version;
private final List<Migration> migrations = new ArrayList<>();

public Version(String version){
this.version = version;
Expand Down
1 change: 1 addition & 0 deletions src/test/resources/test-ant_1.0.0.xml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<project default="test">
<target name="test">
<mkdir dir="solidbase-test-dir"/>
Expand Down
3 changes: 2 additions & 1 deletion src/test/resources/test_1.0.0.xml
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<changeSet>
<createTable tableName="person">
<column name="id" type="int" autoIncrement="true" primaryKey="true" nullable="false"/>
<column name="firstname" type="varchar(50)"/>
<column name="lastname" type="varchar(50)" nullable="false"/>
<column name="state" type="char(2)"/>
</createTable>
</changeSet>
</changeSet>

0 comments on commit 40900ef

Please sign in to comment.