2015-02-11 19:58:37 -07:00
// 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.
2015-01-22 05:49:52 -07:00
package migrations
import (
"errors"
2015-02-11 19:58:37 -07:00
"fmt"
2015-01-23 00:54:16 -07:00
"strings"
"time"
2015-01-22 05:56:50 -07:00
2015-02-11 19:58:37 -07:00
"github.com/Unknwon/com"
2015-01-22 05:49:52 -07:00
"github.com/go-xorm/xorm"
2015-02-11 19:58:37 -07:00
"github.com/gogits/gogs/modules/setting"
2015-01-22 05:49:52 -07:00
)
2015-02-11 19:58:37 -07:00
const _DB_VER = 1
2015-01-22 05:49:52 -07:00
type migration func ( * xorm . Engine ) error
// The version table. Should have only one row with id==1
type Version struct {
2015-01-22 05:56:50 -07:00
Id int64
2015-01-22 05:49:52 -07:00
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"
2015-01-23 00:54:16 -07:00
var migrations = [ ] migration {
accessToCollaboration ,
}
2015-01-22 05:49:52 -07:00
// Migrate database to current version
func Migrate ( x * xorm . Engine ) error {
2015-01-22 06:01:45 -07:00
if err := x . Sync ( new ( Version ) ) ; err != nil {
2015-02-11 19:58:37 -07:00
return fmt . Errorf ( "sync: %v" , err )
2015-01-22 06:01:45 -07:00
}
2015-01-22 05:49:52 -07:00
currentVersion := & Version { Id : 1 }
has , err := x . Get ( currentVersion )
if err != nil {
2015-02-11 19:58:37 -07:00
return fmt . Errorf ( "get: %v" , err )
2015-01-22 05:56:50 -07:00
} else if ! has {
2015-01-23 00:54:16 -07:00
needsMigration , err := x . IsTableExist ( "user" )
if err != nil {
return err
}
2015-02-11 19:58:37 -07:00
// if needsMigration {
// isEmpty, err := x.IsTableEmpty("user")
// if err != nil {
// return err
// }
// needsMigration = !isEmpty
// }
2015-01-23 00:54:16 -07:00
if ! needsMigration {
currentVersion . Version = int64 ( len ( migrations ) )
}
2015-01-22 05:56:50 -07:00
if _ , err = x . InsertOne ( currentVersion ) ; err != nil {
2015-02-11 19:58:37 -07:00
return fmt . Errorf ( "insert: %v" , err )
2015-01-22 05:56:50 -07:00
}
2015-01-22 05:49:52 -07:00
}
v := currentVersion . Version
for i , migration := range migrations [ v : ] {
if err = migration ( x ) ; err != nil {
2015-02-11 19:58:37 -07:00
return fmt . Errorf ( "run migration: %v" , err )
2015-01-22 05:49:52 -07:00
}
currentVersion . Version = v + int64 ( i ) + 1
2015-01-22 06:01:45 -07:00
if _ , err = x . Id ( 1 ) . Update ( currentVersion ) ; err != nil {
return err
}
2015-01-22 05:49:52 -07:00
}
return nil
}
func expiredMigration ( x * xorm . Engine ) error {
return errors . New ( "You are migrating from a too old gogs version" )
}
2015-01-23 00:54:16 -07:00
func accessToCollaboration ( x * xorm . Engine ) error {
type Collaboration struct {
2015-02-11 19:58:37 -07:00
ID int64 ` xorm:"pk autoincr" `
RepoID int64 ` xorm:"UNIQUE(s) INDEX NOT NULL" `
UserID int64 ` xorm:"UNIQUE(s) INDEX NOT NULL" `
Created time . Time
2015-01-23 00:54:16 -07:00
}
x . Sync ( new ( Collaboration ) )
2015-02-11 19:58:37 -07:00
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" )
2015-01-23 00:54:16 -07:00
if err != nil {
return err
}
2015-02-11 19:58:37 -07:00
offset := strings . Split ( time . Now ( ) . String ( ) , " " ) [ 2 ]
2015-01-23 00:54:16 -07:00
for _ , result := range results {
2015-02-11 19:58:37 -07:00
mode := com . StrTo ( result [ "mode" ] ) . MustInt64 ( )
// Collaborators must have write access.
2015-01-23 00:54:16 -07:00
if mode < 2 {
continue
}
2015-02-11 19:58:37 -07:00
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 )
}
2015-02-04 06:47:40 -07:00
// find owner of repository
2015-01-23 00:54:16 -07:00
parts := strings . SplitN ( repoRefName , "/" , 2 )
ownerName := parts [ 0 ]
repoName := parts [ 1 ]
2015-02-11 19:58:37 -07:00
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 )
2015-01-23 00:54:16 -07:00
if err != nil {
return err
}
if len ( results ) < 1 {
continue
}
2015-02-04 06:47:40 -07:00
2015-02-11 19:58:37 -07:00
ownerID := com . StrTo ( results [ 0 ] [ "uid" ] ) . MustInt64 ( )
2015-02-04 06:47:40 -07:00
if ownerID == userID {
continue
}
2015-01-23 00:54:16 -07:00
2015-02-04 06:47:40 -07:00
// test if user is member of owning organization
isMember := false
2015-01-23 00:54:16 -07:00
for _ , member := range results {
2015-02-11 19:58:37 -07:00
memberID := com . StrTo ( member [ "memberid" ] ) . MustInt64 ( )
2015-01-23 00:54:16 -07:00
// We can skip all cases that a user is member of the owning organization
if memberID == userID {
2015-02-04 06:47:40 -07:00
isMember = true
2015-01-23 00:54:16 -07:00
}
}
2015-02-04 06:47:40 -07:00
if isMember {
continue
}
2015-01-23 00:54:16 -07:00
2015-02-11 19:58:37 -07:00
results , err = x . Query ( "SELECT id FROM `repository` WHERE owner_id=? AND lower_name=?" , ownerID , repoName )
2015-01-23 00:54:16 -07:00
if err != nil {
return err
}
if len ( results ) < 1 {
continue
}
2015-02-11 19:58:37 -07:00
if _ , err = x . InsertOne ( & Collaboration {
UserID : userID ,
RepoID : com . StrTo ( results [ 0 ] [ "id" ] ) . MustInt64 ( ) ,
Created : created ,
} ) ; err != nil {
2015-01-23 00:54:16 -07:00
return err
}
}
return nil
}