From 945cc6419fa3d2af621e3637a6c874085e8506bf Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Tue, 24 Sep 2019 18:13:53 -0400 Subject: [PATCH] fixed npe --- .../jpa/migrate/taskdef/ModifyColumnTask.java | 2 +- .../jpa/migrate/taskdef/ModifyColumnTest.java | 39 +++++++++++++++++++ 2 files changed, 40 insertions(+), 1 deletion(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ModifyColumnTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ModifyColumnTask.java index 7e222905ce1..f0683fe496d 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ModifyColumnTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/ModifyColumnTask.java @@ -52,7 +52,7 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask throw new InternalErrorException(e); } - if (isNoColumnShrink()) { + if (getColumnLength() != null && isNoColumnShrink()) { long existingLength = existingType.getLength() != null ? existingType.getLength() : 0; if (existingLength > getColumnLength()) { setColumnLength(existingLength); diff --git a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/ModifyColumnTest.java b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/ModifyColumnTest.java index 6a2cb7c63d3..470870226f0 100644 --- a/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/ModifyColumnTest.java +++ b/hapi-fhir-jpaserver-migrate/src/test/java/ca/uhn/fhir/jpa/migrate/taskdef/ModifyColumnTest.java @@ -98,6 +98,45 @@ public class ModifyColumnTest extends BaseTest { } + @Test + public void testNoShrink_ColumnMakeDateNullable() throws SQLException { + executeSql("create table SOMETABLE (PID bigint not null, DATECOL timestamp not null)"); + assertFalse(JdbcUtils.isColumnNullable(getConnectionProperties(), "SOMETABLE", "PID")); + assertFalse(JdbcUtils.isColumnNullable(getConnectionProperties(), "SOMETABLE", "DATECOL")); + assertEquals(new JdbcUtils.ColumnType(BaseTableColumnTypeTask.ColumnTypeEnum.LONG, 19), JdbcUtils.getColumnType(getConnectionProperties(), "SOMETABLE", "PID")); + assertEquals(new JdbcUtils.ColumnType(BaseTableColumnTypeTask.ColumnTypeEnum.DATE_TIMESTAMP, 26), JdbcUtils.getColumnType(getConnectionProperties(), "SOMETABLE", "DATECOL")); + + getMigrator().setNoColumnShrink(true); + + // PID + ModifyColumnTask task = new ModifyColumnTask(); + task.setTableName("SOMETABLE"); + task.setColumnName("PID"); + task.setColumnType(AddColumnTask.ColumnTypeEnum.LONG); + task.setNullable(true); + getMigrator().addTask(task); + + // STRING + task = new ModifyColumnTask(); + task.setTableName("SOMETABLE"); + task.setColumnName("DATECOL"); + task.setColumnType(AddColumnTask.ColumnTypeEnum.DATE_TIMESTAMP); + task.setNullable(true); + getMigrator().addTask(task); + + // Do migration + getMigrator().migrate(); + + assertTrue(JdbcUtils.isColumnNullable(getConnectionProperties(), "SOMETABLE", "PID")); + assertTrue(JdbcUtils.isColumnNullable(getConnectionProperties(), "SOMETABLE", "DATECOL")); + assertEquals(new JdbcUtils.ColumnType(BaseTableColumnTypeTask.ColumnTypeEnum.LONG, 19), JdbcUtils.getColumnType(getConnectionProperties(), "SOMETABLE", "PID")); + assertEquals(new JdbcUtils.ColumnType(BaseTableColumnTypeTask.ColumnTypeEnum.DATE_TIMESTAMP, 26), JdbcUtils.getColumnType(getConnectionProperties(), "SOMETABLE", "DATECOL")); + + // Make sure additional migrations don't crash + getMigrator().migrate(); + getMigrator().migrate(); + } + @Test public void testColumnMakeNotNullable() throws SQLException { executeSql("create table SOMETABLE (PID bigint, TEXTCOL varchar(255))");