Skip ModifyColumn migration tasks for columns that don't exist

This commit is contained in:
Ruslan_Zinovyev 2019-08-12 14:06:34 -04:00 committed by James Agnew
parent c04f1d15d8
commit a839a645db
2 changed files with 26 additions and 2 deletions

View File

@ -26,6 +26,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Set;
public class ModifyColumnTask extends BaseTableColumnTypeTask<ModifyColumnTask> {
@ -33,10 +34,17 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask<ModifyColumnTask>
@Override
public void execute() {
public void execute() throws SQLException {
String existingType;
boolean nullable;
Set<String> columnNames = JdbcUtils.getColumnNames(getConnectionProperties(), getTableName());
if (!columnNames.contains(getColumnName())) {
ourLog.info("Column {} doesn't exist on table {} - No action performed", getColumnName(), getTableName());
return;
}
try {
existingType = JdbcUtils.getColumnType(getConnectionProperties(), getTableName(), getColumnName());
nullable = JdbcUtils.isColumnNullable(getConnectionProperties(), getTableName(), getColumnName());
@ -115,5 +123,4 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask<ModifyColumnTask>
executeSql(getTableName(), sqlNotNull);
}
}
}

View File

@ -5,6 +5,7 @@ import org.junit.Test;
import java.sql.SQLException;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.*;
public class ModifyColumnTest extends BaseTest {
@ -111,4 +112,20 @@ public class ModifyColumnTest extends BaseTest {
}
@Test
public void testColumnDoesntAlreadyExist() throws SQLException {
executeSql("create table SOMETABLE (PID bigint, TEXTCOL varchar(255))");
ModifyColumnTask task = new ModifyColumnTask();
task.setTableName("SOMETABLE");
task.setColumnName("SOMECOLUMN");
task.setDescription("Make nullable");
task.setNullable(true);
getMigrator().addTask(task);
getMigrator().migrate();
assertThat(JdbcUtils.getColumnNames(getConnectionProperties(), "SOMETABLE"), containsInAnyOrder("PID", "TEXTCOL"));
}
}