From a22574b0c925b40d79770c302ff15d03492b39aa Mon Sep 17 00:00:00 2001 From: Robin Bygrave Date: Wed, 17 Feb 2016 20:34:13 +1300 Subject: [PATCH] #552 - wrong DDL generation with complex columnDefinition --- .../platform/util/PlatformTypeConverter.java | 19 ++++++++++--------- .../platform/PlatformTypeConverterTest.java | 16 ++++++++++++++++ .../com/avaje/tests/model/zero/WithZero.java | 16 ++++++++++++++-- 3 files changed, 40 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/PlatformTypeConverter.java b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/PlatformTypeConverter.java index 514a5607c..d41ba3611 100644 --- a/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/PlatformTypeConverter.java +++ b/src/main/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/util/PlatformTypeConverter.java @@ -5,7 +5,7 @@ import com.avaje.ebean.config.dbplatform.DbTypeMap; /** * Converts a logical column definition into platform specific one. - * + *

* This translates standard sql types into platform specific ones. */ public class PlatformTypeConverter { @@ -44,20 +44,21 @@ public class PlatformTypeConverter { return columnDefinition; } - String type = columnDefinition.substring(0,open); + String suffix = close + 1 < columnDefinition.length() ? columnDefinition.substring(close + 1) : ""; + String type = columnDefinition.substring(0, open); try { DbType dbType = platformTypes.lookup(type); - int comma = columnDefinition.indexOf(',',open); + int comma = columnDefinition.indexOf(',', open); if (comma > -1) { // scale and precision - decimal(10,4) - int scale = Integer.parseInt(columnDefinition.substring(open+1, comma)); - int precision = Integer.parseInt(columnDefinition.substring(comma+1, close)); - return dbType.renderType(scale,precision); + int scale = Integer.parseInt(columnDefinition.substring(open + 1, comma)); + int precision = Integer.parseInt(columnDefinition.substring(comma + 1, close)); + return dbType.renderType(scale, precision) + suffix; } else { // scale - varchar(10) - int scale = Integer.parseInt(columnDefinition.substring(open+1, close)); - return dbType.renderType(scale,0); + int scale = Integer.parseInt(columnDefinition.substring(open + 1, close)); + return dbType.renderType(scale, 0) + suffix; } } catch (IllegalArgumentException e) { @@ -73,7 +74,7 @@ public class PlatformTypeConverter { try { DbType dbType = platformTypes.lookup(columnDefinition); - return dbType.renderType(0,0); + return dbType.renderType(0, 0); } catch (IllegalArgumentException e) { // assume already platform specific, leave as is diff --git a/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformTypeConverterTest.java b/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformTypeConverterTest.java index 619d88aff..423a281b7 100644 --- a/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformTypeConverterTest.java +++ b/src/test/java/com/avaje/ebean/dbmigration/ddlgeneration/platform/PlatformTypeConverterTest.java @@ -10,6 +10,22 @@ import static org.assertj.core.api.Assertions.assertThat; public class PlatformTypeConverterTest { + @Test + public void convert_withSuffix_expect_suffix() { + + PostgresPlatform pg = new PostgresPlatform(); + DbTypeMap dbTypeMap = pg.getDbTypeMap(); + + PlatformTypeConverter converter = new PlatformTypeConverter(dbTypeMap); + + assertThat(converter.convert("varchar(10)")).isEqualTo("varchar(10)"); + assertThat(converter.convert("VARCHAR(10) default 'en' not null")).isEqualTo("varchar(10) default 'en' not null"); + assertThat(converter.convert("DECIMAL(10,2) DEFAULT '0.00' NOT NULL")).isEqualTo("decimal(10,2) DEFAULT '0.00' NOT NULL"); + assertThat(converter.convert("CRAZY(12,0) suffix")).isEqualTo("CRAZY(12,0) suffix"); + assertThat(converter.convert("CRAZY(12) suffix")).isEqualTo("CRAZY(12) suffix"); + assertThat(converter.convert("CRAZY suffix")).isEqualTo("CRAZY suffix"); + } + @Test public void testConvert_given_postgres() throws Exception { diff --git a/src/test/java/com/avaje/tests/model/zero/WithZero.java b/src/test/java/com/avaje/tests/model/zero/WithZero.java index d5a7dc737..9c93f31e2 100644 --- a/src/test/java/com/avaje/tests/model/zero/WithZero.java +++ b/src/test/java/com/avaje/tests/model/zero/WithZero.java @@ -1,5 +1,6 @@ package com.avaje.tests.model.zero; +import javax.persistence.Column; import javax.persistence.Entity; import javax.persistence.Id; import javax.persistence.ManyToOne; @@ -11,10 +12,13 @@ public class WithZero { @Id long id = 0; - String name = null; + String name; @ManyToOne - WithZeroParent parent = null; + WithZeroParent parent; + + @Column(columnDefinition = "varchar(2) default 'en' not null") + String lang = "en"; @Version long version = 0; @@ -35,6 +39,14 @@ public class WithZero { this.name = name; } + public String getLang() { + return lang; + } + + public void setLang(String lang) { + this.lang = lang; + } + public WithZeroParent getParent() { return parent; }