From 40900ef7536309f2790085b816dcf305a0840e12 Mon Sep 17 00:00:00 2001 From: Naoki Takezoe Date: Sat, 18 May 2024 10:46:24 +0900 Subject: [PATCH] Upgrade Liquibase to 4.27.0 (#155) --- README.md | 18 +++---- pom.xml | 4 +- .../solidbase/manager/JDBCVersionManager.java | 2 +- .../solidbase/migration/AntMigration.java | 5 +- .../migration/LiquibaseMigration.java | 54 ++++++++++++++----- .../migration/LiquibaseXmlPreProcessor.java | 5 +- .../solidbase/migration/MigrationUtils.java | 45 ++++------------ .../solidbase/migration/SqlMigration.java | 2 +- .../gitbucket/solidbase/model/Module.java | 4 +- .../gitbucket/solidbase/model/Version.java | 4 +- src/test/resources/test-ant_1.0.0.xml | 1 + src/test/resources/test_1.0.0.xml | 3 +- 12 files changed, 77 insertions(+), 70 deletions(-) diff --git a/README.md b/README.md index 6ba9c68..671c712 100644 --- a/README.md +++ b/README.md @@ -17,7 +17,7 @@ Add following dependency into your Maven `pom.xml`: io.github.gitbucket solidbase - 1.0.5 + 1.1.0 ``` @@ -34,10 +34,10 @@ Create the Liquibase migration xml files under `src/main/resources`. For example ```xml - - - - + + + + ``` @@ -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. diff --git a/pom.xml b/pom.xml index 1b386bc..155ce26 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ io.github.gitbucket solidbase - 1.0.5 + 1.1.0-SNAPSHOT jar solidbase @@ -43,7 +43,7 @@ org.liquibase liquibase-core - 4.16.1 + 4.27.0 org.apache.ant diff --git a/src/main/java/io/github/gitbucket/solidbase/manager/JDBCVersionManager.java b/src/main/java/io/github/gitbucket/solidbase/manager/JDBCVersionManager.java index 4210f7a..f8737ba 100644 --- a/src/main/java/io/github/gitbucket/solidbase/manager/JDBCVersionManager.java +++ b/src/main/java/io/github/gitbucket/solidbase/manager/JDBCVersionManager.java @@ -11,7 +11,7 @@ */ public class JDBCVersionManager implements VersionManager { - private Connection conn; + private final Connection conn; public JDBCVersionManager(Connection conn){ this.conn = conn; diff --git a/src/main/java/io/github/gitbucket/solidbase/migration/AntMigration.java b/src/main/java/io/github/gitbucket/solidbase/migration/AntMigration.java index d5f3417..3f9a685 100644 --- a/src/main/java/io/github/gitbucket/solidbase/migration/AntMigration.java +++ b/src/main/java/io/github/gitbucket/solidbase/migration/AntMigration.java @@ -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); @@ -33,7 +34,7 @@ public void migrate(String moduleId, String version, Map 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); diff --git a/src/main/java/io/github/gitbucket/solidbase/migration/LiquibaseMigration.java b/src/main/java/io/github/gitbucket/solidbase/migration/LiquibaseMigration.java index 6e70105..55c6852 100644 --- a/src/main/java/io/github/gitbucket/solidbase/migration/LiquibaseMigration.java +++ b/src/main/java/io/github/gitbucket/solidbase/migration/LiquibaseMigration.java @@ -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; @@ -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; @@ -30,7 +32,7 @@ */ public class LiquibaseMigration implements Migration { - private String path; + private final String path; /** * Creates LiquibaseMigration that migrates using /$MODULE_ID_$VERSION.xml on the classpath. @@ -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); @@ -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 getAll(String path) throws IOException { + if(path.equals(fileName)){ + List 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; + } + } + } diff --git a/src/main/java/io/github/gitbucket/solidbase/migration/LiquibaseXmlPreProcessor.java b/src/main/java/io/github/gitbucket/solidbase/migration/LiquibaseXmlPreProcessor.java index 7e580d0..8ee96cb 100644 --- a/src/main/java/io/github/gitbucket/solidbase/migration/LiquibaseXmlPreProcessor.java +++ b/src/main/java/io/github/gitbucket/solidbase/migration/LiquibaseXmlPreProcessor.java @@ -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; @@ -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; } @@ -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"); } } diff --git a/src/main/java/io/github/gitbucket/solidbase/migration/MigrationUtils.java b/src/main/java/io/github/gitbucket/solidbase/migration/MigrationUtils.java index b4ea09c..86d5648 100644 --- a/src/main/java/io/github/gitbucket/solidbase/migration/MigrationUtils.java +++ b/src/main/java/io/github/gitbucket/solidbase/migration/MigrationUtils.java @@ -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(); } } } @@ -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(); } } diff --git a/src/main/java/io/github/gitbucket/solidbase/migration/SqlMigration.java b/src/main/java/io/github/gitbucket/solidbase/migration/SqlMigration.java index a5c2663..a1008ed 100644 --- a/src/main/java/io/github/gitbucket/solidbase/migration/SqlMigration.java +++ b/src/main/java/io/github/gitbucket/solidbase/migration/SqlMigration.java @@ -15,7 +15,7 @@ */ public class SqlMigration implements Migration { - private String path; + private final String path; /** * Creates SqlMigration that migrates using /$MODULE_ID_$VERSION.sql on the classpath. diff --git a/src/main/java/io/github/gitbucket/solidbase/model/Module.java b/src/main/java/io/github/gitbucket/solidbase/model/Module.java index 2e8ebe1..257c115 100644 --- a/src/main/java/io/github/gitbucket/solidbase/model/Module.java +++ b/src/main/java/io/github/gitbucket/solidbase/model/Module.java @@ -5,8 +5,8 @@ public class Module { - private String moduleId; - private List versions = new ArrayList<>(); + private final String moduleId; + private final List versions = new ArrayList<>(); public Module(String moduleId){ this.moduleId = moduleId; diff --git a/src/main/java/io/github/gitbucket/solidbase/model/Version.java b/src/main/java/io/github/gitbucket/solidbase/model/Version.java index 7bf36e0..2dd07ec 100644 --- a/src/main/java/io/github/gitbucket/solidbase/model/Version.java +++ b/src/main/java/io/github/gitbucket/solidbase/model/Version.java @@ -7,8 +7,8 @@ public class Version { - private String version; - private List migrations = new ArrayList<>(); + private final String version; + private final List migrations = new ArrayList<>(); public Version(String version){ this.version = version; diff --git a/src/test/resources/test-ant_1.0.0.xml b/src/test/resources/test-ant_1.0.0.xml index f7beb70..63e44b8 100644 --- a/src/test/resources/test-ant_1.0.0.xml +++ b/src/test/resources/test-ant_1.0.0.xml @@ -1,3 +1,4 @@ + diff --git a/src/test/resources/test_1.0.0.xml b/src/test/resources/test_1.0.0.xml index 43940c1..a273023 100644 --- a/src/test/resources/test_1.0.0.xml +++ b/src/test/resources/test_1.0.0.xml @@ -1,3 +1,4 @@ + @@ -5,4 +6,4 @@ - \ No newline at end of file +