diff --git a/src/main/java/io/ebean/annotation/DbMigration.java b/src/main/java/io/ebean/annotation/DbMigration.java index e251acafa..091148e37 100644 --- a/src/main/java/io/ebean/annotation/DbMigration.java +++ b/src/main/java/io/ebean/annotation/DbMigration.java @@ -11,50 +11,50 @@ import io.ebean.Platform; /** * Annotation to specify details for DDL & Migration-generation. (e.g. defaults/renames/...) * This annotation is EXPERMIENTAL and may change. - * + * * @author Roland Praml, FOCONIS AG */ @Retention(RetentionPolicy.RUNTIME) @Target(ElementType.FIELD) @Repeatable(DbMigration.List.class) public @interface DbMigration { - + /** * DdlScripts that will be executed before the 'alter' command. - * - * You may write a custom update routine here. - * If you do not specify an SQL here, and this will alter the table - * to a non-null column, ebean will autogenerate a statement from + * + * You may write a custom update routine here. + * If you do not specify an SQL here, and this will alter the table + * to a non-null column, ebean will autogenerate a statement from * default value like this: *
    * UPDATE table SET column = 'foo' WHERE column IS NULL
    * 
*/ String[] preAlter() default {}; - + /** * DdlScript that will be executed after the 'alter' command */ String[] postAlter() default {}; - + /** * DdlScript that will be executed before the 'add' command */ String[] preAdd() default {}; - + /** * DdlScript that will be executed after the 'add' command. * You may write certain update scripts here. */ String[] postAdd() default {}; - + // TODO: Do we need preDrop / postDrop? Platform[] platforms() default {}; /** - * Repeatable support for {@link Formula}. + * Repeatable support for {@link DbMigration}. */ @Target({ ElementType.FIELD }) @Retention(RetentionPolicy.RUNTIME) diff --git a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java index 63855b2e9..08d7fc7f8 100644 --- a/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java +++ b/src/main/java/io/ebean/dbmigration/ddlgeneration/platform/PlatformDdl.java @@ -18,6 +18,8 @@ import io.ebean.dbmigration.migration.DropHistoryTable; import io.ebean.dbmigration.migration.IdentityType; import io.ebean.dbmigration.model.MTable; import io.ebean.util.StringHelper; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import java.io.IOException; import java.util.List; @@ -27,6 +29,8 @@ import java.util.List; */ public class PlatformDdl { + private static final Logger logger = LoggerFactory.getLogger(PlatformDdl.class); + protected final DatabasePlatform platform; protected PlatformHistoryDdl historyDdl = new NoHistorySupportDdl(); @@ -75,7 +79,7 @@ public class PlatformDdl { protected String dropUniqueConstraint = "drop constraint"; protected String addConstraint = "add constraint"; - + protected String addColumn = "add column"; protected String columnSetType = ""; @@ -87,7 +91,7 @@ public class PlatformDdl { protected String columnSetNotnull = "set not null"; protected String columnSetNull = "set null"; - + protected String updateNullWithDefault = "update ${table} set ${column} = ${default} where ${column} is null"; /** @@ -380,11 +384,11 @@ public class PlatformDdl { appendColumns(columns, buffer); return buffer.toString(); } - + public void alterTableAddColumn(DdlBuffer buffer, String tableName, Column column, boolean onHistoryTable, String defaultValue) throws IOException { String convertedType = convert(column.getType(), false); - + buffer.append("alter table ").append(tableName) .append(" ").append(addColumn).append(" ").append(column.getName()) .append(" ").append(convertedType); @@ -396,14 +400,14 @@ public class PlatformDdl { if (defaultValue != null) { if (typeContainsDefault(convertedType)) { - System.err.println("Cannot set default value for '" + tableName + "." + column.getName() + "'"); + logger.error("Cannot set default value for '" + tableName + "." + column.getName() + "'"); } else { buffer.append(" default "); buffer.append(defaultValue); } } buffer.endOfStatement(); - + // check constraints cannot be added in one statement for h2 if (!StringHelper.isNull(column.getCheckConstraint())) { String ddl = alterTableAddCheckConstraint(tableName, column.getCheckConstraintName(), column.getCheckConstraint()); @@ -412,7 +416,7 @@ public class PlatformDdl { } else { buffer.endOfStatement(); } - + } /** @@ -515,7 +519,7 @@ public class PlatformDdl { public DatabasePlatform getPlatform() { return platform; } - + public String getUpdateNullWithDefault() { return updateNullWithDefault; } diff --git a/src/main/java/io/ebeaninternal/server/deploy/BeanProperty.java b/src/main/java/io/ebeaninternal/server/deploy/BeanProperty.java index 68b52a6f4..f0255bb53 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/BeanProperty.java +++ b/src/main/java/io/ebeaninternal/server/deploy/BeanProperty.java @@ -2,11 +2,9 @@ package io.ebeaninternal.server.deploy; import com.fasterxml.jackson.core.JsonToken; import io.ebean.ValuePair; -import io.ebean.annotation.DbDefault; import io.ebean.bean.EntityBean; import io.ebean.bean.PersistenceContext; import io.ebean.config.EncryptKey; -import io.ebean.config.dbplatform.DbDefaultValue; import io.ebean.config.dbplatform.DbEncryptFunction; import io.ebean.config.dbplatform.DbPlatformType; import io.ebean.plugin.Property; @@ -306,7 +304,7 @@ public class BeanProperty implements ElPropertyValue, Property { this.dbLength = deploy.getDbLength(); this.dbScale = deploy.getDbScale(); this.dbColumnDefn = InternString.intern(deploy.getDbColumnDefn()); - this.dbColumnDefault = DbDefaultValue.toSqlLiteral(deploy.getDbColumnDefault(), deploy.getPropertyType(), deploy.getDbType()); + this.dbColumnDefault = deploy.getDbColumnDefaultSqlLiteral(); this.dbMigrationInfos = deploy.getDbMigrationInfos(); this.inherited = false;// deploy.isInherited(); @@ -418,7 +416,7 @@ public class BeanProperty implements ElPropertyValue, Property { this.dbScale = source.getDbScale(); this.dbColumnDefn = InternString.intern(source.getDbColumnDefn()); this.dbColumnDefault = source.dbColumnDefault; - this.dbMigrationInfos = source.dbMigrationInfos; + this.dbMigrationInfos = source.dbMigrationInfos; this.inherited = source.isInherited(); this.owningType = source.owningType; @@ -1044,7 +1042,7 @@ public class BeanProperty implements ElPropertyValue, Property { public List getDbMigrationInfos() { return dbMigrationInfos; } - + /** * Return the bean Field associated with this property. */ diff --git a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java index 86eb6f338..cc03bb9fd 100644 --- a/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java +++ b/src/main/java/io/ebeaninternal/server/deploy/meta/DeployBeanProperty.java @@ -1006,14 +1006,17 @@ public class DeployBeanProperty { return docMapping.create(); } - public String getDbColumnDefault() { - return dbColumnDefault; + /** + * Return the DB Column default taking into account literal translation. + */ + public String getDbColumnDefaultSqlLiteral() { + return DbDefaultValue.toSqlLiteral(dbColumnDefault, propertyType, dbType); } public void setDbColumnDefault(String dbColumnDefault) { this.dbColumnDefault = dbColumnDefault; } - + public void setTenantId() { this.tenantId = true; this.nullable = false; @@ -1024,15 +1027,16 @@ public class DeployBeanProperty { public boolean isTenantId() { return tenantId; } - + public void addDbMigrationInfo(DbMigrationInfo info) { if (dbMigrationInfos == null) { dbMigrationInfos = new ArrayList<>(); } dbMigrationInfos.add(info); } - + public List getDbMigrationInfos() { return dbMigrationInfos; } + }