Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(structure-comapre): implement database object structure comparison #1364

Merged
merged 5 commits into from
Jan 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion libs/db-browser/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>com.oceanbase</groupId>
<artifactId>db-browser</artifactId>
<version>1.0.4</version>
<version>1.0.5</version>
<name>db-browser</name>
<url>https://github.com/oceanbase/odc/tree/main/libs/db-browser</url>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ public String generateUpdateObjectDDLWithoutRenaming(@NotNull DBTable oldTable,
return sqlBuilder.toString();
}

protected abstract void generateUpdateTableOptionDDL(DBTable oldTable, DBTable newTable, SqlBuilder sqlBuilder);
public abstract void generateUpdateTableOptionDDL(DBTable oldTable, DBTable newTable, SqlBuilder sqlBuilder);

protected abstract SqlBuilder sqlBuilder();

Expand All @@ -199,7 +199,7 @@ protected String getFullyQualifiedTableName(@NotNull DBTable table) {
* 排除唯一性约束</br>
* 创建一个唯一索引,OB 会自动创建一个同名唯一约束;因此在生成 DDL 时,如果已有唯一索引,则需要忽略掉同名唯一约束,不然生成的 DDL 会无法执行</br>
*/
protected List<DBTableConstraint> excludeUniqueConstraint(List<DBTableIndex> indexes,
public List<DBTableConstraint> excludeUniqueConstraint(List<DBTableIndex> indexes,
List<DBTableConstraint> constraints) {
if (CollectionUtils.isEmpty(indexes) || CollectionUtils.isEmpty(constraints)) {
return constraints;
Expand All @@ -219,7 +219,7 @@ protected List<DBTableConstraint> excludeUniqueConstraint(List<DBTableIndex> ind
* 排除主键约束对应的唯一索引</br>
* 创建主键约束的时候,OB 会自动创建一个同名唯一索引;因此在生成 DDL 时,如果已有主键约束,则需要忽略掉同名唯一索引,不然生成的 DDL 会无法执行</br>
*/
protected List<DBTableIndex> excludePrimaryKeyIndex(List<DBTableIndex> indexes,
public List<DBTableIndex> excludePrimaryKeyIndex(List<DBTableIndex> indexes,
List<DBTableConstraint> constraints) {
if (CollectionUtils.isEmpty(indexes) || CollectionUtils.isEmpty(constraints)) {
return indexes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -246,10 +246,10 @@ protected abstract String generateAddPartitionDefinitionDDL(@NotNull DBTablePart
protected String getFullyQualifiedTableName(@NotNull DBTablePartition partition) {
SqlBuilder sqlBuilder = sqlBuilder();
if (StringUtils.isNotEmpty(partition.getSchemaName())) {
sqlBuilder.identifier(partition.getSchemaName()).append(".");
sqlBuilder.identifier(partition.getSchemaName());
}
if (StringUtils.isNotEmpty(partition.getTableName())) {
sqlBuilder.identifier(partition.getTableName());
sqlBuilder.append(".").identifier(partition.getTableName());
}
return sqlBuilder.toString();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ protected void appendTableOptions(DBTable table, SqlBuilder sqlBuilder) {
protected void appendMoreTableOptions(DBTable table, SqlBuilder sqlBuilder) {}

@Override
protected void generateUpdateTableOptionDDL(@NonNull DBTable oldTable, @NonNull DBTable newTable,
public void generateUpdateTableOptionDDL(@NonNull DBTable oldTable, @NonNull DBTable newTable,
@NonNull SqlBuilder sqlBuilder) {
if (Objects.isNull(oldTable.getTableOptions()) || Objects.isNull(newTable.getTableOptions())) {
return;
Expand All @@ -97,6 +97,22 @@ protected void generateUpdateTableOptionDDL(@NonNull DBTable oldTable, @NonNull
.value(newTable.getTableOptions().getComment())
.append(";\n");
}
if (!StringUtils.equals(oldTable.getTableOptions().getCharsetName(),
newTable.getTableOptions().getCharsetName())) {
sqlBuilder.append("ALTER TABLE ")
.append(getFullyQualifiedTableName(newTable))
.append(" CHARACTER SET = ")
.append(newTable.getTableOptions().getCharsetName())
.append(";\n");
}
if (!StringUtils.equals(oldTable.getTableOptions().getCollationName(),
newTable.getTableOptions().getCollationName())) {
sqlBuilder.append("ALTER TABLE ")
.append(getFullyQualifiedTableName(newTable))
.append(" COLLATE = ")
.append(newTable.getTableOptions().getCollationName())
.append(";\n");
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ protected void appendTableOptions(DBTable table, SqlBuilder sqlBuilder) {
}

@Override
protected void generateUpdateTableOptionDDL(DBTable oldTable, DBTable newTable, SqlBuilder sqlBuilder) {
public void generateUpdateTableOptionDDL(DBTable oldTable, DBTable newTable, SqlBuilder sqlBuilder) {
if (!StringUtils.equals(oldTable.getTableOptions().getComment(), newTable.getTableOptions().getComment())) {
appendTableComment(newTable, sqlBuilder);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
* columnName.
*/
@Data
@EqualsAndHashCode(exclude = {"name", "warning", "schemaName", "tableName", "ordinalPosition"})
@EqualsAndHashCode(exclude = {"name", "warning", "schemaName", "tableName", "ordinalPosition", "keyType"})
public class DBTableColumn implements DBObject, DBObjectWarningDescriptor {
/**
* 所属 schemaName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@

@Data
@EqualsAndHashCode(
exclude = {"name", "warning", "schemaName", "tableName", "createTime", "updateTime", "ordinalPosition",
exclude = {"name", "warning", "schemaName", "owner", "tableName", "createTime", "updateTime", "ordinalPosition",
"enabled"})
public class DBTableConstraint implements DBObject, DBObjectWarningDescriptor {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@

@Data
@EqualsAndHashCode(
exclude = {"name", "warning", "schemaName", "tableName", "createTime", "updateTime", "ordinalPosition"})
exclude = {"name", "warning", "owner", "schemaName", "tableName", "createTime", "updateTime",
"ordinalPosition"})
public class DBTableIndex implements DBObject, DBObjectWarningDescriptor {
/**
* 所属 schemaName
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,9 @@ public DBTablePartition getPartition(String schemaName, String tableName) {
DBTablePartition partition = new DBTablePartition();
DBTablePartition subPartition = new DBTablePartition();
partition.setSubpartition(subPartition);
partition.setSchemaName(schemaName);
partition.setTableName(tableName);
subPartition.setSchemaName(schemaName);

DBTablePartitionOption partitionOption = new DBTablePartitionOption();
partitionOption.setType(DBTablePartitionType.NOT_PARTITIONED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ public DBTablePartition getPartition(String schemaName, String tableName) {
DBTablePartitionOption partitionOption = new DBTablePartitionOption();
partitionOption.setType(DBTablePartitionType.NOT_PARTITIONED);
partition.setPartitionOption(partitionOption);
partition.setSchemaName(schemaName);
partition.setTableName(tableName);
subPartition.setSchemaName(schemaName);

DBTablePartitionOption subPartitionOption = new DBTablePartitionOption();
subPartitionOption.setType(DBTablePartitionType.NOT_PARTITIONED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ public DBTablePartition getPartition(String schemaName, String tableName) {
}
DBTablePartition subPartition = new DBTablePartition();
partition.setSubpartition(subPartition);
partition.setSchemaName(schemaName);
partition.setTableName(tableName);
subPartition.setSchemaName(schemaName);

DBTablePartitionOption partitionOption = new DBTablePartitionOption();
partitionOption.setType(DBTablePartitionType.NOT_PARTITIONED);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,9 @@ public void generateUpdateObjectDDL() {
String ddl =
tableEditor.generateUpdateObjectDDL(DBObjectUtilsTest.getOldTable(), DBObjectUtilsTest.getNewTable());
Assert.assertEquals(
"ALTER TABLE `old_table` RENAME TO `whatever_table`;\n",
"ALTER TABLE `old_table` RENAME TO `whatever_table`;\n"
+ "ALTER TABLE `whatever_schema`.`whatever_table` CHARACTER SET = utf8mb4;\n"
+ "ALTER TABLE `whatever_schema`.`whatever_table` COLLATE = utf8mb4_bin;\n",
ddl);
}

Expand Down
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
<alipay.sdk.version>4.20.19.ALL</alipay.sdk.version>
<bucket4j-core.version>4.10.0</bucket4j-core.version>
<springfox-swagger-ui.version>2.10.0</springfox-swagger-ui.version>
<db-browser.version>1.0.4</db-browser.version>
<db-browser.version>1.0.5</db-browser.version>
<ob-sql-parser.version>1.2.0</ob-sql-parser.version>
<pf4j.version>3.10.0</pf4j.version>
<bcpkix-jdk15on.version>1.64</bcpkix-jdk15on.version>
Expand Down Expand Up @@ -273,6 +273,11 @@
<artifactId>schema-plugin-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>schema-plugin-ob-mysql</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>com.oceanbase</groupId>
<artifactId>task-plugin-api</artifactId>
Expand Down
Loading
Loading