2022-10-18 15:26:48 +08:00
|
|
|
package migrations
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2022-10-24 16:51:05 +08:00
|
|
|
"github.com/answerdev/answer/internal/base/data"
|
|
|
|
"github.com/answerdev/answer/internal/entity"
|
2022-10-18 15:26:48 +08:00
|
|
|
"xorm.io/xorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
const minDBVersion = 0 // answer 1.0.0
|
|
|
|
|
|
|
|
// Migration describes on migration from lower version to high version
|
|
|
|
type Migration interface {
|
|
|
|
Description() string
|
|
|
|
Migrate(*xorm.Engine) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type migration struct {
|
|
|
|
description string
|
|
|
|
migrate func(*xorm.Engine) error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Description returns the migration's description
|
|
|
|
func (m *migration) Description() string {
|
|
|
|
return m.description
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate executes the migration
|
|
|
|
func (m *migration) Migrate(x *xorm.Engine) error {
|
|
|
|
return m.migrate(x)
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewMigration creates a new migration
|
|
|
|
func NewMigration(desc string, fn func(*xorm.Engine) error) Migration {
|
|
|
|
return &migration{description: desc, migrate: fn}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use noopMigration when there is a migration that has been no-oped
|
|
|
|
var noopMigration = func(_ *xorm.Engine) error { return nil }
|
|
|
|
|
|
|
|
var migrations = []Migration{
|
|
|
|
// 0->1
|
|
|
|
NewMigration("this is first version, no operation", noopMigration),
|
2022-11-02 15:25:27 +08:00
|
|
|
NewMigration("add user language", addUserLanguage),
|
2022-11-17 10:09:18 +08:00
|
|
|
NewMigration("add recommend and reserved tag fields", addTagRecommendedAndReserved),
|
2022-10-18 15:26:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetCurrentDBVersion returns the current db version
|
|
|
|
func GetCurrentDBVersion(engine *xorm.Engine) (int64, error) {
|
2022-10-19 20:03:41 +08:00
|
|
|
if err := engine.Sync(new(entity.Version)); err != nil {
|
2022-10-18 15:26:48 +08:00
|
|
|
return -1, fmt.Errorf("sync version failed: %v", err)
|
|
|
|
}
|
|
|
|
|
2022-10-19 20:03:41 +08:00
|
|
|
currentVersion := &entity.Version{ID: 1}
|
2022-10-18 15:26:48 +08:00
|
|
|
has, err := engine.Get(currentVersion)
|
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("get first version failed: %v", err)
|
|
|
|
}
|
|
|
|
if !has {
|
2022-10-19 20:03:41 +08:00
|
|
|
_, err := engine.InsertOne(&entity.Version{ID: 1, VersionNumber: 0})
|
2022-10-18 15:26:48 +08:00
|
|
|
if err != nil {
|
|
|
|
return -1, fmt.Errorf("insert first version failed: %v", err)
|
|
|
|
}
|
|
|
|
return 0, nil
|
|
|
|
}
|
|
|
|
return currentVersion.VersionNumber, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ExpectedVersion returns the expected db version
|
|
|
|
func ExpectedVersion() int64 {
|
|
|
|
return int64(minDBVersion + len(migrations))
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate database to current version
|
2022-10-19 14:56:22 +08:00
|
|
|
func Migrate(dataConf *data.Database) error {
|
|
|
|
engine, err := data.NewDB(false, dataConf)
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("new database failed: ", err.Error())
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-18 15:26:48 +08:00
|
|
|
currentDBVersion, err := GetCurrentDBVersion(engine)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
expectedVersion := ExpectedVersion()
|
|
|
|
|
|
|
|
for currentDBVersion < expectedVersion {
|
2022-11-02 15:25:27 +08:00
|
|
|
fmt.Printf("[migrate] current db version is %d, try to migrate version %d, latest version is %d\n",
|
2022-10-18 15:26:48 +08:00
|
|
|
currentDBVersion, currentDBVersion+1, expectedVersion)
|
|
|
|
migrationFunc := migrations[currentDBVersion]
|
2022-11-02 15:25:27 +08:00
|
|
|
fmt.Printf("[migrate] try to migrate db version %d, description: %s\n", currentDBVersion+1, migrationFunc.Description())
|
2022-10-18 15:26:48 +08:00
|
|
|
if err := migrationFunc.Migrate(engine); err != nil {
|
2022-11-02 15:25:27 +08:00
|
|
|
fmt.Printf("[migrate] migrate to db version %d failed: %s\n", currentDBVersion+1, err.Error())
|
2022-10-18 15:26:48 +08:00
|
|
|
return err
|
|
|
|
}
|
2022-11-02 15:25:27 +08:00
|
|
|
fmt.Printf("[migrate] migrate to db version %d success\n", currentDBVersion+1)
|
2022-10-19 20:03:41 +08:00
|
|
|
if _, err := engine.Update(&entity.Version{ID: 1, VersionNumber: currentDBVersion + 1}); err != nil {
|
2022-11-02 15:25:27 +08:00
|
|
|
fmt.Printf("[migrate] migrate to db version %d, update failed: %s", currentDBVersion+1, err.Error())
|
2022-10-18 15:26:48 +08:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
currentDBVersion++
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|