Merge pull request #940 from phsmit/migration_update

Some comments and improvements for migrations
This commit is contained in:
无闻 2015-02-12 12:42:51 -05:00
commit 31eb49c3ae

View file

@ -5,7 +5,6 @@
package migrations package migrations
import ( import (
"errors"
"fmt" "fmt"
"strings" "strings"
"time" "time"
@ -17,7 +16,7 @@ import (
"github.com/gogits/gogs/modules/setting" "github.com/gogits/gogs/modules/setting"
) )
const _DB_VER = 1 const _MIN_DB_VER = 0
type Migration interface { type Migration interface {
Description() string Description() string
@ -48,9 +47,10 @@ type Version struct {
} }
// This is a sequence of migrations. Add new migrations to the bottom of the list. // This is a sequence of migrations. Add new migrations to the bottom of the list.
// If you want to "retire" a migration, replace it with "expiredMigration" // If you want to "retire" a migration, remove it from the top of the list and
// update _MIN_VER_DB accordingly
var migrations = []Migration{ var migrations = []Migration{
NewMigration("generate collaboration from access", accessToCollaboration), NewMigration("generate collaboration from access", accessToCollaboration), // V0 -> V1
} }
// Migrate database to current version // Migrate database to current version
@ -64,6 +64,8 @@ func Migrate(x *xorm.Engine) error {
if err != nil { if err != nil {
return fmt.Errorf("get: %v", err) return fmt.Errorf("get: %v", err)
} else if !has { } else if !has {
// If the user table does not exist it is a fresh installation and we
// can skip all migrations
needsMigration, err := x.IsTableExist("user") needsMigration, err := x.IsTableExist("user")
if err != nil { if err != nil {
return err return err
@ -73,10 +75,12 @@ func Migrate(x *xorm.Engine) error {
if err != nil { if err != nil {
return err return err
} }
// If the user table is empty it is a fresh installation and we can
// skip all migrations
needsMigration = !isEmpty needsMigration = !isEmpty
} }
if !needsMigration { if !needsMigration {
currentVersion.Version = int64(len(migrations)) currentVersion.Version = int64(_MIN_DB_VER + len(migrations))
} }
if _, err = x.InsertOne(currentVersion); err != nil { if _, err = x.InsertOne(currentVersion); err != nil {
@ -85,7 +89,7 @@ func Migrate(x *xorm.Engine) error {
} }
v := currentVersion.Version v := currentVersion.Version
for i, m := range migrations[v:] { for i, m := range migrations[v-_MIN_DB_VER:] {
log.Info("Migration: %s", m.Description()) log.Info("Migration: %s", m.Description())
if err = m.Migrate(x); err != nil { if err = m.Migrate(x); err != nil {
return fmt.Errorf("do migrate: %v", err) return fmt.Errorf("do migrate: %v", err)
@ -98,10 +102,6 @@ func Migrate(x *xorm.Engine) error {
return nil return nil
} }
func expiredMigration(x *xorm.Engine) error {
return errors.New("You are migrating from a too old gogs version")
}
func accessToCollaboration(x *xorm.Engine) error { func accessToCollaboration(x *xorm.Engine) error {
type Collaboration struct { type Collaboration struct {
ID int64 `xorm:"pk autoincr"` ID int64 `xorm:"pk autoincr"`
@ -118,7 +118,12 @@ func accessToCollaboration(x *xorm.Engine) error {
} }
sess := x.NewSession() sess := x.NewSession()
defer sess.Close() defer func() {
if sess.IsCommitedOrRollbacked {
sess.Rollback()
}
sess.Close()
}()
if err = sess.Begin(); err != nil { if err = sess.Begin(); err != nil {
return err return err
} }
@ -151,7 +156,6 @@ func accessToCollaboration(x *xorm.Engine) error {
results, err := sess.Query("SELECT u.id as `uid`, ou.uid as `memberid` FROM `user` u LEFT JOIN org_user ou ON ou.org_id=u.id WHERE u.lower_name=?", ownerName) results, err := sess.Query("SELECT u.id as `uid`, ou.uid as `memberid` FROM `user` u LEFT JOIN org_user ou ON ou.org_id=u.id WHERE u.lower_name=?", ownerName)
if err != nil { if err != nil {
sess.Rollback()
return err return err
} }
if len(results) < 1 { if len(results) < 1 {
@ -189,7 +193,6 @@ func accessToCollaboration(x *xorm.Engine) error {
} }
has, err := sess.Get(collaboration) has, err := sess.Get(collaboration)
if err != nil { if err != nil {
sess.Rollback()
return err return err
} else if has { } else if has {
continue continue
@ -197,7 +200,6 @@ func accessToCollaboration(x *xorm.Engine) error {
collaboration.Created = created collaboration.Created = created
if _, err = sess.InsertOne(collaboration); err != nil { if _, err = sess.InsertOne(collaboration); err != nil {
sess.Rollback()
return err return err
} }
} }