migration task don't compare length if no length is specified in the task

This commit is contained in:
Ken Stevens 2019-12-13 15:12:59 -05:00
parent d9d47bb419
commit 0dde239696
3 changed files with 27 additions and 11 deletions

View File

@ -113,10 +113,10 @@ public class JdbcUtils {
return myLength; return myLength;
} }
public boolean equals(BaseTableColumnTypeTask.ColumnTypeEnum theColumnType, Long theColumnLength) { public boolean equals(BaseTableColumnTypeTask.ColumnTypeEnum theTaskColumnType, Long theTaskColumnLength) {
return myColumnTypeEnum == theColumnType && (myLength == null || myLength.equals(theColumnLength)); 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; return null;
} catch (SQLException e) { } catch (SQLException e) {

View File

@ -61,22 +61,22 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask<ModifyColumnTask>
throw new InternalErrorException(e); throw new InternalErrorException(e);
} }
Long columnLength = getColumnLength(); Long taskColumnLength = getColumnLength();
if (columnLength != null && isNoColumnShrink()) { if (taskColumnLength != null && isNoColumnShrink()) {
long existingLength = existingType.getLength() != null ? existingType.getLength() : 0; long existingLength = existingType.getLength() != null ? existingType.getLength() : 0;
if (existingLength > columnLength) { if (existingLength > taskColumnLength) {
columnLength = existingLength; taskColumnLength = existingLength;
} }
} }
boolean alreadyOfCorrectType = existingType.equals(getColumnType(), columnLength); boolean alreadyOfCorrectType = existingType.equals(getColumnType(), taskColumnLength);
boolean alreadyCorrectNullable = isNullable() == nullable; boolean alreadyCorrectNullable = isNullable() == nullable;
if (alreadyOfCorrectType && alreadyCorrectNullable) { if (alreadyOfCorrectType && alreadyCorrectNullable) {
logInfo(ourLog, "Column {} on table {} is already of type {} and has nullable {} - No action performed", getColumnName(), getTableName(), existingType, nullable); logInfo(ourLog, "Column {} on table {} is already of type {} and has nullable {} - No action performed", getColumnName(), getTableName(), existingType, nullable);
return; return;
} }
String type = getSqlType(columnLength); String type = getSqlType(taskColumnLength);
String notNull = getSqlNotNull(); String notNull = getSqlNotNull();
String sql = null; String sql = null;

View File

@ -3,7 +3,6 @@ package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.JdbcUtils; import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.flywaydb.core.internal.command.DbMigrate; import org.flywaydb.core.internal.command.DbMigrate;
import org.junit.Test; import org.junit.Test;
import org.springframework.dao.DataIntegrityViolationException;
import java.sql.SQLException; import java.sql.SQLException;
@ -234,7 +233,7 @@ public class ModifyColumnTest extends BaseTest {
} }
@Test @Test
public void testFailureNotAllowed() throws SQLException { public void testFailureNotAllowed() {
executeSql("create table SOMETABLE (PID bigint, TEXTCOL varchar(255))"); executeSql("create table SOMETABLE (PID bigint, TEXTCOL varchar(255))");
executeSql("insert into SOMETABLE (TEXTCOL) values ('HELLO')"); 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()));
}
} }