feat: 集成Flyway数据库迁移工具
- 添加Flyway依赖到pom.xml - 配置application.yml启用Flyway - 创建迁移脚本目录db/migration - 添加V1基线脚本和V2项目字段迁移脚本 - 添加DATABASE_MIGRATION.md使用文档
This commit is contained in:
@@ -50,6 +50,27 @@ spring:
|
||||
session-stat-enable: true
|
||||
session-stat-max-count: 10
|
||||
|
||||
|
||||
# Flyway 数据库迁移配置
|
||||
flyway:
|
||||
enabled: true
|
||||
# 迁移脚本位置
|
||||
locations: classpath:db/migration
|
||||
# 版本表名
|
||||
table: flyway_schema_history
|
||||
# 基线版本号(用于已有数据库)
|
||||
baseline-version: 0
|
||||
# 如果数据库不为空,是否自动执行基线
|
||||
baseline-on-migrate: true
|
||||
# 是否校验迁移脚本
|
||||
validate-on-migrate: true
|
||||
# 编码
|
||||
encoding: UTF-8
|
||||
# 是否允许乱序执行
|
||||
out-of-order: false
|
||||
# 是否清理数据库(生产环境务必设为false)
|
||||
clean-disabled: true
|
||||
|
||||
# mybatis
|
||||
mybatis-plus:
|
||||
mapper-locations: classpath:org/springblade/**/mapper/*Mapper.xml
|
||||
|
||||
12
src/main/resources/db/migration/V1__baseline.sql
Normal file
12
src/main/resources/db/migration/V1__baseline.sql
Normal file
@@ -0,0 +1,12 @@
|
||||
-- =====================================================
|
||||
-- Flyway 基线脚本
|
||||
-- 版本: V1
|
||||
-- 描述: 记录当前数据库结构基线,不执行任何操作
|
||||
-- 日期: 2024-12-29
|
||||
-- =====================================================
|
||||
|
||||
-- 此脚本作为基线版本,用于标记已有数据库的初始状态
|
||||
-- 由于 baseline-on-migrate: true,Flyway 会自动将此版本标记为已执行
|
||||
-- 后续的迁移脚本从 V2 开始
|
||||
|
||||
SELECT 1;
|
||||
42
src/main/resources/db/migration/V2__add_project_fields.sql
Normal file
42
src/main/resources/db/migration/V2__add_project_fields.sql
Normal file
@@ -0,0 +1,42 @@
|
||||
-- =====================================================
|
||||
-- 迁移脚本: 添加项目表字段
|
||||
-- 版本: V2
|
||||
-- 描述: 为 martial_project 表添加 event_type 和报名时间字段
|
||||
-- 日期: 2024-12-29
|
||||
-- =====================================================
|
||||
|
||||
-- 添加 event_type 字段(如果不存在)
|
||||
SET @exist := (SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'martial_project'
|
||||
AND column_name = 'event_type');
|
||||
SET @sql := IF(@exist = 0,
|
||||
'ALTER TABLE martial_project ADD COLUMN event_type int DEFAULT NULL COMMENT "项目类型: 1-套路 2-散打 3-器械 4-对练" AFTER category',
|
||||
'SELECT 1');
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 添加 registration_start_time 字段(如果不存在)
|
||||
SET @exist := (SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'martial_project'
|
||||
AND column_name = 'registration_start_time');
|
||||
SET @sql := IF(@exist = 0,
|
||||
'ALTER TABLE martial_project ADD COLUMN registration_start_time datetime DEFAULT NULL COMMENT "报名开始时间" AFTER price',
|
||||
'SELECT 1');
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
|
||||
-- 添加 registration_end_time 字段(如果不存在)
|
||||
SET @exist := (SELECT COUNT(*) FROM information_schema.columns
|
||||
WHERE table_schema = DATABASE()
|
||||
AND table_name = 'martial_project'
|
||||
AND column_name = 'registration_end_time');
|
||||
SET @sql := IF(@exist = 0,
|
||||
'ALTER TABLE martial_project ADD COLUMN registration_end_time datetime DEFAULT NULL COMMENT "报名结束时间" AFTER registration_start_time',
|
||||
'SELECT 1');
|
||||
PREPARE stmt FROM @sql;
|
||||
EXECUTE stmt;
|
||||
DEALLOCATE PREPARE stmt;
|
||||
Reference in New Issue
Block a user