dsn := "user:pass@tcp(127.0.0.1:3306)/testDB?charset=utf8mb4&parseTime=True&loc=Local"
db, err := gorm.Open(mysql.Open(dsn), &gorm.Config{})
if err != nil {
panic("failed to connect database")
}
Now that there is a db
object in the project, how can we route the read and write traffic of this db object to the new database at the bottom layer of gorm?
The goal is to switch all the read and write traffic of the database in the project to the new database without making a lot of changes to the project code.
My idea is to use gorm’s callback to implement it, but I haven’t put it into practice yet. I’m looking forward to a specific example of practice.
In addition, I’m very worried about the reliability of gorm callback. Will there be some situation that causes the callback of the flow switching to fail, resulting in the old database still having traffic?