added final String version field to migration BaseTask

This commit is contained in:
Ken Stevens 2019-10-28 15:34:21 -04:00
parent 55c10c2292
commit 1aabe9bf40
22 changed files with 110 additions and 54 deletions

View File

@ -68,9 +68,6 @@ public class FlywayMigrator {
} }
public void addTask(BaseTask<?> theTask) { public void addTask(BaseTask<?> theTask) {
if (theTask.getVersion() == null) {
theTask.setVersion("2." + (++ourVersion));
}
myTasks.add(new FlywayMigration(theTask, this)); myTasks.add(new FlywayMigration(theTask, this));
} }

View File

@ -37,6 +37,10 @@ public class AddForeignKeyTask extends BaseTableColumnTask<AddForeignKeyTask> {
private String myForeignTableName; private String myForeignTableName;
private String myForeignColumnName; private String myForeignColumnName;
public AddForeignKeyTask(String theVersion) {
super(theVersion);
}
public void setConstraintName(String theConstraintName) { public void setConstraintName(String theConstraintName) {
myConstraintName = theConstraintName; myConstraintName = theConstraintName;
} }

View File

@ -36,7 +36,8 @@ public class AddIdGeneratorTask extends BaseTask<AddIdGeneratorTask> {
private static final Logger ourLog = LoggerFactory.getLogger(AddIdGeneratorTask.class); private static final Logger ourLog = LoggerFactory.getLogger(AddIdGeneratorTask.class);
private final String myGeneratorName; private final String myGeneratorName;
public AddIdGeneratorTask(String theGeneratorName) { public AddIdGeneratorTask(String theVersion, String theGeneratorName) {
super(theVersion);
myGeneratorName = theGeneratorName; myGeneratorName = theGeneratorName;
} }

View File

@ -39,6 +39,10 @@ public class AddIndexTask extends BaseTableTask<AddIndexTask> {
private List<String> myColumns; private List<String> myColumns;
private Boolean myUnique; private Boolean myUnique;
public AddIndexTask(String theVersion) {
super(theVersion);
}
public void setIndexName(String theIndexName) { public void setIndexName(String theIndexName) {
myIndexName = StringUtils.toUpperCase(theIndexName, Locale.US); myIndexName = StringUtils.toUpperCase(theIndexName, Locale.US);
} }

View File

@ -36,6 +36,10 @@ public class AddTableByColumnTask extends BaseTableTask<AddTableByColumnTask> {
private List<AddColumnTask> myAddColumnTasks = new ArrayList<>(); private List<AddColumnTask> myAddColumnTasks = new ArrayList<>();
private String myPkColumn; private String myPkColumn;
public AddTableByColumnTask(String theVersion) {
super(theVersion);
}
public void addAddColumnTask(AddColumnTask theTask) { public void addAddColumnTask(AddColumnTask theTask) {
Validate.notNull(theTask); Validate.notNull(theTask);
myAddColumnTasks.add(theTask); myAddColumnTasks.add(theTask);

View File

@ -37,6 +37,10 @@ public class AddTableRawSqlTask extends BaseTableTask<AddTableRawSqlTask> {
private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>(); private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
private List<String> myDriverNeutralSqls = new ArrayList<>(); private List<String> myDriverNeutralSqls = new ArrayList<>();
public AddTableRawSqlTask(String theVersion) {
super(theVersion);
}
public void addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) { public void addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
Validate.notNull(theDriverType); Validate.notNull(theDriverType);
Validate.notBlank(theSql); Validate.notBlank(theSql);

View File

@ -44,7 +44,8 @@ public class ArbitrarySqlTask extends BaseTask<ArbitrarySqlTask> {
private String myExecuteOnlyIfTableExists; private String myExecuteOnlyIfTableExists;
private List<TableAndColumn> myConditionalOnExistenceOf = new ArrayList<>(); private List<TableAndColumn> myConditionalOnExistenceOf = new ArrayList<>();
public ArbitrarySqlTask(String theTableName, String theDescription) { public ArbitrarySqlTask(String theVersion, String theTableName, String theDescription) {
super(theVersion);
myTableName = theTableName; myTableName = theTableName;
myDescription = theDescription; myDescription = theDescription;
} }

View File

@ -29,6 +29,10 @@ public abstract class BaseTableColumnTask<T extends BaseTableTask> extends BaseT
private String myColumnName; private String myColumnName;
public BaseTableColumnTask(String theVersion) {
super(theVersion);
}
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
public T setColumnName(String theColumnName) { public T setColumnName(String theColumnName) {
myColumnName = StringUtils.toUpperCase(theColumnName, Locale.US); myColumnName = StringUtils.toUpperCase(theColumnName, Locale.US);

View File

@ -38,7 +38,9 @@ public abstract class BaseTableColumnTypeTask<T extends BaseTableTask> extends B
/** /**
* Constructor * Constructor
*/ */
BaseTableColumnTypeTask() {
public BaseTableColumnTypeTask(String theVersion) {
super(theVersion);
setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.H2_EMBEDDED, "integer"); setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.H2_EMBEDDED, "integer");
setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.DERBY_EMBEDDED, "integer"); setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.DERBY_EMBEDDED, "integer");
setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.MARIADB_10_1, "integer"); setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.MARIADB_10_1, "integer");

View File

@ -25,6 +25,10 @@ import org.apache.commons.lang3.Validate;
public abstract class BaseTableTask<T extends BaseTableTask> extends BaseTask { public abstract class BaseTableTask<T extends BaseTableTask> extends BaseTask {
private String myTableName; private String myTableName;
public BaseTableTask(String theVersion) {
super(theVersion);
}
public String getTableName() { public String getTableName() {
return myTableName; return myTableName;
} }

View File

@ -43,8 +43,11 @@ public abstract class BaseTask<T extends BaseTask> {
private boolean myDryRun; private boolean myDryRun;
private List<ExecutedStatement> myExecutedStatements = new ArrayList<>(); private List<ExecutedStatement> myExecutedStatements = new ArrayList<>();
private boolean myNoColumnShrink; private boolean myNoColumnShrink;
// FIXME KHS final private final String version;
private String version;
protected BaseTask(String theVersion) {
version = theVersion;
}
public boolean isNoColumnShrink() { public boolean isNoColumnShrink() {
return myNoColumnShrink; return myNoColumnShrink;
@ -136,11 +139,6 @@ public abstract class BaseTask<T extends BaseTask> {
return version; return version;
} }
public BaseTask<T> setVersion(String theVersion) {
version = theVersion;
return this;
}
public static class ExecutedStatement { public static class ExecutedStatement {
private final String mySql; private final String mySql;
private final List<Object> myArguments; private final List<Object> myArguments;

View File

@ -53,8 +53,8 @@ public class CalculateHashesTask extends BaseTableColumnTask<CalculateHashesTask
/** /**
* Constructor * Constructor
*/ */
public CalculateHashesTask() { public CalculateHashesTask(String theVersion) {
super(); super(theVersion);
} }
@Override @Override

View File

@ -32,6 +32,9 @@ public class DropColumnTask extends BaseTableColumnTask<DropColumnTask> {
private static final Logger ourLog = LoggerFactory.getLogger(DropColumnTask.class); private static final Logger ourLog = LoggerFactory.getLogger(DropColumnTask.class);
public DropColumnTask(String theVersion) {
super(theVersion);
}
@Override @Override
public void execute() throws SQLException { public void execute() throws SQLException {

View File

@ -40,6 +40,10 @@ public class DropForeignKeyTask extends BaseTableTask<DropForeignKeyTask> {
private String myConstraintName; private String myConstraintName;
private String myParentTableName; private String myParentTableName;
public DropForeignKeyTask(String theVersion) {
super(theVersion);
}
public void setConstraintName(String theConstraintName) { public void setConstraintName(String theConstraintName) {
myConstraintName = theConstraintName; myConstraintName = theConstraintName;
} }

View File

@ -36,7 +36,8 @@ public class DropIdGeneratorTask extends BaseTask<DropIdGeneratorTask> {
private static final Logger ourLog = LoggerFactory.getLogger(DropIdGeneratorTask.class); private static final Logger ourLog = LoggerFactory.getLogger(DropIdGeneratorTask.class);
private final String myGeneratorName; private final String myGeneratorName;
public DropIdGeneratorTask(String theGeneratorName) { public DropIdGeneratorTask(String theVersion, String theGeneratorName) {
super(theVersion);
myGeneratorName = theGeneratorName; myGeneratorName = theGeneratorName;
} }

View File

@ -35,6 +35,10 @@ public class DropIndexTask extends BaseTableTask<DropIndexTask> {
private static final Logger ourLog = LoggerFactory.getLogger(DropIndexTask.class); private static final Logger ourLog = LoggerFactory.getLogger(DropIndexTask.class);
private String myIndexName; private String myIndexName;
public DropIndexTask(String theVersion) {
super(theVersion);
}
@Override @Override
public void validate() { public void validate() {
super.validate(); super.validate();

View File

@ -33,6 +33,10 @@ public class DropTableTask extends BaseTableTask<DropTableTask> {
private static final Logger ourLog = LoggerFactory.getLogger(DropTableTask.class); private static final Logger ourLog = LoggerFactory.getLogger(DropTableTask.class);
public DropTableTask(String theVersion) {
super(theVersion);
}
@Override @Override
public void execute() throws SQLException { public void execute() throws SQLException {
Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties()); Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());

View File

@ -34,6 +34,10 @@ public class ExecuteRawSqlTask extends BaseTask<ExecuteRawSqlTask> {
private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>(); private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
private List<String> myDriverNeutralSqls = new ArrayList<>(); private List<String> myDriverNeutralSqls = new ArrayList<>();
public ExecuteRawSqlTask(String theVersion) {
super(theVersion);
}
public ExecuteRawSqlTask addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) { public ExecuteRawSqlTask addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
Validate.notNull(theDriverType); Validate.notNull(theDriverType);
Validate.notBlank(theSql); Validate.notBlank(theSql);

View File

@ -28,7 +28,8 @@ public class LogStartSectionWithMessageTask extends BaseTask {
private static final Logger ourLog = LoggerFactory.getLogger(LogStartSectionWithMessageTask.class); private static final Logger ourLog = LoggerFactory.getLogger(LogStartSectionWithMessageTask.class);
private final String myMessage; private final String myMessage;
public LogStartSectionWithMessageTask(String theMessage) { public LogStartSectionWithMessageTask(String theVersion, String theMessage) {
super(theVersion);
myMessage = theMessage; myMessage = theMessage;
} }

View File

@ -32,6 +32,14 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask<ModifyColumnTask>
private static final Logger ourLog = LoggerFactory.getLogger(ModifyColumnTask.class); private static final Logger ourLog = LoggerFactory.getLogger(ModifyColumnTask.class);
/**
* Constructor
*
* @param theVersion
*/
public ModifyColumnTask(String theVersion) {
super(theVersion);
}
@Override @Override
public void execute() throws SQLException { public void execute() throws SQLException {

View File

@ -38,6 +38,10 @@ public class RenameColumnTask extends BaseTableTask<RenameColumnTask> {
private boolean myAllowNeitherColumnToExist; private boolean myAllowNeitherColumnToExist;
private boolean myDeleteTargetColumnFirstIfBothExist; private boolean myDeleteTargetColumnFirstIfBothExist;
public RenameColumnTask(String theVersion) {
super(theVersion);
}
public void setDeleteTargetColumnFirstIfBothExist(boolean theDeleteTargetColumnFirstIfBothExist) { public void setDeleteTargetColumnFirstIfBothExist(boolean theDeleteTargetColumnFirstIfBothExist) {
myDeleteTargetColumnFirstIfBothExist = theDeleteTargetColumnFirstIfBothExist; myDeleteTargetColumnFirstIfBothExist = theDeleteTargetColumnFirstIfBothExist;
} }

View File

@ -88,37 +88,37 @@ public class BaseMigrationTasks<T extends Enum> {
mySink.addTask(theTask); mySink.addTask(theTask);
} }
public BuilderAddTableRawSql addTableRawSql(String theTableName) { public BuilderAddTableRawSql addTableRawSql(String theVersion, String theTableName) {
return new BuilderAddTableRawSql(theTableName); return new BuilderAddTableRawSql(theVersion, theTableName);
} }
public Builder executeRawSql(@Language("SQL") String theSql) { public Builder executeRawSql(String theVersion, @Language("SQL") String theSql) {
mySink.addTask(new ExecuteRawSqlTask().addSql(theSql)); mySink.addTask(new ExecuteRawSqlTask(theVersion).addSql(theSql));
return this; return this;
} }
public Builder executeRawSql(DriverTypeEnum theDriver, @Language("SQL") String theSql) { public Builder executeRawSql(String theVersion, DriverTypeEnum theDriver, @Language("SQL") String theSql) {
mySink.addTask(new ExecuteRawSqlTask().addSql(theDriver, theSql)); mySink.addTask(new ExecuteRawSqlTask(theVersion).addSql(theDriver, theSql));
return this; return this;
} }
public Builder startSectionWithMessage(String theMessage) { public Builder startSectionWithMessage(String theMessage) {
Validate.notBlank(theMessage); Validate.notBlank(theMessage);
addTask(new LogStartSectionWithMessageTask(theMessage)); addTask(new LogStartSectionWithMessageTask("log message", theMessage));
return this; return this;
} }
public BuilderAddTableByColumns addTableByColumns(String theTableName, String thePkColumnName) { public BuilderAddTableByColumns addTableByColumns(String theVersion, String theTableName, String thePkColumnName) {
return new BuilderAddTableByColumns(mySink, theTableName, thePkColumnName); return new BuilderAddTableByColumns(theVersion, mySink, theTableName, thePkColumnName);
} }
public void addIdGenerator(String theGeneratorName) { public void addIdGenerator(String theVersion, String theGeneratorName) {
AddIdGeneratorTask task = new AddIdGeneratorTask(theGeneratorName); AddIdGeneratorTask task = new AddIdGeneratorTask(theVersion, theGeneratorName);
addTask(task); addTask(task);
} }
public void dropIdGenerator(String theIdGeneratorName) { public void dropIdGenerator(String theVersion, String theIdGeneratorName) {
DropIdGeneratorTask task = new DropIdGeneratorTask(theIdGeneratorName); DropIdGeneratorTask task = new DropIdGeneratorTask(theVersion, theIdGeneratorName);
addTask(task); addTask(task);
} }
@ -126,8 +126,8 @@ public class BaseMigrationTasks<T extends Enum> {
private final AddTableRawSqlTask myTask; private final AddTableRawSqlTask myTask;
protected BuilderAddTableRawSql(String theTableName) { protected BuilderAddTableRawSql(String theVersion, String theTableName) {
myTask = new AddTableRawSqlTask(); myTask = new AddTableRawSqlTask(theVersion);
myTask.setTableName(theTableName); myTask.setTableName(theTableName);
addTask(myTask); addTask(myTask);
} }
@ -146,9 +146,9 @@ public class BaseMigrationTasks<T extends Enum> {
public class BuilderAddTableByColumns extends BuilderWithTableName implements IAcceptsTasks { public class BuilderAddTableByColumns extends BuilderWithTableName implements IAcceptsTasks {
private final AddTableByColumnTask myTask; private final AddTableByColumnTask myTask;
public BuilderAddTableByColumns(IAcceptsTasks theSink, String theTableName, String thePkColumnName) { public BuilderAddTableByColumns(String theVersion, IAcceptsTasks theSink, String theTableName, String thePkColumnName) {
super(theSink, theTableName); super(theSink, theTableName);
myTask = new AddTableByColumnTask(); myTask = new AddTableByColumnTask(theVersion);
myTask.setTableName(theTableName); myTask.setTableName(theTableName);
myTask.setPkColumn(thePkColumnName); myTask.setPkColumn(thePkColumnName);
theSink.addTask(myTask); theSink.addTask(myTask);
@ -182,15 +182,15 @@ public class BaseMigrationTasks<T extends Enum> {
return myTableName; return myTableName;
} }
public void dropIndex(String theIndexName) { public void dropIndex(String theVersion, String theIndexName) {
DropIndexTask task = new DropIndexTask(); DropIndexTask task = new DropIndexTask(theVersion);
task.setIndexName(theIndexName); task.setIndexName(theIndexName);
task.setTableName(myTableName); task.setTableName(myTableName);
addTask(task); addTask(task);
} }
public void dropThisTable() { public void dropThisTable(String theVersion) {
DropTableTask task = new DropTableTask(); DropTableTask task = new DropTableTask(theVersion);
task.setTableName(myTableName); task.setTableName(myTableName);
addTask(task); addTask(task);
} }
@ -203,9 +203,9 @@ public class BaseMigrationTasks<T extends Enum> {
return new BuilderAddColumnWithName(theColumnName, this); return new BuilderAddColumnWithName(theColumnName, this);
} }
public void dropColumn(String theColumnName) { public void dropColumn(String theVersion, String theColumnName) {
Validate.notBlank(theColumnName); Validate.notBlank(theColumnName);
DropColumnTask task = new DropColumnTask(); DropColumnTask task = new DropColumnTask(theVersion);
task.setTableName(myTableName); task.setTableName(myTableName);
task.setColumnName(theColumnName); task.setColumnName(theColumnName);
addTask(task); addTask(task);
@ -225,8 +225,8 @@ public class BaseMigrationTasks<T extends Enum> {
return new BuilderAddForeignKey(theForeignKeyName); return new BuilderAddForeignKey(theForeignKeyName);
} }
public BuilderWithTableName renameColumn(String theOldName, String theNewName) { public BuilderWithTableName renameColumn(String theVersion, String theOldName, String theNewName) {
return renameColumn(theOldName, theNewName, false, false); return renameColumn(theVersion, theOldName, theNewName, false, false);
} }
/** /**
@ -235,8 +235,8 @@ public class BaseMigrationTasks<T extends Enum> {
* @param theAllowNeitherColumnToExist Setting this to true means that it's not an error if neither column exists * @param theAllowNeitherColumnToExist Setting this to true means that it's not an error if neither column exists
* @param theDeleteTargetColumnFirstIfBothEixst Setting this to true causes the migrator to be ok with the target column existing. It will make sure that there is no data in the column with the new name, then delete it if so in order to make room for the renamed column. If there is data it will still bomb out. * @param theDeleteTargetColumnFirstIfBothEixst Setting this to true causes the migrator to be ok with the target column existing. It will make sure that there is no data in the column with the new name, then delete it if so in order to make room for the renamed column. If there is data it will still bomb out.
*/ */
public BuilderWithTableName renameColumn(String theOldName, String theNewName, boolean theAllowNeitherColumnToExist, boolean theDeleteTargetColumnFirstIfBothEixst) { public BuilderWithTableName renameColumn(String theVersion, String theOldName, String theNewName, boolean theAllowNeitherColumnToExist, boolean theDeleteTargetColumnFirstIfBothEixst) {
RenameColumnTask task = new RenameColumnTask(); RenameColumnTask task = new RenameColumnTask(theVersion);
task.setTableName(myTableName); task.setTableName(myTableName);
task.setOldName(theOldName); task.setOldName(theOldName);
task.setNewName(theNewName); task.setNewName(theNewName);
@ -251,8 +251,8 @@ public class BaseMigrationTasks<T extends Enum> {
* @param theFkName the name of the foreign key * @param theFkName the name of the foreign key
* @param theParentTableName the name of the table that exports the foreign key * @param theParentTableName the name of the table that exports the foreign key
*/ */
public void dropForeignKey(String theFkName, String theParentTableName) { public void dropForeignKey(String theVersion, String theFkName, String theParentTableName) {
DropForeignKeyTask task = new DropForeignKeyTask(); DropForeignKeyTask task = new DropForeignKeyTask(theVersion);
task.setConstraintName(theFkName); task.setConstraintName(theFkName);
task.setTableName(getTableName()); task.setTableName(getTableName());
task.setParentTableName(theParentTableName); task.setParentTableName(theParentTableName);
@ -277,8 +277,8 @@ public class BaseMigrationTasks<T extends Enum> {
myUnique = theUnique; myUnique = theUnique;
} }
public void withColumns(String... theColumnNames) { public void withColumns(String theVersion, String... theColumnNames) {
AddIndexTask task = new AddIndexTask(); AddIndexTask task = new AddIndexTask(theVersion);
task.setTableName(myTableName); task.setTableName(myTableName);
task.setIndexName(myIndexName); task.setIndexName(myIndexName);
task.setUnique(myUnique); task.setUnique(myUnique);
@ -316,11 +316,11 @@ public class BaseMigrationTasks<T extends Enum> {
myNullable = theNullable; myNullable = theNullable;
} }
public void withType(BaseTableColumnTypeTask.ColumnTypeEnum theColumnType) { public void withType(String theVersion, BaseTableColumnTypeTask.ColumnTypeEnum theColumnType) {
withType(theColumnType, null); withType(theVersion, theColumnType, null);
} }
public void withType(BaseTableColumnTypeTask.ColumnTypeEnum theColumnType, Integer theLength) { public void withType(String theVersion, BaseTableColumnTypeTask.ColumnTypeEnum theColumnType, Integer theLength) {
if (theColumnType == BaseTableColumnTypeTask.ColumnTypeEnum.STRING) { if (theColumnType == BaseTableColumnTypeTask.ColumnTypeEnum.STRING) {
if (theLength == null || theLength == 0) { if (theLength == null || theLength == 0) {
throw new IllegalArgumentException("Can not specify length 0 for column of type " + theColumnType); throw new IllegalArgumentException("Can not specify length 0 for column of type " + theColumnType);
@ -331,7 +331,7 @@ public class BaseMigrationTasks<T extends Enum> {
} }
} }
ModifyColumnTask task = new ModifyColumnTask(); ModifyColumnTask task = new ModifyColumnTask(theVersion);
task.setColumnName(myColumnName); task.setColumnName(myColumnName);
task.setTableName(myTableName); task.setTableName(myTableName);
if (theLength != null) { if (theLength != null) {
@ -360,8 +360,8 @@ public class BaseMigrationTasks<T extends Enum> {
super(theColumnName); super(theColumnName);
} }
public void references(String theForeignTable, String theForeignColumn) { public void references(String theVersion, String theForeignTable, String theForeignColumn) {
AddForeignKeyTask task = new AddForeignKeyTask(); AddForeignKeyTask task = new AddForeignKeyTask(theVersion);
task.setTableName(myTableName); task.setTableName(myTableName);
task.setConstraintName(myForeignKeyName); task.setConstraintName(myForeignKeyName);
task.setColumnName(getColumnName()); task.setColumnName(getColumnName());