ããã«ã¡ã¯ãæ ªå¼ä¼ç¤¾ã«ããã·ã®ã¨ã³ã¸ã㢠@imu ã§ãã
ã¯ããã«
å¼ç¤¾ã®ã¢ããªã±ã¼ã·ã§ã³ã®ããã¯ã¨ã³ãã¯Goã§DBã¢ã¯ã»ã¹ã©ã¤ãã©ãªã¯GORMã使ã£ã¦ãã¾ãã
ãµã¼ãã¹ãªãªã¼ã¹æããGORMã使ã£ã¦ãããV1ããV2ã«ãããã¨ã§ããã©ã¼ãã³ã¹æ¹åã«ã¤ãªããã¨æããã¢ããã°ã¬ã¼ãã«ãã£ã¬ã³ã¸ãã¾ããã
è²ã
è©°ã¾ãã¨ããããã£ãã®ã§ãå
±æãå«ãã¦ä½ããã£ãã®ãæ¸ãã¦ã¿ã¾ãï¼
çµè«
æ©éçµè«ã§ããâ¦
GORM V2ã¸ã®ã¢ããã°ã¬ã¼ãã¯ç¾æç¹ã§ä¿çã¨ãªãã¾ãã...
ä¿çã«ããçç±ã¯ä»¥ä¸ã®éãã§ãã
- V2ã§ä»æ§å¤æ´ã«ãªã£ãé¢æ°ãå¤ãå½±é¿ç¯å²ãåºã
- V2ã§å»æ¢ãããé¢æ°ããã代æ¿æ¡ã®æ¤è¨¼ãå¿ è¦ã§ãã
- ããããAPIã®ãã¹ããå®å ¨ã§ãªãã®ã§ã¢ããã°ã¬ã¼ããæã
APIã®ç¶²ç¾ ãã¹ããããã°æãåã£ãå¤æ´ãã§ããã®ã§ãããå®å ¨ã§ã¯ãªãã®ã§ä¸æ¦ç¶²ç¾ ãã¹ããæ¸ãã¦ããã¢ããã°ã¬ã¼ããã¹ãã¨ããçµè«ã«è³ãã¾ããã
ããããå
ã¯ã©ããã£ãå¤æ´å¯¾å¿ãããã®ããå
±æãããã¨æãã¾ãã
ä¸é¨æªç¢ºèªã®ç®æãããã¾ãã®ã§ãäºæ¿ãã ããã
対å¿å 容
Logger, Datadog Trace, ãµã¼ããã¼ãã£è£½ã®BulkInsertãå¤æ´ãã¦ããã¾ããããã¾ãè¨äºã§è¦ãããªã対å¿ãå ±æãããã¨æãã¾ãã
- PrimaryKeyã®æå®æ¹æ³ãå¤æ´
// V1 `gorm:"primary_key" json:"id"` // V2 `gorm:"primaryKey" json:"id"`
- jointable_foreignkey, association_jointable_foreignkeyã®æå®æ¹æ³ãå¤æ´
// V1 `gorm:"jointable_foreignkey:hoge;association_jointable_foreignkey:huga;" json:"ids"` // V2 `gorm:"joinForeignKey:hoge;joinReferences:huga;" json:"ids"`
- foreignkey, association_foreignkeyã®æå®æ¹æ³ãå¤æ´
// V1 `gorm:"foreignkey:hoge;association_foreignkey:huga;" json:"id"` // V2 `gorm:"foreignKey:hoge;references:huga;" json:"id"`
詳細ã¯ãã¡ãã確èªãã¦ãã ããã
gorm.io
- V2ã§RecordNotFound()å¤å®
ã¨ã©ã¼ãã³ããªã³ã°æ¹æ³ãå¤ãã£ã¦ãããããèªåã§å
±éé¢æ°ãä½æããerrorãªãã¸ã§ã¯ãã渡ãã¦å¤å®ããããã«ãã¾ããã
import ( "errors" "gorm.io/gorm" ) func RecordNotFound(err error) bool { return errors.Is(err, gorm.ErrRecordNotFound) }
- First, Findã®ã¨ã©ã¼ãã³ããªã³ã°
RecordNotFound()ã¡ã½ãããV2ããå»æ¢ã«ãªã£ããã¨ã§ãã¨ã©ã¼ãã³ããªã³ã°ã§æ³¨æãå¿ è¦ãªã±ã¼ã¹ãããã¾ããã
// V2 var db *gorm.DB user := &orm.User{} ... err := db.Where("uuid = ?", library.UUID()).First(user).Error pretty.Println(err) pretty.Println(errors.Is(err, gorm.ErrRecordNotFound)) > &errors.errorString{s:"record not found"} > bool(true) user := &orm.User{} err := db.Where("uuid = ?", library.UUID()).Find(user).Error pretty.Println(err) pretty.Println(errors.Is(err, gorm.ErrRecordNotFound)) > nil > bool(false)
.Find().Error
ã¨ããå ´åãgorm.ErrRecordNotFound
ã«ãªããªãã®ã§æ³¨æãã¦ãã ããã
.First().Error
ã«ã¡ã½ãããå¤æ´ããããnilãªãNotFoundã«ããããã«ãã¾ãããã
- ãã¼ãã«ååå¾
// V1 var db *gorm.DB var st orm.Users ... pretty.Println(db.NewScope(st).TableName()) > users // V2 var db *gorm.DB var st orm.Users ... stmt := &gorm.Statement{DB: db} stmt.Model = st stmt.Parse(stmt.Model) pretty.Println(stmt.Table) > users
V2ã§ãã¼ãã«åãåå¾ããå¦çã¯ãExecute()æç¹ã§ä½ããããã¦ããã¨è¸ãã§ã³ã¼ããèªãã§æ¸ãã¾ããã
- ã«ã©ã ãã§ãã¯
// V1 var db *gorm.DB var st orm.Users ... pretty.Println(db.NewScope(st).HasColumn("Hoge")) // orm.Usersã«Hogeã«ã©ã ãããã°true > bool(true) // V2 var db *gorm.DB var st orm.Users ... pretty.Println(db.Migrator().HasColumn(st, "Hoge")) > bool(true)
- QueryExpr
// V1 var db *gorm.DB var st orm.Users ... pretty.Println(db.Table("sample").Where("id = 1").QueryExpr()) > &gorm.SqlExpr{ expr: "SELECT * FROM `sample` WHERE (id = 1)", args: nil, } // V2 var db *gorm.DB var st orm.Users ... pretty.Println(db.Table("sample").Where("id = 1")) > V1ã®å¦çã¯ãµãã¯ã¨ãªã ã¨æã£ã¦ããã®ã§ã³ã¬ã§å¤§ä¸å¤«ãªã¯ãâ¦ï¼æªç¢ºèªã§ãï¼
ãããã«
å人çã«ã¯ããããããã¨æã£ã¦åãçµã¿ã¾ããããæãã®å¤æéãæãããããã®ã¨ãV2ã«å¤æ´ããã¨ãã®å®å¿æï¼ãã¹ãï¼ããªãã®ã§ä¿çã¨ããçå°ããã¾ããâ¦ã
éããããªã½ã¼ã¹å
ã§åºæ¥ãªãã£ãæãããæ´ããããã«ï¼ï¼ï¼ãå¼ç¤¾ã§ã¯ä¸ç·ã«åãã¡ã³ãã¼ãå¹
åºãåéãã¦ããã¾ãï¼
ç¹ã«â¦
- ä¸ã¤ã®èª²é¡ã«åãåããã¨ã好ããªæ¹
- é£æ度ãé«ã課é¡ã好ããªæ¹
- ä½ã£ãã¢ããã©ããã£ã使ããæ¹ããã¦ããã®ãèå³ããããç¾å ´ã«è¡ãããæ¹
ã§ãï¼
èå³ãããã話ãèãã¦ã¿ãããå¿åãããã¨ããæ¹ã¯ãæ°è»½ã«ãå¿åãã ããï¼