mirror of
https://github.com/kevin-DL/services.git
synced 2026-01-16 04:54:42 +00:00
Replace DB with Store in service/user (#321)
This commit is contained in:
54
user/migrate/context.go
Normal file
54
user/migrate/context.go
Normal file
@@ -0,0 +1,54 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"github.com/micro/micro/v3/service/logger"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/micro/services/user/migrate/entity"
|
||||
)
|
||||
|
||||
type Migration interface {
|
||||
Migrate([]*entity.Row) error
|
||||
}
|
||||
|
||||
type context struct {
|
||||
db *gorm.DB
|
||||
strategy Migration
|
||||
}
|
||||
|
||||
func NewContext(db *gorm.DB, strg Migration) *context {
|
||||
return &context{
|
||||
db: db,
|
||||
strategy: strg,
|
||||
}
|
||||
}
|
||||
|
||||
func (c *context) Migrate(tableName string) error {
|
||||
var count int64
|
||||
|
||||
db := c.db.Table(tableName)
|
||||
|
||||
if err := db.Count(&count).Error; err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
var offset, limit = 0, 1000
|
||||
|
||||
for offset = 0; offset < int(count); offset = offset + limit {
|
||||
rows := make([]*entity.Row, 0)
|
||||
|
||||
if err := db.Offset(offset).Limit(limit).Find(&rows).Error; err != nil {
|
||||
logger.Errorf("migrate error, table:%v offset:%v limit:%v error:%v", tableName, offset, limit, err)
|
||||
continue
|
||||
}
|
||||
|
||||
if err := c.strategy.Migrate(rows); err != nil {
|
||||
logger.Errorf("migrate error, table:%v offset:%v limit:%v error:%v", tableName, offset, limit, err)
|
||||
continue
|
||||
}
|
||||
}
|
||||
|
||||
logger.Infof("migrate done, table: %v, rows count: %v", tableName, count)
|
||||
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user