feat(migration): 改用版本化迁移管理数据库变更

- 将声明式 schema.sql 改为期望状态定义
- 新增 migrations/ 目录存放版本化迁移文件
- 更新 atlas.hcl 配置支持版本化迁移路径
- 修改生成迁移脚本 generate-migration.sh 实现差异迁移文件生成
- Dockerfile 支持拷贝 migrations 目录
- docker-compose.yml 调整命令,使用迁移文件夹路径
- 优化 README 文档描述版本化迁移及使用步骤
- 移除无用测试数据库docker-compose.test.yml文件
- 添加初始的 baseline 迁移 SQL 文件,包含完整表结构创建语句
This commit is contained in:
danial
2025-12-14 14:43:23 +08:00
parent c36909f7a2
commit 0287e64200
10 changed files with 1585 additions and 90 deletions

View File

@@ -1,8 +1,7 @@
*.sh *.sh
!migrate.sh
.git .git
.gitignore .gitignore
.env.local .env.local
docker-compose.yml docker-compose*.yml
test-local.sh CLAUDE.md
generate-migration.sh .github

View File

@@ -5,6 +5,7 @@ WORKDIR /app
COPY atlas.hcl . COPY atlas.hcl .
COPY schema.sql . COPY schema.sql .
COPY migrations/ ./migrations/
# 设置 ENTRYPOINT使得容器启动时默认运行 Atlas # 设置 ENTRYPOINT使得容器启动时默认运行 Atlas
ENTRYPOINT ["/atlas"] ENTRYPOINT ["/atlas"]

View File

@@ -1,30 +1,29 @@
# Atlas 数据库迁移(声明式 # Atlas 数据库迁移(版本化
## 文件说明 ## 文件说明
- `atlas.hcl` - Atlas 配置文件 - `atlas.hcl` - Atlas 配置文件
- `schema.sql` - 数据库 schema 定义(声明式 - `schema.sql` - 数据库 schema 定义(期望状态
- `migrations/` - 版本化迁移文件目录
- `.env.local` - 本地环境配置 - `.env.local` - 本地环境配置
- `.env.example` - 生产环境配置示例 - `.env.example` - 生产环境配置示例
- `generate-migration.sh` - 从本地数据库导出 schema.sql - `generate-migration.sh` - 生成新的迁移文件
- `migrate.sh` - 生产环境迁移脚本
- `test-local.sh` - 本地测试脚本
- `Dockerfile` - Docker 镜像配置 - `Dockerfile` - Docker 镜像配置
- `docker-compose.yml` - Docker Compose 配置 - `docker-compose.yml` - Docker Compose 配置
## 使用步骤 ## 使用步骤
### 1. 导出当前数据库 schema ### 1. 修改 schema.sql 定义期望状态
直接编辑 `schema.sql` 文件,定义期望的数据库结构。
### 2. 生成迁移文件
```bash ```bash
./generate-migration.sh ./generate-migration.sh
``` ```
### 2. 本地测试 此命令会对比当前数据库和 schema.sql生成新的迁移文件到 `migrations/` 目录。
```bash
./test-local.sh
```
### 3. 生产环境部署 ### 3. 生产环境部署
@@ -51,12 +50,11 @@ docker-compose up db-migrate
docker build -t db-migrate:latest . docker build -t db-migrate:latest .
docker run --rm \ docker run --rm \
-e DB_URL="mysql://user:password@host:3306/database" \ -e DB_URL="mysql://user:password@host:3306/database" \
db-migrate:latest db-migrate:latest migrate apply --url $DB_URL --dir file://migrations
``` ```
## 注意事项 ## 注意事项
1. 本地测试需修改 `.env.local` 中的 `MYSQL_CONTAINER` 为实际容器名称 1. 使用版本化迁移,每次修改 schema.sql 后需运行 `generate-migration.sh` 生成迁移文件
2. 生产环境需配置 `.env` 文件或设置环境变量 2. 迁移文件会按顺序执行,确保数据库状态可追溯
3. 使用声明式迁移schema.sql 是数据库最终状态 3. 生产环境需配置 `.env` 文件或设置环境变量
4. Atlas 自动计算并执行差异变更

View File

@@ -1,11 +1,22 @@
env "local" { env "local" {
src = "file://schema.sql" src = "file://schema.sql"
url = "mysql://root:mysql123@localhost:3306/kami" url = "mysql://root:mysql123@localhost:3306/kami"
dev = "mysql://root:mysql123@localhost:3306/atlas_dev" dev = "mysql://root:mysql123@localhost:3306/kami_dev"
migration {
dir = "file://migrations"
}
} }
// 生产环境配置 // 生产环境配置
env "prod" { env "prod" {
url = getenv("DB_URL") url = getenv("DB_URL")
src = "file://schema.sql" src = "file://schema.sql"
migration {
dir = "file://migrations"
}
// 禁用schema scoping限制
schema {
mode = "database"
}
} }

View File

@@ -1,58 +0,0 @@
version: "3.8"
services:
# 测试数据库
test-mysql:
image: mysql:8.0
environment:
MYSQL_ROOT_PASSWORD: testpass
MYSQL_DATABASE: testdb
ports:
- "13306:3306"
healthcheck:
test:
[
"CMD",
"mysqladmin",
"ping",
"-h",
"localhost",
"-u",
"root",
"-ptestpass",
]
interval: 10s
timeout: 5s
retries: 5
start_period: 30s
# 创建 testdb_dev 数据库
create-dev-db:
image: mysql:8.0
command:
[
"sh",
"-c",
"sleep 10 && mysql -h test-mysql -u root -ptestpass -e 'CREATE DATABASE IF NOT EXISTS testdb_dev;'",
]
depends_on:
test-mysql:
condition: service_healthy
# 数据库迁移服务
db-migrate:
build: .
command:
- "schema"
- "apply"
- "--url"
- "mysql://root:testpass@test-mysql:3306/testdb"
- "--to"
- "file://schema.sql"
- "--dev-url"
- "mysql://root:testpass@test-mysql:3306/testdb_dev"
- "--auto-approve"
depends_on:
create-dev-db:
condition: service_completed_successfully
restart: "no"

View File

@@ -5,12 +5,9 @@ services:
build: . build: .
restart: "no" restart: "no"
command: command:
- "schema" - "migrate"
- "apply" - "apply"
- "--url" - "--url"
- "mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}" - "mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
- "--to" - "--dir"
- "file://schema.sql" - "file://migrations"
- "--dev-url"
- "mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
- "--auto-approve"

View File

@@ -2,11 +2,11 @@
set -e set -e
echo "从本地数据库导出 schema.sql..." echo "生成新的迁移文件..."
atlas schema inspect \ atlas migrate diff \
--env local \ --env local \
--format '{{ sql . }}' > schema.sql
echo "schema.sql 生成完成!" echo "迁移文件生成完成!"
wc -l schema.sql ls -lh migrations/

0
migrations/.gitkeep Normal file
View File

File diff suppressed because it is too large Load Diff

2
migrations/atlas.sum Normal file
View File

@@ -0,0 +1,2 @@
h1:yKwtSWz2E43wQlAgD9VF1LPD2YUYBNod1107GuaiE2Q=
20251213191759_baseline.sql h1:M2vrp2p6koWVC8Y3jzvTpNxq0S7l8nepYnFuJ2BhQac=