danial 4c6137be57 chore(docker): 优化 Docker 配置文件
- 在 .dockerignore 中添加 test-local.sh 和 generate-migration.sh 以减少构建上下文
- 删除 Dockerfile 中重复的 chmod +x migrate.sh 命令
- 保持 ENTRYPOINT 设置为执行 migrate.sh 脚本
2025-12-13 21:35:40 +08:00

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

./generate-migration.sh

2. 本地测试

./test-local.sh

3. 生产环境部署

方式一Docker Compose推荐

# 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

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 自动计算并执行差异变更
Description
No description provided
Readme 59 KiB
Languages
HCL 57.6%
Dockerfile 26.7%
Shell 15.7%