#1295 - Multiple platform DbMigration generation with non-native boolean type (BIT, INTEGER etc) produces incorrect DDL for other DB Platform with native boolean

This commit is contained in:
Rob Bygrave committed 2018-03-01 12:55:22 +13:00
1 parent 0d0a3422fc
commit 6f2e1c0248
6 files changed
+78 -18

No files matched your search

@@ -126,27 +126,33 @@ public class DbPlatformType implements ExtraDbTypes {
StringBuilder sb = new StringBuilder();
sb.append(name);
if (canHaveLength || !strict) {
// see if there is a precision/scale to add (or not)
int len = deployLength != 0 ? deployLength : defaultLength;
if (len == Integer.MAX_VALUE) {
sb.append("(max)"); // TODO: this is sqlserver specific
} else if (len > 0) {
sb.append("(");
sb.append(len);
int scale = deployScale != 0 ? deployScale : defaultScale;
if (scale > 0) {
sb.append(",");
sb.append(scale);
}
sb.append(")");
}
renderLengthScale(deployLength, deployScale, sb);
}
return sb.toString();
}
/**
* Render the length and scale part of the column definition.
*/
protected void renderLengthScale(int deployLength, int deployScale, StringBuilder sb) {
// see if there is a precision/scale to add (or not)
int len = deployLength != 0 ? deployLength : defaultLength;
if (len == Integer.MAX_VALUE) {
sb.append("(max)"); // TODO: this is sqlserver specific
} else if (len > 0) {
sb.append("(");
sb.append(len);
int scale = deployScale != 0 ? deployScale : defaultScale;
if (scale > 0) {
sb.append(",");
sb.append(scale);
}
sb.append(")");
}
}
/**
* Create a copy of the type with a new default length.
*/
@@ -10,8 +10,23 @@ import java.util.Map;
*/
public class DbPlatformTypeMapping {
/**
* Boolean type used for logical model.
*/
private static class BooleanLogicalType extends DbPlatformType {
BooleanLogicalType() {
super("boolean", false);
}
@Override
protected void renderLengthScale(int deployLength, int deployScale, StringBuilder sb) {
// do not have length - even if platform boolean type does like integer(1)
}
}
private static DbPlatformTypeLookup lookup = new DbPlatformTypeLookup();
private static final DbPlatformType BOOLEAN_LOGICAL = new BooleanLogicalType();
private static final DbPlatformType UUID_NATIVE = new DbPlatformType("uuid", false);
@SuppressWarnings("unused")
private static final DbPlatformType UUID_PLACEHOLDER = new DbPlatformType("uuidPlaceholder");
@@ -51,7 +66,7 @@ public class DbPlatformTypeMapping {
*/
private void loadDefaults(boolean logicalTypes) {
put(DbType.BOOLEAN);
put(DbType.BOOLEAN, BOOLEAN_LOGICAL);
put(DbType.BIT);
put(DbType.INTEGER);
put(DbType.BIGINT);
@@ -3,7 +3,7 @@ package io.ebeaninternal.server.type;
/**
* Boolean ScalarType's must implement to support DDL default values etc.
*/
public interface ScalarTypeBool extends ScalarType<Boolean> {
public interface ScalarTypeBool extends ScalarType<Boolean>, ScalarTypeLogicalType {
/**
* Return the DB literal value for FALSE.
@@ -302,6 +302,11 @@ public class ScalarTypeBoolean {
super(Boolean.class, jdbcNative, jdbcType);
}
@Override
public int getLogicalType() {
return Types.BOOLEAN;
}
/**
* Return the DB literal value for false.
*/
@@ -0,0 +1,20 @@
package io.ebean.config.dbplatform;
import org.junit.Test;
import java.sql.Types;
import static org.assertj.core.api.Assertions.assertThat;
public class DbPlatformTypeMappingTest {
@Test
public void logicalBoolean_renderType_expect_noLength() {
DbPlatformTypeMapping logicalMapping = DbPlatformTypeMapping.logicalTypes();
DbPlatformType type = logicalMapping.get(Types.BOOLEAN);
String colDefinition = type.renderType(1, 1, false);
assertThat(colDefinition).isEqualTo("boolean");
}
}
@@ -6,7 +6,12 @@ import io.ebean.text.json.JsonContext;
import org.tests.model.basic.TOne;
import org.junit.Test;
import static org.junit.Assert.*;
import java.sql.Types;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class ScalarTypeBooleanTest {
@@ -40,4 +45,13 @@ public class ScalarTypeBooleanTest {
assertEquals(json, "{\"id\":42,\"active\":false}");
}
@Test
public void IntBoolean_isLogicalBoolean() {
ScalarTypeBoolean.IntBoolean intBoolean = new ScalarTypeBoolean.IntBoolean(1, 0);
assertThat(intBoolean).isInstanceOf(ScalarTypeLogicalType.class);
assertThat(intBoolean.getLogicalType()).isEqualTo(Types.BOOLEAN);
}
}