mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-23 10:21:00 +00:00
Merge pull request #2131 from bradrydzewski/master
read user and log sql queries from file
This commit is contained in:
commit
0eb09cf2b1
12 changed files with 473 additions and 93 deletions
|
@ -6,24 +6,27 @@ import (
|
|||
"io/ioutil"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/drone/drone/store/datastore/sql"
|
||||
"github.com/russross/meddler"
|
||||
)
|
||||
|
||||
func (db *datastore) LogFind(proc *model.Proc) (io.ReadCloser, error) {
|
||||
var log = new(logData)
|
||||
var err = meddler.QueryRow(db, log, rebind(logQuery), proc.ID)
|
||||
var buf = bytes.NewBuffer(log.Data)
|
||||
stmt := sql.Lookup(db.driver, "logs-find-proc")
|
||||
data := new(logData)
|
||||
err := meddler.QueryRow(db, data, stmt, proc.ID)
|
||||
buf := bytes.NewBuffer(data.Data)
|
||||
return ioutil.NopCloser(buf), err
|
||||
}
|
||||
|
||||
func (db *datastore) LogSave(proc *model.Proc, r io.Reader) error {
|
||||
var log = new(logData)
|
||||
var err = meddler.QueryRow(db, log, rebind(logQuery), proc.ID)
|
||||
stmt := sql.Lookup(db.driver, "logs-find-proc")
|
||||
data := new(logData)
|
||||
err := meddler.QueryRow(db, data, stmt, proc.ID)
|
||||
if err != nil {
|
||||
log = &logData{ProcID: proc.ID}
|
||||
data = &logData{ProcID: proc.ID}
|
||||
}
|
||||
log.Data, _ = ioutil.ReadAll(r)
|
||||
return meddler.Save(db, logTable, log)
|
||||
data.Data, _ = ioutil.ReadAll(r)
|
||||
return meddler.Save(db, "logs", data)
|
||||
}
|
||||
|
||||
type logData struct {
|
||||
|
@ -31,12 +34,3 @@ type logData struct {
|
|||
ProcID int64 `meddler:"log_job_id"`
|
||||
Data []byte `meddler:"log_data"`
|
||||
}
|
||||
|
||||
const logTable = "logs"
|
||||
|
||||
const logQuery = `
|
||||
SELECT *
|
||||
FROM logs
|
||||
WHERE log_job_id=?
|
||||
LIMIT 1
|
||||
`
|
||||
|
|
|
@ -6,39 +6,43 @@ import (
|
|||
"testing"
|
||||
|
||||
"github.com/drone/drone/model"
|
||||
"github.com/franela/goblin"
|
||||
)
|
||||
|
||||
func TestLogs(t *testing.T) {
|
||||
db := openTest()
|
||||
defer db.Close()
|
||||
func TestLogCreateFind(t *testing.T) {
|
||||
s := newTest()
|
||||
defer func() {
|
||||
s.Exec("delete from logs")
|
||||
s.Close()
|
||||
}()
|
||||
|
||||
s := From(db)
|
||||
g := goblin.Goblin(t)
|
||||
g.Describe("Logs", func() {
|
||||
|
||||
// before each test be sure to purge the package
|
||||
// table data from the database.
|
||||
g.BeforeEach(func() {
|
||||
db.Exec("DELETE FROM logs")
|
||||
})
|
||||
|
||||
g.It("Should create a log", func() {
|
||||
proc := model.Proc{
|
||||
ID: 1,
|
||||
}
|
||||
buf := bytes.NewBufferString("echo hi")
|
||||
err := s.LogSave(&proc, buf)
|
||||
g.Assert(err == nil).IsTrue()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: log create: %s", err)
|
||||
}
|
||||
|
||||
rc, err := s.LogFind(&proc)
|
||||
g.Assert(err == nil).IsTrue()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: log create: %s", err)
|
||||
}
|
||||
|
||||
defer rc.Close()
|
||||
out, _ := ioutil.ReadAll(rc)
|
||||
g.Assert(string(out)).Equal("echo hi")
|
||||
})
|
||||
if got, want := string(out), "echo hi"; got != want {
|
||||
t.Errorf("Want log data %s, got %s", want, got)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLogUpdate(t *testing.T) {
|
||||
s := newTest()
|
||||
defer func() {
|
||||
s.Exec("delete from logs")
|
||||
s.Close()
|
||||
}()
|
||||
|
||||
g.It("Should update a log", func() {
|
||||
proc := model.Proc{
|
||||
ID: 1,
|
||||
}
|
||||
|
@ -46,15 +50,21 @@ func TestLogs(t *testing.T) {
|
|||
buf2 := bytes.NewBufferString("echo allo?")
|
||||
err1 := s.LogSave(&proc, buf1)
|
||||
err2 := s.LogSave(&proc, buf2)
|
||||
g.Assert(err1 == nil).IsTrue()
|
||||
g.Assert(err2 == nil).IsTrue()
|
||||
if err1 != nil {
|
||||
t.Errorf("Unexpected error: log create: %s", err1)
|
||||
}
|
||||
if err2 != nil {
|
||||
t.Errorf("Unexpected error: log update: %s", err2)
|
||||
}
|
||||
|
||||
rc, err := s.LogFind(&proc)
|
||||
g.Assert(err == nil).IsTrue()
|
||||
if err != nil {
|
||||
t.Errorf("Unexpected error: log create: %s", err)
|
||||
}
|
||||
|
||||
defer rc.Close()
|
||||
out, _ := ioutil.ReadAll(rc)
|
||||
g.Assert(string(out)).Equal("echo allo?")
|
||||
})
|
||||
|
||||
})
|
||||
if got, want := string(out), "echo allo?"; got != want {
|
||||
t.Errorf("Want log data %s, got %s", want, got)
|
||||
}
|
||||
}
|
||||
|
|
9
store/datastore/sql/mysql/files/logs.sql
Normal file
9
store/datastore/sql/mysql/files/logs.sql
Normal 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
|
53
store/datastore/sql/mysql/files/users.sql
Normal file
53
store/datastore/sql/mysql/files/users.sql
Normal 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 = ?
|
|
@ -18,6 +18,7 @@ var index = map[string]string{
|
|||
"files-find-proc-name": filesFindProcName,
|
||||
"files-find-proc-name-data": filesFindProcNameData,
|
||||
"files-delete-build": filesDeleteBuild,
|
||||
"logs-find-proc": logsFindProc,
|
||||
"perms-find-user": permsFindUser,
|
||||
"perms-find-user-repo": permsFindUserRepo,
|
||||
"perms-insert-replace": permsInsertReplace,
|
||||
|
@ -46,6 +47,10 @@ var index = map[string]string{
|
|||
"sender-delete": senderDelete,
|
||||
"task-list": taskList,
|
||||
"task-delete": taskDelete,
|
||||
"user-find": userFind,
|
||||
"user-find-login": userFindLogin,
|
||||
"user-update": userUpdate,
|
||||
"user-delete": userDelete,
|
||||
}
|
||||
|
||||
var configFindId = `
|
||||
|
@ -200,6 +205,16 @@ var filesDeleteBuild = `
|
|||
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 = `
|
||||
SELECT
|
||||
perm_user_id
|
||||
|
@ -525,3 +540,57 @@ FROM tasks
|
|||
var taskDelete = `
|
||||
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 = ?
|
||||
`
|
||||
|
|
9
store/datastore/sql/postgres/files/logs.sql
Normal file
9
store/datastore/sql/postgres/files/logs.sql
Normal 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
|
53
store/datastore/sql/postgres/files/users.sql
Normal file
53
store/datastore/sql/postgres/files/users.sql
Normal 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
|
|
@ -18,6 +18,7 @@ var index = map[string]string{
|
|||
"files-find-proc-name": filesFindProcName,
|
||||
"files-find-proc-name-data": filesFindProcNameData,
|
||||
"files-delete-build": filesDeleteBuild,
|
||||
"logs-find-proc": logsFindProc,
|
||||
"perms-find-user": permsFindUser,
|
||||
"perms-find-user-repo": permsFindUserRepo,
|
||||
"perms-insert-replace": permsInsertReplace,
|
||||
|
@ -46,6 +47,10 @@ var index = map[string]string{
|
|||
"sender-delete": senderDelete,
|
||||
"task-list": taskList,
|
||||
"task-delete": taskDelete,
|
||||
"user-find": userFind,
|
||||
"user-find-login": userFindLogin,
|
||||
"user-update": userUpdate,
|
||||
"user-delete": userDelete,
|
||||
}
|
||||
|
||||
var configFindId = `
|
||||
|
@ -200,6 +205,16 @@ var filesDeleteBuild = `
|
|||
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 = `
|
||||
SELECT
|
||||
perm_user_id
|
||||
|
@ -532,3 +547,57 @@ FROM tasks
|
|||
var taskDelete = `
|
||||
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
|
||||
`
|
||||
|
|
9
store/datastore/sql/sqlite/files/logs.sql
Normal file
9
store/datastore/sql/sqlite/files/logs.sql
Normal 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
|
53
store/datastore/sql/sqlite/files/user.sql
Normal file
53
store/datastore/sql/sqlite/files/user.sql
Normal 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 = ?
|
|
@ -18,6 +18,7 @@ var index = map[string]string{
|
|||
"files-find-proc-name": filesFindProcName,
|
||||
"files-find-proc-name-data": filesFindProcNameData,
|
||||
"files-delete-build": filesDeleteBuild,
|
||||
"logs-find-proc": logsFindProc,
|
||||
"perms-find-user": permsFindUser,
|
||||
"perms-find-user-repo": permsFindUserRepo,
|
||||
"perms-insert-replace": permsInsertReplace,
|
||||
|
@ -46,6 +47,10 @@ var index = map[string]string{
|
|||
"sender-delete": senderDelete,
|
||||
"task-list": taskList,
|
||||
"task-delete": taskDelete,
|
||||
"user-find": userFind,
|
||||
"user-find-login": userFindLogin,
|
||||
"user-update": userUpdate,
|
||||
"user-delete": userDelete,
|
||||
}
|
||||
|
||||
var configFindId = `
|
||||
|
@ -200,6 +205,16 @@ var filesDeleteBuild = `
|
|||
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 = `
|
||||
SELECT
|
||||
perm_user_id
|
||||
|
@ -525,3 +540,57 @@ FROM tasks
|
|||
var taskDelete = `
|
||||
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 = ?
|
||||
`
|
||||
|
|
|
@ -8,20 +8,22 @@ import (
|
|||
|
||||
func (db *datastore) GetUser(id int64) (*model.User, error) {
|
||||
var usr = new(model.User)
|
||||
var err = meddler.Load(db, userTable, usr, id)
|
||||
var err = meddler.Load(db, "users", usr, id)
|
||||
return usr, err
|
||||
}
|
||||
|
||||
func (db *datastore) GetUserLogin(login string) (*model.User, error) {
|
||||
var usr = new(model.User)
|
||||
var err = meddler.QueryRow(db, usr, rebind(userLoginQuery), login)
|
||||
return usr, err
|
||||
stmt := sql.Lookup(db.driver, "user-find-login")
|
||||
data := new(model.User)
|
||||
err := meddler.QueryRow(db, data, stmt, login)
|
||||
return data, err
|
||||
}
|
||||
|
||||
func (db *datastore) GetUserList() ([]*model.User, error) {
|
||||
var users = []*model.User{}
|
||||
var err = meddler.QueryAll(db, &users, rebind(userListQuery))
|
||||
return users, err
|
||||
stmt := sql.Lookup(db.driver, "user-find")
|
||||
data := []*model.User{}
|
||||
err := meddler.QueryAll(db, &data, stmt)
|
||||
return data, err
|
||||
}
|
||||
|
||||
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 {
|
||||
return meddler.Insert(db, userTable, user)
|
||||
return meddler.Insert(db, "users", user)
|
||||
}
|
||||
|
||||
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 {
|
||||
var _, err = db.Exec(rebind(userDeleteStmt), user.ID)
|
||||
stmt := sql.Lookup(db.driver, "user-delete")
|
||||
_, err := db.Exec(stmt, user.ID)
|
||||
return err
|
||||
}
|
||||
|
||||
|
@ -50,23 +53,3 @@ func (db *datastore) UserFeed(user *model.User) ([]*model.Feed, error) {
|
|||
err := meddler.QueryAll(db, &data, stmt, user.ID)
|
||||
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=?
|
||||
`
|
||||
|
|
Loading…
Reference in a new issue