From bef38d9d3f01bf04404aa97d5528ff046da38950 Mon Sep 17 00:00:00 2001 From: Peter Smit Date: Wed, 4 Feb 2015 15:47:40 +0200 Subject: [PATCH] Fix collaboration migration code --- models/migrations/migrations.go | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/models/migrations/migrations.go b/models/migrations/migrations.go index 43ec4e50b..f0ed10b7a 100644 --- a/models/migrations/migrations.go +++ b/models/migrations/migrations.go @@ -90,7 +90,7 @@ func accessToCollaboration(x *xorm.Engine) error { x.Sync(new(Collaboration)) - sql := `SELECT u.id AS uid, a.repo_name AS repo, a.mode AS mode FROM access a JOIN user u ON a.user_name=u.lower_name` + 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(sql) if err != nil { return err @@ -100,12 +100,14 @@ func accessToCollaboration(x *xorm.Engine) error { userID := mustParseInt64(result["uid"]) repoRefName := string(result["repo"]) mode := mustParseInt64(result["mode"]) + created := result["created"] //Collaborators must have write access if mode < 2 { continue } + // find owner of repository parts := strings.SplitN(repoRefName, "/", 2) ownerName := parts[0] repoName := parts[1] @@ -118,15 +120,24 @@ func accessToCollaboration(x *xorm.Engine) error { if len(results) < 1 { continue } + ownerID := mustParseInt64(results[0]["uid"]) + if ownerID == userID { + continue + } + // test if user is member of owning organization + isMember := false for _, member := range results { memberID := mustParseInt64(member["memberid"]) // We can skip all cases that a user is member of the owning organization if memberID == userID { - continue + isMember = true } } + if isMember { + continue + } sql = `SELECT id FROM repository WHERE owner_id=? AND lower_name=?` results, err = x.Query(sql, ownerID, repoName) @@ -139,8 +150,8 @@ func accessToCollaboration(x *xorm.Engine) error { repoID := results[0]["id"] - sql = `INSERT INTO collaboration (user_id, repo_id) VALUES (?,?)` - _, err = x.Exec(sql, userID, repoID) + sql = `INSERT INTO collaboration (user_id, repo_id, created) VALUES (?,?,?)` + _, err = x.Exec(sql, userID, repoID, created) if err != nil { return err }