So I made some alterations to the liquibase source code so that the class AddColumnChange could add a column to all the tables which name ends in a certain suffix.
This is my code
@Override
public SqlStatement[] generateStatements(Database database) throws CustomChangeException {
List<SqlStatement> sql = new ArrayList<>();
List<AddColumnStatement> addColumnStatements = new ArrayList<>();
List<UpdateStatement> addColumnUpdateStatements = new ArrayList<>();
if (getColumns().isEmpty()) {
return new SqlStatement[] {
new AddColumnStatement(catalogName, schemaName, tableName, null, null, null)
};
}
if(tableName.startsWith("%")){
JdbcConnection connection = (JdbcConnection) database.getConnection();
DatabaseMetaData metadata;
tableName=tableName.substring(1);
try {
metadata = connection.getMetaData();
String[] types = {"TABLE"};
ResultSet rs = metadata.getTables(null, null, "%", types);
while (rs.next()) {
String tName = rs.getString(3);
if (tName.endsWith(tableName)) {
for (AddColumnConfig column : getColumns()) {
Set<ColumnConstraint> constraints = new HashSet<>();
ConstraintsConfig constraintsConfig =column.getConstraints();
if (constraintsConfig != null) {
if ((constraintsConfig.isNullable() != null) && !constraintsConfig.isNullable()) {
NotNullConstraint notNullConstraint = new NotNullConstraint();
if (constraintsConfig.getValidateNullable()!=null && !constraintsConfig.getValidateNullable()) {
notNullConstraint.setValidateNullable(false);
}
notNullConstraint.setConstraintName(constraintsConfig.getNotNullConstraintName());
constraints.add(notNullConstraint);
}
if (constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) {
UniqueConstraint uniqueConstraint = new UniqueConstraint(constraintsConfig.getUniqueConstraintName());
if (constraintsConfig.getValidateUnique()!=null && !constraintsConfig.getValidateUnique()) {
uniqueConstraint.setValidateUnique(false);
}
constraints.add(uniqueConstraint);
}
if ((constraintsConfig.isPrimaryKey() != null) && constraintsConfig.isPrimaryKey()) {
PrimaryKeyConstraint primaryKeyConstraint = new PrimaryKeyConstraint(constraintsConfig.getPrimaryKeyName());
if (constraintsConfig.getValidatePrimaryKey()!=null && !constraintsConfig.getValidatePrimaryKey()) {
primaryKeyConstraint.setValidatePrimaryKey(false);
}
constraints.add(primaryKeyConstraint);
}
if ((constraintsConfig.getReferences() != null) || ((constraintsConfig.getReferencedColumnNames() !=
null) && (constraintsConfig.getReferencedTableName() != null))) {
ForeignKeyConstraint foreignKeyConstraint = new ForeignKeyConstraint(constraintsConfig.getForeignKeyName(),
constraintsConfig.getReferences(), constraintsConfig.getReferencedTableName(),
constraintsConfig.getReferencedColumnNames());
if (constraintsConfig.getValidateForeignKey()!=null && !constraintsConfig.getValidateForeignKey()) {
foreignKeyConstraint.setValidateForeignKey(false);
}
constraints.add(foreignKeyConstraint);
}
}
if ((column.isAutoIncrement() != null) && column.isAutoIncrement()) {
constraints.add(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy(), column.getGenerationType(), column.getDefaultOnNull()));
}
AddColumnStatement addColumnStatement = new AddColumnStatement(getCatalogName(), getSchemaName(),
tName,
column.getName(),
column.getType(),
column.getDefaultValueObject(),
column.getRemarks(),
constraints.toArray(new ColumnConstraint[constraints.size()]));
addColumnStatement.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
if ((database instanceof MySQLDatabase) && (column.getAfterColumn() != null)) {
addColumnStatement.setAddAfterColumn(column.getAfterColumn());
} else if (((database instanceof HsqlDatabase) || (database instanceof H2Database))
&& (column.getBeforeColumn() != null)) {
addColumnStatement.setAddBeforeColumn(column.getBeforeColumn());
} else if ((database instanceof FirebirdDatabase) && (column.getPosition() != null)) {
addColumnStatement.setAddAtPosition(column.getPosition());
}
addColumnStatements.add(addColumnStatement);
if (column.getValueObject() != null) {
UpdateStatement updateStatement = new UpdateStatement(getCatalogName(), getSchemaName(), tName);
updateStatement.addNewColumnValue(column.getName(), column.getValueObject());
if(database instanceof DB2Database) {
// Cannot update until table is reorganized in DB2
addColumnUpdateStatements.add(updateStatement);
} else {
sql.add(updateStatement);
}
}
}
}
if (database instanceof DB2Database) {
sql.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), tName));
// Add all the update statements after the reorg table in DB2
sql.addAll(addColumnUpdateStatements);
}
if (addColumnStatements.size() == 1) {
sql.add(0, addColumnStatements.get(0));
} else {
sql.add(0, new AddColumnStatement(addColumnStatements));
}
for (ColumnConfig column : getColumns()) {
String columnRemarks = StringUtil.trimToNull(column.getRemarks());
if (columnRemarks != null) {
SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tName, column.getName(), columnRemarks, column.getType());
if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
if (!(database instanceof MySQLDatabase)) {
//don't re-add the comments with mysql because mysql messes with the column definition
sql.add(remarksStatement);
}
}
}
}
}
} catch (DatabaseException | SQLException ex) {
throw new CustomChangeException("Error enabling trigger: " + ex);
}
}
else{
for (AddColumnConfig column : getColumns()) {
Set<ColumnConstraint> constraints = new HashSet<>();
ConstraintsConfig constraintsConfig =column.getConstraints();
if (constraintsConfig != null) {
if ((constraintsConfig.isNullable() != null) && !constraintsConfig.isNullable()) {
NotNullConstraint notNullConstraint = new NotNullConstraint();
if (constraintsConfig.getValidateNullable()!=null && !constraintsConfig.getValidateNullable()) {
notNullConstraint.setValidateNullable(false);
}
notNullConstraint.setConstraintName(constraintsConfig.getNotNullConstraintName());
constraints.add(notNullConstraint);
}
if (constraintsConfig.isUnique() != null && constraintsConfig.isUnique()) {
UniqueConstraint uniqueConstraint = new UniqueConstraint(constraintsConfig.getUniqueConstraintName());
if (constraintsConfig.getValidateUnique()!=null && !constraintsConfig.getValidateUnique()) {
uniqueConstraint.setValidateUnique(false);
}
constraints.add(uniqueConstraint);
}
if ((constraintsConfig.isPrimaryKey() != null) && constraintsConfig.isPrimaryKey()) {
PrimaryKeyConstraint primaryKeyConstraint = new PrimaryKeyConstraint(constraintsConfig.getPrimaryKeyName());
if (constraintsConfig.getValidatePrimaryKey()!=null && !constraintsConfig.getValidatePrimaryKey()) {
primaryKeyConstraint.setValidatePrimaryKey(false);
}
constraints.add(primaryKeyConstraint);
}
if ((constraintsConfig.getReferences() != null) || ((constraintsConfig.getReferencedColumnNames() !=
null) && (constraintsConfig.getReferencedTableName() != null))) {
ForeignKeyConstraint foreignKeyConstraint = new ForeignKeyConstraint(constraintsConfig.getForeignKeyName(),
constraintsConfig.getReferences(), constraintsConfig.getReferencedTableName(),
constraintsConfig.getReferencedColumnNames());
if (constraintsConfig.getValidateForeignKey()!=null && !constraintsConfig.getValidateForeignKey()) {
foreignKeyConstraint.setValidateForeignKey(false);
}
constraints.add(foreignKeyConstraint);
}
}
if ((column.isAutoIncrement() != null) && column.isAutoIncrement()) {
constraints.add(new AutoIncrementConstraint(column.getName(), column.getStartWith(), column.getIncrementBy(), column.getGenerationType(), column.getDefaultOnNull()));
}
AddColumnStatement addColumnStatement = new AddColumnStatement(getCatalogName(), getSchemaName(),
getTableName(),
column.getName(),
column.getType(),
column.getDefaultValueObject(),
column.getRemarks(),
constraints.toArray(new ColumnConstraint[constraints.size()]));
addColumnStatement.setDefaultValueConstraintName(column.getDefaultValueConstraintName());
if ((database instanceof MySQLDatabase) && (column.getAfterColumn() != null)) {
addColumnStatement.setAddAfterColumn(column.getAfterColumn());
} else if (((database instanceof HsqlDatabase) || (database instanceof H2Database))
&& (column.getBeforeColumn() != null)) {
addColumnStatement.setAddBeforeColumn(column.getBeforeColumn());
} else if ((database instanceof FirebirdDatabase) && (column.getPosition() != null)) {
addColumnStatement.setAddAtPosition(column.getPosition());
}
addColumnStatements.add(addColumnStatement);
if (column.getValueObject() != null) {
UpdateStatement updateStatement = new UpdateStatement(getCatalogName(), getSchemaName(), getTableName());
updateStatement.addNewColumnValue(column.getName(), column.getValueObject());
if(database instanceof DB2Database) {
// Cannot update until table is reorganized in DB2
addColumnUpdateStatements.add(updateStatement);
} else {
sql.add(updateStatement);
}
}
}
if (database instanceof DB2Database) {
sql.add(new ReorganizeTableStatement(getCatalogName(), getSchemaName(), getTableName()));
// Add all the update statements after the reorg table in DB2
sql.addAll(addColumnUpdateStatements);
}
if (addColumnStatements.size() == 1) {
sql.add(0, addColumnStatements.get(0));
} else {
sql.add(0, new AddColumnStatement(addColumnStatements));
}
for (ColumnConfig column : getColumns()) {
String columnRemarks = StringUtil.trimToNull(column.getRemarks());
if (columnRemarks != null) {
SetColumnRemarksStatement remarksStatement = new SetColumnRemarksStatement(catalogName, schemaName, tableName, column.getName(), columnRemarks, column.getType());
if (SqlGeneratorFactory.getInstance().supports(remarksStatement, database)) {
if (!(database instanceof MySQLDatabase)) {
//don't re-add the comments with mysql because mysql messes with the column definition
sql.add(remarksStatement);
}
}
}
}
}
return sql.toArray(new SqlStatement[sql.size()]);
}
How can I test if my code has the behavior that I intend it to have?