Skip to content

Commit

Permalink
feat(db-browser): structural comparison task adaptation on ob-oracle …
Browse files Browse the repository at this point in the history
…mode in db-browser layer (#1395)

* implement get schema table index/constraints/options methods

* DBObjectEditor version-differentiated implementation for constarints\tableOptions\partition

* add check if support modify parition type

* modify parition type

* fix ddl to convert partitioned table to non-partitioned table

* add handleUpdatePartitionType for generateShadowTableUpdateObjectDDL

* optimize DBTableConstraintEditor#generateCreateObjectDDL code

* Change multi-line comments to single-line comments

* Unifiedly append  ";\n" for generateDropObjectDDL and generateCreateObjectDDL method

* append ";\n" in drop table ddl
  • Loading branch information
PeachThinking authored Jan 18, 2024
1 parent c4d9d26 commit 3965233
Show file tree
Hide file tree
Showing 95 changed files with 1,555 additions and 368 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public String generateCreateObjectDDL(@NotNull DBTableColumn column) {
appendColumnDefinition(column, sqlBuilder);
sqlBuilder.append(";").line();
generateColumnComment(column, sqlBuilder);
return sqlBuilder.toString();
String ddl = sqlBuilder.toString();
if (!ddl.trim().endsWith(";")) {
ddl += ";\n";
}
return ddl;
}

protected abstract boolean appendColumnKeyWord();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,17 @@ public boolean editable() {
@Override
public String generateCreateObjectDDL(@NotNull DBTableConstraint constraint) {
SqlBuilder sqlBuilder = sqlBuilder();
sqlBuilder.append("ALTER TABLE ").append(getFullyQualifiedTableName(constraint))
.append(" ADD CONSTRAINT ")
.identifierIf(constraint.getName(), StringUtils.isNotEmpty(constraint.getName())).space();
appendConstraintType(constraint, sqlBuilder);
sqlBuilder.append("ALTER TABLE ").append(getFullyQualifiedTableName(constraint));
if (constraint.getType() == DBConstraintType.PRIMARY_KEY && constraint.getName().isEmpty()) {
sqlBuilder.append(" ADD PRIMARY KEY ");
} else {
sqlBuilder.append(" ADD CONSTRAINT ")
.identifierIf(constraint.getName(), StringUtils.isNotEmpty(constraint.getName())).space();
appendConstraintType(constraint, sqlBuilder);
}
appendConstraintColumns(constraint, sqlBuilder);
appendConstraintOptions(constraint, sqlBuilder);
return sqlBuilder.toString().trim();
return sqlBuilder.toString().trim() + ";\n";
}

protected void appendConstraintOptions(DBTableConstraint constraint, SqlBuilder sqlBuilder) {
Expand Down Expand Up @@ -128,8 +132,8 @@ public String generateUpdateObjectDDL(@NotNull DBTableConstraint oldConstraint,
SqlBuilder sqlBuilder = sqlBuilder();
if (!Objects.equals(oldConstraint, newConstraint)) {
String drop = generateDropObjectDDL(oldConstraint);
sqlBuilder.append(drop).append(";").line()
.append(generateCreateObjectDDL(newConstraint)).append(";").line();
sqlBuilder.append(drop)
.append(generateCreateObjectDDL(newConstraint));
return sqlBuilder.toString();
}
if (!Objects.equals(oldConstraint.getEnabled(), newConstraint.getEnabled())) {
Expand All @@ -152,16 +156,14 @@ public String generateUpdateObjectListDDL(Collection<DBTableConstraint> oldConst
if (CollectionUtils.isEmpty(oldConstraints)) {
if (CollectionUtils.isNotEmpty(newConstraints)) {
newConstraints
.forEach(constraint -> sqlBuilder.append(generateCreateObjectDDL(constraint))
.append(";\n"));
.forEach(constraint -> sqlBuilder.append(generateCreateObjectDDL(constraint)));
}
return sqlBuilder.toString();
}
if (CollectionUtils.isEmpty(newConstraints)) {
if (CollectionUtils.isNotEmpty(oldConstraints)) {
oldConstraints
.forEach(constraint -> sqlBuilder.append(generateDropObjectDDL(constraint))
.append(";\n"));
.forEach(constraint -> sqlBuilder.append(generateDropObjectDDL(constraint)));
}

return sqlBuilder.toString();
Expand All @@ -179,7 +181,7 @@ public String generateUpdateObjectListDDL(Collection<DBTableConstraint> oldConst
for (DBTableConstraint newConstraint : newConstraints) {
// ordinaryPosition is NULL means this is a new constraint
if (Objects.isNull(newConstraint.getOrdinalPosition())) {
sqlBuilder.append(generateCreateObjectDDL(newConstraint)).append(";\n");
sqlBuilder.append(generateCreateObjectDDL(newConstraint));
} else if (position2OldConstraint.containsKey(newConstraint.getOrdinalPosition())) {
// this is an existing constraint
sqlBuilder.append(
Expand All @@ -190,7 +192,7 @@ public String generateUpdateObjectListDDL(Collection<DBTableConstraint> oldConst
for (DBTableConstraint oldConstraint : oldConstraints) {
// means this constraint should be dropped
if (!position2NewConstraint.containsKey(oldConstraint.getOrdinalPosition())) {
sqlBuilder.append(generateDropObjectDDL(oldConstraint)).append(";").line();
sqlBuilder.append(generateDropObjectDDL(oldConstraint));
}
}
return sqlBuilder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public String generateCreateObjectDDL(@NotNull DBTableIndex index) {
appendIndexColumns(index, sqlBuilder);
sqlBuilder.append(")");
appendIndexOptions(index, sqlBuilder);
return sqlBuilder.toString().trim();
return sqlBuilder.toString().trim() + ";\n";
}

@Override
Expand Down Expand Up @@ -98,8 +98,8 @@ public String generateUpdateObjectDDL(@NotNull DBTableIndex oldIndex, @NotNull D
SqlBuilder sqlBuilder = sqlBuilder();
if (!Objects.equals(oldIndex, newIndex)) {
String drop = generateDropObjectDDL(oldIndex);
sqlBuilder.append(drop).append(";").line()
.append(generateCreateObjectDDL(newIndex)).append(";").line();
sqlBuilder.append(drop)
.append(generateCreateObjectDDL(newIndex));
return sqlBuilder.toString();
}
if (!StringUtils.equals(oldIndex.getName(), newIndex.getName())) {
Expand All @@ -114,13 +114,13 @@ public String generateUpdateObjectListDDL(Collection<DBTableIndex> oldIndexes,
SqlBuilder sqlBuilder = sqlBuilder();
if (CollectionUtils.isEmpty(oldIndexes)) {
if (CollectionUtils.isNotEmpty(newIndexes)) {
newIndexes.forEach(column -> sqlBuilder.append(generateCreateObjectDDL(column)).append(";\n"));
newIndexes.forEach(column -> sqlBuilder.append(generateCreateObjectDDL(column)));
}
return sqlBuilder.toString();
}
if (CollectionUtils.isEmpty(newIndexes)) {
if (CollectionUtils.isNotEmpty(oldIndexes)) {
oldIndexes.forEach(column -> sqlBuilder.append(generateDropObjectDDL(column)).append(";\n"));
oldIndexes.forEach(column -> sqlBuilder.append(generateDropObjectDDL(column)));
}
return sqlBuilder.toString();
}
Expand All @@ -136,7 +136,7 @@ public String generateUpdateObjectListDDL(Collection<DBTableIndex> oldIndexes,
for (DBTableIndex newIndex : newIndexes) {
// ordinaryPosition is NULL means this is a new index
if (Objects.isNull(newIndex.getOrdinalPosition())) {
sqlBuilder.append(generateCreateObjectDDL(newIndex)).append(";\n");
sqlBuilder.append(generateCreateObjectDDL(newIndex));
} else if (position2OldIndex.containsKey(newIndex.getOrdinalPosition())) {
// this is an existing index
sqlBuilder.append(generateUpdateObjectDDL(position2OldIndex.get(newIndex.getOrdinalPosition()),
Expand All @@ -146,7 +146,7 @@ public String generateUpdateObjectListDDL(Collection<DBTableIndex> oldIndexes,
for (DBTableIndex oldIndex : oldIndexes) {
// means this index should be dropped
if (!position2NewIndex.containsKey(oldIndex.getOrdinalPosition())) {
sqlBuilder.append(generateDropObjectDDL(oldIndex)).append(";").line();
sqlBuilder.append(generateDropObjectDDL(oldIndex));
}
}
return sqlBuilder.toString();
Expand All @@ -157,7 +157,7 @@ public String generateDropObjectDDL(@NotNull DBTableIndex dbObject) {
SqlBuilder sqlBuilder = sqlBuilder();
sqlBuilder.append("ALTER TABLE ").append(getFullyQualifiedTableName(dbObject))
.append(" DROP INDEX ").identifier(dbObject.getName());
return sqlBuilder.toString();
return sqlBuilder.toString().trim() + ";\n";
}

protected abstract SqlBuilder sqlBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.Validate;

import com.oceanbase.tools.dbbrowser.model.DBTablePartition;
import com.oceanbase.tools.dbbrowser.model.DBTablePartitionDefinition;
Expand All @@ -46,17 +47,6 @@ public boolean editable() {
return false;
}

@Override
public String generateCreateObjectDDL(@NotNull DBTablePartition partition) {
if (partition.getPartitionOption().getType() == DBTablePartitionType.NOT_PARTITIONED) {
return StringUtils.EMPTY;
}
SqlBuilder sqlBuilder = sqlBuilder();
sqlBuilder.append("ALTER TABLE ").append(getFullyQualifiedTableName(partition)).space()
.append(generateCreateDefinitionDDL(partition));
return sqlBuilder.toString();
}

protected void appendExpression(DBTablePartition partition, SqlBuilder sqlBuilder) {
String expression = partition.getPartitionOption().getExpression();
List<String> columnNames = partition.getPartitionOption().getColumnNames();
Expand Down Expand Up @@ -104,6 +94,9 @@ public String generateUpdateObjectDDL(@NotNull DBTablePartition oldPartition,
if (Objects.isNull(oldPartition) || Objects.isNull(newPartition)) {
return StringUtils.EMPTY;
}
if (oldPartition.getPartitionOption().getType() != newPartition.getPartitionOption().getType()) {
return handleUpdatePartitionType(oldPartition, newPartition);
}
SqlBuilder sqlBuilder = sqlBuilder();
String fullyQualifiedTableName = getFullyQualifiedTableName(newPartition);
List<DBTablePartitionDefinition> oldDefinitions = oldPartition.getPartitionDefinitions();
Expand Down Expand Up @@ -146,6 +139,26 @@ public String generateUpdateObjectDDL(@NotNull DBTablePartition oldPartition,
return sqlBuilder.toString();
}

private String handleUpdatePartitionType(@NotNull DBTablePartition oldPartition,
@NotNull DBTablePartition newPartition) {
DBTablePartitionType oldType = oldPartition.getPartitionOption().getType();
DBTablePartitionType newType = newPartition.getPartitionOption().getType();
Validate.isTrue(oldType != newType, "Partition type should be different");
if (newType == DBTablePartitionType.NOT_PARTITIONED) {
// means convert partitioned table to non-partitioned table
return generateDropObjectDDL(oldPartition);
} else if (oldType == DBTablePartitionType.NOT_PARTITIONED) {
// means convert non-partitioned table to partitioned table
return generateCreateObjectDDL(newPartition);
} else {
// means modify table partition type
return modifyPartitionType(oldPartition, newPartition);
}
}

abstract protected String modifyPartitionType(@NotNull DBTablePartition oldPartition,
@NotNull DBTablePartition newPartition);

@Override
public String generateUpdateObjectListDDL(Collection<DBTablePartition> oldObjects,
Collection<DBTablePartition> newObjects) {
Expand All @@ -162,6 +175,9 @@ public String generateShadowTableUpdateObjectDDL(@NotNull DBTablePartition oldPa
if (Objects.isNull(oldPartition) || Objects.isNull(newPartition)) {
return StringUtils.EMPTY;
}
if (oldPartition.getPartitionOption().getType() != newPartition.getPartitionOption().getType()) {
return handleUpdatePartitionType(oldPartition, newPartition);
}
SqlBuilder sqlBuilder = sqlBuilder();
String fullyQualifiedTableName = getFullyQualifiedTableName(newPartition);
List<DBTablePartitionDefinition> oldDefinitions = oldPartition.getPartitionDefinitions();
Expand Down Expand Up @@ -218,18 +234,6 @@ public String generateRenameObjectDDL(@NotNull DBTablePartition oldObject,
return StringUtils.EMPTY;
}

@Override
public String generateDropObjectDDL(@NotNull DBTablePartition partition) {
SqlBuilder sqlBuilder = sqlBuilder();
String fullyQualifiedTableName = getFullyQualifiedTableName(partition);
if (Objects.nonNull(partition.getPartitionDefinitions())) {
partition.getPartitionDefinitions().forEach(definition -> sqlBuilder
.append(generateDropPartitionDefinitionDDL(definition, fullyQualifiedTableName)));
return sqlBuilder.toString();
}
return StringUtils.EMPTY;
}

protected String generateDropPartitionDefinitionDDL(@NotNull DBTablePartitionDefinition definition,
String fullyQualifiedTableName) {
SqlBuilder sqlBuilder = sqlBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public String generateDropObjectDDL(@NotNull DBTableConstraint constraint) {
} else {
sqlBuilder.append("KEY ").identifier(constraint.getName());
}
return sqlBuilder.toString();
return sqlBuilder.toString().trim() + ";\n";
}

@Override
Expand All @@ -72,8 +72,8 @@ public String generateRenameObjectDDL(@NotNull DBTableConstraint oldConstraint,
// 外键不支持重命名,只能删除后重建
if (newConstraint.getType() == DBConstraintType.FOREIGN_KEY) {
String drop = generateDropObjectDDL(oldConstraint);
sqlBuilder.append(drop).append(";").line()
.append(generateCreateObjectDDL(newConstraint)).append(";").line();
sqlBuilder.append(drop)
.append(generateCreateObjectDDL(newConstraint));
} else {
sqlBuilder.append("ALTER TABLE ").append(getFullyQualifiedTableName(oldConstraint))
.append(" RENAME KEY ").identifier(oldConstraint.getName()).append(" TO ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import javax.validation.constraints.NotNull;

import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import com.oceanbase.tools.dbbrowser.editor.DBTablePartitionEditor;
import com.oceanbase.tools.dbbrowser.model.DBTablePartition;
Expand All @@ -42,6 +43,26 @@ protected SqlBuilder sqlBuilder() {
return new MySQLSqlBuilder();
}

@Override
public String generateCreateObjectDDL(@NotNull DBTablePartition partition) {
if (partition.getPartitionOption().getType() == DBTablePartitionType.NOT_PARTITIONED) {
return StringUtils.EMPTY;
}
SqlBuilder sqlBuilder = sqlBuilder();
sqlBuilder.append("ALTER TABLE ").append(getFullyQualifiedTableName(partition)).space()
.append(generateCreateDefinitionDDL(partition));
return sqlBuilder.toString().trim() + ";\n";
}

@Override
public String generateDropObjectDDL(DBTablePartition dbObject) {
SqlBuilder sqlBuilder = sqlBuilder();
String fullyQualifiedTableName = getFullyQualifiedTableName(dbObject);
sqlBuilder.append("ALTER TABLE ").append(fullyQualifiedTableName).append(" REMOVE PARTITIONING").append(";")
.line();
return sqlBuilder.toString();
}

@Override
protected void appendDefinitions(DBTablePartition partition, SqlBuilder sqlBuilder) {
DBTablePartitionType type = partition.getPartitionOption().getType();
Expand Down Expand Up @@ -81,6 +102,12 @@ protected void appendDefinition(DBTablePartitionOption option,
}
}

@Override
protected String modifyPartitionType(@NotNull DBTablePartition oldPartition,
@NotNull DBTablePartition newPartition) {
return generateCreateObjectDDL(newPartition);
}

@Override
protected String generateAddPartitionDefinitionDDL(
@NotNull DBTablePartitionDefinition definition,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,27 +76,21 @@ protected void appendTableOptions(DBTable table, SqlBuilder sqlBuilder) {
if (StringUtils.isNotBlank(options.getCompressionOption())) {
sqlBuilder.append("COMPRESSION = ").append(options.getCompressionOption()).space();
}
appendMoreTableOptions(table, sqlBuilder);
appendParticularTableOptions(table, sqlBuilder);
if (StringUtils.isNotEmpty(options.getComment())) {
sqlBuilder.append("COMMENT = ").value(options.getComment()).space();
}
}

protected void appendMoreTableOptions(DBTable table, SqlBuilder sqlBuilder) {}
protected void appendParticularTableOptions(DBTable table, SqlBuilder sqlBuilder) {}

@Override
public void generateUpdateTableOptionDDL(@NonNull DBTable oldTable, @NonNull DBTable newTable,
@NonNull SqlBuilder sqlBuilder) {
if (Objects.isNull(oldTable.getTableOptions()) || Objects.isNull(newTable.getTableOptions())) {
return;
}
if (!StringUtils.equals(oldTable.getTableOptions().getComment(), newTable.getTableOptions().getComment())) {
sqlBuilder.append("ALTER TABLE ")
.append(getFullyQualifiedTableName(newTable))
.append(" COMMENT = ")
.value(newTable.getTableOptions().getComment())
.append(";\n");
}
generateUpdateTableCommentDDL(oldTable, newTable, sqlBuilder);
if (!StringUtils.equals(oldTable.getTableOptions().getCharsetName(),
newTable.getTableOptions().getCharsetName())) {
sqlBuilder.append("ALTER TABLE ")
Expand All @@ -115,6 +109,17 @@ public void generateUpdateTableOptionDDL(@NonNull DBTable oldTable, @NonNull DBT
}
}

protected void generateUpdateTableCommentDDL(@NonNull DBTable oldTable, @NonNull DBTable newTable,
@NonNull SqlBuilder sqlBuilder) {
if (!StringUtils.equals(oldTable.getTableOptions().getComment(), newTable.getTableOptions().getComment())) {
sqlBuilder.append("ALTER TABLE ")
.append(getFullyQualifiedTableName(newTable))
.append(" COMMENT = ")
.value(newTable.getTableOptions().getComment())
.append(";\n");
}
}

@Override
protected SqlBuilder sqlBuilder() {
return new MySQLSqlBuilder();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,21 +13,18 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.oceanbase.odc.service.structurecompare.util;
package com.oceanbase.tools.dbbrowser.editor.mysql;

import com.oceanbase.tools.dbbrowser.model.DBTablePartition;

/**
* @author jingtian
* @date 2024/1/9
* @date 2024/1/16
* @since ODC_release_4.2.4
*/
public class StructureCompareUtil {
private static final String DEFAULT_SQL_DELIMITER = ";";

public static String appendDelimiterIfNotExist(String sql) {
String returnVal = sql.trim();
if (!returnVal.endsWith(DEFAULT_SQL_DELIMITER)) {
return returnVal + DEFAULT_SQL_DELIMITER + "\n";
}
return sql;
public class OBMySQLDBTablePartitionEditor extends MySQLDBTablePartitionEditor {
@Override
public String generateDropObjectDDL(DBTablePartition dbObject) {
return "-- Unsupported operation to convert partitioned table to non-partitioned table\n";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
* @Date: 2022/10/10 下午9:04
* @Description: []
*/
public class OBMySQLLessThan2277PartitionEditor extends MySQLDBTablePartitionEditor {
public class OBMySQLLessThan2277PartitionEditor extends OBMySQLLessThan400DBTablePartitionEditor {

@Override
protected void appendDefinitions(DBTablePartition partition, SqlBuilder sqlBuilder) {
Expand Down
Loading

0 comments on commit 3965233

Please sign in to comment.