This commit is contained in:
@@ -42,6 +42,9 @@ public class SetupService {
|
||||
|
||||
private static final Logger LOGGER = LoggerFactory.getLogger(SetupService.class);
|
||||
private static final String MYSQL_PREFIX = "jdbc:mysql://";
|
||||
private static final String MYSQL_CHARACTER_SET = "utf8mb4";
|
||||
private static final String MYSQL_COLLATION = "utf8mb4_0900_ai_ci";
|
||||
private static final String RECOVERABLE_V068_SCRIPT = "V068__source_table_column_validation_metadata.sql";
|
||||
private static final List<String> SUPPORTED_TYPES = List.of(DatabaseType.MYSQL.name());
|
||||
private static final Set<Integer> MYSQL_PERMISSION_ERROR_CODES = Set.of(1044, 1045, 1142, 1227, 1370);
|
||||
private static final long RESTART_TRIGGER_DELAY_MILLIS = 2_000L;
|
||||
@@ -81,9 +84,10 @@ public class SetupService {
|
||||
false, "PostgreSQL 连接可用,但当前 Preview 的业务迁移仅支持 MySQL 8.4");
|
||||
}
|
||||
ConnectionResult connection = testConnection(settings);
|
||||
prepareDatabaseForInstallation(settings);
|
||||
validateSchemaForInstallation(settings);
|
||||
return new SetupViews.Connection(true, settings.type().name(), connection.version(), true,
|
||||
"MySQL 8.4 连接和完整迁移权限验证通过");
|
||||
"MySQL 8.4 连接、排序规则和完整迁移权限验证通过");
|
||||
}
|
||||
|
||||
public SetupViews.Completed complete(SetupContracts.CompleteRequest request) {
|
||||
@@ -129,6 +133,7 @@ public class SetupService {
|
||||
|
||||
private void migrateAndCreateAdministrator(DatabaseSettings settings, SetupContracts.CompleteRequest request) {
|
||||
try {
|
||||
prepareDatabaseForInstallation(settings);
|
||||
validateSchemaForInstallation(settings);
|
||||
flyway(settings).migrate();
|
||||
DriverManagerDataSource dataSource = new DriverManagerDataSource(settings.jdbcUrl(), settings.username(),
|
||||
@@ -146,6 +151,105 @@ public class SetupService {
|
||||
}
|
||||
}
|
||||
|
||||
private void prepareDatabaseForInstallation(DatabaseSettings settings) {
|
||||
JdbcTemplate jdbc = new JdbcTemplate(new DriverManagerDataSource(settings.jdbcUrl(), settings.username(),
|
||||
settings.password()));
|
||||
try {
|
||||
DatabaseCollation current = databaseCollation(jdbc, settings.database());
|
||||
if (!MYSQL_CHARACTER_SET.equalsIgnoreCase(current.characterSet())
|
||||
|| !MYSQL_COLLATION.equalsIgnoreCase(current.collation())) {
|
||||
jdbc.execute("ALTER DATABASE " + quoteIdentifier(settings.database())
|
||||
+ " CHARACTER SET " + MYSQL_CHARACTER_SET + " COLLATE " + MYSQL_COLLATION);
|
||||
DatabaseCollation updated = databaseCollation(jdbc, settings.database());
|
||||
if (!MYSQL_CHARACTER_SET.equalsIgnoreCase(updated.characterSet())
|
||||
|| !MYSQL_COLLATION.equalsIgnoreCase(updated.collation())) {
|
||||
throw new SetupException(org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY,
|
||||
"DATABASE_COLLATION_UNSUPPORTED", "数据库字符集或排序规则未能统一为 "
|
||||
+ MYSQL_CHARACTER_SET + "/" + MYSQL_COLLATION);
|
||||
}
|
||||
LOGGER.info("Normalized database {} from {}/{} to {}/{}", settings.database(),
|
||||
current.characterSet(), current.collation(), updated.characterSet(), updated.collation());
|
||||
}
|
||||
recoverV068CollationFailure(jdbc, settings.database());
|
||||
} catch (SetupException exception) {
|
||||
throw exception;
|
||||
} catch (RuntimeException exception) {
|
||||
LOGGER.error("Database collation preparation failed", exception);
|
||||
SQLException sqlException = findSqlException(exception);
|
||||
String detail = sqlException == null
|
||||
? "数据库字符集和排序规则初始化失败:" + safeErrorMessage(exception)
|
||||
: "数据库字符集和排序规则初始化失败" + sqlErrorSummary(sqlException);
|
||||
throw new SetupException(org.springframework.http.HttpStatus.UNPROCESSABLE_ENTITY,
|
||||
"DATABASE_COLLATION_PREPARATION_FAILED", detail);
|
||||
}
|
||||
}
|
||||
|
||||
private DatabaseCollation databaseCollation(JdbcTemplate jdbc, String database) {
|
||||
return jdbc.queryForObject("""
|
||||
SELECT default_character_set_name, default_collation_name
|
||||
FROM information_schema.schemata
|
||||
WHERE schema_name = ?
|
||||
""", (result, row) -> new DatabaseCollation(
|
||||
result.getString("default_character_set_name"), result.getString("default_collation_name")),
|
||||
database);
|
||||
}
|
||||
|
||||
private void recoverV068CollationFailure(JdbcTemplate jdbc, String database) {
|
||||
Integer historyCount = jdbc.queryForObject("""
|
||||
SELECT COUNT(*)
|
||||
FROM information_schema.tables
|
||||
WHERE table_schema = ? AND table_name = 'flyway_schema_history'
|
||||
""", Integer.class, database);
|
||||
if (historyCount == null || historyCount == 0) {
|
||||
return;
|
||||
}
|
||||
List<FailedMigration> failures = jdbc.query("""
|
||||
SELECT installed_rank, version, script
|
||||
FROM flyway_schema_history
|
||||
WHERE success = FALSE
|
||||
ORDER BY installed_rank
|
||||
""", (result, row) -> new FailedMigration(result.getInt("installed_rank"),
|
||||
result.getString("version"), result.getString("script")));
|
||||
if (failures.size() != 1) {
|
||||
return;
|
||||
}
|
||||
FailedMigration failure = failures.get(0);
|
||||
if (!("68".equals(failure.version()) || "068".equals(failure.version()))
|
||||
|| !RECOVERABLE_V068_SCRIPT.equals(failure.script())) {
|
||||
return;
|
||||
}
|
||||
Integer predecessorCount = jdbc.queryForObject("""
|
||||
SELECT COUNT(*)
|
||||
FROM flyway_schema_history
|
||||
WHERE version IN ('67', '067') AND success = TRUE
|
||||
""", Integer.class);
|
||||
if (predecessorCount == null || predecessorCount != 1) {
|
||||
return;
|
||||
}
|
||||
Integer laterSuccessCount = jdbc.queryForObject("""
|
||||
SELECT COUNT(*)
|
||||
FROM flyway_schema_history
|
||||
WHERE installed_rank > ? AND success = TRUE
|
||||
""", Integer.class, failure.installedRank());
|
||||
if (laterSuccessCount != null && laterSuccessCount > 0) {
|
||||
return;
|
||||
}
|
||||
int deleted = jdbc.update("""
|
||||
DELETE FROM flyway_schema_history
|
||||
WHERE installed_rank = ? AND version = ? AND script = ? AND success = FALSE
|
||||
""", failure.installedRank(), failure.version(), failure.script());
|
||||
if (deleted != 1) {
|
||||
throw new SetupException(org.springframework.http.HttpStatus.CONFLICT,
|
||||
"DATABASE_MIGRATION_RECOVERY_CONFLICT", "V068 迁移恢复状态已变化,请重新测试数据库连接");
|
||||
}
|
||||
LOGGER.warn("Removed the recoverable failed {} history row after database collation normalization",
|
||||
RECOVERABLE_V068_SCRIPT);
|
||||
}
|
||||
|
||||
private String quoteIdentifier(String identifier) {
|
||||
return "`" + identifier.replace("`", "``") + "`";
|
||||
}
|
||||
|
||||
private boolean ensureCompatibleSchema(DatabaseSettings settings) {
|
||||
JdbcTemplate jdbc = new JdbcTemplate(new DriverManagerDataSource(settings.jdbcUrl(), settings.username(),
|
||||
settings.password()));
|
||||
@@ -580,6 +684,12 @@ public class SetupService {
|
||||
private record ConnectionResult(boolean successful, String version) {
|
||||
}
|
||||
|
||||
private record DatabaseCollation(String characterSet, String collation) {
|
||||
}
|
||||
|
||||
private record FailedMigration(int installedRank, String version, String script) {
|
||||
}
|
||||
|
||||
private record InstallationState(String setupTokenSha256, long administratorUserId,
|
||||
String administratorUsername) {
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.kaidi.finance.setup;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
|
||||
import com.fasterxml.jackson.databind.JsonNode;
|
||||
import com.kaidi.setup.SetupApplication;
|
||||
@@ -13,6 +14,7 @@ import java.sql.Statement;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import org.flywaydb.core.Flyway;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
@@ -60,6 +62,8 @@ class SetupApplicationIntegrationTest {
|
||||
|
||||
@Test
|
||||
void firstRunTestsDatabaseCreatesAdministratorAndLocksWizard() throws Exception {
|
||||
seedRecoverableV068CollationFailure();
|
||||
|
||||
ResponseEntity<JsonNode> initial = rest.getForEntity(url("/api/v1/setup/status"), JsonNode.class);
|
||||
assertThat(initial.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(initial.getBody().path("data").path("required").asBoolean()).isTrue();
|
||||
@@ -77,7 +81,8 @@ class SetupApplicationIntegrationTest {
|
||||
assertThat(tested.getStatusCode()).isEqualTo(HttpStatus.OK);
|
||||
assertThat(tested.getBody().path("data").path("successful").asBoolean()).isTrue();
|
||||
assertThat(tested.getBody().path("data").path("schemaReady").asBoolean()).isTrue();
|
||||
assertThat(tested.getBody().path("data").path("message").asText()).contains("完整迁移权限验证通过");
|
||||
assertThat(tested.getBody().path("data").path("message").asText())
|
||||
.contains("排序规则和完整迁移权限验证通过");
|
||||
|
||||
try (Connection connection = DriverManager.getConnection(MYSQL.getJdbcUrl(), MYSQL.getUsername(),
|
||||
MYSQL.getPassword()); Statement statement = connection.createStatement()) {
|
||||
@@ -92,6 +97,22 @@ class SetupApplicationIntegrationTest {
|
||||
result.next();
|
||||
assertThat(result.getInt(1)).isZero();
|
||||
}
|
||||
try (ResultSet result = statement.executeQuery("""
|
||||
SELECT default_collation_name
|
||||
FROM information_schema.schemata
|
||||
WHERE schema_name = DATABASE()
|
||||
""")) {
|
||||
result.next();
|
||||
assertThat(result.getString(1)).isEqualTo("utf8mb4_0900_ai_ci");
|
||||
}
|
||||
try (ResultSet result = statement.executeQuery("""
|
||||
SELECT COUNT(*)
|
||||
FROM flyway_schema_history
|
||||
WHERE success = FALSE
|
||||
""")) {
|
||||
result.next();
|
||||
assertThat(result.getInt(1)).isZero();
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, Object> complete = new LinkedHashMap<>(database);
|
||||
@@ -141,6 +162,33 @@ class SetupApplicationIntegrationTest {
|
||||
assertThat(locked.getBody().path("data").path("ready").asBoolean()).isFalse();
|
||||
}
|
||||
|
||||
private void seedRecoverableV068CollationFailure() throws Exception {
|
||||
try (Connection connection = DriverManager.getConnection(MYSQL.getJdbcUrl(), MYSQL.getUsername(),
|
||||
MYSQL.getPassword()); Statement statement = connection.createStatement()) {
|
||||
statement.execute("ALTER DATABASE `" + MYSQL.getDatabaseName()
|
||||
+ "` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci");
|
||||
}
|
||||
assertThatThrownBy(() -> Flyway.configure()
|
||||
.dataSource(MYSQL.getJdbcUrl(), MYSQL.getUsername(), MYSQL.getPassword())
|
||||
.locations("classpath:db/migration")
|
||||
.target("68")
|
||||
.load()
|
||||
.migrate())
|
||||
.hasMessageContaining("V068__source_table_column_validation_metadata.sql");
|
||||
try (Connection connection = DriverManager.getConnection(MYSQL.getJdbcUrl(), MYSQL.getUsername(),
|
||||
MYSQL.getPassword()); Statement statement = connection.createStatement();
|
||||
ResultSet result = statement.executeQuery("""
|
||||
SELECT COUNT(*)
|
||||
FROM flyway_schema_history
|
||||
WHERE version = '68'
|
||||
AND script = 'V068__source_table_column_validation_metadata.sql'
|
||||
AND success = FALSE
|
||||
""")) {
|
||||
result.next();
|
||||
assertThat(result.getInt(1)).isEqualTo(1);
|
||||
}
|
||||
}
|
||||
|
||||
private Map<String, Object> databaseRequest(String setupCode) {
|
||||
Map<String, Object> request = new LinkedHashMap<>();
|
||||
request.put("setupCode", setupCode);
|
||||
|
||||
Reference in New Issue
Block a user