Merge branch 'master' into wip/h2database-v2

This commit is contained in:
Rob Bygrave authored and GitHub committed 2022-01-25 21:30:36 +13:00
commit bc6d2fb3be
113 files changed
+1589 -372

No files matched your search

@@ -5,6 +5,7 @@ import io.ebean.config.DatabaseConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.ddlrunner.DdlRunner;
import io.ebean.ddlrunner.ScriptTransform;
import io.ebean.util.IOUtils;
import io.ebean.util.JdbcClose;
import io.ebeaninternal.api.SpiDdlGenerator;
import io.ebeaninternal.api.SpiEbeanServer;
@@ -17,13 +18,11 @@ import org.slf4j.LoggerFactory;
import javax.persistence.PersistenceException;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.LineNumberReader;
import java.io.Reader;
import java.io.Writer;
import java.sql.Connection;
import java.sql.SQLException;
@@ -50,6 +49,7 @@ public class DdlGenerator implements SpiDdlGenerator {
private final ScriptTransform scriptTransform;
private final Platform platform;
private final String platformName;
private final boolean useMigrationStoredProcedures;
private CurrentModel currentModel;
private String dropAllContent;
@@ -71,9 +71,11 @@ public class DdlGenerator implements SpiDdlGenerator {
log.warn("DDL can't be run on startup with TenantMode " + config.getTenantMode());
this.runDdl = false;
this.ddlAutoCommit = false;
this.useMigrationStoredProcedures = false;
} else {
this.runDdl = config.isDdlRun();
this.ddlAutoCommit = databasePlatform.isDdlAutoCommit();
this.useMigrationStoredProcedures = config.getDatabasePlatform().isUseMigrationStoredProcedures();
}
this.scriptTransform = createScriptTransform(config);
this.baseDir = initBaseDir();
@@ -187,7 +189,7 @@ public class DdlGenerator implements SpiDdlGenerator {
protected void runDropSql(Connection connection) throws IOException {
if (!createOnly) {
if (extraDdl && jaxbPresent) {
if (extraDdl && jaxbPresent && useMigrationStoredProcedures) {
String extraApply = ExtraDdlXmlReader.buildExtra(platform, true);
if (extraApply != null) {
runScript(connection, false, extraApply, "extra-ddl");
@@ -268,7 +270,7 @@ public class DdlGenerator implements SpiDdlGenerator {
if (is == null) {
log.warn("sql script {} was not found as a resource", sqlScript);
} else {
String content = readContent(new InputStreamReader(is));
String content = readContent(IOUtils.newReader(is)); // 'is' is closed
runScript(connection, false, content, sqlScript);
}
}
@@ -337,7 +339,7 @@ public class DdlGenerator implements SpiDdlGenerator {
protected void writeFile(String fileName, String fileContent) throws IOException {
File f = new File(baseDir, fileName);
try (FileWriter fw = new FileWriter(f)) {
try (Writer fw = IOUtils.newWriter(f)) {
fw.write(fileContent);
fw.flush();
}
@@ -348,7 +350,9 @@ public class DdlGenerator implements SpiDdlGenerator {
if (!f.exists()) {
return null;
}
return readContent(new FileReader(f));
try (Reader reader = IOUtils.newReader(f)) {
return readContent(reader);
}
}
protected String readContent(Reader reader) throws IOException {
@@ -1,8 +1,22 @@
package io.ebeaninternal.dbmigration;
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
import java.io.File;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.StringJoiner;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import io.ebean.DB;
import io.ebean.Database;
import io.ebean.annotation.Platform;
import io.ebean.config.ClassLoadConfig;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.DbConstraintNaming;
import io.ebean.config.PlatformConfig;
@@ -27,6 +41,8 @@ import io.ebean.config.dbplatform.sqlite.SQLitePlatform;
import io.ebean.config.dbplatform.sqlserver.SqlServer16Platform;
import io.ebean.config.dbplatform.sqlserver.SqlServer17Platform;
import io.ebean.dbmigration.DbMigration;
import io.ebean.util.IOUtils;
import io.ebean.util.StringHelper;
import io.ebeaninternal.api.DbOffline;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlOptions;
@@ -42,17 +58,6 @@ import io.ebeaninternal.dbmigration.model.PlatformDdlWriter;
import io.ebeaninternal.extraddl.model.DdlScript;
import io.ebeaninternal.extraddl.model.ExtraDdl;
import io.ebeaninternal.extraddl.model.ExtraDdlXmlReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import static io.ebeaninternal.api.PlatformMatch.matchPlatform;
/**
* Generates DB Migration xml and sql scripts.
@@ -392,19 +397,22 @@ public class DefaultDbMigration implements DbMigration {
private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform, boolean tablePartitioning) throws IOException {
if (dbPlatform != null) {
if (tablePartitioning && includeBuiltInPartitioning) {
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltinTablePartitioning());
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltinTablePartitioning(), false);
}
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltin());
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.read());
// skip built-in migration stored procedures based on isUseMigrationStoredProcedures
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltin(), true);
generateExtraDdlFor(migrationDir, dbPlatform, ExtraDdlXmlReader.read(), false);
}
}
private void generateExtraDdlFor(File migrationDir, DatabasePlatform dbPlatform, ExtraDdl extraDdl) throws IOException {
private void generateExtraDdlFor(File migrationDir, DatabasePlatform dbPlatform, ExtraDdl extraDdl, boolean checkSkip) throws IOException {
if (extraDdl != null) {
List<DdlScript> ddlScript = extraDdl.getDdlScript();
for (DdlScript script : ddlScript) {
if (!script.isDrop() && matchPlatform(dbPlatform.getPlatform(), script.getPlatforms())) {
writeExtraDdl(migrationDir, script);
if (!checkSkip || dbPlatform.isUseMigrationStoredProcedures()) {
writeExtraDdl(migrationDir, script);
}
}
}
}
@@ -417,7 +425,7 @@ public class DefaultDbMigration implements DbMigration {
String fullName = repeatableMigrationName(script.isInit(), script.getName());
logger.debug("writing repeatable script {}", fullName);
File file = new File(migrationDir, fullName);
try (FileWriter writer = new FileWriter(file)) {
try (Writer writer = IOUtils.newWriter(file)) {
writer.write(script.getValue());
writer.flush();
}
@@ -2,10 +2,11 @@ package io.ebeaninternal.dbmigration;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.migration.MigrationVersion;
import io.ebean.util.IOUtils;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
@@ -50,17 +51,17 @@ class IndexMigration {
private void generateIndex() throws IOException {
Collections.sort(all);
FileWriter writer = new FileWriter(indexFile);
for (Entry entry : all) {
writeChecksumPadded(writer, entry.checksum);
writer.write(entry.fileName);
try (Writer writer = IOUtils.newWriter(indexFile)) {
for (Entry entry : all) {
writeChecksumPadded(writer, entry.checksum);
writer.write(entry.fileName);
writer.write(eol);
}
writer.write(eol);
}
writer.write(eol);
writer.close();
}
private void writeChecksumPadded(FileWriter writer, int checksum) throws IOException {
private void writeChecksumPadded(Writer writer, int checksum) throws IOException {
final String asStr = String.valueOf(checksum);
writer.write(asStr);
writer.write(',');
@@ -4,6 +4,8 @@ import java.io.*;
import java.nio.charset.StandardCharsets;
import java.util.zip.CRC32;
import io.ebean.util.IOUtils;
/**
* Calculates the checksum for the given file content.
*/
@@ -13,9 +15,8 @@ class MChecksum {
* Returns the checksum of the file. Agnostic of encoding and new line character.
*/
static int calculate(File file) {
try {
try (BufferedReader bufferedReader = IOUtils.newReader(file)) {
final CRC32 crc32 = new CRC32();
BufferedReader bufferedReader = new BufferedReader(new FileReader(file));
String line;
while ((line = bufferedReader.readLine()) != null) {
final byte[] lineBytes = line.getBytes(StandardCharsets.UTF_8);
@@ -18,12 +18,15 @@ public class MySqlDdl extends PlatformDdl {
// this flag is for compatibility. Use it with care.
private static final boolean USE_CHECK_CONSTRAINT = Boolean.getBoolean("ebean.mysql.useCheckConstraint");
private final boolean useMigrationStoredProcedures;
public MySqlDdl(DatabasePlatform platform) {
super(platform);
this.alterColumn = "modify";
this.dropUniqueConstraint = "drop index";
this.historyDdl = new MySqlHistoryDdl();
this.inlineComments = true;
this.useMigrationStoredProcedures = platform.isUseMigrationStoredProcedures();
}
/**
@@ -34,6 +37,15 @@ public class MySqlDdl extends PlatformDdl {
return "drop index " + maxConstraintName(indexName) + " on " + tableName;
}
@Override
public void alterTableDropColumn(final DdlBuffer buffer, final String tableName, final String columnName) throws IOException {
if (this.useMigrationStoredProcedures) {
buffer.append("CALL usp_ebean_drop_column('").append(tableName).append("', '").append(columnName).append("')").endOfStatement();
} else {
super.alterTableDropColumn(buffer, tableName, columnName);
}
}
/**
* Return the drop foreign key clause.
*/
@@ -1,14 +1,15 @@
package io.ebeaninternal.dbmigration.migrationreader;
import io.ebean.util.IOUtils;
import io.ebeaninternal.dbmigration.migration.Migration;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
/**
* Simple writer for output of the Migration/ChangeSet as an XML document.
@@ -26,7 +27,7 @@ public class MigrationXmlWriter {
*/
public void write(Migration migration, File file) {
try (FileWriter writer = new FileWriter(file)) {
try (Writer writer = IOUtils.newWriter(file)) {
writer.write("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n");
if (comment != null) {
@@ -2,6 +2,7 @@ package io.ebeaninternal.dbmigration.model;
import io.ebean.config.DatabaseConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.util.IOUtils;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlBuffer;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlHandler;
import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite;
@@ -14,7 +15,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;
import java.util.List;
@@ -70,16 +70,16 @@ public class PlatformDdlWriter {
*/
protected void writePlatformDdl(DdlWrite write, File resourcePath, String fullVersion) throws IOException {
if (!write.isApplyEmpty()) {
try (FileWriter applyWriter = createWriter(resourcePath, fullVersion, ".sql")) {
try (Writer applyWriter = createWriter(resourcePath, fullVersion, ".sql")) {
writeApplyDdl(applyWriter, write);
applyWriter.flush();
}
}
}
protected FileWriter createWriter(File path, String fullVersion, String suffix) throws IOException {
protected Writer createWriter(File path, String fullVersion, String suffix) throws IOException {
File applyFile = new File(path, fullVersion + suffix);
return new FileWriter(applyFile);
return IOUtils.newWriter(applyFile);
}
/**