From 18aeedc1537b7ec9589ddad3a57179dd7b215628 Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Tue, 18 Aug 2015 22:15:11 +1200 Subject: [PATCH] #381 - Change DbMigration to use default server platform when no platform specified --- .../avaje/ebean/config/DbMigrationConfig.java | 27 ++++---- .../avaje/ebean/dbmigration/DbMigration.java | 63 +++++++++++++++++-- .../ebean/config/DbMigrationConfigTest.java | 1 - 3 files changed, 70 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/avaje/ebean/config/DbMigrationConfig.java b/src/main/java/com/avaje/ebean/config/DbMigrationConfig.java index 030b026f6..3783ba087 100644 --- a/src/main/java/com/avaje/ebean/config/DbMigrationConfig.java +++ b/src/main/java/com/avaje/ebean/config/DbMigrationConfig.java @@ -6,27 +6,26 @@ package com.avaje.ebean.config; public class DbMigrationConfig { /** - * The application name which is used as the unique code when applying migrations. + * Resource path for the migration xml and sql. + * Typically you would change 'app' to be a better/more unique. */ - private String appName; + private String resourcePath = "dbmigration/app"; /** - * Path where migration + * Return the resource path for db migrations. */ - private String resourcePath; - - public String getAppName() { - return appName; - } - - public void setAppName(String appName) { - this.appName = appName; - } - public String getResourcePath() { return resourcePath; } + /** + * Set the resource path for db migrations. + *

+ * Typically this would be something like "dbmigration/myapp" where myapp gives it a + * unique resource path in the case there are multiple EbeanServer applications in the + * single classpath. + *

+ */ public void setResourcePath(String resourcePath) { this.resourcePath = resourcePath; } @@ -35,8 +34,6 @@ public class DbMigrationConfig { * Load the settings from the PropertiesWrapper. */ public void loadSettings(PropertiesWrapper properties) { - - appName = properties.get("migration.appName", appName); resourcePath = properties.get("migration.resourcePath", resourcePath); } } diff --git a/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java b/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java index 9009cd00a..5ee238209 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java +++ b/src/main/java/com/avaje/ebean/dbmigration/DbMigration.java @@ -33,7 +33,13 @@ 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). + *

*/ public class DbMigration { @@ -54,18 +60,29 @@ public class DbMigration { protected DbConstraintNaming constraintNaming; public DbMigration() { - DbOffline.asH2(); } + /** + * Set the path from the current working directory to the application resources. + * + * This defaults to maven style 'src/main/resources'. + */ public void setPathToResources(String pathToResources) { this.pathToResources = pathToResources; } + /** + * Set the server to use to determine the current model. + * Typically this is not called explicitly. + */ public void setServer(EbeanServer ebeanServer) { this.server = (SpiEbeanServer) ebeanServer; setServerConfig(server.getServerConfig()); } + /** + * Set the serverConfig to use. Typically this is not called explicitly. + */ public void setServerConfig(ServerConfig config) { if (this.serverConfig == null) { this.serverConfig = config; @@ -78,10 +95,22 @@ public class DbMigration { } } + /** + * Set the specific platform to generate DDL for. + *

+ * If not set this defaults to the platform of the default server. + *

+ */ public void setPlatform(DbPlatformName platform) { setPlatform(getPlatform(platform)); } + /** + * Set the specific platform to generate DDL for. + *

+ * If not set this defaults to the platform of the default server. + *

+ */ public void setPlatform(DatabasePlatform databasePlatform) { this.databasePlatform = databasePlatform; DbOffline.setPlatform(databasePlatform.getName()); @@ -89,15 +118,21 @@ public class DbMigration { /** * 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). + *

*/ public void addPlatform(DbPlatformName platform, String prefix) { if (!prefix.endsWith("-")) { - prefix+="-"; + prefix += "-"; } platforms.add(new Pair(getPlatform(platform), prefix)); } - + /** + * Run the migration generating the next migration xml file and associated apply and rollback sql scripts. + */ public void runMigration() throws IOException { // use this flag to stop other plugins like full DDL generation @@ -158,19 +193,34 @@ public class DbMigration { } } + /** + * Write the migration xml. + */ protected void writeMigrationXml(Migration dbMigration, File resourcePath, int migrationVersion) { - File file = new File(resourcePath, "v"+migrationVersion+".0.xml"); + File file = new File(resourcePath, "v"+migrationVersion+".0.xml"); MigrationXmlWriter xmlWriter = new MigrationXmlWriter(); xmlWriter.write(dbMigration, file); } + /** + * Set default server and platform if necessary. + */ protected void setDefaults() { if (server == null) { setServer(Ebean.getDefaultServer()); } + if (databasePlatform == null && platforms.isEmpty()) { + // not explicitly set not set a list of platforms so + // default to the platform of the default server + databasePlatform = server.getDatabasePlatform(); + logger.debug("set platform to {}", databasePlatform.getName()); + } } + /** + * Return the file path to write the xml and sql to. + */ protected File getWritePath() { // path to src/main/resources in typical maven project @@ -188,6 +238,9 @@ public class DbMigration { return path; } + /** + * Return the DatabasePlatform given the platform key. + */ protected DatabasePlatform getPlatform(DbPlatformName platform) { switch (platform) { case H2: diff --git a/src/test/java/com/avaje/ebean/config/DbMigrationConfigTest.java b/src/test/java/com/avaje/ebean/config/DbMigrationConfigTest.java index 061ff7987..9b2ccd843 100644 --- a/src/test/java/com/avaje/ebean/config/DbMigrationConfigTest.java +++ b/src/test/java/com/avaje/ebean/config/DbMigrationConfigTest.java @@ -17,7 +17,6 @@ public class DbMigrationConfigTest { DbMigrationConfig migrationConfig = config.getMigrationConfig(); - assertThat(migrationConfig.getAppName()).isEqualTo("myapp"); assertThat(migrationConfig.getResourcePath()).isEqualTo("dbmigration/myapp"); } } \ No newline at end of file