cs.setChangeLevel(validator.getFinalChangeLevel().name());
cs.setTableId(tid);
final TableName oldName, newName;
ChangedTableDescription desc = findByID(validator.getState().descriptions, tid);
if(desc == null) {
Table table = newAIS.getTable(tid);
oldName = newName = table.getName();
} else {
oldName = desc.getOldName();
newName = desc.getNewName();
}
cs.setOldSchema(oldName.getSchemaName());
cs.setOldName(oldName.getTableName());
cs.setNewSchema(newName.getSchemaName());
cs.setNewName(newName.getTableName());
Table oldTable = oldAIS.getTable(tid);
Table newTable = newAIS.getTable(tid);
Set<String> handledIndexes = new HashSet<>();
// Only the table being directly modified has explicit change list
if(tid == changedTableID) {
// Full column information needed to create projects for new row
for(TableChange cc : validator.getState().columnChanges) {
cs.addColumnChange(ChangeSetHelper.createFromTableChange(cc));
}
for(TableChange ic : validator.getState().tableIndexChanges) {
TableChanges.IndexChange.Builder builder = TableChanges.IndexChange.newBuilder();
builder.setIndexType(IndexType.TABLE.name());
builder.setChange(ChangeSetHelper.createFromTableChange(ic));
cs.addIndexChange(builder);
if(ic.getNewName() != null) {
handledIndexes.add(ic.getNewName());
}
}
}
Collection<TableIndex> additionalIndexes = Collections.emptyList();
if(desc != null) {
additionalIndexes = findTableIndexesToBuild(desc, oldTable, newTable);
}
for(TableIndex index : additionalIndexes) {
if(!handledIndexes.contains(index.getIndexName().getName())) {
TableChanges.IndexChange.Builder builder = TableChanges.IndexChange.newBuilder();
builder.setIndexType(index.getIndexType().name());
String name = index.getIndexName().getName();
builder.setChange(ChangeSetHelper.createModifyChange(name, name));
cs.addIndexChange(builder);
}
}
Group newGroup = newTable.getGroup();
for(String indexName : validator.getState().dataAffectedGI.keySet()) {
GroupIndex index = newGroup.getIndex(indexName);
if(newTable.getGroupIndexes().contains(index)) {
TableChanges.IndexChange.Builder builder = TableChanges.IndexChange.newBuilder();
builder.setIndexType(index.getIndexType().name());
builder.setChange(ChangeSetHelper.createModifyChange(indexName, indexName));
cs.addIndexChange(builder);
}
}
if(desc != null) {
for(TableName seqName : desc.getDroppedSequences()) {
cs.addIdentityChange(ChangeSetHelper.createDropChange(seqName.getTableName()));
}
for(String identityCol : desc.getIdentityAdded()) {
Column c = newTable.getColumn(identityCol);
assert (c != null) && (c.getIdentityGenerator() != null) : c;
cs.addIdentityChange(ChangeSetHelper.createAddChange(c.getIdentityGenerator().getSequenceName().getTableName()));
}
}