DB Migrations #67
|
@ -19,6 +19,7 @@ package cmd
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
"code.vikunja.io/api/pkg/mail"
|
"code.vikunja.io/api/pkg/mail"
|
||||||
|
"code.vikunja.io/api/pkg/migration"
|
||||||
"code.vikunja.io/api/pkg/models"
|
"code.vikunja.io/api/pkg/models"
|
||||||
"code.vikunja.io/api/pkg/routes"
|
"code.vikunja.io/api/pkg/routes"
|
||||||
"code.vikunja.io/api/pkg/swagger"
|
"code.vikunja.io/api/pkg/swagger"
|
||||||
|
@ -43,6 +44,9 @@ var webCmd = &cobra.Command{
|
||||||
// Set logger
|
// Set logger
|
||||||
log.InitLogger()
|
log.InitLogger()
|
||||||
|
|
||||||
|
// Run the migrations
|
||||||
|
migration.Migrate(nil)
|
||||||
|
|
||||||
// Set Engine
|
// Set Engine
|
||||||
err := models.SetEngine()
|
err := models.SetEngine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -64,9 +64,7 @@ func CreateDBEngine() (engine *xorm.Engine, err error) {
|
||||||
case "redis":
|
case "redis":
|
||||||
cacher := xrc.NewRedisCacher(viper.GetString("redis.host"), viper.GetString("redis.password"), xrc.DEFAULT_EXPIRATION, engine.Logger())
|
cacher := xrc.NewRedisCacher(viper.GetString("redis.host"), viper.GetString("redis.password"), xrc.DEFAULT_EXPIRATION, engine.Logger())
|
||||||
engine.SetDefaultCacher(cacher)
|
engine.SetDefaultCacher(cacher)
|
||||||
// Get the tables from migration
|
gob.Register(GetTables())
|
||||||
gob.Register(tables)
|
|
||||||
gob.Register(tablesWithPointer) // Need to register tables with pointer as well...
|
|
||||||
default:
|
default:
|
||||||
log.Log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
|
log.Log.Info("Did not find a valid cache type. Caching disabled. Please refer to the docs for poosible cache types.")
|
||||||
}
|
}
|
||||||
|
|
37
pkg/db/tables.go
Normal file
37
pkg/db/tables.go
Normal file
|
@ -0,0 +1,37 @@
|
||||||
|
// Vikunja is a todo-list application to facilitate your life.
|
||||||
|
// Copyright 2019 Vikunja and contributors. All rights reserved.
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License as published by
|
||||||
|
// the Free Software Foundation, either version 3 of the License, or
|
||||||
|
// (at your option) any later version.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <https://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
|
package db
|
||||||
|
|
||||||
|
import "code.vikunja.io/api/pkg/models"
|
||||||
|
|
||||||
|
func GetTables() []interface{} {
|
||||||
|
return []interface{}{
|
||||||
|
&models.User{},
|
||||||
|
&models.List{},
|
||||||
|
&models.ListTask{},
|
||||||
|
&models.Team{},
|
||||||
|
&models.TeamMember{},
|
||||||
|
&models.TeamList{},
|
||||||
|
&models.TeamNamespace{},
|
||||||
|
&models.Namespace{},
|
||||||
|
&models.ListUser{},
|
||||||
|
&models.NamespaceUser{},
|
||||||
|
&models.ListTaskAssginee{},
|
||||||
|
&models.Label{},
|
||||||
|
&models.LabelTask{},
|
||||||
|
}
|
||||||
|
}
|
|
@ -19,6 +19,7 @@ package migration
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
|
"code.vikunja.io/api/pkg/models"
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
"sort"
|
"sort"
|
||||||
"src.techknowlogick.com/xormigrate"
|
"src.techknowlogick.com/xormigrate"
|
||||||
|
@ -28,6 +29,26 @@ import (
|
||||||
|
|
||||||
var migrations []*xormigrate.Migration
|
var migrations []*xormigrate.Migration
|
||||||
|
|
||||||
|
var tables []interface{}
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
tables = append(tables,
|
||||||
|
new(models.User),
|
||||||
|
new(models.List),
|
||||||
|
new(models.ListTask),
|
||||||
|
new(models.Team),
|
||||||
|
new(models.TeamMember),
|
||||||
|
new(models.TeamList),
|
||||||
|
new(models.TeamNamespace),
|
||||||
|
new(models.Namespace),
|
||||||
|
new(models.ListUser),
|
||||||
|
new(models.NamespaceUser),
|
||||||
|
new(models.ListTaskAssginee),
|
||||||
|
new(models.Label),
|
||||||
|
new(models.LabelTask),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
func Migrate(x *xorm.Engine) {
|
func Migrate(x *xorm.Engine) {
|
||||||
// Get our own xorm engine if we don't have one
|
// Get our own xorm engine if we don't have one
|
||||||
if x == nil {
|
if x == nil {
|
||||||
|
@ -45,9 +66,12 @@ func Migrate(x *xorm.Engine) {
|
||||||
return migrations[i].ID < migrations[j].ID
|
return migrations[i].ID < migrations[j].ID
|
||||||
})
|
})
|
||||||
|
|
||||||
m := xormigrate.New(nil, migrations)
|
m := xormigrate.New(x, migrations)
|
||||||
|
|
||||||
m.NewLogger(log.GetLogWriter("database"))
|
m.NewLogger(log.GetLogWriter("database"))
|
||||||
|
err := m.Migrate()
|
||||||
|
if err != nil {
|
||||||
|
log.Log.Criticalf("Migration failed: %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
//m.InitSchema()
|
//m.InitSchema()
|
||||||
|
|
||||||
|
|
|
@ -19,7 +19,6 @@ package models
|
||||||
import (
|
import (
|
||||||
"code.vikunja.io/api/pkg/db"
|
"code.vikunja.io/api/pkg/db"
|
||||||
"code.vikunja.io/api/pkg/log"
|
"code.vikunja.io/api/pkg/log"
|
||||||
"code.vikunja.io/api/pkg/migration"
|
|
||||||
_ "github.com/go-sql-driver/mysql" // Because.
|
_ "github.com/go-sql-driver/mysql" // Because.
|
||||||
"github.com/go-xorm/xorm"
|
"github.com/go-xorm/xorm"
|
||||||
_ "github.com/mattn/go-sqlite3" // Because.
|
_ "github.com/mattn/go-sqlite3" // Because.
|
||||||
|
@ -28,45 +27,8 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
x *xorm.Engine
|
x *xorm.Engine
|
||||||
|
|
||||||
tables []interface{}
|
|
||||||
tablesWithPointer []interface{}
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
|
||||||
tables = append(tables,
|
|
||||||
new(User),
|
|
||||||
new(List),
|
|
||||||
new(ListTask),
|
|
||||||
new(Team),
|
|
||||||
new(TeamMember),
|
|
||||||
new(TeamList),
|
|
||||||
new(TeamNamespace),
|
|
||||||
new(Namespace),
|
|
||||||
new(ListUser),
|
|
||||||
new(NamespaceUser),
|
|
||||||
new(ListTaskAssginee),
|
|
||||||
new(Label),
|
|
||||||
new(LabelTask),
|
|
||||||
)
|
|
||||||
|
|
||||||
tablesWithPointer = append(tables,
|
|
||||||
&User{},
|
|
||||||
&List{},
|
|
||||||
&ListTask{},
|
|
||||||
&Team{},
|
|
||||||
&TeamMember{},
|
|
||||||
&TeamList{},
|
|
||||||
&TeamNamespace{},
|
|
||||||
&Namespace{},
|
|
||||||
&ListUser{},
|
|
||||||
&NamespaceUser{},
|
|
||||||
&ListTaskAssginee{},
|
|
||||||
&Label{},
|
|
||||||
&LabelTask{},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
// SetEngine sets the xorm.Engine
|
// SetEngine sets the xorm.Engine
|
||||||
func SetEngine() (err error) {
|
func SetEngine() (err error) {
|
||||||
x, err = db.CreateDBEngine()
|
x, err = db.CreateDBEngine()
|
||||||
|
@ -75,8 +37,6 @@ func SetEngine() (err error) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
migration.Migrate(x)
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user