Added stubbed tasks for add and drop index

This commit is contained in:
Ken Stevens 2019-12-09 14:42:37 -05:00
parent 5117b93add
commit deb5fd40a7
20 changed files with 69 additions and 23 deletions

View File

@ -42,7 +42,7 @@ public class AddColumnTask extends BaseTableColumnTypeTask<AddColumnTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> columnNames = JdbcUtils.getColumnNames(getConnectionProperties(), getTableName());
if (columnNames.contains(getColumnName())) {
logInfo(ourLog, "Column {} already exists on table {} - No action performed", getColumnName(), getTableName());

View File

@ -66,7 +66,7 @@ public class AddForeignKeyTask extends BaseTableColumnTask<AddForeignKeyTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> existing = JdbcUtils.getForeignKeys(getConnectionProperties(), myForeignTableName, getTableName());
if (existing.contains(myConstraintName)) {

View File

@ -50,7 +50,7 @@ public class AddIdGeneratorTask extends BaseTask<AddIdGeneratorTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
String sql = null;

View File

@ -67,7 +67,7 @@ public class AddIndexTask extends BaseTableTask<AddIndexTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> indexNames = JdbcUtils.getIndexNames(getConnectionProperties(), getTableName());
if (indexNames.contains(myIndexName)) {
logInfo(ourLog, "Index {} already exists on table {} - No action performed", myIndexName, getTableName());

View File

@ -58,7 +58,7 @@ public class AddTableByColumnTask extends BaseTableTask<AddTableByColumnTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
if (JdbcUtils.getTableNames(getConnectionProperties()).contains(getTableName())) {
logInfo(ourLog, "Already have table named {} - No action performed", getTableName());

View File

@ -58,7 +58,7 @@ public class AddTableRawSqlTask extends BaseTableTask<AddTableRawSqlTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
if (tableNames.contains(getTableName())) {
logInfo(ourLog, "Table {} already exists - No action performed", getTableName());

View File

@ -63,7 +63,7 @@ public class ArbitrarySqlTask extends BaseTask<ArbitrarySqlTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
logInfo(ourLog, "Starting: {}", myDescription);
if (StringUtils.isNotBlank(myExecuteOnlyIfTableExists)) {

View File

@ -25,7 +25,6 @@ import org.intellij.lang.annotations.Language;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.dao.DataAccessException;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.transaction.support.TransactionTemplate;
@ -47,6 +46,7 @@ public abstract class BaseTask<T extends BaseTask> {
private String myDescription;
private int myChangesCount;
private boolean myDryRun;
private boolean myDoNothing;
private List<ExecutedStatement> myExecutedStatements = new ArrayList<>();
private boolean myNoColumnShrink;
private boolean myFailureAllowed;
@ -155,7 +155,15 @@ public abstract class BaseTask<T extends BaseTask> {
return getConnectionProperties().newJdbcTemplate();
}
public abstract void execute() throws SQLException;
public void execute() throws SQLException {
if (myDoNothing) {
ourLog.info("Skipping stubbed task: {}", getDescription());
return;
}
doExecute();
}
public abstract void doExecute() throws SQLException;
public void setFailureAllowed(boolean theFailureAllowed) {
myFailureAllowed = theFailureAllowed;
@ -180,6 +188,15 @@ public abstract class BaseTask<T extends BaseTask> {
}
}
public boolean isDoNothing() {
return myDoNothing;
}
public BaseTask<T> setDoNothing(boolean theDoNothing) {
myDoNothing = theDoNothing;
return this;
}
public static class ExecutedStatement {
private final String mySql;
private final List<Object> myArguments;

View File

@ -58,7 +58,7 @@ public class CalculateHashesTask extends BaseTableColumnTask<CalculateHashesTask
}
@Override
public synchronized void execute() throws SQLException {
public synchronized void doExecute() throws SQLException {
if (isDryRun()) {
return;
}

View File

@ -43,7 +43,7 @@ public class DropColumnTask extends BaseTableColumnTask<DropColumnTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> columnNames = JdbcUtils.getColumnNames(getConnectionProperties(), getTableName());
if (!columnNames.contains(getColumnName())) {
logInfo(ourLog, "Column {} does not exist on table {} - No action performed", getColumnName(), getTableName());

View File

@ -65,7 +65,7 @@ public class DropForeignKeyTask extends BaseTableTask<DropForeignKeyTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> existing = JdbcUtils.getForeignKeys(getConnectionProperties(), myParentTableName, getTableName());
if (!existing.contains(myConstraintName)) {

View File

@ -50,7 +50,7 @@ public class DropIdGeneratorTask extends BaseTask<DropIdGeneratorTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
String sql = null;

View File

@ -53,7 +53,7 @@ public class DropIndexTask extends BaseTableTask<DropIndexTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> indexNames = JdbcUtils.getIndexNames(getConnectionProperties(), getTableName());
if (!indexNames.contains(myIndexName)) {

View File

@ -44,7 +44,7 @@ public class DropTableTask extends BaseTableTask<DropTableTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
if (!tableNames.contains(getTableName())) {
return;

View File

@ -67,7 +67,7 @@ public class ExecuteRawSqlTask extends BaseTask<ExecuteRawSqlTask> {
}
@Override
public void execute() {
public void doExecute() {
List<String> sqlStatements = myDriverToSqls.computeIfAbsent(getDriverType(), t -> new ArrayList<>());
sqlStatements.addAll(myDriverNeutralSqls);

View File

@ -48,7 +48,7 @@ public class InitializeSchemaTask extends BaseTask<InitializeSchemaTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
DriverTypeEnum driverType = getDriverType();
Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());

View File

@ -43,7 +43,7 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask<ModifyColumnTask>
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
JdbcUtils.ColumnType existingType;
boolean nullable;

View File

@ -65,7 +65,7 @@ public class RenameColumnTask extends BaseTableTask<RenameColumnTask> {
}
@Override
public void execute() throws SQLException {
public void doExecute() throws SQLException {
Set<String> columnNames = JdbcUtils.getColumnNames(getConnectionProperties(), getTableName());
boolean haveOldName = columnNames.contains(myOldName.toUpperCase());
boolean haveNewName = columnNames.contains(myNewName.toUpperCase());

View File

@ -147,9 +147,18 @@ public class Builder {
}
public void dropIndex(String theVersion, String theIndexName) {
dropIndexOptional(false, theVersion, theIndexName);
}
public void dropIndexStub(String theVersion, String theIndexName) {
dropIndexOptional(true, theVersion, theIndexName);
}
private void dropIndexOptional(boolean theDoNothing, String theVersion, String theIndexName) {
DropIndexTask task = new DropIndexTask(myRelease, theVersion);
task.setIndexName(theIndexName);
task.setTableName(myTableName);
task.setDoNothing(theDoNothing);
addTask(task);
}
@ -244,12 +253,21 @@ public class Builder {
myUnique = theUnique;
}
public void withColumnsStub(String... theColumnNames) {
withColumnsOptional(true, theColumnNames);
}
public void withColumns(String... theColumnNames) {
withColumnsOptional(false, theColumnNames);
}
private void withColumnsOptional(boolean theDoNothing, String... theColumnNames) {
AddIndexTask task = new AddIndexTask(myRelease, myVersion);
task.setTableName(myTableName);
task.setIndexName(myIndexName);
task.setUnique(myUnique);
task.setColumns(theColumnNames);
task.setDoNothing(theDoNothing);
addTask(task);
}
}
@ -355,6 +373,7 @@ public class Builder {
private final String myVersion;
private final String myColumnName;
private final BaseMigrationTasks.IAcceptsTasks myTaskSink;
private boolean myDoNothing;
public BuilderAddColumnWithName(String theRelease, String theVersion, String theColumnName, BaseMigrationTasks.IAcceptsTasks theTaskSink) {
myRelease = theRelease;
@ -396,6 +415,7 @@ public class Builder {
}
myTaskSink.addTask(task);
}
}
}
}

View File

@ -7,12 +7,13 @@ import ca.uhn.fhir.util.VersionEnum;
import org.junit.Test;
import java.sql.SQLException;
import java.util.Set;
import java.util.stream.Collectors;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.junit.Assert.assertThat;
public class AddTableByColumnTaskTest extends BaseTest {
@Test
public void testAddTable() throws SQLException {
@ -21,9 +22,14 @@ public class AddTableByColumnTaskTest extends BaseTest {
getMigrator().migrate();
assertThat(JdbcUtils.getTableNames(getConnectionProperties()), containsInAnyOrder("FOO_TABLE", "TGT_TABLE"));
Set<String> indexes = JdbcUtils.getIndexNames(getConnectionProperties(), "FOO_TABLE")
.stream()
.filter(s -> !s.startsWith("FK_REF_INDEX_"))
.filter(s -> !s.startsWith("PRIMARY_KEY_"))
.collect(Collectors.toSet());
assertThat(indexes, containsInAnyOrder("IDX_HELLO"));
}
private static class MyMigrationTasks extends BaseMigrationTasks<VersionEnum> {
public MyMigrationTasks() {
Builder v = forVersion(VersionEnum.V3_5_0);
@ -34,10 +40,13 @@ public class AddTableByColumnTaskTest extends BaseTest {
Builder.BuilderAddTableByColumns fooTable = v.addTableByColumns("3", "FOO_TABLE", "PID");
fooTable.addColumn("PID").nonNullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.LONG);
fooTable.addColumn("HELLO").nullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.STRING, 200);
fooTable.addColumn("GOODBYE").nullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.STRING, 200);
fooTable.addColumn("COL_REF").nullable().type(BaseTableColumnTypeTask.ColumnTypeEnum.LONG);
fooTable.addIndex("4", "IDX_HELLO").unique(true).withColumns("HELLO");
fooTable.addForeignKey("5", "FK_REF").toColumn("COL_REF").references("TGT_TABLE", "PID");
// FIXME KHS add doNothing() somewhere in this builder (find the sync this is using)
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");
}
}
}