Migrator enhancements
This commit is contained in:
parent
5d60b90b81
commit
7e4d6afc5f
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
|
@ -44,14 +44,14 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
|
||||
List<BaseTask<?>> retVal = new ArrayList<>();
|
||||
for (Object nextVersion : EnumUtils.getEnumList(theFrom.getClass())) {
|
||||
if (((T)nextVersion).ordinal() <= theFrom.ordinal()) {
|
||||
if (((T) nextVersion).ordinal() <= theFrom.ordinal()) {
|
||||
continue;
|
||||
}
|
||||
if (((T)nextVersion).ordinal() > theTo.ordinal()) {
|
||||
if (((T) nextVersion).ordinal() > theTo.ordinal()) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Collection<BaseTask<?>> nextValues = myTasks.get((T)nextVersion);
|
||||
Collection<BaseTask<?>> nextValues = myTasks.get((T) nextVersion);
|
||||
if (nextValues != null) {
|
||||
retVal.addAll(nextValues);
|
||||
}
|
||||
|
@ -120,6 +120,14 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
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) {
|
||||
theTask.setTableName(myTableName);
|
||||
Builder.this.addTask(theTask);
|
||||
|
@ -165,10 +173,17 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
|
||||
public class BuilderAddColumnWithNameNullable {
|
||||
public void type(AddColumnTask.ColumnTypeEnum theColumnType) {
|
||||
type(theColumnType, null);
|
||||
}
|
||||
|
||||
public void type(AddColumnTask.ColumnTypeEnum theColumnType, Integer theLength) {
|
||||
AddColumnTask task = new AddColumnTask();
|
||||
task.setColumnName(myColumnName);
|
||||
task.setNullable(myNullable);
|
||||
task.setColumnType(theColumnType);
|
||||
if (theLength != null) {
|
||||
task.setColumnLength(theLength);
|
||||
}
|
||||
addTask(task);
|
||||
}
|
||||
}
|
||||
|
@ -206,7 +221,7 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
task.setNullable(myNullable);
|
||||
task.setColumnType(theColumnType);
|
||||
addTask(task);
|
||||
} else if (theLength > 0){
|
||||
} else if (theLength > 0) {
|
||||
throw new IllegalArgumentException("Can not specify length for column of type " + theColumnType);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue