added final String version field to migration BaseTask
This commit is contained in:
parent
55c10c2292
commit
1aabe9bf40
|
@ -68,9 +68,6 @@ public class FlywayMigrator {
|
|||
}
|
||||
|
||||
public void addTask(BaseTask<?> theTask) {
|
||||
if (theTask.getVersion() == null) {
|
||||
theTask.setVersion("2." + (++ourVersion));
|
||||
}
|
||||
myTasks.add(new FlywayMigration(theTask, this));
|
||||
}
|
||||
|
||||
|
|
|
@ -37,6 +37,10 @@ public class AddForeignKeyTask extends BaseTableColumnTask<AddForeignKeyTask> {
|
|||
private String myForeignTableName;
|
||||
private String myForeignColumnName;
|
||||
|
||||
public AddForeignKeyTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
public void setConstraintName(String theConstraintName) {
|
||||
myConstraintName = theConstraintName;
|
||||
}
|
||||
|
|
|
@ -36,7 +36,8 @@ public class AddIdGeneratorTask extends BaseTask<AddIdGeneratorTask> {
|
|||
private static final Logger ourLog = LoggerFactory.getLogger(AddIdGeneratorTask.class);
|
||||
private final String myGeneratorName;
|
||||
|
||||
public AddIdGeneratorTask(String theGeneratorName) {
|
||||
public AddIdGeneratorTask(String theVersion, String theGeneratorName) {
|
||||
super(theVersion);
|
||||
myGeneratorName = theGeneratorName;
|
||||
}
|
||||
|
||||
|
|
|
@ -39,6 +39,10 @@ public class AddIndexTask extends BaseTableTask<AddIndexTask> {
|
|||
private List<String> myColumns;
|
||||
private Boolean myUnique;
|
||||
|
||||
public AddIndexTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
public void setIndexName(String theIndexName) {
|
||||
myIndexName = StringUtils.toUpperCase(theIndexName, Locale.US);
|
||||
}
|
||||
|
|
|
@ -36,6 +36,10 @@ public class AddTableByColumnTask extends BaseTableTask<AddTableByColumnTask> {
|
|||
private List<AddColumnTask> myAddColumnTasks = new ArrayList<>();
|
||||
private String myPkColumn;
|
||||
|
||||
public AddTableByColumnTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
public void addAddColumnTask(AddColumnTask theTask) {
|
||||
Validate.notNull(theTask);
|
||||
myAddColumnTasks.add(theTask);
|
||||
|
|
|
@ -37,6 +37,10 @@ public class AddTableRawSqlTask extends BaseTableTask<AddTableRawSqlTask> {
|
|||
private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
|
||||
private List<String> myDriverNeutralSqls = new ArrayList<>();
|
||||
|
||||
public AddTableRawSqlTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
public void addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
|
||||
Validate.notNull(theDriverType);
|
||||
Validate.notBlank(theSql);
|
||||
|
|
|
@ -44,7 +44,8 @@ public class ArbitrarySqlTask extends BaseTask<ArbitrarySqlTask> {
|
|||
private String myExecuteOnlyIfTableExists;
|
||||
private List<TableAndColumn> myConditionalOnExistenceOf = new ArrayList<>();
|
||||
|
||||
public ArbitrarySqlTask(String theTableName, String theDescription) {
|
||||
public ArbitrarySqlTask(String theVersion, String theTableName, String theDescription) {
|
||||
super(theVersion);
|
||||
myTableName = theTableName;
|
||||
myDescription = theDescription;
|
||||
}
|
||||
|
|
|
@ -29,6 +29,10 @@ public abstract class BaseTableColumnTask<T extends BaseTableTask> extends BaseT
|
|||
|
||||
private String myColumnName;
|
||||
|
||||
public BaseTableColumnTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public T setColumnName(String theColumnName) {
|
||||
myColumnName = StringUtils.toUpperCase(theColumnName, Locale.US);
|
||||
|
|
|
@ -38,7 +38,9 @@ public abstract class BaseTableColumnTypeTask<T extends BaseTableTask> extends B
|
|||
/**
|
||||
* Constructor
|
||||
*/
|
||||
BaseTableColumnTypeTask() {
|
||||
|
||||
public BaseTableColumnTypeTask(String theVersion) {
|
||||
super(theVersion);
|
||||
setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.H2_EMBEDDED, "integer");
|
||||
setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.DERBY_EMBEDDED, "integer");
|
||||
setColumnType(ColumnTypeEnum.INT, DriverTypeEnum.MARIADB_10_1, "integer");
|
||||
|
|
|
@ -25,6 +25,10 @@ import org.apache.commons.lang3.Validate;
|
|||
public abstract class BaseTableTask<T extends BaseTableTask> extends BaseTask {
|
||||
private String myTableName;
|
||||
|
||||
public BaseTableTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
public String getTableName() {
|
||||
return myTableName;
|
||||
}
|
||||
|
|
|
@ -43,8 +43,11 @@ public abstract class BaseTask<T extends BaseTask> {
|
|||
private boolean myDryRun;
|
||||
private List<ExecutedStatement> myExecutedStatements = new ArrayList<>();
|
||||
private boolean myNoColumnShrink;
|
||||
// FIXME KHS final
|
||||
private String version;
|
||||
private final String version;
|
||||
|
||||
protected BaseTask(String theVersion) {
|
||||
version = theVersion;
|
||||
}
|
||||
|
||||
public boolean isNoColumnShrink() {
|
||||
return myNoColumnShrink;
|
||||
|
@ -136,11 +139,6 @@ public abstract class BaseTask<T extends BaseTask> {
|
|||
return version;
|
||||
}
|
||||
|
||||
public BaseTask<T> setVersion(String theVersion) {
|
||||
version = theVersion;
|
||||
return this;
|
||||
}
|
||||
|
||||
public static class ExecutedStatement {
|
||||
private final String mySql;
|
||||
private final List<Object> myArguments;
|
||||
|
|
|
@ -53,8 +53,8 @@ public class CalculateHashesTask extends BaseTableColumnTask<CalculateHashesTask
|
|||
/**
|
||||
* Constructor
|
||||
*/
|
||||
public CalculateHashesTask() {
|
||||
super();
|
||||
public CalculateHashesTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
@ -32,6 +32,9 @@ public class DropColumnTask extends BaseTableColumnTask<DropColumnTask> {
|
|||
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(DropColumnTask.class);
|
||||
|
||||
public DropColumnTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() throws SQLException {
|
||||
|
|
|
@ -40,6 +40,10 @@ public class DropForeignKeyTask extends BaseTableTask<DropForeignKeyTask> {
|
|||
private String myConstraintName;
|
||||
private String myParentTableName;
|
||||
|
||||
public DropForeignKeyTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
public void setConstraintName(String theConstraintName) {
|
||||
myConstraintName = theConstraintName;
|
||||
}
|
||||
|
|
|
@ -36,7 +36,8 @@ public class DropIdGeneratorTask extends BaseTask<DropIdGeneratorTask> {
|
|||
private static final Logger ourLog = LoggerFactory.getLogger(DropIdGeneratorTask.class);
|
||||
private final String myGeneratorName;
|
||||
|
||||
public DropIdGeneratorTask(String theGeneratorName) {
|
||||
public DropIdGeneratorTask(String theVersion, String theGeneratorName) {
|
||||
super(theVersion);
|
||||
myGeneratorName = theGeneratorName;
|
||||
}
|
||||
|
||||
|
|
|
@ -35,6 +35,10 @@ public class DropIndexTask extends BaseTableTask<DropIndexTask> {
|
|||
private static final Logger ourLog = LoggerFactory.getLogger(DropIndexTask.class);
|
||||
private String myIndexName;
|
||||
|
||||
public DropIndexTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void validate() {
|
||||
super.validate();
|
||||
|
|
|
@ -33,6 +33,10 @@ public class DropTableTask extends BaseTableTask<DropTableTask> {
|
|||
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(DropTableTask.class);
|
||||
|
||||
public DropTableTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() throws SQLException {
|
||||
Set<String> tableNames = JdbcUtils.getTableNames(getConnectionProperties());
|
||||
|
|
|
@ -34,6 +34,10 @@ public class ExecuteRawSqlTask extends BaseTask<ExecuteRawSqlTask> {
|
|||
private Map<DriverTypeEnum, List<String>> myDriverToSqls = new HashMap<>();
|
||||
private List<String> myDriverNeutralSqls = new ArrayList<>();
|
||||
|
||||
public ExecuteRawSqlTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
public ExecuteRawSqlTask addSql(DriverTypeEnum theDriverType, @Language("SQL") String theSql) {
|
||||
Validate.notNull(theDriverType);
|
||||
Validate.notBlank(theSql);
|
||||
|
|
|
@ -28,7 +28,8 @@ public class LogStartSectionWithMessageTask extends BaseTask {
|
|||
private static final Logger ourLog = LoggerFactory.getLogger(LogStartSectionWithMessageTask.class);
|
||||
private final String myMessage;
|
||||
|
||||
public LogStartSectionWithMessageTask(String theMessage) {
|
||||
public LogStartSectionWithMessageTask(String theVersion, String theMessage) {
|
||||
super(theVersion);
|
||||
myMessage = theMessage;
|
||||
}
|
||||
|
||||
|
|
|
@ -32,6 +32,14 @@ public class ModifyColumnTask extends BaseTableColumnTypeTask<ModifyColumnTask>
|
|||
|
||||
private static final Logger ourLog = LoggerFactory.getLogger(ModifyColumnTask.class);
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*
|
||||
* @param theVersion
|
||||
*/
|
||||
public ModifyColumnTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void execute() throws SQLException {
|
||||
|
|
|
@ -38,6 +38,10 @@ public class RenameColumnTask extends BaseTableTask<RenameColumnTask> {
|
|||
private boolean myAllowNeitherColumnToExist;
|
||||
private boolean myDeleteTargetColumnFirstIfBothExist;
|
||||
|
||||
public RenameColumnTask(String theVersion) {
|
||||
super(theVersion);
|
||||
}
|
||||
|
||||
public void setDeleteTargetColumnFirstIfBothExist(boolean theDeleteTargetColumnFirstIfBothExist) {
|
||||
myDeleteTargetColumnFirstIfBothExist = theDeleteTargetColumnFirstIfBothExist;
|
||||
}
|
||||
|
|
|
@ -88,37 +88,37 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
mySink.addTask(theTask);
|
||||
}
|
||||
|
||||
public BuilderAddTableRawSql addTableRawSql(String theTableName) {
|
||||
return new BuilderAddTableRawSql(theTableName);
|
||||
public BuilderAddTableRawSql addTableRawSql(String theVersion, String theTableName) {
|
||||
return new BuilderAddTableRawSql(theVersion, theTableName);
|
||||
}
|
||||
|
||||
public Builder executeRawSql(@Language("SQL") String theSql) {
|
||||
mySink.addTask(new ExecuteRawSqlTask().addSql(theSql));
|
||||
public Builder executeRawSql(String theVersion, @Language("SQL") String theSql) {
|
||||
mySink.addTask(new ExecuteRawSqlTask(theVersion).addSql(theSql));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder executeRawSql(DriverTypeEnum theDriver, @Language("SQL") String theSql) {
|
||||
mySink.addTask(new ExecuteRawSqlTask().addSql(theDriver, theSql));
|
||||
public Builder executeRawSql(String theVersion, DriverTypeEnum theDriver, @Language("SQL") String theSql) {
|
||||
mySink.addTask(new ExecuteRawSqlTask(theVersion).addSql(theDriver, theSql));
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder startSectionWithMessage(String theMessage) {
|
||||
Validate.notBlank(theMessage);
|
||||
addTask(new LogStartSectionWithMessageTask(theMessage));
|
||||
addTask(new LogStartSectionWithMessageTask("log message", theMessage));
|
||||
return this;
|
||||
}
|
||||
|
||||
public BuilderAddTableByColumns addTableByColumns(String theTableName, String thePkColumnName) {
|
||||
return new BuilderAddTableByColumns(mySink, theTableName, thePkColumnName);
|
||||
public BuilderAddTableByColumns addTableByColumns(String theVersion, String theTableName, String thePkColumnName) {
|
||||
return new BuilderAddTableByColumns(theVersion, mySink, theTableName, thePkColumnName);
|
||||
}
|
||||
|
||||
public void addIdGenerator(String theGeneratorName) {
|
||||
AddIdGeneratorTask task = new AddIdGeneratorTask(theGeneratorName);
|
||||
public void addIdGenerator(String theVersion, String theGeneratorName) {
|
||||
AddIdGeneratorTask task = new AddIdGeneratorTask(theVersion, theGeneratorName);
|
||||
addTask(task);
|
||||
}
|
||||
|
||||
public void dropIdGenerator(String theIdGeneratorName) {
|
||||
DropIdGeneratorTask task = new DropIdGeneratorTask(theIdGeneratorName);
|
||||
public void dropIdGenerator(String theVersion, String theIdGeneratorName) {
|
||||
DropIdGeneratorTask task = new DropIdGeneratorTask(theVersion, theIdGeneratorName);
|
||||
addTask(task);
|
||||
}
|
||||
|
||||
|
@ -126,8 +126,8 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
|
||||
private final AddTableRawSqlTask myTask;
|
||||
|
||||
protected BuilderAddTableRawSql(String theTableName) {
|
||||
myTask = new AddTableRawSqlTask();
|
||||
protected BuilderAddTableRawSql(String theVersion, String theTableName) {
|
||||
myTask = new AddTableRawSqlTask(theVersion);
|
||||
myTask.setTableName(theTableName);
|
||||
addTask(myTask);
|
||||
}
|
||||
|
@ -146,9 +146,9 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
public class BuilderAddTableByColumns extends BuilderWithTableName implements IAcceptsTasks {
|
||||
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);
|
||||
myTask = new AddTableByColumnTask();
|
||||
myTask = new AddTableByColumnTask(theVersion);
|
||||
myTask.setTableName(theTableName);
|
||||
myTask.setPkColumn(thePkColumnName);
|
||||
theSink.addTask(myTask);
|
||||
|
@ -182,15 +182,15 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
return myTableName;
|
||||
}
|
||||
|
||||
public void dropIndex(String theIndexName) {
|
||||
DropIndexTask task = new DropIndexTask();
|
||||
public void dropIndex(String theVersion, String theIndexName) {
|
||||
DropIndexTask task = new DropIndexTask(theVersion);
|
||||
task.setIndexName(theIndexName);
|
||||
task.setTableName(myTableName);
|
||||
addTask(task);
|
||||
}
|
||||
|
||||
public void dropThisTable() {
|
||||
DropTableTask task = new DropTableTask();
|
||||
public void dropThisTable(String theVersion) {
|
||||
DropTableTask task = new DropTableTask(theVersion);
|
||||
task.setTableName(myTableName);
|
||||
addTask(task);
|
||||
}
|
||||
|
@ -203,9 +203,9 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
return new BuilderAddColumnWithName(theColumnName, this);
|
||||
}
|
||||
|
||||
public void dropColumn(String theColumnName) {
|
||||
public void dropColumn(String theVersion, String theColumnName) {
|
||||
Validate.notBlank(theColumnName);
|
||||
DropColumnTask task = new DropColumnTask();
|
||||
DropColumnTask task = new DropColumnTask(theVersion);
|
||||
task.setTableName(myTableName);
|
||||
task.setColumnName(theColumnName);
|
||||
addTask(task);
|
||||
|
@ -225,8 +225,8 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
return new BuilderAddForeignKey(theForeignKeyName);
|
||||
}
|
||||
|
||||
public BuilderWithTableName renameColumn(String theOldName, String theNewName) {
|
||||
return renameColumn(theOldName, theNewName, false, false);
|
||||
public BuilderWithTableName renameColumn(String theVersion, String theOldName, String theNewName) {
|
||||
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 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) {
|
||||
RenameColumnTask task = new RenameColumnTask();
|
||||
public BuilderWithTableName renameColumn(String theVersion, String theOldName, String theNewName, boolean theAllowNeitherColumnToExist, boolean theDeleteTargetColumnFirstIfBothEixst) {
|
||||
RenameColumnTask task = new RenameColumnTask(theVersion);
|
||||
task.setTableName(myTableName);
|
||||
task.setOldName(theOldName);
|
||||
task.setNewName(theNewName);
|
||||
|
@ -251,8 +251,8 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
* @param theFkName the name of the foreign key
|
||||
* @param theParentTableName the name of the table that exports the foreign key
|
||||
*/
|
||||
public void dropForeignKey(String theFkName, String theParentTableName) {
|
||||
DropForeignKeyTask task = new DropForeignKeyTask();
|
||||
public void dropForeignKey(String theVersion, String theFkName, String theParentTableName) {
|
||||
DropForeignKeyTask task = new DropForeignKeyTask(theVersion);
|
||||
task.setConstraintName(theFkName);
|
||||
task.setTableName(getTableName());
|
||||
task.setParentTableName(theParentTableName);
|
||||
|
@ -277,8 +277,8 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
myUnique = theUnique;
|
||||
}
|
||||
|
||||
public void withColumns(String... theColumnNames) {
|
||||
AddIndexTask task = new AddIndexTask();
|
||||
public void withColumns(String theVersion, String... theColumnNames) {
|
||||
AddIndexTask task = new AddIndexTask(theVersion);
|
||||
task.setTableName(myTableName);
|
||||
task.setIndexName(myIndexName);
|
||||
task.setUnique(myUnique);
|
||||
|
@ -316,11 +316,11 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
myNullable = theNullable;
|
||||
}
|
||||
|
||||
public void withType(BaseTableColumnTypeTask.ColumnTypeEnum theColumnType) {
|
||||
withType(theColumnType, null);
|
||||
public void withType(String theVersion, BaseTableColumnTypeTask.ColumnTypeEnum theColumnType) {
|
||||
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 (theLength == null || theLength == 0) {
|
||||
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.setTableName(myTableName);
|
||||
if (theLength != null) {
|
||||
|
@ -360,8 +360,8 @@ public class BaseMigrationTasks<T extends Enum> {
|
|||
super(theColumnName);
|
||||
}
|
||||
|
||||
public void references(String theForeignTable, String theForeignColumn) {
|
||||
AddForeignKeyTask task = new AddForeignKeyTask();
|
||||
public void references(String theVersion, String theForeignTable, String theForeignColumn) {
|
||||
AddForeignKeyTask task = new AddForeignKeyTask(theVersion);
|
||||
task.setTableName(myTableName);
|
||||
task.setConstraintName(myForeignKeyName);
|
||||
task.setColumnName(getColumnName());
|
||||
|
|
Loading…
Reference in New Issue