add renameIndex task

This commit is contained in:
Ken Stevens 2019-12-09 15:42:40 -05:00
parent deb5fd40a7
commit b0b410384f
3 changed files with 154 additions and 1 deletions

View File

@ -0,0 +1,142 @@
package ca.uhn.fhir.jpa.migrate.taskdef;
/*-
* #%L
* HAPI FHIR JPA Server - Migration
* %%
* Copyright (C) 2014 - 2019 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.DriverTypeEnum;
import ca.uhn.fhir.jpa.migrate.JdbcUtils;
import org.apache.commons.lang3.Validate;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.intellij.lang.annotations.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Set;
public class RenameIndexTask extends BaseTableTask<RenameIndexTask> {
private static final Logger ourLog = LoggerFactory.getLogger(RenameIndexTask.class);
private String myOldIndexName;
private String myNewIndexName;
public RenameIndexTask(String theProductVersion, String theSchemaVersion) {
super(theProductVersion, theSchemaVersion);
}
@Override
public void validate() {
super.validate();
Validate.notBlank(myOldIndexName, "The old index name must not be blank");
Validate.notBlank(myNewIndexName, "The new index name must not be blank");
setDescription("Rename index from " + myOldIndexName + " to " + myNewIndexName + " on table " + getTableName());
}
@Override
public void doExecute() throws SQLException {
Set<String> indexNames = JdbcUtils.getIndexNames(getConnectionProperties(), getTableName());
if (!indexNames.contains(myOldIndexName)) {
logInfo(ourLog, "Index {} does not exist on table {} - No action needed", myOldIndexName, getTableName());
return;
}
List<String> sqls = createRenameIndexSql(getConnectionProperties(), getTableName(), myOldIndexName, myNewIndexName, getDriverType());
if (!sqls.isEmpty()) {
logInfo(ourLog, "Renaming index from {} to {} on table {}", myOldIndexName, myNewIndexName, getTableName());
}
for (@Language("SQL") String sql : sqls) {
executeSql(getTableName(), sql);
}
}
public RenameIndexTask setNewIndexName(String theNewIndexName) {
myNewIndexName = theNewIndexName;
return this;
}
public RenameIndexTask setOldIndexName(String theOldIndexName) {
myOldIndexName = theOldIndexName;
return this;
}
static List<String> createRenameIndexSql(DriverTypeEnum.ConnectionProperties theConnectionProperties, String theTableName, String theOldIndexName, String theNewIndexName, DriverTypeEnum theDriverType) throws SQLException {
Validate.notBlank(theOldIndexName, "theOldIndexName must not be blank");
Validate.notBlank(theNewIndexName, "theNewIndexName must not be blank");
Validate.notBlank(theTableName, "theTableName must not be blank");
if (!JdbcUtils.getIndexNames(theConnectionProperties, theTableName).contains(theOldIndexName)) {
return Collections.emptyList();
}
boolean isUnique = JdbcUtils.isIndexUnique(theConnectionProperties, theTableName, theOldIndexName);
List<String> sql = new ArrayList<>();
// Drop constraint
switch (theDriverType) {
case MYSQL_5_7:
case MARIADB_10_1:
case DERBY_EMBEDDED:
sql.add("rename index " + theOldIndexName + " to " + theNewIndexName);
break;
case H2_EMBEDDED:
case POSTGRES_9_4:
case ORACLE_12C:
sql.add("alter index " + theOldIndexName + " rename to " + theNewIndexName);
break;
case MSSQL_2012:
// FIXME KHS really?
sql.add("EXEC sp_rename '" + theOldIndexName + "', '" + theNewIndexName + "'; GO");
break;
}
return sql;
}
@Override
public boolean equals(Object theO) {
if (this == theO) return true;
if (theO == null || getClass() != theO.getClass()) return false;
RenameIndexTask that = (RenameIndexTask) theO;
return new EqualsBuilder()
.appendSuper(super.equals(theO))
.append(myOldIndexName, that.myOldIndexName)
.append(myNewIndexName, that.myNewIndexName)
.isEquals();
}
@Override
public int hashCode() {
return new HashCodeBuilder(17, 37)
.appendSuper(super.hashCode())
.append(myOldIndexName)
.append(myNewIndexName)
.toHashCode();
}
}

View File

@ -162,6 +162,14 @@ public class Builder {
addTask(task);
}
public void renameIndex(String theVersion, String theOldIndexName, String theNewIndexName) {
RenameIndexTask task = new RenameIndexTask(myRelease, theVersion);
task.setOldIndexName(theOldIndexName);
task.setNewIndexName(theNewIndexName);
task.setTableName(myTableName);
addTask(task);
}
public void dropThisTable(String theVersion) {
DropTableTask task = new DropTableTask(myRelease, theVersion);
task.setTableName(myTableName);

View File

@ -27,7 +27,7 @@ public class AddTableByColumnTaskTest extends BaseTest {
.filter(s -> !s.startsWith("FK_REF_INDEX_"))
.filter(s -> !s.startsWith("PRIMARY_KEY_"))
.collect(Collectors.toSet());
assertThat(indexes, containsInAnyOrder("IDX_HELLO"));
assertThat(indexes, containsInAnyOrder("IDX_BONJOUR"));
}
private static class MyMigrationTasks extends BaseMigrationTasks<VersionEnum> {
@ -47,6 +47,9 @@ public class AddTableByColumnTaskTest extends BaseTest {
fooTable.addIndex("5", "IDX_GOODBYE").unique(true).withColumnsStub("GOODBYE");
fooTable.dropIndexStub("6", "IDX_HELLO");
fooTable.addForeignKey("7", "FK_REF").toColumn("COL_REF").references("TGT_TABLE", "PID");
Builder.BuilderWithTableName renameIndexTable = v.onTable("FOO_TABLE");
renameIndexTable.renameIndex("8", "IDX_HELLO", "IDX_BONJOUR");
}
}
}