mirror of
https://github.com/ebean-orm/ebean.git
synced 2026-10-23 05:35:54 +00:00
#2283 - ddl-generator - Add DbMigration.addPlatform(Platform) with default sub-directory based on Platform
This commit is contained in:
@@ -196,19 +196,23 @@ public interface DbMigration {
|
||||
void setLockTimeout(int seconds);
|
||||
|
||||
/**
|
||||
* Add an additional platform to write the migration DDL.
|
||||
* Add a platform to write the migration DDL.
|
||||
* <p>
|
||||
* 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).
|
||||
* </p>
|
||||
*/
|
||||
void addPlatform(Platform platform);
|
||||
|
||||
/**
|
||||
* Add a platform to write with a given prefix.
|
||||
*/
|
||||
void addPlatform(Platform platform, String prefix);
|
||||
|
||||
/**
|
||||
* Add an additional databasePlatform to write the migration DDL.
|
||||
* Add a databasePlatform to write the migration DDL.
|
||||
* <p>
|
||||
* Use this when you want to add preconfigured database platforms.
|
||||
* </p>
|
||||
*/
|
||||
void addDatabasePlatform(DatabasePlatform databasePlatform, String prefix);
|
||||
|
||||
@@ -256,9 +260,9 @@ public interface DbMigration {
|
||||
*
|
||||
* migration.setPathToResources("src/main/resources");
|
||||
*
|
||||
* migration.addPlatform(Platform.POSTGRES, "pg");
|
||||
* migration.addPlatform(Platform.MYSQL, "mysql");
|
||||
* migration.addPlatform(Platform.ORACLE, "oracle");
|
||||
* migration.addPlatform(Platform.POSTGRES);
|
||||
* migration.addPlatform(Platform.MYSQL);
|
||||
* migration.addPlatform(Platform.ORACLE);
|
||||
*
|
||||
* migration.generateMigration();
|
||||
*
|
||||
|
||||
+29
-33
@@ -259,13 +259,12 @@ public class DefaultDbMigration implements DbMigration {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Add an additional platform to write the migration DDL.
|
||||
* <p>
|
||||
* 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).
|
||||
* </p>
|
||||
*/
|
||||
@Override
|
||||
public void addPlatform(Platform platform) {
|
||||
String prefix = platform.base().name().toLowerCase();
|
||||
addPlatform(platform, prefix);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void addPlatform(Platform platform, String prefix) {
|
||||
platforms.add(new Pair(getPlatform(platform), prefix));
|
||||
@@ -286,7 +285,7 @@ public class DefaultDbMigration implements DbMigration {
|
||||
*
|
||||
* DbMigration migration = DbMigration.create();
|
||||
* migration.setPathToResources("src/main/resources");
|
||||
* migration.setPlatform(DbPlatformName.ORACLE);
|
||||
* migration.setPlatform(Platform.ORACLE);
|
||||
*
|
||||
* migration.generateMigration();
|
||||
*
|
||||
@@ -298,9 +297,9 @@ public class DefaultDbMigration implements DbMigration {
|
||||
* 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.addPlatform(Platform.POSTGRES);
|
||||
* migration.addPlatform(Platform.MYSQL);
|
||||
* migration.addPlatform(Platform.ORACLE);
|
||||
*
|
||||
* migration.generateMigration();
|
||||
*
|
||||
@@ -318,10 +317,9 @@ public class DefaultDbMigration implements DbMigration {
|
||||
return generateMigrationFor(true);
|
||||
}
|
||||
|
||||
private String generateMigrationFor(boolean dbinitMigration) throws IOException {
|
||||
|
||||
// use this flag to stop other plugins like full DDL generation
|
||||
private String generateMigrationFor(boolean initMigration) throws IOException {
|
||||
if (!online) {
|
||||
// use this flag to stop other plugins like full DDL generation
|
||||
DbOffline.setGenerateMigration();
|
||||
if (databasePlatform == null && !platforms.isEmpty()) {
|
||||
// for multiple platform generation the first platform
|
||||
@@ -334,8 +332,8 @@ public class DefaultDbMigration implements DbMigration {
|
||||
configurePlatforms();
|
||||
}
|
||||
try {
|
||||
Request request = createRequest(dbinitMigration);
|
||||
if (!dbinitMigration) {
|
||||
Request request = createRequest(initMigration);
|
||||
if (!initMigration) {
|
||||
// repeatable migrations
|
||||
if (platforms.isEmpty()) {
|
||||
generateExtraDdl(request.migrationDir, databasePlatform, request.isTablePartitioning());
|
||||
@@ -512,31 +510,31 @@ public class DefaultDbMigration implements DbMigration {
|
||||
return version;
|
||||
}
|
||||
|
||||
private Request createRequest(boolean dbinitMigration) {
|
||||
return new Request(dbinitMigration);
|
||||
private Request createRequest(boolean initMigration) {
|
||||
return new Request(initMigration);
|
||||
}
|
||||
|
||||
private class Request {
|
||||
|
||||
final boolean dbinitMigration;
|
||||
final boolean initMigration;
|
||||
final File migrationDir;
|
||||
final File modelDir;
|
||||
final CurrentModel currentModel;
|
||||
final ModelContainer migrated;
|
||||
final ModelContainer current;
|
||||
|
||||
private Request(boolean dbinitMigration) {
|
||||
this.dbinitMigration = dbinitMigration;
|
||||
private Request(boolean initMigration) {
|
||||
this.initMigration = initMigration;
|
||||
this.currentModel = new CurrentModel(server, constraintNaming);
|
||||
this.current = currentModel.read();
|
||||
this.migrationDir = getMigrationDirectory(dbinitMigration);
|
||||
if (dbinitMigration) {
|
||||
this.migrationDir = getMigrationDirectory(initMigration);
|
||||
if (initMigration) {
|
||||
this.modelDir = null;
|
||||
this.migrated = new ModelContainer();
|
||||
} else {
|
||||
this.modelDir = getModelDirectory(migrationDir);
|
||||
MigrationModel migrationModel = new MigrationModel(modelDir, modelSuffix);
|
||||
this.migrated = migrationModel.read(dbinitMigration);
|
||||
this.migrated = migrationModel.read(false);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -551,7 +549,7 @@ public class DefaultDbMigration implements DbMigration {
|
||||
// always read the next version using the main migration directory (not dbinit)
|
||||
File migDirectory = getMigrationDirectory(false);
|
||||
File modelDir = getModelDirectory(migDirectory);
|
||||
return LastMigration.nextVersion(migDirectory, modelDir, dbinitMigration);
|
||||
return LastMigration.nextVersion(migDirectory, modelDir, initMigration);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -588,7 +586,7 @@ public class DefaultDbMigration implements DbMigration {
|
||||
String fullVersion = getFullVersion(request.nextVersion(), dropsFor);
|
||||
|
||||
logInfo("generating migration:%s", fullVersion);
|
||||
if (!request.dbinitMigration && !writeMigrationXml(dbMigration, request.modelDir, fullVersion)) {
|
||||
if (!request.initMigration && !writeMigrationXml(dbMigration, request.modelDir, fullVersion)) {
|
||||
logError("migration already exists, not generating DDL");
|
||||
return null;
|
||||
} else {
|
||||
@@ -782,17 +780,15 @@ public class DefaultDbMigration implements DbMigration {
|
||||
/**
|
||||
* Return the file path to write the xml and sql to.
|
||||
*/
|
||||
File getMigrationDirectory(boolean dbinitMigration) {
|
||||
|
||||
File getMigrationDirectory(boolean initMigration) {
|
||||
// path to src/main/resources in typical maven project
|
||||
File resourceRootDir = new File(pathToResources);
|
||||
if (!resourceRootDir.exists()) {
|
||||
String msg = String.format("Error - path to resources %s does not exist. Absolute path is %s", pathToResources, resourceRootDir.getAbsolutePath());
|
||||
throw new UnknownResourcePathException(msg);
|
||||
}
|
||||
String resourcePath = getMigrationPath(dbinitMigration);
|
||||
|
||||
// expect to be a path to something like - src/main/resources/dbmigration/model
|
||||
String resourcePath = getMigrationPath(initMigration);
|
||||
// expect to be a path to something like - src/main/resources/dbmigration
|
||||
File path = new File(resourceRootDir, resourcePath);
|
||||
if (!path.exists()) {
|
||||
if (!path.mkdirs()) {
|
||||
@@ -802,8 +798,8 @@ public class DefaultDbMigration implements DbMigration {
|
||||
return path;
|
||||
}
|
||||
|
||||
private String getMigrationPath(boolean dbinitMigration) {
|
||||
return dbinitMigration ? migrationInitPath : migrationPath;
|
||||
private String getMigrationPath(boolean initMigration) {
|
||||
return initMigration ? migrationInitPath : migrationPath;
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -20,13 +20,12 @@ class LastMigration {
|
||||
/**
|
||||
* Return the next migation version given the migration directory.
|
||||
*/
|
||||
static String nextVersion(File migDir, File modelDir, boolean dbinitMigration) {
|
||||
|
||||
static String nextVersion(File migDir, File modelDir, boolean initMigration) {
|
||||
String last = lastVersion(migDir, modelDir);
|
||||
if (last == null) {
|
||||
return null;
|
||||
}
|
||||
return (dbinitMigration) ? last : MigrationVersion.parse(last).nextVersion();
|
||||
return (initMigration) ? last : MigrationVersion.parse(last).nextVersion();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
+5
-7
@@ -34,16 +34,14 @@ public class MigrationModel {
|
||||
* Read all the migrations returning the model with all
|
||||
* the migrations applied in version order.
|
||||
*
|
||||
* @param dbinitMigration If true we don't apply model changes, migration is from scratch.
|
||||
* @param initMigration If true we don't apply model changes, migration is from scratch.
|
||||
*/
|
||||
public ModelContainer read(boolean dbinitMigration) {
|
||||
|
||||
readMigrations(dbinitMigration);
|
||||
public ModelContainer read(boolean initMigration) {
|
||||
readMigrations(initMigration);
|
||||
return model;
|
||||
}
|
||||
|
||||
private void readMigrations(boolean dbinitMigration) {
|
||||
|
||||
private void readMigrations(boolean initMigration) {
|
||||
// find all the migration xml files
|
||||
File[] xmlFiles = modelDirectory.listFiles(pathname -> pathname.getName().toLowerCase().endsWith(modelSuffix));
|
||||
if (xmlFiles == null || xmlFiles.length == 0) {
|
||||
@@ -57,7 +55,7 @@ public class MigrationModel {
|
||||
// sort into version order before applying
|
||||
Collections.sort(resources);
|
||||
|
||||
if (!dbinitMigration) {
|
||||
if (!initMigration) {
|
||||
for (MigrationResource migrationResource : resources) {
|
||||
logger.debug("read {}", migrationResource);
|
||||
model.apply(migrationResource.read(), migrationResource.getVersion());
|
||||
|
||||
+6
-6
@@ -46,16 +46,16 @@ public class DbMigrationGenerateTest {
|
||||
|
||||
// migration.addPlatform(Platform.GENERIC, "generic"); there is no ddl handler for generic
|
||||
// migration.addPlatform(Platform.SQLANYWHERE, "sqlanywhere"); and sqlanywhere
|
||||
migration.addPlatform(Platform.DB2, "db2");
|
||||
migration.addPlatform(Platform.H2, "h2");
|
||||
migration.addPlatform(Platform.DB2);
|
||||
migration.addPlatform(Platform.H2);
|
||||
migration.addPlatform(Platform.HSQLDB, "hsqldb");
|
||||
migration.addPlatform(Platform.MYSQL, "mysql");
|
||||
migration.addPlatform(Platform.MYSQL55, "mysql55");
|
||||
migration.addPlatform(Platform.POSTGRES, "postgres");
|
||||
migration.addPlatform(Platform.ORACLE, "oracle");
|
||||
migration.addPlatform(Platform.SQLITE, "sqlite");
|
||||
migration.addPlatform(Platform.POSTGRES);
|
||||
migration.addPlatform(Platform.ORACLE);
|
||||
migration.addPlatform(Platform.SQLITE);
|
||||
migration.addPlatform(Platform.SQLSERVER17, "sqlserver17");
|
||||
migration.addPlatform(Platform.HANA, "hana");
|
||||
migration.addPlatform(Platform.HANA);
|
||||
|
||||
DatabaseConfig config = new DatabaseConfig();
|
||||
config.setName("migrationtest");
|
||||
|
||||
Reference in New Issue
Block a user