Multiple Schema Migrations

Using the Atlas migration engine, an Ent schema can be defined and managed across multiple database schemas. This guides show how to achieve this with three simple steps:

Multiple Schema Migrations - 图1ATLAS LOGIN REQUIRED

The multi-schema migration feature is implemented completely in the Atlas CLI and requires login to Atlas:

  1. atlas login

Install Atlas

To install the latest release of Atlas, simply run one of the following commands in your terminal, or check out the Atlas website:

  • macOS + Linux
  • Homebrew
  • Docker
  • Windows
  1. curl -sSf https://atlasgo.sh | sh
  1. brew install ariga/tap/atlas
  1. docker pull arigaio/atlas
  2. docker run --rm arigaio/atlas --help

If the container needs access to the host network or a local directory, use the --net=host flag and mount the desired directory:

  1. docker run --rm --net=host \
  2. -v $(pwd)/migrations:/migrations \
  3. arigaio/atlas migrate apply
  4. --url "mysql://root:pass@:3306/test"

Download the latest release and move the atlas binary to a file location on your system PATH.

Login to Atlas

  1. $ atlas login a8m
  2. You are now connected to "a8m" on Atlas Cloud.

Annotate your Ent schemas

The entsql package allows annotating an Ent schema with a database schema name. For example:

  1. // Annotations of the User.
  2. func (User) Annotations() []schema.Annotation {
  3. return []schema.Annotation{
  4. entsql.Schema("db3"),
  5. }
  6. }

To share the same schema configuration across multiple Ent schemas, you can either use ent.Mixin or define and embed a base schema:

  • Mixin schema
  • Base schema

mixin.go

  1. // Mixin holds the default configuration for most schemas in this package.
  2. type Mixin struct {
  3. mixin.Schema
  4. }
  5. // Annotations of the Mixin.
  6. func (Mixin) Annotations() []schema.Annotation {
  7. return []schema.Annotation{
  8. entsql.Schema("db1"),
  9. }
  10. }

user.go

  1. // User holds the edge schema definition of the User entity.
  2. type User struct {
  3. ent.Schema
  4. }
  5. // Mixin defines the schemas that mixed into this schema.
  6. func (User) Mixin() []ent.Mixin {
  7. return []ent.Mixin{
  8. Mixin{},
  9. }
  10. }

base.go

  1. // base holds the default configuration for most schemas in this package.
  2. type base struct {
  3. ent.Schema
  4. }
  5. // Annotations of the base schema.
  6. func (base) Annotations() []schema.Annotation {
  7. return []schema.Annotation{
  8. entsql.Schema("db1"),
  9. }
  10. }

user.go

  1. // User holds the edge schema definition of the User entity.
  2. type User struct {
  3. base
  4. }

Generate migrations

To generate a migration, use the atlas migrate diff command. For example:

  • MySQL
  • MariaDB
  • PostgreSQL
  1. atlas migrate diff \
  2. --to "ent://ent/schema" \
  3. --dev-url "docker://mysql/8"
  1. atlas migrate diff \
  2. --to "ent://ent/schema" \
  3. --dev-url "docker://maria/8"
  1. atlas migrate diff \
  2. --to "ent://ent/schema" \
  3. --dev-url "docker://postgres/15/dev"

Multiple Schema Migrations - 图2note

The migrate diff command generates a list of SQL statements without indentation by default. If you would like to generate the SQL statements with indentation, use the --format flag. For example:

  1. atlas migrate diff \
  2. --to "ent://ent/schema" \
  3. --dev-url "docker://postgres/15/dev" \
  4. --format "{{ sql . \" \" }}"