- 添加数据库 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,提供代码库整体说明及迁移工作流程指导
2.5 KiB
2.5 KiB
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.sqlas 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
# Export current database schema to schema.sql
./generate-migration.sh
# Run local testing (creates test database and applies migrations)
./test-local.sh
Production Deployment
# 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 templateschema.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_URLenvironment variable for production database connection
Both environments use atlas_dev as the development database for Atlas operations.
Migration Process
- Schema Development: Make changes to local database
- Export Schema: Run
./generate-migration.shto updateschema.sql - Local Testing: Run
./test-local.shto verify migrations work - Production Deployment: Use Docker Compose or Docker run with production credentials
Testing
The test-local.sh script performs a complete integration test:
- Creates
atlas_devdatabase for Atlas operations - Generates current schema from local database
- Creates fresh
kami_testdatabase - Applies migrations to test database
- Verifies migration results by listing tables
Docker Configuration
- Base image:
arigaio/atlas:latest - Entry point:
/app/migrate.sh(expectsDB_URLenvironment variable) - Built with Atlas CLI and migration scripts