From 0f36aaa463b32dbbd8bbf42094a6d45c1056b830 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Sun, 5 Oct 2014 19:57:33 -0700 Subject: [PATCH] fixed permission insert to use upsert for improved sync --- server/datastore/database/perm.go | 5 +++ server/datastore/database/perm_test.go | 42 ++++++++++++++------------ server/handler/login.go | 3 -- 3 files changed, 28 insertions(+), 22 deletions(-) diff --git a/server/datastore/database/perm.go b/server/datastore/database/perm.go index 5fe6e09e..68905bd9 100644 --- a/server/datastore/database/perm.go +++ b/server/datastore/database/perm.go @@ -23,6 +23,11 @@ func (db *Permstore) GetPerm(user *model.User, repo *model.Repo) (*model.Perm, e // PostPerm saves permission in the datastore. func (db *Permstore) PostPerm(perm *model.Perm) error { + var _perm = new(model.Perm) + meddler.QueryRow(db, _perm, rebind(permQuery), perm.UserID, perm.RepoID) + if _perm.ID != 0 { + perm.ID = _perm.ID + } return meddler.Save(db, permTable, perm) } diff --git a/server/datastore/database/perm_test.go b/server/datastore/database/perm_test.go index 3c8d29f3..5a0fea9c 100644 --- a/server/datastore/database/perm_test.go +++ b/server/datastore/database/perm_test.go @@ -47,6 +47,29 @@ func TestPermstore(t *testing.T) { g.Assert(perm1.ID != 0).IsTrue() }) + g.It("Should Upsert a Perm", func() { + perm1 := model.Perm{ + UserID: 1, + RepoID: 2, + Read: true, + Write: true, + Admin: true, + } + ps.PostPerm(&perm1) + perm1.Read = true + perm1.Write = true + perm1.Admin = false + perm1.ID = 0 + err := ps.PostPerm(&perm1) + g.Assert(err == nil).IsTrue() + g.Assert(perm1.ID != 0).IsTrue() + getperm, err := ps.GetPerm(&model.User{ID: 1}, &model.Repo{ID: 2}) + g.Assert(err == nil).IsTrue() + g.Assert(getperm.Read).IsTrue() + g.Assert(getperm.Write).IsTrue() + g.Assert(getperm.Admin).IsFalse() + }) + g.It("Should Get a Perm", func() { ps.PostPerm(&model.Perm{ UserID: 1, @@ -81,24 +104,5 @@ func TestPermstore(t *testing.T) { g.Assert(err2 == nil).IsTrue() g.Assert(err3 == nil).IsFalse() }) - - g.It("Should Enforce Unique Perm", func() { - err1 := ps.PostPerm(&model.Perm{ - UserID: 1, - RepoID: 2, - Read: true, - Write: true, - Admin: true, - }) - err2 := ps.PostPerm(&model.Perm{ - UserID: 1, - RepoID: 2, - Read: true, - Write: true, - Admin: true, - }) - g.Assert(err1 == nil).IsTrue() - g.Assert(err2 == nil).IsFalse() - }) }) } diff --git a/server/handler/login.go b/server/handler/login.go index 90c757c0..036c702e 100644 --- a/server/handler/login.go +++ b/server/handler/login.go @@ -94,9 +94,6 @@ func GetLogin(c web.C, w http.ResponseWriter, r *http.Request) { // // todo(bradrydzewski) this login should be a bit more intelligent // than the current implementation. - // - // todo(bradrydzewski) the github implementation will only sync a - // maximum of 100 repositories due to the api pagination. need to fix. if u.Syncing { redirect = "/sync" log.Println("sync user account.", u.Login)