fix: V15迁移改为FlywayConfig直接执行SQL避免扫描问题
This commit is contained in:
@@ -6,21 +6,41 @@ import org.springframework.boot.autoconfigure.flyway.FlywayMigrationStrategy;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
import java.sql.Statement;
|
||||
|
||||
/**
|
||||
* Flyway 自动修复配置
|
||||
* 启动时自动清理残留的失败迁移记录,再执行待迁移
|
||||
* Flyway 自动修复 + V15 手工迁移
|
||||
*/
|
||||
@Slf4j
|
||||
@Configuration
|
||||
public class FlywayConfig {
|
||||
|
||||
@Bean
|
||||
public FlywayMigrationStrategy flywayMigrationStrategy() {
|
||||
public FlywayMigrationStrategy flywayMigrationStrategy(DataSource dataSource) {
|
||||
return (Flyway flyway) -> {
|
||||
log.info("Flyway repair: cleaning failed migration records...");
|
||||
flyway.repair();
|
||||
log.info("Flyway migrate: applying pending migrations...");
|
||||
flyway.migrate();
|
||||
|
||||
// V15: tags 列 + 新表(Flyway Java 迁移可能因包路径问题不触达)
|
||||
try (var conn = dataSource.getConnection(); var stmt = conn.createStatement()) {
|
||||
stmt.execute("ALTER TABLE sites ADD COLUMN IF NOT EXISTS tags JSON NULL COMMENT '标签列表' AFTER description");
|
||||
stmt.execute("CREATE TABLE IF NOT EXISTS site_pages (" +
|
||||
"id BIGINT AUTO_INCREMENT PRIMARY KEY, site_id VARCHAR(64) NOT NULL, version VARCHAR(20) NOT NULL DEFAULT '1.0.0', " +
|
||||
"entries_json LONGTEXT NOT NULL, deleted TINYINT NOT NULL DEFAULT 0, " +
|
||||
"created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, " +
|
||||
"INDEX idx_site_id (site_id, deleted)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
|
||||
stmt.execute("CREATE TABLE IF NOT EXISTS risk_rules (" +
|
||||
"id BIGINT AUTO_INCREMENT PRIMARY KEY, site_type VARCHAR(32) NOT NULL, version INT NOT NULL DEFAULT 1, " +
|
||||
"rules_json LONGTEXT NOT NULL, deleted TINYINT NOT NULL DEFAULT 0, " +
|
||||
"created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, " +
|
||||
"INDEX idx_site_type (site_type, deleted)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
|
||||
log.info("V15 tables initialized via FlywayConfig");
|
||||
} catch (Exception e) {
|
||||
log.warn("V15 migration failed (may already exist): {}", e.getMessage());
|
||||
}
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,38 +0,0 @@
|
||||
package com.par.core.db.migration;
|
||||
|
||||
import org.flywaydb.core.api.migration.BaseJavaMigration;
|
||||
import org.flywaydb.core.api.migration.Context;
|
||||
|
||||
import java.sql.Statement;
|
||||
|
||||
public class V15__AddConfigTables extends BaseJavaMigration {
|
||||
|
||||
@Override
|
||||
public void migrate(Context context) throws Exception {
|
||||
Statement stmt = context.getConnection().createStatement();
|
||||
|
||||
stmt.execute("ALTER TABLE sites ADD COLUMN IF NOT EXISTS tags JSON NULL COMMENT '标签列表' AFTER description");
|
||||
|
||||
stmt.execute("CREATE TABLE IF NOT EXISTS site_pages (" +
|
||||
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
||||
"site_id VARCHAR(64) NOT NULL, " +
|
||||
"version VARCHAR(20) NOT NULL DEFAULT '1.0.0', " +
|
||||
"entries_json LONGTEXT NOT NULL, " +
|
||||
"deleted TINYINT NOT NULL DEFAULT 0, " +
|
||||
"created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, " +
|
||||
"updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, " +
|
||||
"INDEX idx_site_id (site_id, deleted)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
|
||||
|
||||
stmt.execute("CREATE TABLE IF NOT EXISTS risk_rules (" +
|
||||
"id BIGINT AUTO_INCREMENT PRIMARY KEY, " +
|
||||
"site_type VARCHAR(32) NOT NULL, " +
|
||||
"version INT NOT NULL DEFAULT 1, " +
|
||||
"rules_json LONGTEXT NOT NULL, " +
|
||||
"deleted TINYINT NOT NULL DEFAULT 0, " +
|
||||
"created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, " +
|
||||
"updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, " +
|
||||
"INDEX idx_site_type (site_type, deleted)) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4");
|
||||
|
||||
System.out.println("[Flyway V15] tags column + site_pages + risk_rules tables created");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user