feat(db-migrate): 初始化声明式数据库迁移项目骨架
- 添加数据库 schema 声明文件 schema.sql,包含多张表的完整结构定义 - 新增 Dockerfile,配置基于 arigaio/atlas 的迁移镜像 - 添加 migrate.sh 脚本执行生产环境迁移操作 - 创建 generate-migration.sh 脚本导出本地数据库 schema - 新建 test-local.sh 脚本用于本地迁移兼容性测试 - 配置 atlas.hcl 支持本地(local)和生产(prod)两个环境 - 添加 docker-compose.yml 支持基于环境变量的容器化迁移服务 - 新增 .drone.yml 配置自动构建并推送迁移镜像流水线 - 提供 .env.example 和 .env.local 模板方便环境变量管理 - 更新 .gitignore 和 .dockerignore 优化开发与构建忽略规则 - 编写 README.md,详细说明项目架构、使用步骤及注意事项 - 编写 CLAUDE.md,提供代码库整体说明及迁移工作流程指导
This commit is contained in:
6
.dockerignore
Normal file
6
.dockerignore
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
*.sh
|
||||||
|
!migrate.sh
|
||||||
|
.git
|
||||||
|
.gitignore
|
||||||
|
.env.local
|
||||||
|
docker-compose.yml
|
||||||
26
.drone.yml
Normal file
26
.drone.yml
Normal file
@@ -0,0 +1,26 @@
|
|||||||
|
---
|
||||||
|
kind: pipeline
|
||||||
|
type: ssh
|
||||||
|
name: master-machine
|
||||||
|
|
||||||
|
server:
|
||||||
|
host: 38.38.251.113:34156
|
||||||
|
user: root
|
||||||
|
password:
|
||||||
|
from_secret: www_password
|
||||||
|
|
||||||
|
clone:
|
||||||
|
depth: 1
|
||||||
|
|
||||||
|
steps:
|
||||||
|
- name: build new image
|
||||||
|
environment:
|
||||||
|
DOCKER_LOGIN:
|
||||||
|
from_secret: docker_login
|
||||||
|
DOCKER_TOKEN:
|
||||||
|
from_secret: docker_token
|
||||||
|
commands:
|
||||||
|
- docker login git.oceanpay.cc -u $DOCKER_LOGIN -p $DOCKER_TOKEN
|
||||||
|
- docker build -t git.oceanpay.cc/danial/kami-db-migrate${DRONE_BRANCH}:latest .
|
||||||
|
- docker push git.oceanpay.cc/danial/kami-db-migrate${DRONE_BRANCH}:latest
|
||||||
|
- docker logout git.oceanpay.cc
|
||||||
6
.env.example
Normal file
6
.env.example
Normal file
@@ -0,0 +1,6 @@
|
|||||||
|
# 生产环境数据库配置
|
||||||
|
DB_USER=root
|
||||||
|
DB_PASSWORD=your_password
|
||||||
|
DB_HOST=mysql_host
|
||||||
|
DB_PORT=3306
|
||||||
|
DB_NAME=production_db
|
||||||
9
.env.local
Normal file
9
.env.local
Normal file
@@ -0,0 +1,9 @@
|
|||||||
|
# MySQL Docker 容器名称(根据实际情况修改)
|
||||||
|
MYSQL_CONTAINER=mysql-server
|
||||||
|
|
||||||
|
# 本地数据库连接信息
|
||||||
|
DB_USER=root
|
||||||
|
DB_PASSWORD=mysql123
|
||||||
|
DB_NAME=kami
|
||||||
|
DB_HOST=localhost
|
||||||
|
DB_PORT=3306
|
||||||
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
/.idea/
|
||||||
|
/.vscode/
|
||||||
74
CLAUDE.md
Normal file
74
CLAUDE.md
Normal file
@@ -0,0 +1,74 @@
|
|||||||
|
# CLAUDE.md
|
||||||
|
|
||||||
|
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
|
||||||
|
|
||||||
|
## Overview
|
||||||
|
|
||||||
|
This is a database migration repository using Atlas for declarative schema management. The project manages MySQL database schema migrations using Atlas' declarative approach.
|
||||||
|
|
||||||
|
## Architecture
|
||||||
|
|
||||||
|
- **Declarative Schema Management**: Uses `schema.sql` as the single source of truth for database schema
|
||||||
|
- **Atlas**: Uses Atlas CLI for schema inspection and migration
|
||||||
|
- **Docker-based**: All operations run in Docker containers for consistency
|
||||||
|
- **Environment Configuration**: Supports both local development and production deployments
|
||||||
|
|
||||||
|
## Key Commands
|
||||||
|
|
||||||
|
### Local Development
|
||||||
|
```bash
|
||||||
|
# Export current database schema to schema.sql
|
||||||
|
./generate-migration.sh
|
||||||
|
|
||||||
|
# Run local testing (creates test database and applies migrations)
|
||||||
|
./test-local.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### Production Deployment
|
||||||
|
```bash
|
||||||
|
# Using Docker Compose (recommended)
|
||||||
|
cp .env.example .env
|
||||||
|
# Edit .env with production database credentials
|
||||||
|
docker-compose up db-migrate
|
||||||
|
|
||||||
|
# Using Docker directly
|
||||||
|
docker build -t db-migrate:latest .
|
||||||
|
docker run --rm -e DB_URL="mysql://user:password@host:3306/database" db-migrate:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
## Configuration Files
|
||||||
|
|
||||||
|
- `atlas.hcl`: Atlas environment configuration (local and prod)
|
||||||
|
- `.env.local`: Local development settings (includes MySQL container name)
|
||||||
|
- `.env.example`: Production environment template
|
||||||
|
- `schema.sql`: Database schema definition (generated from local database)
|
||||||
|
|
||||||
|
## Environment Setup
|
||||||
|
|
||||||
|
The project uses two Atlas environments:
|
||||||
|
- **local**: Connects to local MySQL instance at `localhost:3306/kami`
|
||||||
|
- **prod**: Uses `DB_URL` environment variable for production database connection
|
||||||
|
|
||||||
|
Both environments use `atlas_dev` as the development database for Atlas operations.
|
||||||
|
|
||||||
|
## Migration Process
|
||||||
|
|
||||||
|
1. **Schema Development**: Make changes to local database
|
||||||
|
2. **Export Schema**: Run `./generate-migration.sh` to update `schema.sql`
|
||||||
|
3. **Local Testing**: Run `./test-local.sh` to verify migrations work
|
||||||
|
4. **Production Deployment**: Use Docker Compose or Docker run with production credentials
|
||||||
|
|
||||||
|
## Testing
|
||||||
|
|
||||||
|
The `test-local.sh` script performs a complete integration test:
|
||||||
|
1. Creates `atlas_dev` database for Atlas operations
|
||||||
|
2. Generates current schema from local database
|
||||||
|
3. Creates fresh `kami_test` database
|
||||||
|
4. Applies migrations to test database
|
||||||
|
5. Verifies migration results by listing tables
|
||||||
|
|
||||||
|
## Docker Configuration
|
||||||
|
|
||||||
|
- Base image: `arigaio/atlas:latest`
|
||||||
|
- Entry point: `/app/migrate.sh` (expects `DB_URL` environment variable)
|
||||||
|
- Built with Atlas CLI and migration scripts
|
||||||
11
Dockerfile
Normal file
11
Dockerfile
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
FROM arigaio/atlas:latest
|
||||||
|
|
||||||
|
WORKDIR /app
|
||||||
|
|
||||||
|
COPY atlas.hcl .
|
||||||
|
COPY schema.sql .
|
||||||
|
COPY migrate.sh .
|
||||||
|
|
||||||
|
RUN chmod +x migrate.sh
|
||||||
|
|
||||||
|
ENTRYPOINT ["/app/migrate.sh"]
|
||||||
62
README.md
Normal file
62
README.md
Normal file
@@ -0,0 +1,62 @@
|
|||||||
|
# Atlas 数据库迁移(声明式)
|
||||||
|
|
||||||
|
## 文件说明
|
||||||
|
|
||||||
|
- `atlas.hcl` - Atlas 配置文件
|
||||||
|
- `schema.sql` - 数据库 schema 定义(声明式)
|
||||||
|
- `.env.local` - 本地环境配置
|
||||||
|
- `.env.example` - 生产环境配置示例
|
||||||
|
- `generate-migration.sh` - 从本地数据库导出 schema.sql
|
||||||
|
- `migrate.sh` - 生产环境迁移脚本
|
||||||
|
- `test-local.sh` - 本地测试脚本
|
||||||
|
- `Dockerfile` - Docker 镜像配置
|
||||||
|
- `docker-compose.yml` - Docker Compose 配置
|
||||||
|
|
||||||
|
## 使用步骤
|
||||||
|
|
||||||
|
### 1. 导出当前数据库 schema
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./generate-migration.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### 2. 本地测试
|
||||||
|
|
||||||
|
```bash
|
||||||
|
./test-local.sh
|
||||||
|
```
|
||||||
|
|
||||||
|
### 3. 生产环境部署
|
||||||
|
|
||||||
|
#### 方式一:Docker Compose(推荐)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# 1. 复制环境变量配置
|
||||||
|
cp .env.example .env
|
||||||
|
|
||||||
|
# 2. 修改 .env 文件,填入生产数据库信息
|
||||||
|
# DB_USER=root
|
||||||
|
# DB_PASSWORD=your_password
|
||||||
|
# DB_HOST=mysql_host
|
||||||
|
# DB_PORT=3306
|
||||||
|
# DB_NAME=production_db
|
||||||
|
|
||||||
|
# 3. 执行迁移
|
||||||
|
docker-compose up db-migrate
|
||||||
|
```
|
||||||
|
|
||||||
|
#### 方式二:Docker Run
|
||||||
|
|
||||||
|
```bash
|
||||||
|
docker build -t db-migrate:latest .
|
||||||
|
docker run --rm \
|
||||||
|
-e DB_URL="mysql://user:password@host:3306/database" \
|
||||||
|
db-migrate:latest
|
||||||
|
```
|
||||||
|
|
||||||
|
## 注意事项
|
||||||
|
|
||||||
|
1. 本地测试需修改 `.env.local` 中的 `MYSQL_CONTAINER` 为实际容器名称
|
||||||
|
2. 生产环境需配置 `.env` 文件或设置环境变量
|
||||||
|
3. 使用声明式迁移,schema.sql 是数据库最终状态
|
||||||
|
4. Atlas 自动计算并执行差异变更
|
||||||
11
atlas.hcl
Normal file
11
atlas.hcl
Normal file
@@ -0,0 +1,11 @@
|
|||||||
|
env "local" {
|
||||||
|
src = "file://schema.sql"
|
||||||
|
url = "mysql://root:mysql123@localhost:3306/kami"
|
||||||
|
dev = "mysql://root:mysql123@localhost:3306/atlas_dev"
|
||||||
|
}
|
||||||
|
|
||||||
|
env "prod" {
|
||||||
|
src = "file://schema.sql"
|
||||||
|
url = getenv("DB_URL")
|
||||||
|
dev = "mysql://root:mysql123@localhost:3306/atlas_dev"
|
||||||
|
}
|
||||||
10
docker-compose.yml
Normal file
10
docker-compose.yml
Normal file
@@ -0,0 +1,10 @@
|
|||||||
|
version: "3.8"
|
||||||
|
|
||||||
|
services:
|
||||||
|
# 数据库迁移服务
|
||||||
|
db-migrate:
|
||||||
|
build: .
|
||||||
|
environment:
|
||||||
|
# 从环境变量读取数据库连接信息
|
||||||
|
DB_URL: "mysql://${DB_USER}:${DB_PASSWORD}@${DB_HOST}:${DB_PORT}/${DB_NAME}"
|
||||||
|
restart: "no"
|
||||||
12
generate-migration.sh
Executable file
12
generate-migration.sh
Executable file
@@ -0,0 +1,12 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "从本地数据库导出 schema.sql..."
|
||||||
|
|
||||||
|
atlas schema inspect \
|
||||||
|
--env local \
|
||||||
|
--format '{{ sql . }}' > schema.sql
|
||||||
|
|
||||||
|
echo "schema.sql 生成完成!"
|
||||||
|
wc -l schema.sql
|
||||||
16
migrate.sh
Executable file
16
migrate.sh
Executable file
@@ -0,0 +1,16 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
echo "开始数据库迁移..."
|
||||||
|
|
||||||
|
if [ -z "$DB_URL" ]; then
|
||||||
|
echo "错误: DB_URL 环境变量未设置"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
atlas schema apply \
|
||||||
|
--env prod \
|
||||||
|
--auto-approve
|
||||||
|
|
||||||
|
echo "迁移完成!"
|
||||||
1561
schema.sql
Normal file
1561
schema.sql
Normal file
File diff suppressed because it is too large
Load Diff
44
test-local.sh
Executable file
44
test-local.sh
Executable file
@@ -0,0 +1,44 @@
|
|||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
set -e
|
||||||
|
|
||||||
|
# 加载配置
|
||||||
|
if [ -f .env.local ]; then
|
||||||
|
export $(cat .env.local | grep -v '^#' | xargs)
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "========================================="
|
||||||
|
echo "本地测试数据库迁移"
|
||||||
|
echo "使用容器: $MYSQL_CONTAINER"
|
||||||
|
echo "========================================="
|
||||||
|
|
||||||
|
# 1. 创建 atlas_dev 数据库
|
||||||
|
echo ""
|
||||||
|
echo "步骤 1: 创建 atlas_dev 数据库"
|
||||||
|
docker exec $MYSQL_CONTAINER mysql -uroot -pmysql123 -e "CREATE DATABASE IF NOT EXISTS atlas_dev;"
|
||||||
|
|
||||||
|
# 2. 生成 schema.sql
|
||||||
|
echo ""
|
||||||
|
echo "步骤 2: 生成 schema.sql"
|
||||||
|
./generate-migration.sh
|
||||||
|
|
||||||
|
# 3. 创建测试数据库
|
||||||
|
echo ""
|
||||||
|
echo "步骤 3: 创建测试数据库"
|
||||||
|
docker exec $MYSQL_CONTAINER mysql -uroot -pmysql123 -e "DROP DATABASE IF EXISTS kami_test; CREATE DATABASE kami_test;"
|
||||||
|
|
||||||
|
# 4. 测试迁移
|
||||||
|
echo ""
|
||||||
|
echo "步骤 4: 测试迁移到 kami_test"
|
||||||
|
export DB_URL="mysql://root:mysql123@localhost:3306/kami_test"
|
||||||
|
atlas schema apply --env prod --auto-approve
|
||||||
|
|
||||||
|
# 5. 验证结果
|
||||||
|
echo ""
|
||||||
|
echo "步骤 5: 验证迁移结果"
|
||||||
|
docker exec $MYSQL_CONTAINER mysql -uroot -pmysql123 kami_test -e "SHOW TABLES;"
|
||||||
|
|
||||||
|
echo ""
|
||||||
|
echo "========================================="
|
||||||
|
echo "测试完成!"
|
||||||
|
echo "========================================="
|
||||||
Reference in New Issue
Block a user