#1011 - Create valid migration scripts for non-Postgres databases and @DbArray columns.

Merge branch 'test/dbarray-create-migration' of https://github.com/FOCONIS/ebean into FOCONIS-test/dbarray-create-migration

commit 3a8f32b173
Author: Michael Benz <michael.benz@foconis.de>
Date:   Wed Apr 19 18:16:51 2017 +0200

    adding dbmigration ddlgeneration test for Ebean @DbArray extension.
This commit is contained in:
rob bygrave
2017-05-20 23:40:02 +12:00
parent 79f7c73983
commit fe9500e6d0
11 changed files with 184 additions and 24 deletions
@@ -12,4 +12,8 @@ public class H2Ddl extends PlatformDdl {
this.historyDdl = new H2HistoryDdl();
}
@Override
protected String convertArrayType(String logicalArrayType) {
return "array";
}
}
@@ -99,6 +99,8 @@ public class PlatformDdl {
protected final DbDefaultValue dbDefaultValue;
protected String fallbackArrayType = "varchar(1000)";
public PlatformDdl(DatabasePlatform platform) {
this.platform = platform;
this.dbIdentity = platform.getDbIdentity();
@@ -215,10 +217,24 @@ public class PlatformDdl {
* Convert the standard type to the platform specific type.
*/
public String convert(String type, boolean identity) {
if (type.contains("[]")) {
return convertArrayType(type);
}
String platformType = typeConverter.convert(type);
return identity ? asIdentityColumn(platformType) : platformType;
}
/**
* Convert the logical array type to a db platform specific type to support the array data.
*/
protected String convertArrayType(String logicalArrayType) {
if (logicalArrayType.endsWith("]")) {
return fallbackArrayType;
}
int colonPos = logicalArrayType.lastIndexOf(']');
return "varchar" + logicalArrayType.substring(colonPos + 1);
}
/**
* Add history support to this table using the platform specific mechanism.
*/
@@ -16,6 +16,17 @@ public class PostgresDdl extends PlatformDdl {
this.columnSetNull = "drop not null";
}
@Override
protected String convertArrayType(String logicalArrayType) {
int colonPos = logicalArrayType.lastIndexOf(']');
if (colonPos == -1) {
return logicalArrayType;
} else {
// trim of the fallback varchar length
return logicalArrayType.substring(0, colonPos + 1);
}
}
/**
* Map bigint, integer and smallint into their equivalent serial types.
*/
@@ -245,13 +245,15 @@ public class DeployUtil {
prop.setDbType(dbType);
prop.setScalarType(scalarType);
if (scalarType instanceof ScalarTypeArray) {
prop.setDbColumnDefn(((ScalarTypeArray) scalarType).getDbColumnDefn());
}
if (dbType == Types.VARCHAR) {
// determine the db column size
int dbLength = dbArray.length();
int columnLength = (dbLength > 0) ? dbLength : DEFAULT_ARRAY_VARCHAR_LENGTH;
prop.setDbLength(columnLength);
String columnDefn = ((ScalarTypeArray) scalarType).getDbColumnDefn();
if (dbArray.length() > 0) {
// fallback varchar column length when ARRAY not support by DB
columnDefn += "(" + dbArray.length() + ")";
}
prop.setDbLength(dbArray.length());
prop.setDbColumnDefn(columnDefn);
} else {
throw new RuntimeException("Not mapped to ScalarTypeArray? " + scalarType.getClass());
}
}
@@ -52,11 +52,6 @@ class ScalarTypeArrayListH2 extends ScalarTypeArrayList {
super(arrayType, docPropertyType, converter);
}
@Override
public String getDbColumnDefn() {
return "array";
}
@Override
public void bind(DataBind bind, List value) throws SQLException {
if (value == null) {
@@ -52,11 +52,6 @@ class ScalarTypeArraySetH2 extends ScalarTypeArraySet {
super(arrayType, docPropertyType, converter);
}
@Override
public String getDbColumnDefn() {
return "array";
}
@Override
public void bind(DataBind bind, Set value) throws SQLException {
if (value == null) {
@@ -6,7 +6,7 @@ import java.io.DataInput;
import java.io.DataOutput;
import java.io.IOException;
abstract class ScalarTypeJsonCollection<T> extends ScalarTypeBase<T> {
abstract class ScalarTypeJsonCollection<T> extends ScalarTypeBase<T> implements ScalarTypeArray {
protected DocPropertyType docPropertyType;
@@ -15,6 +15,23 @@ abstract class ScalarTypeJsonCollection<T> extends ScalarTypeBase<T> {
this.docPropertyType = docPropertyType;
}
/**
* Return the logical db column definition based on the element document type.
*/
@Override
public String getDbColumnDefn() {
switch (docPropertyType) {
case SHORT:
case INTEGER:
case LONG:
return "integer[]";
case DOUBLE:
case FLOAT:
return "decimal[]";
}
return "varchar[]";
}
/**
* Consider as a mutable type. Use the isDirty() method to check for dirty state.
*/