Appendix: programmatic planning

In the previous sections, we saw how to use the Atlas CLI to generate migration files. However, we can also generate these files programmatically. In this section we will review how to write Go code that can be used for automatically planning migration files.

1. Enable the versioned migration feature flag

Appendix: programmatic planning - 图1Supporting repository

The change described in this section can be found in PR #2 in the supporting repository.

The first step is to enable the versioned migration feature by passing in the sql/versioned-migration feature flag. Depending on how you execute the Ent code generator, you have to use one of the two options:

  • Using Ent CLI
  • Using the entc package

If you are using the default go generate configuration, simply add the --feature sql/versioned-migration to the ent/generate.go file as follows:

  1. package ent
  2. //go:generate go run -mod=mod entgo.io/ent/cmd/ent generate --feature sql/versioned-migration ./schema

If you are using the code generation package (e.g. if you are using an Ent extension like entgql), add the feature flag as follows:

  1. //go:build ignore
  2. package main
  3. import (
  4. "log"
  5. "entgo.io/ent/entc"
  6. "entgo.io/ent/entc/gen"
  7. )
  8. func main() {
  9. err := entc.Generate("./schema", &gen.Config{
  10. Features: []gen.Feature{gen.FeatureVersionedMigration},
  11. })
  12. if err != nil {
  13. log.Fatalf("running ent codegen: %v", err)
  14. }
  15. }

Next, re-run code-generation:

  1. go generate ./...

After running the code-generation, you should see the following methods added to ent/migrate/migrate.go:

  • Diff
  • NamedDiff

These methods are used to compare the state read from a database connection or a migration directory with the state defined by the Ent schema.

2. Automatic Migration planning script

Appendix: programmatic planning - 图2Supporting repository

The change described in this section can be found in PR #4 in the supporting repository.

Dev database

To be able to plan accurate and consistent migration files, Atlas introduces the concept of a Dev database, a temporary database which is used to simulate the state of the database after different changes. Therefore, to use Atlas to automatically plan migrations, we need to supply a connection string to such a database to our migration planning script. Such a database is most commonly spun up using a local Docker container. Let’s do this now by running the following command:

  1. docker run --rm --name atlas-db-dev -d -p 3306:3306 -e MYSQL_DATABASE=dev -e MYSQL_ROOT_PASSWORD=pass mysql:8

Using the Dev database we have just configured, we can write a script that will use Atlas to plan migration files for us. Let’s create a new file called main.go in the ent/migrate directory of our project:

ent/migrate/main.go

  1. //go:build ignore
  2. package main
  3. import (
  4. "context"
  5. "log"
  6. "os"
  7. "<project>/ent/migrate"
  8. atlas "ariga.io/atlas/sql/migrate"
  9. "entgo.io/ent/dialect"
  10. "entgo.io/ent/dialect/sql/schema"
  11. _ "github.com/go-sql-driver/mysql"
  12. )
  13. const (
  14. dir = "ent/migrate/migrations"
  15. )
  16. func main() {
  17. ctx := context.Background()
  18. // Create a local migration directory able to understand Atlas migration file format for replay.
  19. if err := os.MkdirAll(dir, 0755); err != nil {
  20. log.Fatalf("creating migration directory: %v", err)
  21. }
  22. dir, err := atlas.NewLocalDir(dir)
  23. if err != nil {
  24. log.Fatalf("failed creating atlas migration directory: %v", err)
  25. }
  26. // Migrate diff options.
  27. opts := []schema.MigrateOption{
  28. schema.WithDir(dir), // provide migration directory
  29. schema.WithMigrationMode(schema.ModeReplay), // provide migration mode
  30. schema.WithDialect(dialect.MySQL), // Ent dialect to use
  31. schema.WithFormatter(atlas.DefaultFormatter),
  32. }
  33. if len(os.Args) != 2 {
  34. log.Fatalln("migration name is required. Use: 'go run -mod=mod ent/migrate/main.go <name>'")
  35. }
  36. // Generate migrations using Atlas support for MySQL (note the Ent dialect option passed above).
  37. err = migrate.NamedDiff(ctx, "mysql://root:pass@localhost:3306/dev", os.Args[1], opts...)
  38. if err != nil {
  39. log.Fatalf("failed generating migration file: %v", err)
  40. }
  41. }

Appendix: programmatic planning - 图3info

Notice that you need to make some modifications to the script above in the highlighted lines. Edit the import path of the migrate package to match your project and to supply the connection string to your Dev database.

To run the script, first create a migrations directory in the ent/migrate directory of your project:

  1. mkdir ent/migrate/migrations

Then, run the script to create the initial migration file for your project:

  1. go run -mod=mod ent/migrate/main.go initial

Notice that initial here is just a label for the migration file. You can use any name you want.

Observe that after running the script, two new files were created in the ent/migrate/migrations directory. The first file is named atlas.sum, which is a checksum file used by Atlas to enforce a linear history of migrations:

ent/migrate/migrations/atlas.sum

  1. h1:Dt6N5dIebSto365ZEyIqiBKDqp4INvd7xijLIokqWqA=
  2. 20221114165732_initialize.sql h1:/33+7ubMlxuTkW6Ry55HeGEZQ58JqrzaAl2x1TmUTdE=

The second file is the actual migration file, which is named after the label we passed to the script:

ent/migrate/migrations/20221114165732_initial.sql

  1. -- create "users" table
  2. CREATE TABLE `users` (`id` bigint NOT NULL AUTO_INCREMENT, `name` varchar(255) NOT NULL, `email` varchar(255) NOT NULL, PRIMARY KEY (`id`), UNIQUE INDEX `email` (`email`)) CHARSET utf8mb4 COLLATE utf8mb4_bin;
  3. -- create "blogs" table
  4. CREATE TABLE `blogs` (`id` bigint NOT NULL AUTO_INCREMENT, `title` varchar(255) NOT NULL, `body` longtext NOT NULL, `created_at` timestamp NOT NULL, `user_blog_posts` bigint NULL, PRIMARY KEY (`id`), CONSTRAINT `blogs_users_blog_posts` FOREIGN KEY (`user_blog_posts`) REFERENCES `users` (`id`) ON DELETE SET NULL) CHARSET utf8mb4 COLLATE utf8mb4_bin;

Other migration tools

Atlas integrates very well with Ent, but it is not the only migration tool that can be used to manage database schemas in Ent projects. The following is a list of other migration tools that are supported:

To learn more about how to use these tools with Ent, see the docs on this subject.