Auto Migration

Automatically migrate your schema, to keep your schema up to date.

NOTE: AutoMigrate will create tables, missing foreign keys, constraints, columns and indexes. It will change existing column’s type if its size, precision, nullable changed. It WON’T delete unused columns to protect your data.

  1. db.AutoMigrate(&User{})
  2. db.AutoMigrate(&User{}, &Product{}, &Order{})
  3. // Add table suffix when creating tables
  4. db.Set("gorm:table_options", "ENGINE=InnoDB").AutoMigrate(&User{})

NOTE AutoMigrate creates database foreign key constraints automatically, you can disable this feature during initialization, for example:

  1. db, err := gorm.Open(sqlite.Open("gorm.db"), &gorm.Config{
  2. DisableForeignKeyConstraintWhenMigrating: true,
  3. })

Migrator Interface

GORM provides a migrator interface, which contains unified API interfaces for each database that could be used to build your database-independent migrations, for example:

SQLite doesn’t support ALTER COLUMN, DROP COLUMN, GORM will create a new table as the one you are trying to change, copy all data, drop the old table, rename the new table

MySQL doesn’t support rename column, index for some versions, GORM will perform different SQL based on the MySQL version you are using

  1. type Migrator interface {
  2. // AutoMigrate
  3. AutoMigrate(dst ...interface{}) error
  4. // Database
  5. CurrentDatabase() string
  6. FullDataTypeOf(*schema.Field) clause.Expr
  7. // Tables
  8. CreateTable(dst ...interface{}) error
  9. DropTable(dst ...interface{}) error
  10. HasTable(dst interface{}) bool
  11. RenameTable(oldName, newName interface{}) error
  12. GetTables() (tableList []string, err error)
  13. // Columns
  14. AddColumn(dst interface{}, field string) error
  15. DropColumn(dst interface{}, field string) error
  16. AlterColumn(dst interface{}, field string) error
  17. MigrateColumn(dst interface{}, field *schema.Field, columnType ColumnType) error
  18. HasColumn(dst interface{}, field string) bool
  19. RenameColumn(dst interface{}, oldName, field string) error
  20. ColumnTypes(dst interface{}) ([]ColumnType, error)
  21. // Views
  22. CreateView(name string, option ViewOption) error
  23. DropView(name string) error
  24. // Constraints
  25. CreateConstraint(dst interface{}, name string) error
  26. DropConstraint(dst interface{}, name string) error
  27. HasConstraint(dst interface{}, name string) bool
  28. // Indexes
  29. CreateIndex(dst interface{}, name string) error
  30. DropIndex(dst interface{}, name string) error
  31. HasIndex(dst interface{}, name string) bool
  32. RenameIndex(dst interface{}, oldName, newName string) error
  33. }

CurrentDatabase

Returns current using database name

  1. db.Migrator().CurrentDatabase()

Tables

  1. // Create table for `User`
  2. db.Migrator().CreateTable(&User{})
  3. // Append "ENGINE=InnoDB" to the creating table SQL for `User`
  4. db.Set("gorm:table_options", "ENGINE=InnoDB").Migrator().CreateTable(&User{})
  5. // Check table for `User` exists or not
  6. db.Migrator().HasTable(&User{})
  7. db.Migrator().HasTable("users")
  8. // Drop table if exists (will ignore or delete foreign key constraints when dropping)
  9. db.Migrator().DropTable(&User{})
  10. db.Migrator().DropTable("users")
  11. // Rename old table to new table
  12. db.Migrator().RenameTable(&User{}, &UserInfo{})
  13. db.Migrator().RenameTable("users", "user_infos")

Columns

  1. type User struct {
  2. Name string
  3. }
  4. // Add name field
  5. db.Migrator().AddColumn(&User{}, "Name")
  6. // Drop name field
  7. db.Migrator().DropColumn(&User{}, "Name")
  8. // Alter name field
  9. db.Migrator().AlterColumn(&User{}, "Name")
  10. // Check column exists
  11. db.Migrator().HasColumn(&User{}, "Name")
  12. type User struct {
  13. Name string
  14. NewName string
  15. }
  16. // Rename column to new name
  17. db.Migrator().RenameColumn(&User{}, "Name", "NewName")
  18. db.Migrator().RenameColumn(&User{}, "name", "new_name")
  19. // ColumnTypes
  20. db.Migrator().ColumnTypes(&User{}) ([]gorm.ColumnType, error)
  21. type ColumnType interface {
  22. Name() string
  23. DatabaseTypeName() string // varchar
  24. ColumnType() (columnType string, ok bool) // varchar(64)
  25. PrimaryKey() (isPrimaryKey bool, ok bool)
  26. AutoIncrement() (isAutoIncrement bool, ok bool)
  27. Length() (length int64, ok bool)
  28. DecimalSize() (precision int64, scale int64, ok bool)
  29. Nullable() (nullable bool, ok bool)
  30. Unique() (unique bool, ok bool)
  31. ScanType() reflect.Type
  32. Comment() (value string, ok bool)
  33. DefaultValue() (value string, ok bool)
  34. }

Views

Create views by ViewOption. About ViewOption:

  • Query is a subquery, which is required.
  • If Replace is true, exec CREATE OR REPLACE otherwise exec CREATE.
  • If CheckOption is not empty, append to sql, e.g. WITH LOCAL CHECK OPTION.

NOTE SQLite currently does not support Replace in ViewOption

  1. query := db.Model(&User{}).Where("age > ?", 20)
  2. // Create View
  3. db.Migrator().CreateView("users_pets", gorm.ViewOption{Query: query})
  4. // CREATE VIEW `users_view` AS SELECT * FROM `users` WHERE age > 20
  5. // Create or Replace View
  6. db.Migrator().CreateView("users_pets", gorm.ViewOption{Query: query, Replace: true})
  7. // CREATE OR REPLACE VIEW `users_pets` AS SELECT * FROM `users` WHERE age > 20
  8. // Create View With Check Option
  9. db.Migrator().CreateView("users_pets", gorm.ViewOption{Query: query, CheckOption: "WITH CHECK OPTION"})
  10. // CREATE VIEW `users_pets` AS SELECT * FROM `users` WHERE age > 20 WITH CHECK OPTION
  11. // Drop View
  12. db.Migrator().DropView("users_pets")
  13. // DROP VIEW IF EXISTS "users_pets"

Constraints

  1. type UserIndex struct {
  2. Name string `gorm:"check:name_checker,name <> 'jinzhu'"`
  3. }
  4. // Create constraint
  5. db.Migrator().CreateConstraint(&User{}, "name_checker")
  6. // Drop constraint
  7. db.Migrator().DropConstraint(&User{}, "name_checker")
  8. // Check constraint exists
  9. db.Migrator().HasConstraint(&User{}, "name_checker")

Create foreign keys for relations

  1. type User struct {
  2. gorm.Model
  3. CreditCards []CreditCard
  4. }
  5. type CreditCard struct {
  6. gorm.Model
  7. Number string
  8. UserID uint
  9. }
  10. // create database foreign key for user & credit_cards
  11. db.Migrator().CreateConstraint(&User{}, "CreditCards")
  12. db.Migrator().CreateConstraint(&User{}, "fk_users_credit_cards")
  13. // ALTER TABLE `credit_cards` ADD CONSTRAINT `fk_users_credit_cards` FOREIGN KEY (`user_id`) REFERENCES `users`(`id`)
  14. // check database foreign key for user & credit_cards exists or not
  15. db.Migrator().HasConstraint(&User{}, "CreditCards")
  16. db.Migrator().HasConstraint(&User{}, "fk_users_credit_cards")
  17. // drop database foreign key for user & credit_cards
  18. db.Migrator().DropConstraint(&User{}, "CreditCards")
  19. db.Migrator().DropConstraint(&User{}, "fk_users_credit_cards")

Indexes

  1. type User struct {
  2. gorm.Model
  3. Name string `gorm:"size:255;index:idx_name,unique"`
  4. }
  5. // Create index for Name field
  6. db.Migrator().CreateIndex(&User{}, "Name")
  7. db.Migrator().CreateIndex(&User{}, "idx_name")
  8. // Drop index for Name field
  9. db.Migrator().DropIndex(&User{}, "Name")
  10. db.Migrator().DropIndex(&User{}, "idx_name")
  11. // Check Index exists
  12. db.Migrator().HasIndex(&User{}, "Name")
  13. db.Migrator().HasIndex(&User{}, "idx_name")
  14. type User struct {
  15. gorm.Model
  16. Name string `gorm:"size:255;index:idx_name,unique"`
  17. Name2 string `gorm:"size:255;index:idx_name_2,unique"`
  18. }
  19. // Rename index name
  20. db.Migrator().RenameIndex(&User{}, "Name", "Name2")
  21. db.Migrator().RenameIndex(&User{}, "idx_name", "idx_name_2")

Constraints

GORM creates constraints when auto migrating or creating table, see Constraints or Database Indexes for details

Atlas Integration

Atlas is an open-source database migration tool that has an official integration with GORM.

While GORM’s AutoMigrate feature works in most cases, at some point you many need to switch to a versioned migrations strategy.

Once this happens, the responsibility for planning migration scripts and making sure they are in line with what GORM expects at runtime is moved to developers.

Atlas can automatically plan database schema migrations for developers using the official GORM Provider. After configuring the provider you can automatically plan migrations by running:

  1. atlas migrate diff --env gorm

To learn how to use Atlas with GORM, check out the official documentation.

Other Migration Tools

To use GORM with other Go-based migration tools, GORM provides a generic DB interface that might be helpful for you.

  1. // returns `*sql.DB`
  2. db.DB()

Refer to Generic Interface for more details.