Merge branch 'add-start-to-plugins' into ebean-new

# Conflicts:
#	ebean-core/src/main/java/io/ebeaninternal/server/core/DefaultServer.java
#	ebean-ddl-generator/src/main/java/io/ebeaninternal/dbmigration/DdlPlugin.java
This commit is contained in:
Roland Praml
2022-01-03 16:09:16 +01:00
9 changed files with 80 additions and 139 deletions
@@ -14,6 +14,13 @@ public interface Plugin {
* Called just before the server starts indicating if it is coming up in online mode.
*/
void online(boolean online);
/**
* Called when server.start is invoked. This may be executed for each tenant.
*/
default void start() {
};
/**
* Called when the server is shutting down.
@@ -1,21 +0,0 @@
package io.ebeaninternal.api;
/**
* DDL generate and run for drop all/create all.
*/
public interface SpiDdlGenerator {
/**
* Generate and run the DDL for drop-all and create-all scripts.
* <p>
* Run based on on property settings for ebean.ddl.generate
*/
void generateDdl();
/**
* Runs the created DDL files.
* <p>
* Run based on on property settings for ebean.ddl.run etc.
*/
void runDdl();
}
@@ -1,13 +0,0 @@
package io.ebeaninternal.api;
/**
* Provides the DDL Generator for create-all/drop-all.
*/
public interface SpiDdlGeneratorProvider {
/**
* Provide the DDL generator.
*/
SpiDdlGenerator generator(SpiEbeanServer server);
}
@@ -161,7 +161,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
private final ReadAuditLogger readAuditLogger;
private final CQueryEngine cqueryEngine;
private final List<Plugin> serverPlugins;
private final SpiDdlGenerator ddlGenerator;
private final ScriptRunner scriptRunner;
private final ExpressionFactory expressionFactory;
private final SpiBackgroundExecutor backgroundExecutor;
@@ -224,7 +223,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
this.metaInfoManager = new DefaultMetaInfoManager(this);
this.serverPlugins = config.getPlugins();
this.tempFileProvider = config.getConfig().getTempFileProvider();
this.ddlGenerator = config.initDdlGenerator(this);
this.scriptRunner = new DScriptRunner(this);
configureServerPlugins();
@@ -254,9 +252,6 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
* Execute all the plugins with an online flag indicating the DB is up or not.
*/
public void executePlugins(boolean online) {
if (!config.isDocStoreOnly()) {
ddlGenerator.generateDdl();
}
for (Plugin plugin : serverPlugins) {
plugin.online(online);
}
@@ -386,8 +381,9 @@ public final class DefaultServer implements SpiServer, SpiEbeanServer {
migrationRunner.setPlatform(config.getDatabasePlatform().getPlatform().base().name().toLowerCase());
migrationRunner.loadProperties(config.getProperties());
migrationRunner.run(config.getDataSource());
} else if (config.isDdlRun()) {
ddlGenerator.runDdl();
}
for (Plugin plugin : serverPlugins) {
plugin.start();
}
}
@@ -601,31 +601,4 @@ public final class InternalConfiguration {
return new QueryPlanLoggerExplain();
}
}
/**
* Return the DDL generator.
*/
public SpiDdlGenerator initDdlGenerator(SpiEbeanServer server) {
final SpiDdlGeneratorProvider service = service(SpiDdlGeneratorProvider.class);
return service == null ? new NoopDdl(server.config().isDdlRun()) : service.generator(server);
}
private static class NoopDdl implements SpiDdlGenerator {
private final boolean ddlRun;
NoopDdl(boolean ddlRun) {
this.ddlRun = ddlRun;
}
@Override
public void generateDdl() {
// do nothing
}
@Override
public void runDdl() {
if (ddlRun) {
CoreLog.log.error("Configured to run DDL but ebean-ddl-generator is not in the classpath (or ebean-test in the test classpath?)");
}
}
}
}
@@ -1,13 +0,0 @@
package io.ebeaninternal.dbmigration;
import io.ebeaninternal.api.SpiDdlGenerator;
import io.ebeaninternal.api.SpiDdlGeneratorProvider;
import io.ebeaninternal.api.SpiEbeanServer;
public class DdlGeneratorProvider implements SpiDdlGeneratorProvider {
@Override
public SpiDdlGenerator generator(SpiEbeanServer server) {
return new DdlGenerator(server);
}
}
@@ -5,8 +5,9 @@ import io.ebean.config.DatabaseConfig;
import io.ebean.config.dbplatform.DatabasePlatform;
import io.ebean.ddlrunner.DdlRunner;
import io.ebean.ddlrunner.ScriptTransform;
import io.ebean.plugin.Plugin;
import io.ebean.plugin.SpiServer;
import io.ebean.util.JdbcClose;
import io.ebeaninternal.api.SpiDdlGenerator;
import io.ebeaninternal.api.SpiEbeanServer;
import io.ebeaninternal.dbmigration.model.CurrentModel;
import io.ebeaninternal.dbmigration.model.MTable;
@@ -33,46 +34,85 @@ import java.sql.SQLException;
* Typically the "Create All" DDL is executed for running tests etc and has nothing to do
* with DB Migration (diff based) DDL.
*/
public class DdlGenerator implements SpiDdlGenerator {
public class DdlPlugin implements Plugin {
private static final Logger log = LoggerFactory.getLogger(DdlGenerator.class);
private static final Logger log = LoggerFactory.getLogger(DdlPlugin.class);
private static final String[] BUILD_DIRS = {"target", "build"};
private final SpiEbeanServer server;
private SpiEbeanServer server;
private final boolean generateDdl;
private final boolean extraDdl;
private final boolean createOnly;
private final boolean jaxbPresent;
private final boolean ddlAutoCommit;
private final String dbSchema;
private final ScriptTransform scriptTransform;
private final Platform platform;
private final String platformName;
private final boolean useMigrationStoredProcedures;
private boolean generateDdl;
private boolean runDdl;
private boolean extraDdl;
private boolean createOnly;
private boolean jaxbPresent;
private boolean ddlAutoCommit;
private String dbSchema;
private ScriptTransform scriptTransform;
private Platform platform;
private String platformName;
private boolean useMigrationStoredProcedures;
private CurrentModel currentModel;
private String dropAllContent;
private String createAllContent;
private final File baseDir;
public DdlGenerator(SpiEbeanServer server) {
this.server = server;
private File baseDir;
@Override
public void configure(SpiServer server) {
this.server = (SpiEbeanServer) server;
final DatabaseConfig config = server.config();
this.jaxbPresent = Detect.isJAXBPresent(config);
this.generateDdl = config.isDdlGenerate();
this.extraDdl = config.isDdlExtra();
this.createOnly = config.isDdlCreateOnly();
this.dbSchema = config.getDbSchema();
final DatabasePlatform databasePlatform = server.databasePlatform();
this.platform = databasePlatform.getPlatform();
this.platformName = platform.base().name();
this.ddlAutoCommit = databasePlatform.isDdlAutoCommit();
this.useMigrationStoredProcedures = config.getDatabasePlatform().isUseMigrationStoredProcedures();
this.scriptTransform = createScriptTransform(config);
this.baseDir = initBaseDir();
this.runDdl = config.isDdlRun() && !config.isDocStoreOnly();
if (generateDdl || runDdl) {
this.jaxbPresent = Detect.isJAXBPresent(config);
this.extraDdl = config.isDdlExtra();
this.createOnly = config.isDdlCreateOnly();
this.dbSchema = config.getDbSchema();
final DatabasePlatform databasePlatform = server.databasePlatform();
this.platform = databasePlatform.getPlatform();
this.platformName = platform.base().name();
this.ddlAutoCommit = databasePlatform.isDdlAutoCommit();
this.useMigrationStoredProcedures = config.getDatabasePlatform().isUseMigrationStoredProcedures();
this.scriptTransform = createScriptTransform(config);
this.baseDir = initBaseDir();
}
}
/**
* Generate the DDL drop and create scripts if the properties have been set.
*/
@Override
public void online(boolean online) {
if (generateDdl) {
if (!createOnly) {
writeDrop(getDropFileName());
}
writeCreate(getCreateFileName());
}
}
/**
* Run the DDL drop and DDL create scripts if properties have been set.
*/
@Override
public void start() {
if (runDdl) {
Connection connection = null;
try {
connection = obtainConnection();
runDdlWith(connection);
} finally {
JdbcClose.rollback(connection);
JdbcClose.close(connection);
}
}
}
@Override
public void shutdown() {}
private File initBaseDir() {
for (String buildDir : BUILD_DIRS) {
File dir = new File(buildDir);
@@ -83,34 +123,6 @@ public class DdlGenerator implements SpiDdlGenerator {
return new File(".");
}
/**
* Generate the DDL drop and create scripts if the properties have been set.
*/
@Override
public void generateDdl() {
if (generateDdl) {
if (!createOnly) {
writeDrop(getDropFileName());
}
writeCreate(getCreateFileName());
}
}
/**
* Run the DDL drop and DDL create scripts if properties have been set.
*/
@Override
public void runDdl() {
Connection connection = null;
try {
connection = obtainConnection();
runDdlWith(connection);
} finally {
JdbcClose.rollback(connection);
JdbcClose.close(connection);
}
}
private void runDdlWith(Connection connection) {
try {
if (dbSchema != null) {
@@ -0,0 +1 @@
io.ebeaninternal.dbmigration.DdlPlugin
@@ -1 +0,0 @@
io.ebeaninternal.dbmigration.DdlGeneratorProvider