1
0
mirror of https://github.com/woodpecker-ci/woodpecker.git synced 2024-11-24 08:02:18 +02:00

Merge pull request #2131 from bradrydzewski/master

read user and log sql queries from file
This commit is contained in:
Brad Rydzewski 2017-07-20 16:43:24 -04:00 committed by GitHub
commit 0eb09cf2b1
12 changed files with 473 additions and 93 deletions

View File

@ -6,24 +6,27 @@ import (
"io/ioutil" "io/ioutil"
"github.com/drone/drone/model" "github.com/drone/drone/model"
"github.com/drone/drone/store/datastore/sql"
"github.com/russross/meddler" "github.com/russross/meddler"
) )
func (db *datastore) LogFind(proc *model.Proc) (io.ReadCloser, error) { func (db *datastore) LogFind(proc *model.Proc) (io.ReadCloser, error) {
var log = new(logData) stmt := sql.Lookup(db.driver, "logs-find-proc")
var err = meddler.QueryRow(db, log, rebind(logQuery), proc.ID) data := new(logData)
var buf = bytes.NewBuffer(log.Data) err := meddler.QueryRow(db, data, stmt, proc.ID)
buf := bytes.NewBuffer(data.Data)
return ioutil.NopCloser(buf), err return ioutil.NopCloser(buf), err
} }
func (db *datastore) LogSave(proc *model.Proc, r io.Reader) error { func (db *datastore) LogSave(proc *model.Proc, r io.Reader) error {
var log = new(logData) stmt := sql.Lookup(db.driver, "logs-find-proc")
var err = meddler.QueryRow(db, log, rebind(logQuery), proc.ID) data := new(logData)
err := meddler.QueryRow(db, data, stmt, proc.ID)
if err != nil { if err != nil {
log = &logData{ProcID: proc.ID} data = &logData{ProcID: proc.ID}
} }
log.Data, _ = ioutil.ReadAll(r) data.Data, _ = ioutil.ReadAll(r)
return meddler.Save(db, logTable, log) return meddler.Save(db, "logs", data)
} }
type logData struct { type logData struct {
@ -31,12 +34,3 @@ type logData struct {
ProcID int64 `meddler:"log_job_id"` ProcID int64 `meddler:"log_job_id"`
Data []byte `meddler:"log_data"` Data []byte `meddler:"log_data"`
} }
const logTable = "logs"
const logQuery = `
SELECT *
FROM logs
WHERE log_job_id=?
LIMIT 1
`

View File

@ -6,55 +6,65 @@ import (
"testing" "testing"
"github.com/drone/drone/model" "github.com/drone/drone/model"
"github.com/franela/goblin"
) )
func TestLogs(t *testing.T) { func TestLogCreateFind(t *testing.T) {
db := openTest() s := newTest()
defer db.Close() defer func() {
s.Exec("delete from logs")
s.Close()
}()
s := From(db) proc := model.Proc{
g := goblin.Goblin(t) ID: 1,
g.Describe("Logs", func() { }
buf := bytes.NewBufferString("echo hi")
err := s.LogSave(&proc, buf)
if err != nil {
t.Errorf("Unexpected error: log create: %s", err)
}
// before each test be sure to purge the package rc, err := s.LogFind(&proc)
// table data from the database. if err != nil {
g.BeforeEach(func() { t.Errorf("Unexpected error: log create: %s", err)
db.Exec("DELETE FROM logs") }
})
g.It("Should create a log", func() { defer rc.Close()
proc := model.Proc{ out, _ := ioutil.ReadAll(rc)
ID: 1, if got, want := string(out), "echo hi"; got != want {
} t.Errorf("Want log data %s, got %s", want, got)
buf := bytes.NewBufferString("echo hi") }
err := s.LogSave(&proc, buf) }
g.Assert(err == nil).IsTrue()
func TestLogUpdate(t *testing.T) {
rc, err := s.LogFind(&proc) s := newTest()
g.Assert(err == nil).IsTrue() defer func() {
defer rc.Close() s.Exec("delete from logs")
out, _ := ioutil.ReadAll(rc) s.Close()
g.Assert(string(out)).Equal("echo hi") }()
})
proc := model.Proc{
g.It("Should update a log", func() { ID: 1,
proc := model.Proc{ }
ID: 1, buf1 := bytes.NewBufferString("echo hi")
} buf2 := bytes.NewBufferString("echo allo?")
buf1 := bytes.NewBufferString("echo hi") err1 := s.LogSave(&proc, buf1)
buf2 := bytes.NewBufferString("echo allo?") err2 := s.LogSave(&proc, buf2)
err1 := s.LogSave(&proc, buf1) if err1 != nil {
err2 := s.LogSave(&proc, buf2) t.Errorf("Unexpected error: log create: %s", err1)
g.Assert(err1 == nil).IsTrue() }
g.Assert(err2 == nil).IsTrue() if err2 != nil {
t.Errorf("Unexpected error: log update: %s", err2)
rc, err := s.LogFind(&proc) }
g.Assert(err == nil).IsTrue()
defer rc.Close() rc, err := s.LogFind(&proc)
out, _ := ioutil.ReadAll(rc) if err != nil {
g.Assert(string(out)).Equal("echo allo?") t.Errorf("Unexpected error: log create: %s", err)
}) }
}) defer rc.Close()
out, _ := ioutil.ReadAll(rc)
if got, want := string(out), "echo allo?"; got != want {
t.Errorf("Want log data %s, got %s", want, got)
}
} }

View File

@ -0,0 +1,9 @@
-- name: logs-find-proc
SELECT
log_id
,log_job_id
,log_data
FROM logs
WHERE log_job_id = ?
LIMIT 1

View File

@ -0,0 +1,53 @@
-- name: user-find
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
ORDER BY user_login ASC
-- name: user-find-login
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
WHERE user_login = ?
LIMIT 1
-- name: user-update
UPDATE users
SET
,user_token = ?
,user_secret = ?
,user_expiry = ?
,user_email = ?
,user_avatar = ?
,user_active = ?
,user_synced = ?
,user_admin = ?
,user_hash = ?
WHERE user_id = ?
-- name: user-delete
DELETE FROM users WHERE user_id = ?

View File

@ -18,6 +18,7 @@ var index = map[string]string{
"files-find-proc-name": filesFindProcName, "files-find-proc-name": filesFindProcName,
"files-find-proc-name-data": filesFindProcNameData, "files-find-proc-name-data": filesFindProcNameData,
"files-delete-build": filesDeleteBuild, "files-delete-build": filesDeleteBuild,
"logs-find-proc": logsFindProc,
"perms-find-user": permsFindUser, "perms-find-user": permsFindUser,
"perms-find-user-repo": permsFindUserRepo, "perms-find-user-repo": permsFindUserRepo,
"perms-insert-replace": permsInsertReplace, "perms-insert-replace": permsInsertReplace,
@ -46,6 +47,10 @@ var index = map[string]string{
"sender-delete": senderDelete, "sender-delete": senderDelete,
"task-list": taskList, "task-list": taskList,
"task-delete": taskDelete, "task-delete": taskDelete,
"user-find": userFind,
"user-find-login": userFindLogin,
"user-update": userUpdate,
"user-delete": userDelete,
} }
var configFindId = ` var configFindId = `
@ -200,6 +205,16 @@ var filesDeleteBuild = `
DELETE FROM files WHERE file_build_id = ? DELETE FROM files WHERE file_build_id = ?
` `
var logsFindProc = `
SELECT
log_id
,log_job_id
,log_data
FROM logs
WHERE log_job_id = ?
LIMIT 1
`
var permsFindUser = ` var permsFindUser = `
SELECT SELECT
perm_user_id perm_user_id
@ -525,3 +540,57 @@ FROM tasks
var taskDelete = ` var taskDelete = `
DELETE FROM tasks WHERE task_id = ? DELETE FROM tasks WHERE task_id = ?
` `
var userFind = `
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
ORDER BY user_login ASC
`
var userFindLogin = `
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
WHERE user_login = ?
LIMIT 1
`
var userUpdate = `
UPDATE users
SET
,user_token = ?
,user_secret = ?
,user_expiry = ?
,user_email = ?
,user_avatar = ?
,user_active = ?
,user_synced = ?
,user_admin = ?
,user_hash = ?
WHERE user_id = ?
`
var userDelete = `
DELETE FROM users WHERE user_id = ?
`

View File

@ -0,0 +1,9 @@
-- name: logs-find-proc
SELECT
log_id
,log_job_id
,log_data
FROM logs
WHERE log_job_id = $1
LIMIT 1

View File

@ -0,0 +1,53 @@
-- name: user-find
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
ORDER BY user_login ASC
-- name: user-find-login
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
WHERE user_login = $1
LIMIT 1
-- name: user-update
UPDATE users
SET
,user_token = $1
,user_secret = $2
,user_expiry = $3
,user_email = $4
,user_avatar = $5
,user_active = $6
,user_synced = $7
,user_admin = $8
,user_hash = $9
WHERE user_id = $10
-- name: user-delete
DELETE FROM users WHERE user_id = $1

View File

@ -18,6 +18,7 @@ var index = map[string]string{
"files-find-proc-name": filesFindProcName, "files-find-proc-name": filesFindProcName,
"files-find-proc-name-data": filesFindProcNameData, "files-find-proc-name-data": filesFindProcNameData,
"files-delete-build": filesDeleteBuild, "files-delete-build": filesDeleteBuild,
"logs-find-proc": logsFindProc,
"perms-find-user": permsFindUser, "perms-find-user": permsFindUser,
"perms-find-user-repo": permsFindUserRepo, "perms-find-user-repo": permsFindUserRepo,
"perms-insert-replace": permsInsertReplace, "perms-insert-replace": permsInsertReplace,
@ -46,6 +47,10 @@ var index = map[string]string{
"sender-delete": senderDelete, "sender-delete": senderDelete,
"task-list": taskList, "task-list": taskList,
"task-delete": taskDelete, "task-delete": taskDelete,
"user-find": userFind,
"user-find-login": userFindLogin,
"user-update": userUpdate,
"user-delete": userDelete,
} }
var configFindId = ` var configFindId = `
@ -200,6 +205,16 @@ var filesDeleteBuild = `
DELETE FROM files WHERE file_build_id = $1 DELETE FROM files WHERE file_build_id = $1
` `
var logsFindProc = `
SELECT
log_id
,log_job_id
,log_data
FROM logs
WHERE log_job_id = $1
LIMIT 1
`
var permsFindUser = ` var permsFindUser = `
SELECT SELECT
perm_user_id perm_user_id
@ -532,3 +547,57 @@ FROM tasks
var taskDelete = ` var taskDelete = `
DELETE FROM tasks WHERE task_id = $1 DELETE FROM tasks WHERE task_id = $1
` `
var userFind = `
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
ORDER BY user_login ASC
`
var userFindLogin = `
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
WHERE user_login = $1
LIMIT 1
`
var userUpdate = `
UPDATE users
SET
,user_token = $1
,user_secret = $2
,user_expiry = $3
,user_email = $4
,user_avatar = $5
,user_active = $6
,user_synced = $7
,user_admin = $8
,user_hash = $9
WHERE user_id = $10
`
var userDelete = `
DELETE FROM users WHERE user_id = $1
`

View File

@ -0,0 +1,9 @@
-- name: logs-find-proc
SELECT
log_id
,log_job_id
,log_data
FROM logs
WHERE log_job_id = ?
LIMIT 1

View File

@ -0,0 +1,53 @@
-- name: user-find
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
ORDER BY user_login ASC
-- name: user-find-login
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
WHERE user_login = ?
LIMIT 1
-- name: user-update
UPDATE users
SET
,user_token = ?
,user_secret = ?
,user_expiry = ?
,user_email = ?
,user_avatar = ?
,user_active = ?
,user_synced = ?
,user_admin = ?
,user_hash = ?
WHERE user_id = ?
-- name: user-delete
DELETE FROM users WHERE user_id = ?

View File

@ -18,6 +18,7 @@ var index = map[string]string{
"files-find-proc-name": filesFindProcName, "files-find-proc-name": filesFindProcName,
"files-find-proc-name-data": filesFindProcNameData, "files-find-proc-name-data": filesFindProcNameData,
"files-delete-build": filesDeleteBuild, "files-delete-build": filesDeleteBuild,
"logs-find-proc": logsFindProc,
"perms-find-user": permsFindUser, "perms-find-user": permsFindUser,
"perms-find-user-repo": permsFindUserRepo, "perms-find-user-repo": permsFindUserRepo,
"perms-insert-replace": permsInsertReplace, "perms-insert-replace": permsInsertReplace,
@ -46,6 +47,10 @@ var index = map[string]string{
"sender-delete": senderDelete, "sender-delete": senderDelete,
"task-list": taskList, "task-list": taskList,
"task-delete": taskDelete, "task-delete": taskDelete,
"user-find": userFind,
"user-find-login": userFindLogin,
"user-update": userUpdate,
"user-delete": userDelete,
} }
var configFindId = ` var configFindId = `
@ -200,6 +205,16 @@ var filesDeleteBuild = `
DELETE FROM files WHERE file_build_id = ? DELETE FROM files WHERE file_build_id = ?
` `
var logsFindProc = `
SELECT
log_id
,log_job_id
,log_data
FROM logs
WHERE log_job_id = ?
LIMIT 1
`
var permsFindUser = ` var permsFindUser = `
SELECT SELECT
perm_user_id perm_user_id
@ -525,3 +540,57 @@ FROM tasks
var taskDelete = ` var taskDelete = `
DELETE FROM tasks WHERE task_id = ? DELETE FROM tasks WHERE task_id = ?
` `
var userFind = `
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
ORDER BY user_login ASC
`
var userFindLogin = `
SELECT
user_id
,user_login
,user_token
,user_secret
,user_expiry
,user_email
,user_avatar
,user_active
,user_synced
,user_admin
,user_hash
FROM users
WHERE user_login = ?
LIMIT 1
`
var userUpdate = `
UPDATE users
SET
,user_token = ?
,user_secret = ?
,user_expiry = ?
,user_email = ?
,user_avatar = ?
,user_active = ?
,user_synced = ?
,user_admin = ?
,user_hash = ?
WHERE user_id = ?
`
var userDelete = `
DELETE FROM users WHERE user_id = ?
`

View File

@ -8,20 +8,22 @@ import (
func (db *datastore) GetUser(id int64) (*model.User, error) { func (db *datastore) GetUser(id int64) (*model.User, error) {
var usr = new(model.User) var usr = new(model.User)
var err = meddler.Load(db, userTable, usr, id) var err = meddler.Load(db, "users", usr, id)
return usr, err return usr, err
} }
func (db *datastore) GetUserLogin(login string) (*model.User, error) { func (db *datastore) GetUserLogin(login string) (*model.User, error) {
var usr = new(model.User) stmt := sql.Lookup(db.driver, "user-find-login")
var err = meddler.QueryRow(db, usr, rebind(userLoginQuery), login) data := new(model.User)
return usr, err err := meddler.QueryRow(db, data, stmt, login)
return data, err
} }
func (db *datastore) GetUserList() ([]*model.User, error) { func (db *datastore) GetUserList() ([]*model.User, error) {
var users = []*model.User{} stmt := sql.Lookup(db.driver, "user-find")
var err = meddler.QueryAll(db, &users, rebind(userListQuery)) data := []*model.User{}
return users, err err := meddler.QueryAll(db, &data, stmt)
return data, err
} }
func (db *datastore) GetUserCount() (count int, err error) { func (db *datastore) GetUserCount() (count int, err error) {
@ -32,15 +34,16 @@ func (db *datastore) GetUserCount() (count int, err error) {
} }
func (db *datastore) CreateUser(user *model.User) error { func (db *datastore) CreateUser(user *model.User) error {
return meddler.Insert(db, userTable, user) return meddler.Insert(db, "users", user)
} }
func (db *datastore) UpdateUser(user *model.User) error { func (db *datastore) UpdateUser(user *model.User) error {
return meddler.Update(db, userTable, user) return meddler.Update(db, "users", user)
} }
func (db *datastore) DeleteUser(user *model.User) error { func (db *datastore) DeleteUser(user *model.User) error {
var _, err = db.Exec(rebind(userDeleteStmt), user.ID) stmt := sql.Lookup(db.driver, "user-delete")
_, err := db.Exec(stmt, user.ID)
return err return err
} }
@ -50,23 +53,3 @@ func (db *datastore) UserFeed(user *model.User) ([]*model.Feed, error) {
err := meddler.QueryAll(db, &data, stmt, user.ID) err := meddler.QueryAll(db, &data, stmt, user.ID)
return data, err return data, err
} }
const userTable = "users"
const userLoginQuery = `
SELECT *
FROM users
WHERE user_login=?
LIMIT 1
`
const userListQuery = `
SELECT *
FROM users
ORDER BY user_login ASC
`
const userDeleteStmt = `
DELETE FROM users
WHERE user_id=?
`