package io.ebeaninternal.dbmigration; import io.ebean.Ebean; import io.ebean.EbeanServer; import io.ebean.annotation.Platform; import io.ebean.config.DbConstraintNaming; import io.ebean.config.DbMigrationConfig; import io.ebean.config.PlatformConfig; import io.ebean.config.ServerConfig; import io.ebean.config.dbplatform.DatabasePlatform; import io.ebean.config.dbplatform.db2.DB2Platform; import io.ebean.config.dbplatform.h2.H2Platform; import io.ebean.config.dbplatform.hsqldb.HsqldbPlatform; import io.ebean.config.dbplatform.mysql.MySqlPlatform; import io.ebean.config.dbplatform.oracle.OraclePlatform; import io.ebean.config.dbplatform.postgres.PostgresPlatform; import io.ebean.config.dbplatform.sqlanywhere.SqlAnywherePlatform; 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.ebeaninternal.api.SpiEbeanServer; import io.ebeaninternal.dbmigration.ddlgeneration.DdlWrite; import io.ebeaninternal.dbmigration.migration.Migration; import io.ebeaninternal.dbmigration.migrationreader.MigrationXmlWriter; import io.ebeaninternal.dbmigration.model.CurrentModel; import io.ebeaninternal.dbmigration.model.MConfiguration; import io.ebeaninternal.dbmigration.model.MigrationModel; import io.ebeaninternal.dbmigration.model.MigrationVersion; import io.ebeaninternal.dbmigration.model.ModelContainer; import io.ebeaninternal.dbmigration.model.ModelDiff; 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; /** * Generates DB Migration xml and sql scripts. *
* Reads the prior migrations and compares with the current model of the EbeanServer * and generates a migration 'diff' in the form of xml document with the logical schema * changes and a series of sql scripts to apply, rollback the applied changes if necessary * and drop objects (drop tables, drop columns). *
** This does not run the migration or ddl scripts but just generates them. *
*{@code
*
* DbMigration migration = DbMigration.create();
* migration.setPathToResources("src/main/resources");
* migration.setPlatform(DbPlatformName.ORACLE);
*
* migration.generateMigration();
*
* }
*/
public class DefaultDbMigration implements DbMigration {
protected static final Logger logger = LoggerFactory.getLogger(DefaultDbMigration.class);
private static final String initialVersion = "1.0";
private static final String GENERATED_COMMENT = "THIS IS A GENERATED FILE - DO NOT MODIFY";
/**
* Set to true if DefaultDbMigration run with online EbeanServer instance.
*/
protected final boolean online;
protected SpiEbeanServer server;
protected DbMigrationConfig migrationConfig;
protected String pathToResources = "src/main/resources";
protected DatabasePlatform databasePlatform;
private boolean vanillaPlatform;
protected List* This defaults to maven style 'src/main/resources'. */ @Override public void setPathToResources(String pathToResources) { this.pathToResources = pathToResources; } /** * Set the server to use to determine the current model. * Typically this is not called explicitly. */ @Override public void setServer(EbeanServer ebeanServer) { this.server = (SpiEbeanServer) ebeanServer; setServerConfig(server.getServerConfig()); } /** * Set the serverConfig to use. Typically this is not called explicitly. */ @Override public void setServerConfig(ServerConfig config) { if (this.serverConfig == null) { this.serverConfig = config; } if (migrationConfig == null) { this.migrationConfig = serverConfig.getMigrationConfig(); } if (constraintNaming == null) { this.constraintNaming = serverConfig.getConstraintNaming(); } } @Override public void setStrictMode(boolean strictMode) { this.strictMode = strictMode; } @Override public void setApplyPrefix(String applyPrefix) { this.applyPrefix = applyPrefix; } @Override public void setVersion(String version) { this.version = version; } @Override public void setName(String name) { this.name = name; } @Override public void setGeneratePendingDrop(String generatePendingDrop) { this.generatePendingDrop = generatePendingDrop; } @Override public void setIncludeGeneratedFileComment(boolean includeGeneratedFileComment) { this.includeGeneratedFileComment = includeGeneratedFileComment; } @Override public void setHeader(String header) { this.header = header; } /** * Set the specific platform to generate DDL for. *
* If not set this defaults to the platform of the default server. *
*/ @Override public void setPlatform(Platform platform) { vanillaPlatform = true; setPlatform(getPlatform(platform)); } /** * Set the specific platform to generate DDL for. ** If not set this defaults to the platform of the default server. *
*/ @Override public void setPlatform(DatabasePlatform databasePlatform) { this.databasePlatform = databasePlatform; if (!online) { DbOffline.setPlatform(databasePlatform.getPlatform()); } } /** * Add an additional platform to write the migration DDL. ** Use this when you want to generate sql scripts for multiple database platforms * from the migration (e.g. generate migration sql for MySql, Postgres and Oracle). *
*/ @Override public void addPlatform(Platform platform, String prefix) { platforms.add(new Pair(getPlatform(platform), prefix)); } @Override public void addDatabasePlatform(DatabasePlatform databasePlatform, String prefix) { platforms.add(new Pair(databasePlatform, prefix)); } /** * Generate the next migration xml file and associated apply and rollback sql scripts. ** This does not run the migration or ddl scripts but just generates them. *
*{@code
*
* DbMigration migration = DbMigration.create();
* migration.setPathToResources("src/main/resources");
* migration.setPlatform(DbPlatformName.ORACLE);
*
* migration.generateMigration();
*
* }
* *
{@code
*
* DbMigration migration = DbMigration.create();
* migration.setPathToResources("src/main/resources");
*
* migration.addPlatform(DbPlatformName.POSTGRES, "pg");
* migration.addPlatform(DbPlatformName.MYSQL, "mysql");
* migration.addPlatform(DbPlatformName.ORACLE, "mysql");
*
* migration.generateMigration();
*
* }
* @return the generated migration or null
*/
@Override
public String generateMigration() throws IOException {
// use this flag to stop other plugins like full DDL generation
if (!online) {
DbOffline.setGenerateMigration();
if (databasePlatform == null && !platforms.isEmpty()) {
// for multiple platform generation the first platform
// is used to generate the "logical" model diff
setPlatform(platforms.get(0).platform);
}
}
setDefaults();
if (!platforms.isEmpty()) {
configurePlatforms();
}
try {
Request request = createRequest();
if (platforms.isEmpty()) {
generateExtraDdl(request.migrationDir, databasePlatform);
}
String pendingVersion = generatePendingDrop();
if (pendingVersion != null) {
return generatePendingDrop(request, pendingVersion);
} else {
return generateDiff(request);
}
} finally {
if (!online) {
DbOffline.reset();
}
}
}
/**
* Load the configuration for each of the target platforms.
*/
private void configurePlatforms() {
for (Pair pair : platforms) {
PlatformConfig config = serverConfig.newPlatformConfig("dbmigration.platform", pair.prefix);
pair.platform.configure(config);
}
}
/**
* Generate "repeatable" migration scripts.
* * These take scrips from extra-dll.xml (typically views) and outputs "repeatable" * migration scripts (starting with "R__") to be run by FlywayDb or Ebean's own * migration runner. *
*/ private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform) throws IOException { if (dbPlatform != null) { generateExtraDdl(migrationDir, dbPlatform, ExtraDdlXmlReader.readBuiltin()); generateExtraDdl(migrationDir, dbPlatform, ExtraDdlXmlReader.read()); } } private void generateExtraDdl(File migrationDir, DatabasePlatform dbPlatform, ExtraDdl extraDdl) throws IOException { if (extraDdl != null) { List* The full version can contain a comment suffix after a "__" double underscore. */ private String getFullVersion(MigrationModel migrationModel, String dropsFor) { String version = migrationConfig.getVersion(); if (version == null) { version = migrationModel.getNextVersion(initialVersion); } String fullVersion = migrationConfig.getApplyPrefix() + version; if (migrationConfig.getName() != null) { fullVersion += "__" + toUnderScore(migrationConfig.getName()); } else if (dropsFor != null) { fullVersion += "__" + toUnderScore("dropsFor_" + MigrationVersion.trim(dropsFor)); } else if (version.equals(initialVersion)) { fullVersion += "__initial"; } return fullVersion; } /** * Replace spaces with underscores. */ private String toUnderScore(String name) { return name.replace(' ', '_'); } /** * Write any extra platform ddl. */ protected void writeExtraPlatformDdl(String fullVersion, CurrentModel currentModel, Migration dbMigration, File writePath) throws IOException { for (Pair pair : platforms) { DdlWrite platformBuffer = new DdlWrite(new MConfiguration(), currentModel.read()); PlatformDdlWriter platformWriter = createDdlWriter(pair.platform); File subPath = platformWriter.subPath(writePath, pair.prefix); platformWriter.processMigration(dbMigration, platformBuffer, subPath, fullVersion); generateExtraDdl(subPath, pair.platform); } } private PlatformDdlWriter createDdlWriter(DatabasePlatform platform) { return new PlatformDdlWriter(platform, serverConfig, migrationConfig); } /** * Write the migration xml. */ protected boolean writeMigrationXml(Migration dbMigration, File resourcePath, String fullVersion) { String modelFile = fullVersion + migrationConfig.getModelSuffix(); File file = new File(resourcePath, modelFile); if (file.exists()) { return false; } String comment = migrationConfig.isIncludeGeneratedFileComment() ? GENERATED_COMMENT : null; MigrationXmlWriter xmlWriter = new MigrationXmlWriter(comment); xmlWriter.write(dbMigration, file); return true; } /** * Set default server and platform if necessary. */ protected void setDefaults() { if (server == null) { setServer(Ebean.getDefaultServer()); } if (vanillaPlatform || databasePlatform == null) { // not explicitly set so use the platform of the server databasePlatform = server.getDatabasePlatform(); logger.trace("set platform to {}", databasePlatform.getName()); } if (migrationConfig != null) { if (strictMode != null) { migrationConfig.setStrictMode(strictMode); } if (applyPrefix != null) { migrationConfig.setApplyPrefix(applyPrefix); } if (header != null) { migrationConfig.setDdlHeader(header); } if (includeGeneratedFileComment != null) { migrationConfig.setIncludeGeneratedFileComment(includeGeneratedFileComment); } if (version != null) { migrationConfig.setVersion(version); } if (name != null) { migrationConfig.setName(name); } if (generatePendingDrop != null) { migrationConfig.setGeneratePendingDrop(generatePendingDrop); } } } /** * Return the file path to write the xml and sql to. */ protected File getMigrationDirectory() { // path to src/main/resources in typical maven project File resourceRootDir = new File(pathToResources); String resourcePath = migrationConfig.getMigrationPath(); // expect to be a path to something like - src/main/resources/dbmigration/model File path = new File(resourceRootDir, resourcePath); if (!path.exists()) { if (!path.mkdirs()) { logger.debug("Unable to ensure migration directory exists at {}", path.getAbsolutePath()); } } return path; } /** * Return the model directory (relative to the migration directory). */ protected File getModelDirectory(File migrationDirectory) { String modelPath = migrationConfig.getModelPath(); if (modelPath == null || modelPath.isEmpty()) { return migrationDirectory; } File modelDir = new File(migrationDirectory, migrationConfig.getModelPath()); if (!modelDir.exists() && !modelDir.mkdirs()) { logger.debug("Unable to ensure migration model directory exists at {}", modelDir.getAbsolutePath()); } return modelDir; } /** * Return the DatabasePlatform given the platform key. */ protected DatabasePlatform getPlatform(Platform platform) { switch (platform) { case H2: return new H2Platform(); case HSQLDB: return new HsqldbPlatform(); case POSTGRES: return new PostgresPlatform(); case MYSQL: return new MySqlPlatform(); case ORACLE: return new OraclePlatform(); case SQLANYWHERE: return new SqlAnywherePlatform(); case SQLSERVER16: return new SqlServer16Platform(); case SQLSERVER17: return new SqlServer17Platform(); case SQLSERVER: throw new IllegalArgumentException("Please choose the more specific SQLSERVER16 or SQLSERVER17 platform. Refer to issue #1340 for details"); case DB2: return new DB2Platform(); case SQLITE: return new SQLitePlatform(); case GENERIC: return new DatabasePlatform(); default: throw new IllegalArgumentException("Platform missing? " + platform); } } /** * Holds a platform and prefix. Used to generate multiple platform specific DDL * for a single migration. */ public static class Pair { /** * The platform to generate the DDL for. */ public final DatabasePlatform platform; /** * A prefix included into the file/resource names indicating the platform. */ public final String prefix; public Pair(DatabasePlatform platform, String prefix) { this.platform = platform; this.prefix = prefix; } } }