Migrator enhancements

This commit is contained in:
James Agnew 2018-09-17 09:24:04 -04:00
parent 5d60b90b81
commit 7e4d6afc5f
3 changed files with 101 additions and 6 deletions

View File

@ -0,0 +1,48 @@
package ca.uhn.fhir.jpa.migrate.taskdef;
/*-
* #%L
* HAPI FHIR JPA Server - Migration
* %%
* Copyright (C) 2014 - 2018 University Health Network
* %%
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* #L%
*/
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.Set;
public class DropColumnTask extends BaseTableColumnTypeTask<DropColumnTask> {
private static final Logger ourLog = LoggerFactory.getLogger(DropColumnTask.class);
@Override
public void execute() throws SQLException {
Set<String> columnNames = JdbcUtils.getColumnNames(getConnectionProperties(), getTableName());
if (!columnNames.contains(getColumnName())) {
ourLog.info("Column {} does not exist on table {} - No action performed", getColumnName(), getTableName());
return;
}
String sql = "alter table " + getTableName() + " drop column " + getColumnName();
ourLog.info("Dropping column {} on table {}", getColumnName(), getTableName());
executeSql(sql);
}
}

View File

@ -44,14 +44,14 @@ public class BaseMigrationTasks<T extends Enum> {
List<BaseTask<?>> retVal = new ArrayList<>(); List<BaseTask<?>> retVal = new ArrayList<>();
for (Object nextVersion : EnumUtils.getEnumList(theFrom.getClass())) { for (Object nextVersion : EnumUtils.getEnumList(theFrom.getClass())) {
if (((T)nextVersion).ordinal() <= theFrom.ordinal()) { if (((T) nextVersion).ordinal() <= theFrom.ordinal()) {
continue; continue;
} }
if (((T)nextVersion).ordinal() > theTo.ordinal()) { if (((T) nextVersion).ordinal() > theTo.ordinal()) {
continue; continue;
} }
Collection<BaseTask<?>> nextValues = myTasks.get((T)nextVersion); Collection<BaseTask<?>> nextValues = myTasks.get((T) nextVersion);
if (nextValues != null) { if (nextValues != null) {
retVal.addAll(nextValues); retVal.addAll(nextValues);
} }
@ -120,6 +120,14 @@ public class BaseMigrationTasks<T extends Enum> {
return new BuilderAddColumnWithName(); return new BuilderAddColumnWithName();
} }
public void dropColumn(String theColumnName) {
Validate.notBlank(theColumnName);
DropColumnTask task = new DropColumnTask();
task.setTableName(myTableName);
task.setColumnName(theColumnName);
addTask(task);
}
public void addTask(BaseTableTask<?> theTask) { public void addTask(BaseTableTask<?> theTask) {
theTask.setTableName(myTableName); theTask.setTableName(myTableName);
Builder.this.addTask(theTask); Builder.this.addTask(theTask);
@ -165,10 +173,17 @@ public class BaseMigrationTasks<T extends Enum> {
public class BuilderAddColumnWithNameNullable { public class BuilderAddColumnWithNameNullable {
public void type(AddColumnTask.ColumnTypeEnum theColumnType) { public void type(AddColumnTask.ColumnTypeEnum theColumnType) {
type(theColumnType, null);
}
public void type(AddColumnTask.ColumnTypeEnum theColumnType, Integer theLength) {
AddColumnTask task = new AddColumnTask(); AddColumnTask task = new AddColumnTask();
task.setColumnName(myColumnName); task.setColumnName(myColumnName);
task.setNullable(myNullable); task.setNullable(myNullable);
task.setColumnType(theColumnType); task.setColumnType(theColumnType);
if (theLength != null) {
task.setColumnLength(theLength);
}
addTask(task); addTask(task);
} }
} }
@ -206,7 +221,7 @@ public class BaseMigrationTasks<T extends Enum> {
task.setNullable(myNullable); task.setNullable(myNullable);
task.setColumnType(theColumnType); task.setColumnType(theColumnType);
addTask(task); addTask(task);
} else if (theLength > 0){ } else if (theLength > 0) {
throw new IllegalArgumentException("Can not specify length for column of type " + theColumnType); throw new IllegalArgumentException("Can not specify length for column of type " + theColumnType);
} }
} }

View File

@ -0,0 +1,32 @@
package ca.uhn.fhir.jpa.migrate.taskdef;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.junit.Test;
import java.sql.SQLException;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
public class DropColumnTest extends BaseTest {
@Test
public void testDropColumn() throws SQLException {
executeSql("create table SOMETABLE (PID bigint not null, TEXTCOL varchar(255))");
DropColumnTask task = new DropColumnTask();
task.setTableName("SOMETABLE");
task.setColumnName("TEXTCOL");
getMigrator().addTask(task);
getMigrator().migrate();
assertThat(JdbcUtils.getColumnNames(getConnectionProperties(), "SOMETABLE"), containsInAnyOrder("PID"));
// Do it again to make sure there is no error
getMigrator().migrate();
getMigrator().migrate();
}
}