Files
kami-db-migrate/CLAUDE.md
danial db526ce3ab 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,提供代码库整体说明及迁移工作流程指导
2025-12-13 21:32:12 +08:00

74 lines
2.5 KiB
Markdown

# 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