Skip to content

Commit

Permalink
fix: case TEXT to NTEXT
Browse files Browse the repository at this point in the history
  • Loading branch information
getrebuild committed Mar 11, 2024
1 parent ea86c42 commit 11480f8
Showing 1 changed file with 14 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/main/java/com/rebuild/core/metadata/impl/Field2Schema.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import cn.devezhao.persist4j.Field;
import cn.devezhao.persist4j.Record;
import cn.devezhao.persist4j.dialect.Dialect;
import cn.devezhao.persist4j.dialect.FieldType;
import cn.devezhao.persist4j.engine.ID;
import cn.devezhao.persist4j.metadata.CascadeModel;
import cn.devezhao.persist4j.metadata.impl.AnyEntity;
Expand All @@ -38,6 +39,7 @@
import org.apache.commons.lang.CharSet;
import org.apache.commons.lang.StringUtils;

import java.sql.DataTruncation;
import java.text.MessageFormat;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -436,6 +438,9 @@ public boolean castType(Field field, DisplayType toType, boolean force) {

Record meta = EntityHelper.forUpdate(metaRecordId, getUser(), false);
meta.setString("displayType", toType.name());
if (toType.getMaxLength() != FieldType.NO_NEED_LENGTH) {
meta.setInt("maxLength", toType.getMaxLength());
}
Application.getCommonsService().update(meta, false);

// 类型生效
Expand All @@ -452,7 +457,7 @@ public boolean castType(Field field, DisplayType toType, boolean force) {

alterTypeSql = String.format("alter table `%s` change column `%s` ",
field.getOwnEntity().getPhysicalName(), field.getPhysicalName());
alterTypeSql += ddl.toString().trim().replace(" ", "");
alterTypeSql += ddl.toString().trim().replace(" ", " ");

Application.getSqlExecutor().executeBatch(new String[]{alterTypeSql}, DDL_TIMEOUT);
log.info("Cast field type : {}", alterTypeSql);
Expand All @@ -463,7 +468,14 @@ public boolean castType(Field field, DisplayType toType, boolean force) {
Application.getCommonsService().update(meta, false);

log.error("DDL ERROR : \n" + alterTypeSql, ex);
throw new MetadataModificationException(ThrowableUtils.getRootCause(ex).getLocalizedMessage());

Throwable cause = ThrowableUtils.getRootCause(ex);
String causeMsg = cause.getLocalizedMessage();
if (cause instanceof DataTruncation) {
causeMsg = Language.L("已有数据内容长度超出限制,无法完成转换");
}
throw new MetadataModificationException(causeMsg);

} finally {
MetadataHelper.getMetadataFactory().refresh();
DynamicMetadataContextHolder.isSkipLanguageRefresh(true);
Expand Down

0 comments on commit 11480f8

Please sign in to comment.