Skip to content

Commit a9072b9

Browse files
committed
fix: when index name length is too long
1 parent 22aef39 commit a9072b9

File tree

6 files changed

+21
-14
lines changed

6 files changed

+21
-14
lines changed

connectors-common/connector-core/src/main/java/io/tapdata/kit/DbKit.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,9 @@ public static String buildIndexName(String table) {
163163
}
164164

165165
public static String buildIndexName(String table, TapIndex index, int maxLength) {
166+
if (EmptyKit.isNotBlank(index.getName()) && index.getName().length() <= maxLength) {
167+
return index.getName();
168+
}
166169
String indexName = table + "_" + index.getIndexFields().stream().map(TapIndexField::getName).collect(Collectors.joining("_"));
167170
if (indexName.length() + 4 <= maxLength) {
168171
return "IDX_" + indexName;

connectors-common/mysql-core/src/main/java/io/tapdata/connector/mysql/config/MysqlConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ public MysqlConfig() {
1616
setDbType("mysql");
1717
setEscapeChar('`');
1818
setJdbcDriver("com.mysql.cj.jdbc.Driver");
19+
setMaxIndexNameLength(64);
1920
}
2021

2122
private static final Map<String, String> DEFAULT_PROPERTIES = new HashMap<String, String>() {{

connectors-common/postgres-core/src/main/java/io/tapdata/connector/postgres/config/PostgresConfig.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ public class PostgresConfig extends CommonDbConfig implements Serializable {
3232
public PostgresConfig() {
3333
setDbType("postgresql");
3434
setJdbcDriver("org.postgresql.Driver");
35+
setMaxIndexNameLength(63);
3536
}
3637

3738
@Override

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class CommonDbConfig implements Serializable {
6565
private String sslKeyPassword;
6666
protected String sslRandomPath;
6767

68+
private int maxIndexNameLength = 30;
69+
6870
//pattern for jdbc-url
6971
public String getDatabaseUrlPattern() {
7072
// last %s reserved for extend params
@@ -435,4 +437,12 @@ public String getSslRandomPath() {
435437
public void setSslRandomPath(String sslRandomPath) {
436438
this.sslRandomPath = sslRandomPath;
437439
}
440+
441+
public int getMaxIndexNameLength() {
442+
return maxIndexNameLength;
443+
}
444+
445+
public void setMaxIndexNameLength(int maxIndexNameLength) {
446+
this.maxIndexNameLength = maxIndexNameLength;
447+
}
438448
}

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -646,13 +646,9 @@ protected String getCreateIndexSql(TapTable tapTable, TapIndex tapIndex) {
646646
sb.append("unique ");
647647
}
648648
sb.append("index ");
649-
if (EmptyKit.isNotBlank(tapIndex.getName())) {
650-
sb.append(escapeChar).append(tapIndex.getName()).append(escapeChar);
651-
} else {
652-
String indexName = DbKit.buildIndexName(tapTable.getId(), tapIndex, 32);
653-
tapIndex.setName(indexName);
654-
sb.append(escapeChar).append(indexName).append(escapeChar);
655-
}
649+
String indexName = DbKit.buildIndexName(tapTable.getId(), tapIndex, commonDbConfig.getMaxIndexNameLength());
650+
tapIndex.setName(indexName);
651+
sb.append(escapeChar).append(indexName).append(escapeChar);
656652
sb.append(" on ").append(getSchemaAndTable(tapTable.getId())).append('(')
657653
.append(tapIndex.getIndexFields().stream().map(f -> escapeChar + f.getName() + escapeChar + " " + (f.getFieldAsc() ? "asc" : "desc"))
658654
.collect(Collectors.joining(","))).append(')');

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

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,13 +1017,9 @@ protected String getCreateIndexSql(TapTable tapTable, TapIndex tapIndex) {
10171017
sb.append("unique ");
10181018
}
10191019
sb.append("index ");
1020-
if (EmptyKit.isNotBlank(tapIndex.getName())) {
1021-
sb.append(escapeChar).append(tapIndex.getName()).append(escapeChar);
1022-
} else {
1023-
String indexName = DbKit.buildIndexName(tapTable.getId(), tapIndex, 64);
1024-
tapIndex.setName(indexName);
1025-
sb.append(escapeChar).append(indexName).append(escapeChar);
1026-
}
1020+
String indexName = DbKit.buildIndexName(tapTable.getId(), tapIndex, commonDbConfig.getMaxIndexNameLength());
1021+
tapIndex.setName(indexName);
1022+
sb.append(escapeChar).append(indexName).append(escapeChar);
10271023
sb.append(" on ").append(getSchemaAndTable(tapTable.getId())).append('(')
10281024
.append(tapIndex.getIndexFields().stream().map(f -> escapeChar + f.getName() + escapeChar + (EmptyKit.isNotNull(f.getSubPosition()) ? "(" + f.getSubPosition() + ")" : " ") + (f.getFieldAsc() ? "asc" : "desc"))
10291025
.collect(Collectors.joining(","))).append(')');

0 commit comments

Comments
 (0)