summaryrefslogtreecommitdiffstats
path: root/models/migrations/migrations.go
blob: e51bc3c876ac114edbcdfaace9978f8c35676454 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package migrations

import (
	"errors"

	"github.com/go-xorm/xorm"
)

type migration func(*xorm.Engine) error

// The version table. Should have only one row with id==1
type Version struct {
	Id      int64
	Version int64
}

// 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"
var migrations = []migration{}

// Migrate database to current version
func Migrate(x *xorm.Engine) error {
	x.Sync(new(Version))

	currentVersion := &Version{Id: 1}
	has, err := x.Get(currentVersion)
	if err != nil {
		return err
	} else if !has {
		if _, err = x.InsertOne(currentVersion); err != nil {
			return err
		}
	}

	v := currentVersion.Version

	for i, migration := range migrations[v:] {
		if err = migration(x); err != nil {
			return err
		}
		currentVersion.Version = v + int64(i) + 1
		x.Id(1).Update(currentVersion)
	}
	return nil
}

func expiredMigration(x *xorm.Engine) error {
	return errors.New("You are migrating from a too old gogs version")
}