golang根据代码自动更新数据库scheme

2016-09-11 YongHao Hu 更多博文 » 博客 » GitHub »

go

原文链接 http://yonghaowu.github.io/2016/09/11/go_database_auto_migration/
注:以下为加速网络访问所做的原文缓存,经过重新格式化,可能存在格式方面的问题,或偶有遗漏信息,请以原文为准。


最近在写测试, 很明显每一个单元测试最好就在运行时自动清空数据库. gorm的Auto Migration功能就可以满足此功能.

Auto Migration

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

WARNING: AutoMigrate will ONLY create tables, missing columns and missing indexes, and WON'T change existing column's type or delete unused columns to protect your data.

最重要的是, 索引(index), 约束(constrants), 类型(type)还有默认值(default)都可以设置,但是文档并没有详细的介绍,经过我的搜索与测试,终于摸清所有的关系.

AutoMigration只会根据struct tag建立新表, 没有的列以及索引, 不会改变已经存在的列的类型或者删除没有用到的列. 所以需要动态更新的话,还是需要在auto migration前DROP TABLE删除整个表再重建. 如:

func clearDatebase() {
    db := GetTestClient().NewConn()
    db.Exec("DROP TABLE books")
    db.AutoMigrate(&mystructtag.books{})
    db.Exec("DROP TABLE book_users")
    db.AutoMigrate(&mystructtag.book_users{})
}

gorm:

  • primary_key 设置主键
  • not null 非空约束
  • size:64 类型大小,通常是指varchar

以上主要就是size和not null并没有在文档上出现, 其他都可以从例子找到, 自己意会啦.

type User struct {
    gorm.Model
    Birthday     time.Time
    Age          int
    Name         string  `gorm:"size:255"` // Default size for string is 255, reset it with this tag
    Num          int     `gorm:"AUTO_INCREMENT"`

    CreditCard        CreditCard      // One-To-One relationship (has one - use CreditCard's UserID as foreign key)
    Emails            []Email         // One-To-Many relationship (has many - use Email's UserID as foreign key)

    BillingAddress    Address         // One-To-One relationship (belongs to - use BillingAddressID as foreign key)
    BillingAddressID  sql.NullInt64

    ShippingAddress   Address         // One-To-One relationship (belongs to - use ShippingAddressID as foreign key)
    ShippingAddressID int

    IgnoreMe          int `gorm:"-"`   // Ignore this field
    Languages         []Language `gorm:"many2many:user_languages;"` // Many-To-Many relationship, 'user_languages' is join table
}