add GaoPackage

This commit is contained in:
Timi
2026-09-01 01:16:52 +08:00
parent b4b6a13f8d
commit eeb7b5b32e
24 changed files with 272 additions and 49 deletions
@@ -0,0 +1,76 @@
package db.migration.timiserver;
import org.flywaydb.core.api.migration.BaseJavaMigration;
import org.flywaydb.core.api.migration.Context;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
/// 对账附件名称字段长度
///
/// 生产环境可能已经紧急扩容,达到目标长度时不重复执行 DDL
public class V50__reconcile_attachment_name_length extends BaseJavaMigration {
private static final int NAME_LENGTH = 255;
@Override
public void migrate(Context context) throws Exception {
Connection connection = context.getConnection();
int attachmentNameLength = queryCurrentLength(connection, "attachment");
int variantNameLength = queryCurrentLength(connection, "attachment_variant");
reconcile(connection, "attachment", attachmentNameLength);
reconcile(connection, "attachment_variant", variantNameLength);
}
/// 将名称字段扩容到目标长度
///
/// @param connection Flyway 数据库连接
/// @param tableName 表名
/// @param currentLength 当前字符长度
/// @throws SQLException DDL 执行失败
private void reconcile(Connection connection, String tableName, int currentLength) throws SQLException {
if (NAME_LENGTH <= currentLength) {
return;
}
String sql = """
ALTER TABLE `%s`
MODIFY COLUMN `name` VARCHAR(%d) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci NULL
""".formatted(tableName, NAME_LENGTH);
try (Statement statement = connection.createStatement()) {
statement.executeUpdate(sql);
}
}
/// 查询名称字段当前长度
///
/// @param connection Flyway 数据库连接
/// @param tableName 表名
/// @return 当前字符长度
/// @throws SQLException 表或字段不存在、字段类型不正确或查询失败
private int queryCurrentLength(Connection connection, String tableName) throws SQLException {
String sql = """
SELECT `DATA_TYPE`, `CHARACTER_MAXIMUM_LENGTH`
FROM `INFORMATION_SCHEMA`.`COLUMNS`
WHERE `TABLE_SCHEMA` = DATABASE()
AND `TABLE_NAME` = ?
AND `COLUMN_NAME` = 'name'
""";
try (PreparedStatement statement = connection.prepareStatement(sql)) {
statement.setString(1, tableName);
try (ResultSet resultSet = statement.executeQuery()) {
if (!resultSet.next()) {
throw new SQLException("表 %s 的 name 字段不存在".formatted(tableName));
}
String dataType = resultSet.getString("DATA_TYPE");
if (!"varchar".equalsIgnoreCase(dataType)) {
throw new SQLException("表 %s 的 name 字段类型不是 VARCHAR,而是 %s".formatted(tableName, dataType));
}
return resultSet.getInt("CHARACTER_MAXIMUM_LENGTH");
}
}
}
}