[#7217] Added configurable sort order

This commit is contained in:
lukaseder 2018-03-20 11:56:13 +01:00
parent 42d137a56a
commit 26054fd8c6
2 changed files with 39 additions and 4 deletions

View File

@ -18189,6 +18189,17 @@ CREATE TABLE book_to_book_store (
<key>scripts</key>
<value>src/main/resources/database.sql</value>
</property>
<!-- The sort order of the scripts within a directory, where:
- semantic: sorts versions, e.g. v-3.10.0 is after v-3.9.0 (default)
- alphanumeric: sorts strings, e.g. v-3.10.0 is before v-3.9.0
- none: doesn't sort directory contents after fetching them from the directory
-->
<property>
<key>sort</key>
<value>semantic</value>
</propert>
</properties>
</database>
</generator>
@ -18202,9 +18213,13 @@ CREATE TABLE book_to_book_store (
.withGenerator(new Generator(
.withDatabase(new Database()
.withName("org.jooq.util.ddl.DDLDatabase")
.withProperties(new Property()
.withKey("scripts")
.withValue("src/main/resources/database.sql")))));]]></java><html>
.withProperties(
new Property()
.withKey("scripts")
.withValue("src/main/resources/database.sql"),
new Property()
.withKey("sort")
.withValue("semantic")))));]]></java><html>
<p>
<strong>Gradle configuration</strong>
@ -18219,6 +18234,10 @@ CREATE TABLE book_to_book_store (
key = 'scripts'
value = 'src/main/resources/database.sql'
}
property {
key = 'sort'
value = 'semantic'
}
}
}
}

View File

@ -48,6 +48,7 @@ import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
@ -89,12 +90,26 @@ public class DDLDatabase extends H2Database {
private Connection connection;
private DSLContext ctx;
private Comparator<File> fileComparator;
@Override
protected DSLContext create0() {
if (connection == null) {
String scripts = getProperties().getProperty("scripts");
String encoding = getProperties().getProperty("encoding", "UTF-8");
String sort = getProperties().getProperty("sort", "semantic").toLowerCase();
if ("alphanumeric".equals(sort))
fileComparator = new Comparator<File>() {
@Override
public int compare(File o1, File o2) {
return o1.compareTo(o2);
}
};
else if ("none".equals(sort))
fileComparator = null;
else
fileComparator = FileComparator.INSTANCE;
if (isBlank(scripts)) {
scripts = "";
@ -169,7 +184,8 @@ public class DDLDatabase extends H2Database {
File[] files = file.listFiles();
if (files != null) {
Arrays.sort(files, FileComparator.INSTANCE);
if (fileComparator != null)
Arrays.sort(files, fileComparator);
for (File f : files)
load(encoding, f, pattern);