first pass work on migration #925

This commit is contained in:
Unknwon 2015-02-11 21:58:37 -05:00
parent c5c467a9cd
commit 7e7160eefd
9 changed files with 226 additions and 164 deletions

View File

@ -43,7 +43,7 @@ func setup(logPath string) {
models.LoadModelsConfig() models.LoadModelsConfig()
if models.UseSQLite3 { if setting.UseSQLite3 {
workDir, _ := setting.WorkDir() workDir, _ := setting.WorkDir()
os.Chdir(workDir) os.Chdir(workDir)
} }

View File

@ -7,8 +7,6 @@ package models
import ( import (
"strings" "strings"
"time" "time"
"github.com/go-xorm/xorm"
) )
type AccessType int type AccessType int
@ -27,35 +25,40 @@ type Access struct {
Created time.Time `xorm:"CREATED"` Created time.Time `xorm:"CREATED"`
} }
// AddAccess adds new access record. func addAccess(e Engine, access *Access) error {
func AddAccess(access *Access) error {
access.UserName = strings.ToLower(access.UserName) access.UserName = strings.ToLower(access.UserName)
access.RepoName = strings.ToLower(access.RepoName) access.RepoName = strings.ToLower(access.RepoName)
_, err := x.Insert(access) _, err := e.Insert(access)
return err return err
} }
// AddAccess adds new access record.
func AddAccess(access *Access) error {
return addAccess(x, access)
}
func updateAccess(e Engine, access *Access) error {
if _, err := e.Id(access.Id).Update(access); err != nil {
return err
}
return nil
}
// UpdateAccess updates access information. // UpdateAccess updates access information.
func UpdateAccess(access *Access) error { func UpdateAccess(access *Access) error {
access.UserName = strings.ToLower(access.UserName) access.UserName = strings.ToLower(access.UserName)
access.RepoName = strings.ToLower(access.RepoName) access.RepoName = strings.ToLower(access.RepoName)
_, err := x.Id(access.Id).Update(access) return updateAccess(x, access)
}
func deleteAccess(e Engine, access *Access) error {
_, err := e.Delete(access)
return err return err
} }
// DeleteAccess deletes access record. // DeleteAccess deletes access record.
func DeleteAccess(access *Access) error { func DeleteAccess(access *Access) error {
_, err := x.Delete(access) return deleteAccess(x, access)
return err
}
// UpdateAccess updates access information with session for rolling back.
func UpdateAccessWithSession(sess *xorm.Session, access *Access) error {
if _, err := sess.Id(access.Id).Update(access); err != nil {
sess.Rollback()
return err
}
return nil
} }
// HasAccess returns true if someone can read or write to given repository. // HasAccess returns true if someone can read or write to given repository.
@ -93,9 +96,10 @@ func (u *User) GetAccessibleRepositories() (map[*Repository]AccessType, error) {
if err != nil { if err != nil {
return nil, err return nil, err
} }
err = repo.GetOwner() if err = repo.GetOwner(); err != nil {
if err != nil {
return nil, err return nil, err
} else if repo.OwnerId == u.Id {
continue
} }
repos[repo] = access.Mode repos[repo] = access.Mode
} }

View File

@ -282,30 +282,33 @@ type IssueUser struct {
} }
// NewIssueUserPairs adds new issue-user pairs for new issue of repository. // NewIssueUserPairs adds new issue-user pairs for new issue of repository.
func NewIssueUserPairs(repo *Repository, iid, oid, pid, aid int64) (err error) { func NewIssueUserPairs(repo *Repository, issueID, orgID, posterID, assigneeID int64) (err error) {
iu := &IssueUser{IssueId: iid, RepoId: repo.Id} users, err := repo.GetCollaborators()
us, err := repo.GetCollaborators()
if err != nil { if err != nil {
return err return err
} }
iu := &IssueUser{
IssueId: issueID,
RepoId: repo.Id,
}
isNeedAddPoster := true isNeedAddPoster := true
for _, u := range us { for _, u := range users {
iu.Uid = u.Id iu.Uid = u.Id
iu.IsPoster = iu.Uid == pid iu.IsPoster = iu.Uid == posterID
if isNeedAddPoster && iu.IsPoster { if isNeedAddPoster && iu.IsPoster {
isNeedAddPoster = false isNeedAddPoster = false
} }
iu.IsAssigned = iu.Uid == aid iu.IsAssigned = iu.Uid == assigneeID
if _, err = x.Insert(iu); err != nil { if _, err = x.Insert(iu); err != nil {
return err return err
} }
} }
if isNeedAddPoster { if isNeedAddPoster {
iu.Uid = pid iu.Uid = posterID
iu.IsPoster = true iu.IsPoster = true
iu.IsAssigned = iu.Uid == aid iu.IsAssigned = iu.Uid == assigneeID
if _, err = x.Insert(iu); err != nil { if _, err = x.Insert(iu); err != nil {
return err return err
} }

View File

@ -1,14 +1,23 @@
// Copyright 2015 The Gogs Authors. All rights reserved.
// Use of this source code is governed by a MIT-style
// license that can be found in the LICENSE file.
package migrations package migrations
import ( import (
"errors" "errors"
"strconv" "fmt"
"strings" "strings"
"time" "time"
"github.com/Unknwon/com"
"github.com/go-xorm/xorm" "github.com/go-xorm/xorm"
"github.com/gogits/gogs/modules/setting"
) )
const _DB_VER = 1
type migration func(*xorm.Engine) error type migration func(*xorm.Engine) error
// The version table. Should have only one row with id==1 // The version table. Should have only one row with id==1
@ -26,39 +35,38 @@ var migrations = []migration{
// Migrate database to current version // Migrate database to current version
func Migrate(x *xorm.Engine) error { func Migrate(x *xorm.Engine) error {
if err := x.Sync(new(Version)); err != nil { if err := x.Sync(new(Version)); err != nil {
return err return fmt.Errorf("sync: %v", err)
} }
currentVersion := &Version{Id: 1} currentVersion := &Version{Id: 1}
has, err := x.Get(currentVersion) has, err := x.Get(currentVersion)
if err != nil { if err != nil {
return err return fmt.Errorf("get: %v", err)
} else if !has { } else if !has {
needsMigration, err := x.IsTableExist("user") needsMigration, err := x.IsTableExist("user")
if err != nil { if err != nil {
return err return err
} }
if needsMigration { // if needsMigration {
isEmpty, err := x.IsTableEmpty("user") // isEmpty, err := x.IsTableEmpty("user")
if err != nil { // if err != nil {
return err // return err
} // }
needsMigration = !isEmpty // needsMigration = !isEmpty
} // }
if !needsMigration { if !needsMigration {
currentVersion.Version = int64(len(migrations)) currentVersion.Version = int64(len(migrations))
} }
if _, err = x.InsertOne(currentVersion); err != nil { if _, err = x.InsertOne(currentVersion); err != nil {
return err return fmt.Errorf("insert: %v", err)
} }
} }
v := currentVersion.Version v := currentVersion.Version
for i, migration := range migrations[v:] { for i, migration := range migrations[v:] {
if err = migration(x); err != nil { if err = migration(x); err != nil {
return err return fmt.Errorf("run migration: %v", err)
} }
currentVersion.Version = v + int64(i) + 1 currentVersion.Version = v + int64(i) + 1
if _, err = x.Id(1).Update(currentVersion); err != nil { if _, err = x.Id(1).Update(currentVersion); err != nil {
@ -72,48 +80,48 @@ func expiredMigration(x *xorm.Engine) error {
return errors.New("You are migrating from a too old gogs version") return errors.New("You are migrating from a too old gogs version")
} }
func mustParseInt64(in []byte) int64 {
i, err := strconv.ParseInt(string(in), 10, 64)
if err != nil {
i = 0
}
return i
}
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"`
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"` RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"` UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
Created time.Time `xorm:"CREATED"` Created time.Time
} }
x.Sync(new(Collaboration)) x.Sync(new(Collaboration))
sql := `SELECT u.id AS uid, a.repo_name AS repo, a.mode AS mode, a.created as created FROM access a JOIN user u ON a.user_name=u.lower_name` results, err := x.Query("SELECT u.id AS `uid`, a.repo_name AS `repo`, a.mode AS `mode`, a.created as `created` FROM `access` a JOIN `user` u ON a.user_name=u.lower_name")
results, err := x.Query(sql)
if err != nil { if err != nil {
return err return err
} }
offset := strings.Split(time.Now().String(), " ")[2]
for _, result := range results { for _, result := range results {
userID := mustParseInt64(result["uid"]) mode := com.StrTo(result["mode"]).MustInt64()
repoRefName := string(result["repo"]) // Collaborators must have write access.
mode := mustParseInt64(result["mode"])
created := result["created"]
//Collaborators must have write access
if mode < 2 { if mode < 2 {
continue continue
} }
userID := com.StrTo(result["uid"]).MustInt64()
repoRefName := string(result["repo"])
var created time.Time
switch {
case setting.UseSQLite3:
created, _ = time.Parse(time.RFC3339, string(result["created"]))
case setting.UseMySQL:
created, _ = time.Parse("2006-01-02 15:04:05-0700", string(result["created"])+offset)
case setting.UsePostgreSQL:
created, _ = time.Parse("2006-01-02T15:04:05Z-0700", string(result["created"])+offset)
}
// find owner of repository // find owner of repository
parts := strings.SplitN(repoRefName, "/", 2) parts := strings.SplitN(repoRefName, "/", 2)
ownerName := parts[0] ownerName := parts[0]
repoName := parts[1] repoName := parts[1]
sql = `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=?` results, err := x.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 := x.Query(sql, ownerName)
if err != nil { if err != nil {
return err return err
} }
@ -121,7 +129,7 @@ func accessToCollaboration(x *xorm.Engine) error {
continue continue
} }
ownerID := mustParseInt64(results[0]["uid"]) ownerID := com.StrTo(results[0]["uid"]).MustInt64()
if ownerID == userID { if ownerID == userID {
continue continue
} }
@ -129,7 +137,7 @@ func accessToCollaboration(x *xorm.Engine) error {
// test if user is member of owning organization // test if user is member of owning organization
isMember := false isMember := false
for _, member := range results { for _, member := range results {
memberID := mustParseInt64(member["memberid"]) memberID := com.StrTo(member["memberid"]).MustInt64()
// We can skip all cases that a user is member of the owning organization // We can skip all cases that a user is member of the owning organization
if memberID == userID { if memberID == userID {
isMember = true isMember = true
@ -139,8 +147,7 @@ func accessToCollaboration(x *xorm.Engine) error {
continue continue
} }
sql = `SELECT id FROM repository WHERE owner_id=? AND lower_name=?` results, err = x.Query("SELECT id FROM `repository` WHERE owner_id=? AND lower_name=?", ownerID, repoName)
results, err = x.Query(sql, ownerID, repoName)
if err != nil { if err != nil {
return err return err
} }
@ -148,11 +155,11 @@ func accessToCollaboration(x *xorm.Engine) error {
continue continue
} }
repoID := results[0]["id"] if _, err = x.InsertOne(&Collaboration{
UserID: userID,
sql = `INSERT INTO collaboration (user_id, repo_id, created) VALUES (?,?,?)` RepoID: com.StrTo(results[0]["id"]).MustInt64(),
_, err = x.Exec(sql, userID, repoID, created) Created: created,
if err != nil { }); err != nil {
return err return err
} }
} }

View File

@ -40,24 +40,29 @@ var (
} }
EnableSQLite3 bool EnableSQLite3 bool
UseSQLite3 bool
) )
func init() { func init() {
tables = append(tables, tables = append(tables,
new(User), new(PublicKey), new(Follow), new(Oauth2), new(AccessToken), new(User), new(PublicKey), new(Oauth2), new(AccessToken),
new(Repository), new(Watch), new(Star), new(Action), new(Access), new(Repository), new(Collaboration), new(Access),
new(Watch), new(Star), new(Follow), new(Action),
new(Issue), new(Comment), new(Attachment), new(IssueUser), new(Label), new(Milestone), new(Issue), new(Comment), new(Attachment), new(IssueUser), new(Label), new(Milestone),
new(Mirror), new(Release), new(LoginSource), new(Webhook), new(Mirror), new(Release), new(LoginSource), new(Webhook),
new(UpdateTask), new(HookTask), new(Team), new(OrgUser), new(TeamUser), new(UpdateTask), new(HookTask), new(Team), new(OrgUser), new(TeamUser),
new(Notice), new(EmailAddress), new(Collaboration)) new(Notice), new(EmailAddress))
} }
func LoadModelsConfig() { func LoadModelsConfig() {
sec := setting.Cfg.Section("database") sec := setting.Cfg.Section("database")
DbCfg.Type = sec.Key("DB_TYPE").String() DbCfg.Type = sec.Key("DB_TYPE").String()
if DbCfg.Type == "sqlite3" { switch DbCfg.Type {
UseSQLite3 = true case "sqlite3":
setting.UseSQLite3 = true
case "mysql":
setting.UseMySQL = true
case "postgres":
setting.UsePostgreSQL = true
} }
DbCfg.Host = sec.Key("HOST").String() DbCfg.Host = sec.Key("HOST").String()
DbCfg.Name = sec.Key("NAME").String() DbCfg.Name = sec.Key("NAME").String()
@ -141,7 +146,7 @@ func NewEngine() (err error) {
} }
if err = migrations.Migrate(x); err != nil { if err = migrations.Migrate(x); err != nil {
return err return fmt.Errorf("migrate: %v", err)
} }
if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil { if err = x.StoreEngine("InnoDB").Sync2(tables...); err != nil {

View File

@ -847,7 +847,8 @@ func ChangeRepositoryName(userName, oldRepoName, newRepoName string) (err error)
for i := range accesses { for i := range accesses {
accesses[i].RepoName = userName + "/" + newRepoName accesses[i].RepoName = userName + "/" + newRepoName
if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil { if err = updateAccess(sess, &accesses[i]); err != nil {
sess.Rollback()
return err return err
} }
} }
@ -1062,76 +1063,6 @@ func GetRepositoryCount(user *User) (int64, error) {
return x.Count(&Repository{OwnerId: user.Id}) return x.Count(&Repository{OwnerId: user.Id})
} }
// GetCollaborators returns the collaborators for a repository
func (r *Repository) GetCollaborators() ([]*User, error) {
collaborations := make([]*Collaboration, 0)
if err := x.Find(&collaborations, &Collaboration{RepoID: r.Id}); err != nil {
return nil, err
}
users := make([]*User, len(collaborations))
for i, c := range collaborations {
user, err := GetUserById(c.UserID)
if err != nil {
return nil, err
}
users[i] = user
}
return users, nil
}
// Add collaborator and accompanying access
func (r *Repository) AddCollaborator(u *User) error {
collaboration := &Collaboration{RepoID: r.Id, UserID: u.Id}
has, err := x.Get(collaboration)
if err != nil {
return err
}
if has {
return nil
}
if _, err = x.InsertOne(collaboration); err != nil {
return err
}
if err = r.GetOwner(); err != nil {
return err
}
return AddAccess(&Access{UserName: u.LowerName, RepoName: path.Join(r.Owner.LowerName, r.LowerName), Mode: WRITABLE})
}
// Delete collaborator and accompanying access
func (r *Repository) DeleteCollaborator(u *User) error {
collaboration := &Collaboration{RepoID: r.Id, UserID: u.Id}
if has, err := x.Delete(collaboration); err != nil || has == 0 {
return err
}
if err := r.GetOwner(); err != nil {
return err
}
needDelete := true
if r.Owner.IsOrganization() {
auth, err := GetHighestAuthorize(r.Owner.Id, u.Id, r.Id, 0)
if err != nil {
return err
}
if auth > 0 {
needDelete = false
}
}
if needDelete {
return DeleteAccess(&Access{UserName: u.LowerName, RepoName: path.Join(r.Owner.LowerName, r.LowerName), Mode: WRITABLE})
}
return nil
}
type SearchOption struct { type SearchOption struct {
Keyword string Keyword string
Uid int64 Uid int64
@ -1279,6 +1210,120 @@ func GitGcRepos() error {
}) })
} }
// _________ .__ .__ ___. __ .__
// \_ ___ \ ____ | | | | _____ \_ |__ ________________ _/ |_|__| ____ ____
// / \ \/ / _ \| | | | \__ \ | __ \ / _ \_ __ \__ \\ __\ |/ _ \ / \
// \ \___( <_> ) |_| |__/ __ \| \_\ ( <_> ) | \// __ \| | | ( <_> ) | \
// \______ /\____/|____/____(____ /___ /\____/|__| (____ /__| |__|\____/|___| /
// \/ \/ \/ \/ \/
// A Collaboration is a relation between an individual and a repository
type Collaboration struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
Created time.Time `xorm:"CREATED"`
}
// Add collaborator and accompanying access
func (r *Repository) AddCollaborator(u *User) error {
collaboration := &Collaboration{RepoID: r.Id, UserID: u.Id}
has, err := x.Get(collaboration)
if err != nil {
return err
} else if has {
return nil
}
if err = r.GetOwner(); err != nil {
return err
}
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
if _, err = sess.InsertOne(collaboration); err != nil {
sess.Rollback()
return err
} else if err = addAccess(sess, &Access{
UserName: u.LowerName,
RepoName: path.Join(r.Owner.LowerName, r.LowerName),
Mode: WRITABLE}); err != nil {
sess.Rollback()
return err
}
return sess.Commit()
}
// GetCollaborators returns the collaborators for a repository
func (r *Repository) GetCollaborators() ([]*User, error) {
collaborations := make([]*Collaboration, 0, 5)
if err := x.Where("repo_id=?", r.Id).Find(&collaborations); err != nil {
return nil, err
}
users := make([]*User, len(collaborations))
for i, c := range collaborations {
user, err := GetUserById(c.UserID)
if err != nil {
return nil, err
}
users[i] = user
}
return users, nil
}
// Delete collaborator and accompanying access
func (r *Repository) DeleteCollaborator(u *User) (err error) {
collaboration := &Collaboration{RepoID: r.Id, UserID: u.Id}
has, err := x.Get(collaboration)
if err != nil {
return err
} else if !has {
return nil
}
if err = r.GetOwner(); err != nil {
return err
}
sess := x.NewSession()
defer sess.Close()
if err = sess.Begin(); err != nil {
return err
}
needDelete := true
if r.Owner.IsOrganization() {
auth, err := getHighestAuthorize(sess, r.Owner.Id, u.Id, r.Id, 0)
if err != nil {
sess.Rollback()
return err
}
if auth > 0 {
needDelete = false
}
}
if needDelete {
if err = deleteAccess(sess, &Access{
UserName: u.LowerName,
RepoName: path.Join(r.Owner.LowerName, r.LowerName),
Mode: WRITABLE}); err != nil {
sess.Rollback()
return err
} else if _, err = sess.Delete(collaboration); err != nil {
sess.Rollback()
return err
}
}
return sess.Commit()
}
// __ __ __ .__ // __ __ __ .__
// / \ / \_____ _/ |_ ____ | |__ // / \ / \_____ _/ |_ ____ | |__
// \ \/\/ /\__ \\ __\/ ___\| | \ // \ \/\/ /\__ \\ __\/ ___\| | \
@ -1559,11 +1604,3 @@ func ForkRepository(u *User, oldRepo *Repository, name, desc string) (*Repositor
return repo, nil return repo, nil
} }
// A Collaboration is a relation between an individual and a repository
type Collaboration struct {
ID int64 `xorm:"pk autoincr"`
RepoID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
UserID int64 `xorm:"UNIQUE(s) INDEX NOT NULL"`
Created time.Time `xorm:"CREATED"`
}

View File

@ -415,7 +415,8 @@ func ChangeUserName(u *User, newUserName string) (err error) {
if strings.HasPrefix(accesses[i].RepoName, u.LowerName+"/") { if strings.HasPrefix(accesses[i].RepoName, u.LowerName+"/") {
accesses[i].RepoName = strings.Replace(accesses[i].RepoName, u.LowerName, newUserName, 1) accesses[i].RepoName = strings.Replace(accesses[i].RepoName, u.LowerName, newUserName, 1)
} }
if err = UpdateAccessWithSession(sess, &accesses[i]); err != nil { if err = updateAccess(sess, &accesses[i]); err != nil {
sess.Rollback()
return err return err
} }
} }
@ -435,7 +436,8 @@ func ChangeUserName(u *User, newUserName string) (err error) {
// if the access is not the user's access (already updated above) // if the access is not the user's access (already updated above)
if accesses[j].UserName != u.LowerName { if accesses[j].UserName != u.LowerName {
accesses[j].RepoName = newUserName + "/" + repos[i].LowerName accesses[j].RepoName = newUserName + "/" + repos[i].LowerName
if err = UpdateAccessWithSession(sess, &accesses[j]); err != nil { if err = updateAccess(sess, &accesses[j]); err != nil {
sess.Rollback()
return err return err
} }
} }
@ -564,8 +566,7 @@ func UserPath(userName string) string {
func GetUserByKeyId(keyId int64) (*User, error) { func GetUserByKeyId(keyId int64) (*User, error) {
user := new(User) user := new(User)
rawSql := "SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?" has, err := x.Sql("SELECT a.* FROM `user` AS a, public_key AS b WHERE a.id = b.owner_id AND b.id=?", keyId).Get(user)
has, err := x.Sql(rawSql, keyId).Get(user)
if err != nil { if err != nil {
return nil, err return nil, err
} else if !has { } else if !has {

View File

@ -67,6 +67,11 @@ var (
CookieRememberName string CookieRememberName string
ReverseProxyAuthUser string ReverseProxyAuthUser string
// Database settings.
UseSQLite3 bool
UseMySQL bool
UsePostgreSQL bool
// Webhook settings. // Webhook settings.
Webhook struct { Webhook struct {
TaskInterval int TaskInterval int

View File

@ -1,7 +1,7 @@
</div> </div>
<footer id="footer"> <footer id="footer">
<div class="container clear"> <div class="container clear">
<p class="left" id="footer-rights">© 2015 GoGits · {{.i18n.Tr "version"}}: {{AppVer}} · {{.i18n.Tr "page"}}: <strong>{{LoadTimes .PageStartTime}}</strong> · <p class="left" id="footer-rights">© 2015 Gogs · {{.i18n.Tr "version"}}: {{AppVer}} · {{.i18n.Tr "page"}}: <strong>{{LoadTimes .PageStartTime}}</strong> ·
{{.i18n.Tr "template"}}: <strong>{{call .TmplLoadTimes}}</strong></p> {{.i18n.Tr "template"}}: <strong>{{call .TmplLoadTimes}}</strong></p>
<div class="right" id="footer-links"> <div class="right" id="footer-links">