diff --git a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java index 6e66298a827..2b8daf16972 100644 --- a/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java +++ b/hapi-fhir-jpaserver-migrate/src/main/java/ca/uhn/fhir/jpa/migrate/taskdef/BaseTask.java @@ -173,6 +173,11 @@ public abstract class BaseTask { myFailureAllowed = theFailureAllowed; } + protected boolean isFailureAllowed() { + return myFailureAllowed; + } + + public String getFlywayVersion() { String releasePart = myProductVersion; if (releasePart.startsWith("V")) { 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 2b6033a0be9..e21d008bccf 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 @@ -62,10 +62,17 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask { } Long taskColumnLength = getColumnLength(); - if (taskColumnLength != null && isNoColumnShrink()) { + boolean isShrinkOnly = false; + if (taskColumnLength != null) { long existingLength = existingType.getLength() != null ? existingType.getLength() : 0; if (existingLength > taskColumnLength) { - taskColumnLength = existingLength; + if (isNoColumnShrink()) { + taskColumnLength = existingLength; + } else { + if (existingType.getColumnTypeEnum() == getColumnType()) { + isShrinkOnly = true; + } + } } } @@ -129,6 +136,10 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask { throw new IllegalStateException("Dont know how to handle " + getDriverType()); } + if (!isFailureAllowed() && isShrinkOnly) { + setFailureAllowed(true); + } + logInfo(ourLog, "Updating column {} on table {} to type {}", getColumnName(), getTableName(), type); if (sql != null) { executeSql(getTableName(), sql); @@ -139,4 +150,5 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask { executeSql(getTableName(), sqlNotNull); } } + } 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 d34d4bb942e..a7763ee927d 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 @@ -269,4 +269,29 @@ public class ModifyColumnTest extends BaseTest { assertTrue(existingColumnType.equals(task.getColumnType(), task.getColumnLength())); } + + @Test + public void testShrinkDoesntFailIfShrinkCannotProceed() throws SQLException { + executeSql("create table SOMETABLE (PID bigint not null, TEXTCOL varchar(10))"); + executeSql("insert into SOMETABLE (PID, TEXTCOL) values (1, '0123456789')"); + + ModifyColumnTask task = new ModifyColumnTask("1", "123456.7"); + task.setTableName("SOMETABLE"); + task.setColumnName("TEXTCOL"); + task.setColumnType(AddColumnTask.ColumnTypeEnum.STRING); + task.setNullable(true); + task.setColumnLength(5); + + getMigrator().addTask(task); + getMigrator().migrate(); + + assertEquals(1, task.getExecutedStatements().size()); + assertEquals(new JdbcUtils.ColumnType(BaseTableColumnTypeTask.ColumnTypeEnum.STRING, 10), JdbcUtils.getColumnType(getConnectionProperties(), "SOMETABLE", "TEXTCOL")); + + // Make sure additional migrations don't crash + getMigrator().migrate(); + getMigrator().migrate(); + + } + }