From 0dde239696d5c46ea774103c896940cd72f8361c Mon Sep 17 00:00:00 2001 From: Ken Stevens Date: Fri, 13 Dec 2019 15:12:59 -0500 Subject: [PATCH] migration task don't compare length if no length is specified in the task --- .../ca/uhn/fhir/jpa/migrate/JdbcUtils.java | 7 ++++--- .../jpa/migrate/taskdef/ModifyColumnTask.java | 12 ++++++------ .../jpa/migrate/taskdef/ModifyColumnTest.java | 19 +++++++++++++++++-- 3 files changed, 27 insertions(+), 11 deletions(-) diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java index 0a0ea8154c0..6398937cbc7 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/JdbcUtils.java @@ -113,10 +113,10 @@ public class JdbcUtils { return myLength; } - public boolean equals(BaseTableColumnTypeTask.ColumnTypeEnum theColumnType, Long theColumnLength) { - return myColumnTypeEnum == theColumnType && (myLength == null || myLength.equals(theColumnLength)); + public boolean equals(BaseTableColumnTypeTask.ColumnTypeEnum theTaskColumnType, Long theTaskColumnLength) { + ourLog.debug("Comparing existing {} {} to new {} {}", myColumnTypeEnum, myLength, theTaskColumnType, theTaskColumnLength); + return myColumnTypeEnum == theTaskColumnType && (theTaskColumnLength == null || theTaskColumnLength.equals(myLength)); } - } /** @@ -244,6 +244,7 @@ public class JdbcUtils { } + ourLog.debug("Unable to find column {} in table {}.", theColumnName, theTableName); return null; } catch (SQLException e) { 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 4d3c26dbafe..fb24e1c492a 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 @@ -61,22 +61,22 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask throw new InternalErrorException(e); } - Long columnLength = getColumnLength(); - if (columnLength != null && isNoColumnShrink()) { + Long taskColumnLength = getColumnLength(); + if (taskColumnLength != null && isNoColumnShrink()) { long existingLength = existingType.getLength() != null ? existingType.getLength() : 0; - if (existingLength > columnLength) { - columnLength = existingLength; + if (existingLength > taskColumnLength) { + taskColumnLength = existingLength; } } - boolean alreadyOfCorrectType = existingType.equals(getColumnType(), columnLength); + boolean alreadyOfCorrectType = existingType.equals(getColumnType(), taskColumnLength); boolean alreadyCorrectNullable = isNullable() == nullable; if (alreadyOfCorrectType && alreadyCorrectNullable) { logInfo(ourLog, "Column {} on table {} is already of type {} and has nullable {} - No action performed", getColumnName(), getTableName(), existingType, nullable); return; } - String type = getSqlType(columnLength); + String type = getSqlType(taskColumnLength); String notNull = getSqlNotNull(); String sql = null; 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 559af8ec09a..d34d4bb942e 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 @@ -3,7 +3,6 @@ package ca.uhn.fhir.jpa.migrate.taskdef; import ca.uhn.fhir.jpa.migrate.JdbcUtils; import org.flywaydb.core.internal.command.DbMigrate; import org.junit.Test; -import org.springframework.dao.DataIntegrityViolationException; import java.sql.SQLException; @@ -234,7 +233,7 @@ public class ModifyColumnTest extends BaseTest { } @Test - public void testFailureNotAllowed() throws SQLException { + public void testFailureNotAllowed() { executeSql("create table SOMETABLE (PID bigint, TEXTCOL varchar(255))"); executeSql("insert into SOMETABLE (TEXTCOL) values ('HELLO')"); @@ -254,4 +253,20 @@ public class ModifyColumnTest extends BaseTest { } + @Test + public void dontCompareLengthIfNoneSpecifiedInTask() throws SQLException { + executeSql("create table SOMETABLE (PID bigint, TEXTCOL varchar(255))"); + + ModifyColumnTask task = new ModifyColumnTask("1", "1"); + task.setTableName("SOMETABLE"); + task.setColumnName("PID"); + task.setColumnType(BaseTableColumnTypeTask.ColumnTypeEnum.LONG); + task.setNullable(true); + + JdbcUtils.ColumnType existingColumnType = JdbcUtils.getColumnType(getConnectionProperties(), "SOMETABLE", "PID"); + assertEquals(BaseTableColumnTypeTask.ColumnTypeEnum.LONG, existingColumnType.getColumnTypeEnum()); + assertEquals(19L, existingColumnType.getLength().longValue()); + assertTrue(existingColumnType.equals(task.getColumnType(), task.getColumnLength())); + } + }