Skip to content

Commit ded1c04

Browse files
authored
fix(sync-table): the default value is considered missing when it is whitespace (#4247)
* add log * fix * fix * fix
1 parent 587d414 commit ded1c04

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

server/odc-common/src/main/java/com/oceanbase/odc/common/util/StringUtils.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,22 @@ public static void quoteColumnDefaultValuesForMySQL(DBTable table) {
199199
}
200200
}
201201

202+
// do not ignore default value of empty string
203+
public static void quoteColumnDefaultValuesForMySQLCopied(DBTable table) {
204+
if (!CollectionUtils.isEmpty(table.getColumns())) {
205+
table.getColumns().forEach(column -> {
206+
String defaultValue = column.getDefaultValue();
207+
if (StringUtils.isNotEmpty(defaultValue)) {
208+
if (!isDefaultValueBuiltInFunction(column) && !DataTypeUtil.isBitType(column.getTypeName())) {
209+
column.setDefaultValue("'".concat(defaultValue.replace("'", "''")).concat("'"));
210+
}
211+
} else if (!column.getNullable()) {
212+
column.setDefaultValue("''");
213+
}
214+
});
215+
}
216+
}
217+
202218
/**
203219
* Check whether the data_default contain built in function. Any of the synonyms for
204220
* CURRENT_TIMESTAMP have the same meaning as CURRENT_TIMESTAMP. These are CURRENT_TIMESTAMP(),

server/odc-service/src/main/java/com/oceanbase/odc/service/dlm/DLMTableStructureSynchronizer.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import javax.sql.DataSource;
3131

3232
import com.oceanbase.odc.common.util.JdbcOperationsUtil;
33+
import com.oceanbase.odc.common.util.StringUtils;
3334
import com.oceanbase.odc.core.shared.constant.ConnectType;
3435
import com.oceanbase.odc.core.shared.constant.DialectType;
3536
import com.oceanbase.odc.service.connection.model.ConnectionConfig;
@@ -94,6 +95,8 @@ public static void sync(ConnectionConfig srcConfig, ConnectionConfig tgtConfig,
9495
Collections.singletonList(srcTableName)).get(srcTableName);
9596
DBTable tgtTable = tgtAccessor.getTables(tgtConfig.getDefaultSchema(),
9697
Collections.singletonList(tgtTableName)).get(tgtTableName);
98+
StringUtils.quoteColumnDefaultValuesForMySQLCopied(srcTable);
99+
StringUtils.quoteColumnDefaultValuesForMySQLCopied(tgtTable);
97100
DBTableStructureComparator comparator = new DBTableStructureComparator(tgtTableEditor,
98101
tgtConfig.getType().getDialectType(), srcConfig.getDefaultSchema(), tgtConfig.getDefaultSchema());
99102
List<String> changeSqlScript = new LinkedList<>();
@@ -155,8 +158,10 @@ public static void createTempTable(DataSourceInfo sourceInfo, String srcTableNam
155158
if (!tables.containsKey(tempTableName)) {
156159
DBTable srcTable = tables.get(srcTableName);
157160
srcTable.setName(tempTableName);
161+
StringUtils.quoteColumnDefaultValuesForMySQLCopied(srcTable);
158162
DBTableEditor tableEditor = getDBTableEditor(srcConfig.getType(), srcDbVersion);
159163
String createTableDdl = tableEditor.generateCreateObjectDDL(srcTable);
164+
log.info("Start to create temporary table,ddl={}", createTableDdl);
160165
try (Connection conn = sourceDs.getConnection();
161166
PreparedStatement ps = conn.prepareStatement(createTableDdl)) {
162167
ps.execute();

0 commit comments

Comments
 (0)