Skip to content

Commit 921d176

Browse files
authored
Merge pull request #513 from tapdata/TAP-6443-develop
TAP-6443:fix mysql no pk AUTO_INCREMENT filed no sync
2 parents ec338c7 + e6c0214 commit 921d176

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

connectors-common/sql-core/src/main/java/io/tapdata/common/CommonDbConnector.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,18 @@ protected void createIndex(TapConnectorContext connectorContext, TapTable tapTab
446446
}
447447
}
448448
});
449+
List<String> afterUniqueAutoIncrementSql =getAfterUniqueAutoIncrementFields(tapTable, indexList);
450+
if(EmptyKit.isNotEmpty(afterUniqueAutoIncrementSql)){
451+
afterUniqueAutoIncrementSql.forEach(sql -> {
452+
try {
453+
jdbcContext.execute(sql);
454+
} catch (SQLException e) {
455+
tapLogger.warn("Failed to update auto-increment column {}, please execute it manually [{}]", e.getMessage(), sql);
456+
}
457+
});
458+
}
449459
}
460+
450461
}
451462

452463
protected void createConstraint(TapConnectorContext connectorContext, TapTable tapTable, TapCreateConstraintEvent createConstraintEvent, boolean create) {
@@ -964,4 +975,8 @@ protected long countByAdvanceFilterV2(TapConnectorContext connectorContext, TapT
964975
});
965976
return count.get();
966977
}
978+
protected List<String> getAfterUniqueAutoIncrementFields(TapTable tapTable,List<TapIndex> indexList) {
979+
return new ArrayList<>();
980+
}
981+
967982
}

connectors/mysql-connector/src/main/java/io/tapdata/connector/mysql/MysqlConnector.java

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1030,4 +1030,23 @@ protected String getCreateIndexSql(TapTable tapTable, TapIndex tapIndex) {
10301030
return sb.toString();
10311031
}
10321032

1033+
protected List<String> getAfterUniqueAutoIncrementFields(TapTable tapTable,List<TapIndex> indexList) {
1034+
if(!mysqlConfig.getCreateAutoInc())return new ArrayList<>();
1035+
String sql = "ALTER TABLE `%s` MODIFY COLUMN `%s` %s AUTO_INCREMENT";
1036+
List<String> uniqueFields = new ArrayList<>();
1037+
List<String> uniqueAutoIncrementFields = new ArrayList<>();
1038+
indexList.forEach(index -> {
1039+
if(index.isUnique()){
1040+
uniqueFields.addAll(index.getIndexFields().stream().map(TapIndexField::getName).collect(Collectors.toList()));
1041+
}
1042+
});
1043+
tapTable.getNameFieldMap().values().forEach(f -> {
1044+
if(f.getAutoInc() && !f.getPrimaryKey() && uniqueFields.contains(f.getName())){
1045+
uniqueAutoIncrementFields.add(f.getName());
1046+
}
1047+
});
1048+
1049+
return uniqueAutoIncrementFields.stream().map(f -> String.format(sql, tapTable.getId(), f, tapTable.getNameFieldMap().get(f).getDataType())).collect(Collectors.toList());
1050+
}
1051+
10331052
}

0 commit comments

Comments
 (0)