Database Migration

The migration support for ent provides the option for keeping the database schema aligned with the schema objects defined in ent/migrate/schema.go under the root of your project.

Auto Migration

Run the auto-migration logic in the initialization of the application:

  1. if err := client.Schema.Create(ctx); err != nil {
  2. log.Fatalf("failed creating schema resources: %v", err)
  3. }

Create creates all database resources needed for your ent project. By default, Create works in an “append-only” mode; which means, it only creates new tables and indexes, appends columns to tables or extends column types. For example, changing int to bigint.

What about dropping columns or indexes?

Drop Resources

WithDropIndex and WithDropColumn are 2 options for dropping table columns and indexes.

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "<project>/ent"
  6. "<project>/ent/migrate"
  7. )
  8. func main() {
  9. client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
  10. if err != nil {
  11. log.Fatalf("failed connecting to mysql: %v", err)
  12. }
  13. defer client.Close()
  14. ctx := context.Background()
  15. // Run migration.
  16. err = client.Schema.Create(
  17. ctx,
  18. migrate.WithDropIndex(true),
  19. migrate.WithDropColumn(true),
  20. )
  21. if err != nil {
  22. log.Fatalf("failed creating schema resources: %v", err)
  23. }
  24. }

In order to run the migration in debug mode (printing all SQL queries), run:

  1. err := client.Debug().Schema.Create(
  2. ctx,
  3. migrate.WithDropIndex(true),
  4. migrate.WithDropColumn(true),
  5. )
  6. if err != nil {
  7. log.Fatalf("failed creating schema resources: %v", err)
  8. }

Universal IDs

By default, SQL primary-keys start from 1 for each table; which means that multiple entities of different types can share the same ID. Unlike AWS Neptune, where node IDs are UUIDs.

This does not work well if you work with GraphQL, which requires the object ID to be unique.

To enable the Universal-IDs support for your project, pass the WithGlobalUniqueID option to the migration.

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "<project>/ent"
  6. "<project>/ent/migrate"
  7. )
  8. func main() {
  9. client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
  10. if err != nil {
  11. log.Fatalf("failed connecting to mysql: %v", err)
  12. }
  13. defer client.Close()
  14. ctx := context.Background()
  15. // Run migration.
  16. if err := client.Schema.Create(ctx, migrate.WithGlobalUniqueID(true)); err != nil {
  17. log.Fatalf("failed creating schema resources: %v", err)
  18. }
  19. }

How does it work? ent migration allocates a 1<<32 range for the IDs of each entity (table), and store this information in a table named ent_types. For example, type A will have the range of [1,4294967296) for its IDs, and type B will have the range of [4294967296,8589934592), etc.

Note that if this option is enabled, the maximum number of possible tables is 65535.

Offline Mode

Offline mode allows you to write the schema changes to an io.Writer before executing them on the database. It’s useful for verifying the SQL commands before they’re executed on the database, or to get an SQL script to run manually.

Print changes

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "os"
  6. "<project>/ent"
  7. "<project>/ent/migrate"
  8. )
  9. func main() {
  10. client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
  11. if err != nil {
  12. log.Fatalf("failed connecting to mysql: %v", err)
  13. }
  14. defer client.Close()
  15. ctx := context.Background()
  16. // Dump migration changes to stdout.
  17. if err := client.Schema.WriteTo(ctx, os.Stdout); err != nil {
  18. log.Fatalf("failed printing schema changes: %v", err)
  19. }
  20. }

Write changes to a file

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "os"
  6. "<project>/ent"
  7. "<project>/ent/migrate"
  8. )
  9. func main() {
  10. client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
  11. if err != nil {
  12. log.Fatalf("failed connecting to mysql: %v", err)
  13. }
  14. defer client.Close()
  15. ctx := context.Background()
  16. // Dump migration changes to an SQL script.
  17. f, err := os.Create("migrate.sql")
  18. if err != nil {
  19. log.Fatalf("create migrate file: %v", err)
  20. }
  21. defer f.Close()
  22. if err := client.Schema.WriteTo(ctx, f); err != nil {
  23. log.Fatalf("failed printing schema changes: %v", err)
  24. }
  25. }

Foreign Keys

By default, ent uses foreign-keys when defining relationships (edges) to enforce correctness and consistency on the database side.

However, ent also provide an option to disable this functionality using the WithForeignKeys option. You should note that setting this option to false, will tell the migration to not create foreign-keys in the schema DDL and the edges validation and clearing must be handled manually by the developer.

We expect to provide a set of hooks for implementing the foreign-key constraints in the application level in the near future.

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "<project>/ent"
  6. "<project>/ent/migrate"
  7. )
  8. func main() {
  9. client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
  10. if err != nil {
  11. log.Fatalf("failed connecting to mysql: %v", err)
  12. }
  13. defer client.Close()
  14. ctx := context.Background()
  15. // Run migration.
  16. err = client.Schema.Create(
  17. ctx,
  18. migrate.WithForeignKeys(false), // Disable foreign keys.
  19. )
  20. if err != nil {
  21. log.Fatalf("failed creating schema resources: %v", err)
  22. }
  23. }

Migration Hooks

The framework provides an option to add hooks (middlewares) to the migration phase. This option is ideal for modifying or filtering the tables that the migration is working on, or for creating custom resources in the database.

  1. package main
  2. import (
  3. "context"
  4. "log"
  5. "<project>/ent"
  6. "<project>/ent/migrate"
  7. "entgo.io/ent/dialect/sql/schema"
  8. )
  9. func main() {
  10. client, err := ent.Open("mysql", "root:pass@tcp(localhost:3306)/test")
  11. if err != nil {
  12. log.Fatalf("failed connecting to mysql: %v", err)
  13. }
  14. defer client.Close()
  15. ctx := context.Background()
  16. // Run migration.
  17. err = client.Schema.Create(
  18. ctx,
  19. schema.WithHooks(func(next schema.Creator) schema.Creator {
  20. return schema.CreateFunc(func(ctx context.Context, tables ...*schema.Table) error {
  21. // Run custom code here.
  22. return next.Create(ctx, tables...)
  23. })
  24. }),
  25. )
  26. if err != nil {
  27. log.Fatalf("failed creating schema resources: %v", err)
  28. }
  29. }