diff --git a/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java b/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java index b809268a..9af92bba 100644 --- a/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java +++ b/src/main/java/liquibase/ext/hibernate/snapshot/TableSnapshotGenerator.java @@ -9,6 +9,7 @@ import liquibase.structure.core.DataType; import liquibase.structure.core.Schema; import liquibase.structure.core.Table; +import liquibase.util.StringUtils; import org.hibernate.cfg.Configuration; import org.hibernate.dialect.Dialect; import org.hibernate.engine.spi.Mapping; @@ -20,7 +21,7 @@ public class TableSnapshotGenerator extends HibernateSnapshotGenerator { - private final static Pattern pattern = Pattern.compile("([^\\(]*)\\s*\\(?\\s*(\\d*)?\\s*,?\\s*(\\d*)?\\s*\\)?"); + private final static Pattern pattern = Pattern.compile("([^\\(]*)\\s*\\(?\\s*(\\d*)?\\s*,?\\s*(\\d*)?\\s*([^\\(]*?)\\)?"); public TableSnapshotGenerator() { super(Table.class, new Class[]{Schema.class}); @@ -52,21 +53,13 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot // TODO autoincrement String hibernateType = hibernateColumn.getSqlType(dialect, mapping); - LOG.info("Found column " + column.getName() + " " + hibernateType); - Matcher matcher = pattern.matcher(hibernateColumn.getSqlType(dialect, mapping)); - if (!matcher.matches()) - throw new DatabaseException("Unable to find column data type for column " + column.getName()); - DataType dataType = new DataType(matcher.group(1)); - if (matcher.group(3).isEmpty()) { - if (!matcher.group(2).isEmpty()) - dataType.setColumnSize(Integer.parseInt(matcher.group(2))); - } else { - dataType.setColumnSize(Integer.parseInt(matcher.group(2))); - dataType.setDecimalDigits(Integer.parseInt(matcher.group(3))); + DataType dataType = toDataType(hibernateType, hibernateColumn.getSqlTypeCode()); + if (dataType == null) { + throw new DatabaseException("Unable to find column data type for column " + hibernateColumn.getName()); } - dataType.setDataTypeId(hibernateColumn.getSqlTypeCode()); column.setType(dataType); + LOG.info("Found column " + column.getName() + " " + column.getType().toString()); column.setRemarks(hibernateColumn.getComment()); column.setDefaultValue(hibernateColumn.getDefaultValue()); @@ -88,6 +81,31 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot return table; } + protected DataType toDataType(String hibernateType, int sqlTypeCode) throws DatabaseException { + Matcher matcher = pattern.matcher(hibernateType); + if (!matcher.matches()) { + return null; + } + DataType dataType = new DataType(matcher.group(1)); + if (matcher.group(3).isEmpty()) { + if (!matcher.group(2).isEmpty()) + dataType.setColumnSize(Integer.parseInt(matcher.group(2))); + } else { + dataType.setColumnSize(Integer.parseInt(matcher.group(2))); + dataType.setDecimalDigits(Integer.parseInt(matcher.group(3))); + } + + String extra = StringUtils.trimToNull(matcher.group(4)); + if (extra != null) { + if (extra.equalsIgnoreCase("char")) { + dataType.setColumnSizeUnit(DataType.ColumnSizeUnit.CHAR); + } + } + + dataType.setDataTypeId(sqlTypeCode); + return dataType; + } + @Override protected void addTo(DatabaseObject foundObject, DatabaseSnapshot snapshot) throws DatabaseException, InvalidExampleException { if (!snapshot.getSnapshotControl().shouldInclude(Table.class)) { diff --git a/src/test/java/liquibase/ext/hibernate/snapshot/TableSnapshotGeneratorTest.java b/src/test/java/liquibase/ext/hibernate/snapshot/TableSnapshotGeneratorTest.java new file mode 100644 index 00000000..eb561d43 --- /dev/null +++ b/src/test/java/liquibase/ext/hibernate/snapshot/TableSnapshotGeneratorTest.java @@ -0,0 +1,33 @@ +package liquibase.ext.hibernate.snapshot; + +import junit.framework.Assert; +import liquibase.exception.DatabaseException; +import liquibase.structure.core.DataType; +import org.junit.Test; + +import java.sql.Types; + +import static junit.framework.Assert.assertEquals; +import static junit.framework.TestCase.assertNull; + +public class TableSnapshotGeneratorTest { + + @Test + public void toDataType() throws DatabaseException { + TableSnapshotGenerator tableSnapshotGenerator = new TableSnapshotGenerator(); + DataType varchar = tableSnapshotGenerator.toDataType("varchar(255)", Types.VARCHAR); + assertEquals("varchar", varchar.getTypeName()); + assertEquals(255, varchar.getColumnSize().intValue()); + assertEquals(Types.VARCHAR, varchar.getDataTypeId().intValue()); + assertNull(varchar.getColumnSizeUnit()); + + DataType intType = tableSnapshotGenerator.toDataType("integer", Types.INTEGER); + assertEquals("integer", intType.getTypeName()); + + DataType varcharChar = tableSnapshotGenerator.toDataType("varchar2(30 char)", Types.INTEGER); + assertEquals("varchar2", varcharChar.getTypeName()); + assertEquals(30, varcharChar.getColumnSize().intValue()); + assertEquals(DataType.ColumnSizeUnit.CHAR, varcharChar.getColumnSizeUnit()); + + } +}