Atlas 是与语言无关的数据库模式管理工具,支持声明式与版本化两种工作流。声明式类似 Terraform,比较当前与目标状态自动生成迁移,例如 atlas schema apply --url "postgres://..." --to "file://schema.hcl" --dev-url "docker://postgres/15";版本化则从模式变更生成迁移文件并纳入版本控制,如 atlas migrate diff add_users --dir "file://migrations" --to "file://schema.sql" --dev-url "docker://postgres/15" 与 atlas migrate apply --dir "file://migrations" --url "postgres://..."。
模式即代码支持 HCL(推荐,按数据库使用 .pg.hcl、.my.hcl、.lt.hcl 扩展名)与标准 SQL DDL。项目通过 atlas.hcl 配置环境,可定义 src、url、dev 及 migration.dir,并用 atlas schema apply --env local --var "db_url=postgres://..." 运行。
迁移 lint 可检测破坏性变更(DS)、数据依赖变更(MF)、向后不兼容(BC)等,可在 atlas.hcl 的 lint 块配置 destructive、data_depend、naming 等规则,用 atlas migrate lint --env local --latest 1 执行,并可用 -- atlas:nolint destructive 在文件中抑制特定检查。
模式测试写在 .test.hcl 中,支持 exec、catch、assert、cleanup 及 for_each 表驱动测试,用 atlas schema test --env local schema.test.hcl 运行。事务模式可用 -- atlas:txmode none 等指令按文件控制。
常用命令还包括 atlas migrate validate、atlas migrate status、atlas migrate push、atlas schema inspect、atlas schema diff 等。CI/CD 可配合 ariga/setup-atlas 与 atlas migrate lint --env ci --git-base origin/main。已有数据库可用 baseline 迁移接入。ORM 集成通过 external_schema 数据源支持 GORM、Sequelize、TypeORM、Django、SQLAlchemy、Prisma 等。
限制与注意:migrate diff 与 schema apply 需 dev 数据库;生产建议用版本化迁移;部分功能(如并发索引检查、预执行检查)标注为 Pro,具体收费与平台支持资料未提供。