Skip to content

Commit

Permalink
improve alter table interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
s7monk committed Jan 9, 2024
1 parent 9c0ebdc commit 90a5e67
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,8 @@ public class AlterTableRequest {
private TableColumn oldColumn;

private TableColumn newColumn;

private boolean moveToFirst;

private String afterColumnName;
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
Expand Down Expand Up @@ -183,7 +184,7 @@ public R<Void> alterTable(
TableColumn oldColumn = alterTableRequest.getOldColumn();
TableColumn newColumn = alterTableRequest.getNewColumn();

List<TableChange> tableChanges = createTableChanges(oldColumn, newColumn);
List<TableChange> tableChanges = createTableChanges(alterTableRequest);

if (!Objects.equals(newColumn.getField(), oldColumn.getField())) {
ColumnMetadata columnMetadata =
Expand Down Expand Up @@ -315,24 +316,51 @@ public List<TableVO> listTables(TableDTO tableDTO) {
return resultList;
}

private List<TableChange> createTableChanges(TableColumn oldColumn, TableColumn newColumn) {
private List<TableChange> createTableChanges(AlterTableRequest alterTableRequest) {
TableColumn oldColumn = alterTableRequest.getOldColumn();
TableColumn newColumn = alterTableRequest.getNewColumn();
ColumnMetadata columnMetadata =
new ColumnMetadata(
newColumn.getField(),
DataTypeConvertUtils.convert(oldColumn.getDataType()),
oldColumn.getComment());

List<TableChange> tableChanges = new ArrayList<>();

TableChange.ModifyColumnType modifyColumnType =
TableChange.modifyColumnType(
columnMetadata, DataTypeConvertUtils.convert(newColumn.getDataType()));
tableChanges.add(modifyColumnType);

TableChange.ModifyColumnComment modifyColumnComment =
TableChange.modifyColumnComment(columnMetadata, newColumn.getComment());

List<TableChange> tableChanges = new ArrayList<>();
tableChanges.add(modifyColumnType);
tableChanges.add(modifyColumnComment);

if (!Objects.equals(newColumn.getDefaultValue(), oldColumn.getDefaultValue())) {
TableChange.SetOption setOption =
TableChange.set(
FIELDS_PREFIX + "." + newColumn.getField() + "." + DEFAULT_VALUE_SUFFIX,
newColumn.getDefaultValue());
tableChanges.add(setOption);
}

if (alterTableRequest.isMoveToFirst()) {
TableChange.ModifyColumnPosition modifyColumnPosition =
TableChange.modifyColumnPosition(
columnMetadata, TableChange.ColumnPosition.first());
tableChanges.add(modifyColumnPosition);
}

if (!alterTableRequest.isMoveToFirst()
&& StringUtils.isNotBlank(alterTableRequest.getAfterColumnName())) {
TableChange.ModifyColumnPosition modifyColumnPosition =
TableChange.modifyColumnPosition(
columnMetadata,
TableChange.ColumnPosition.after(
alterTableRequest.getAfterColumnName()));
tableChanges.add(modifyColumnPosition);
}

return tableChanges;
}

Expand Down

0 comments on commit 90a5e67

Please sign in to comment.