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-comparison): integrate structural comparison into the service layer #1473

Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
fb17bca
structure comparison access to flowable
PeachThinking Jan 26, 2024
bcaf35c
fix oboracle several comparison bugs
PeachThinking Jan 29, 2024
bd94aa5
Merge branch 'dev/4.2.x' into feat/jingtian_424_structure_compare_acc…
PeachThinking Jan 29, 2024
3e74835
fix ut test
PeachThinking Jan 29, 2024
2c859f9
Fixed several issues
PeachThinking Jan 30, 2024
77a5142
put drop constraint\index\partition statement in comment
PeachThinking Jan 31, 2024
02bca92
Fix the bug of unequal judgment constraints due to inconsistent colum…
PeachThinking Feb 1, 2024
1581e5e
put drop table ddl in comment
PeachThinking Feb 1, 2024
b99ffe1
fix null in change script
PeachThinking Feb 1, 2024
b5f2bf1
fix npe
PeachThinking Feb 1, 2024
51da3a0
Merge branch 'dev/4.2.x' into feat/jingtian_424_structure_compare_acc…
PeachThinking Feb 4, 2024
494ba29
Optimize query for all table objects under a schema
PeachThinking Feb 5, 2024
9e9a9c6
fix ut
PeachThinking Feb 5, 2024
6468bdf
rename method
PeachThinking Feb 5, 2024
198c7bf
Merge branch 'dev/4.2.x' into feat/jingtian_424_structure_compare_acc…
PeachThinking Feb 5, 2024
277ed84
delete structure_comparison_task.total_change_sql_script
PeachThinking Feb 5, 2024
ae70c13
add getTables method in DBSchemaAccessor
PeachThinking Feb 5, 2024
d1290a9
Merge branch 'dev/4.2.x' into feat/jingtian_424_structure_compare_acc…
PeachThinking Feb 5, 2024
a8f4128
adapt for doris
PeachThinking Feb 5, 2024
6ab5a40
delete useless ut script
PeachThinking Feb 6, 2024
62a1026
response to review
PeachThinking Feb 6, 2024
d3cebef
fix npe
PeachThinking Feb 6, 2024
bac18ac
response to review
PeachThinking Feb 7, 2024
2232d63
response to reveiw
PeachThinking Feb 7, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ protected void appendExpression(DBTablePartition partition, SqlBuilder sqlBuilde

@Override
public String generateCreateDefinitionDDL(@NotNull DBTablePartition partition) {
if (partition.getPartitionOption().getType() == DBTablePartitionType.NOT_PARTITIONED) {
if (partition.getPartitionOption().getType() == null
|| partition.getPartitionOption().getType() == DBTablePartitionType.NOT_PARTITIONED) {
return StringUtils.EMPTY;
}
SqlBuilder sqlBuilder = sqlBuilder();
Expand All @@ -79,6 +80,10 @@ public String generateCreateDefinitionDDL(@NotNull DBTablePartition partition) {
if (Objects.nonNull(partition.getPartitionOption().getPartitionsNum())) {
sqlBuilder.append("\nPARTITIONS ")
.append(String.valueOf(partition.getPartitionOption().getPartitionsNum()));
if (partition.getPartitionOption().getType() == DBTablePartitionType.HASH
|| partition.getPartitionOption().getType() == DBTablePartitionType.KEY) {
return sqlBuilder.toString();
}
}
appendDefinitions(partition, sqlBuilder);
return sqlBuilder.toString();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
@Data
@EqualsAndHashCode(
exclude = {"name", "warning", "owner", "schemaName", "tableName", "createTime", "updateTime",
"ordinalPosition"})
"ordinalPosition", "ddl"})
public class DBTableIndex implements DBObject, DBObjectWarningDescriptor {
/**
* 所属 schemaName
Expand Down Expand Up @@ -95,6 +95,10 @@ public class DBTableIndex implements DBObject, DBObjectWarningDescriptor {
private Long keyBlockSize;

private String warning;
/**
* Oracle special
*/
private String ddl;

@Override
public String name() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
import com.oceanbase.tools.dbbrowser.model.DBSequence;
import com.oceanbase.tools.dbbrowser.model.DBSynonym;
import com.oceanbase.tools.dbbrowser.model.DBSynonymType;
import com.oceanbase.tools.dbbrowser.model.DBTable;
import com.oceanbase.tools.dbbrowser.model.DBTable.DBTableOptions;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn;
import com.oceanbase.tools.dbbrowser.model.DBTableConstraint;
Expand Down Expand Up @@ -243,4 +244,6 @@ default List<String> showTables(String schemaName) {
DBSequence getSequence(String schemaName, String sequenceName);

DBSynonym getSynonym(String schemaName, String synonymName, DBSynonymType synonymType);

Map<String, DBTable> getTables(String schemaName, List<String> tableNames);
}
Original file line number Diff line number Diff line change
Expand Up @@ -1210,4 +1210,9 @@ public DBSequence getSequence(String schemaName, String sequenceName) {
public DBSynonym getSynonym(String schemaName, String synonymName, DBSynonymType synonymType) {
throw new UnsupportedOperationException("Not supported yet");
}

@Override
public Map<String, DBTable> getTables(String schemaName, List<String> tableNames) {
throw new UnsupportedOperationException("Not supported yet");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@
import com.oceanbase.tools.dbbrowser.model.DBSequence;
import com.oceanbase.tools.dbbrowser.model.DBSynonym;
import com.oceanbase.tools.dbbrowser.model.DBSynonymType;
import com.oceanbase.tools.dbbrowser.model.DBTable;
import com.oceanbase.tools.dbbrowser.model.DBTable.DBTableOptions;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn.KeyType;
Expand Down Expand Up @@ -1254,4 +1255,35 @@ public DBSequence getSequence(String schemaName, String sequenceName) {
public DBSynonym getSynonym(String schemaName, String synonymName, DBSynonymType synonymType) {
throw new UnsupportedOperationException("Not supported yet");
}

@Override
public Map<String, DBTable> getTables(@NonNull String schemaName, List<String> tableNames) {
// TODO: Only query the table information of tableNames passed upstream
Map<String, DBTable> returnVal = new HashMap<>();
tableNames = showTables(schemaName);
if (tableNames.isEmpty()) {
return returnVal;
}
Map<String, List<DBTableColumn>> tableName2Columns = listTableColumns(schemaName);
Map<String, List<DBTableIndex>> tableName2Indexes = listTableIndexes(schemaName);
Map<String, List<DBTableConstraint>> tableName2Constraints = listTableConstraints(schemaName);
Map<String, DBTableOptions> tableName2Options = listTableOptions(schemaName);
for (String tableName : tableNames) {
if (!tableName2Columns.containsKey(tableName)) {
continue;
}
DBTable table = new DBTable();
table.setSchemaName(schemaName);
table.setOwner(schemaName);
table.setName(tableName);
table.setColumns(tableName2Columns.getOrDefault(tableName, new ArrayList<>()));
table.setIndexes(tableName2Indexes.getOrDefault(tableName, new ArrayList<>()));
table.setConstraints(tableName2Constraints.getOrDefault(tableName, new ArrayList<>()));
table.setTableOptions(tableName2Options.getOrDefault(tableName, new DBTableOptions()));
table.setPartition(getPartition(schemaName, tableName));
table.setDDL(getTableDDL(schemaName, tableName));
returnVal.put(tableName, table);
}
return returnVal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

import org.springframework.jdbc.core.JdbcOperations;
Expand Down Expand Up @@ -88,7 +89,9 @@ public DBDatabase getDatabase(String schemaName) {
protected void fillIndexRange(List<DBTableIndex> indexList, String schemaName,
String tableName) {
setIndexRangeByDDL(indexList, schemaName, tableName);
setIndexRangeByQuery(indexList, schemaName, tableName);
if (indexList.stream().anyMatch(idx -> Objects.isNull(idx.getGlobal()))) {
setIndexRangeByQuery(indexList, schemaName, tableName);
}
}

protected void setIndexRangeByQuery(List<DBTableIndex> indexList, String schemaName, String tableName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.oceanbase.tools.dbbrowser.schema.mysql;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
Expand All @@ -29,7 +31,10 @@
import com.oceanbase.tools.dbbrowser.model.DBObjectIdentity;
import com.oceanbase.tools.dbbrowser.model.DBObjectType;
import com.oceanbase.tools.dbbrowser.model.DBObjectWarningDescriptor;
import com.oceanbase.tools.dbbrowser.model.DBTable;
import com.oceanbase.tools.dbbrowser.model.DBTable.DBTableOptions;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn;
import com.oceanbase.tools.dbbrowser.model.DBTableConstraint;
import com.oceanbase.tools.dbbrowser.model.DBTableIndex;
import com.oceanbase.tools.dbbrowser.parser.SqlParser;
import com.oceanbase.tools.dbbrowser.parser.result.ParseSqlResult;
Expand All @@ -39,6 +44,7 @@
import com.oceanbase.tools.dbbrowser.util.MySQLSqlBuilder;
import com.oceanbase.tools.dbbrowser.util.StringUtils;

import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;

/**
Expand Down Expand Up @@ -215,6 +221,18 @@ public Map<String, List<DBTableIndex>> listTableIndexes(String schemaName) {
return tableName2Indexes;
}

public Map<String, List<DBTableIndex>> listTableIndexes(String schemaName, Map<String, String> tableName2Ddl) {
Map<String, List<DBTableIndex>> tableName2Indexes = super.listTableIndexes(schemaName);
tableName2Indexes.keySet().forEach(tableName -> {
if (tableName2Ddl.containsKey(tableName)) {
parseDdlToSetIndexRange(tableName2Ddl.get(tableName), tableName2Indexes.get(tableName));
} else {
fillIndexRange(tableName2Indexes.get(tableName), schemaName, tableName);
}
});
return tableName2Indexes;
}

@Override
protected boolean isIndexDistinguishesVisibility() {
return true;
Expand All @@ -236,23 +254,31 @@ protected void setIndexRangeByDDL(List<DBTableIndex> indexList, String schemaNam
if (CollectionUtils.isEmpty(ddl) || StringUtils.isBlank(ddl.get(0))) {
fillWarning(indexList, DBObjectType.INDEX, "get index DDL failed");
} else {
ParseSqlResult result = SqlParser.parseMysql(ddl.get(0));
if (CollectionUtils.isEmpty(result.getIndexes())) {
fillWarning(indexList, DBObjectType.INDEX, "parse index DDL failed");
} else {
indexList.forEach(index -> result.getIndexes().forEach(dbIndex -> {
if (StringUtils.equals(index.getName(), dbIndex.getName())) {
index.setGlobal("GLOBAL".equalsIgnoreCase(dbIndex.getRange().name()));
}
}));
}
parseDdlToSetIndexRange(ddl.get(0), indexList);
}
} catch (Exception e) {
fillWarning(indexList, DBObjectType.INDEX, "query index ddl failed");
log.warn("Fetch table index through ddl parsing failed", e);
}
}

private void parseDdlToSetIndexRange(String ddl, List<DBTableIndex> indexList) {
if (StringUtils.isBlank(ddl)) {
fillWarning(indexList, DBObjectType.INDEX, "table ddl is blank, can not set index range by parse ddl");
return;
}
ParseSqlResult result = SqlParser.parseMysql(ddl);
if (CollectionUtils.isEmpty(result.getIndexes())) {
fillWarning(indexList, DBObjectType.INDEX, "parse index DDL failed");
} else {
indexList.forEach(index -> result.getIndexes().forEach(dbIndex -> {
if (StringUtils.equals(index.getName(), dbIndex.getName())) {
index.setGlobal("GLOBAL".equalsIgnoreCase(dbIndex.getRange().name()));
}
}));
}
}

protected <T extends DBObjectWarningDescriptor> void fillWarning(List<T> warningDescriptor, DBObjectType type,
String reason) {
if (CollectionUtils.isEmpty(warningDescriptor)) {
Expand All @@ -275,4 +301,41 @@ public List<DBObjectIdentity> listSequences(String schemaName) {
Collectors.toList());
}

@Override
public Map<String, DBTable> getTables(@NonNull String schemaName, List<String> tableNames) {
// TODO: Only query the table information of tableNames passed upstream
Map<String, DBTable> returnVal = new HashMap<>();
tableNames = showTables(schemaName);
if (tableNames.isEmpty()) {
return returnVal;
}
Map<String, String> tableName2Ddl = new HashMap<>();
tableNames.stream()
.forEach(tableName -> tableName2Ddl.put(tableName, getTableDDL(schemaName, tableName)));
Map<String, List<DBTableColumn>> tableName2Columns = listTableColumns(schemaName);
Map<String, List<DBTableIndex>> tableName2Indexes = listTableIndexes(schemaName, tableName2Ddl);
Map<String, List<DBTableConstraint>> tableName2Constraints = listTableConstraints(schemaName);
Map<String, DBTableOptions> tableName2Options = listTableOptions(schemaName);
for (String tableName : tableNames) {
if (!tableName2Columns.containsKey(tableName)) {
continue;
}
DBTable table = new DBTable();
table.setSchemaName(schemaName);
table.setOwner(schemaName);
table.setName(tableName);
table.setColumns(tableName2Columns.getOrDefault(tableName, new ArrayList<>()));
table.setIndexes(tableName2Indexes.getOrDefault(tableName, new ArrayList<>()));
table.setConstraints(tableName2Constraints.getOrDefault(tableName, new ArrayList<>()));
table.setTableOptions(tableName2Options.getOrDefault(tableName, new DBTableOptions()));
try {
table.setPartition(getPartition(schemaName, tableName));
} catch (Exception e) {
log.warn("Failed to set table partition", e);
}
table.setDDL(tableName2Ddl.get(tableName));
returnVal.put(tableName, table);
}
return returnVal;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public DBTablePartition getPartition(String schemaName, String tableName) {
partition.setPartitionDefinitions(new ArrayList<>());
partition.setSchemaName(schemaName);
partition.setTableName(tableName);
partition.getPartitionOption().setType(DBTablePartitionType.NOT_PARTITIONED);

String sql = this.sqlMapper.getSql(Statements.GET_TABLE_PARTITION);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.sql.ResultSetMetaData;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -49,9 +50,11 @@
import com.oceanbase.tools.dbbrowser.model.DBSequence;
import com.oceanbase.tools.dbbrowser.model.DBSynonym;
import com.oceanbase.tools.dbbrowser.model.DBSynonymType;
import com.oceanbase.tools.dbbrowser.model.DBTable;
import com.oceanbase.tools.dbbrowser.model.DBTable.DBTableOptions;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn;
import com.oceanbase.tools.dbbrowser.model.DBTableColumn.CharUnit;
import com.oceanbase.tools.dbbrowser.model.DBTableConstraint;
import com.oceanbase.tools.dbbrowser.model.DBTableIndex;
import com.oceanbase.tools.dbbrowser.model.DBTrigger;
import com.oceanbase.tools.dbbrowser.model.DBType;
Expand Down Expand Up @@ -222,6 +225,9 @@ protected void fillIndexRange(List<DBTableIndex> indexList) {
DBSchemaAccessorUtil.fillWarning(index, index.type(), "parse index DDL failed");
index.setGlobal(true);
} else {
if (index.getType() != DBIndexType.UNIQUE) {
index.setDdl(indexDdl);
}
// we get one single create index statement for each table index
// so here we should only get one index object from this statement
index.setGlobal("GLOBAL".equalsIgnoreCase(result.getIndexes().get(0).getRange().name()));
Expand Down Expand Up @@ -960,4 +966,36 @@ protected String getSynonymOwnerSymbol(DBSynonymType synonymType, String schemaN
}
}

@Override
public Map<String, DBTable> getTables(@NonNull String schemaName, List<String> tableNames) {
// TODO: Only query the table information of tableNames passed upstream
Map<String, DBTable> returnVal = new HashMap<>();
tableNames = showTables(schemaName);
if (tableNames.isEmpty()) {
return returnVal;
}
Map<String, List<DBTableColumn>> tableName2Columns = listTableColumns(schemaName);
Map<String, List<DBTableIndex>> tableName2Indexes = listTableIndexes(schemaName);
Map<String, List<DBTableConstraint>> tableName2Constraints = listTableConstraints(schemaName);
Map<String, DBTableOptions> tableName2Options = listTableOptions(schemaName);
for (String tableName : tableNames) {
if (!tableName2Columns.containsKey(tableName)) {
continue;
}
DBTable table = new DBTable();
table.setSchemaName(schemaName);
table.setOwner(schemaName);
table.setName(tableName);
List<DBTableColumn> columns = tableName2Columns.getOrDefault(tableName, new ArrayList<>());
List<DBTableIndex> indexes = tableName2Indexes.getOrDefault(tableName, new ArrayList<>());
table.setColumns(columns);
table.setIndexes(indexes);
table.setConstraints(tableName2Constraints.getOrDefault(tableName, new ArrayList<>()));
table.setTableOptions(tableName2Options.getOrDefault(tableName, new DBTableOptions()));
table.setPartition(getPartition(schemaName, tableName));
table.setDDL(getTableDDL(schemaName, tableName, columns, indexes));
returnVal.put(tableName, table);
}
return returnVal;
}
}
Loading
Loading