From 15d92c5fa323ae51417d09d387aaf5022531cbb2 Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Thu, 20 Jul 2017 16:34:52 -0400 Subject: [PATCH] read user and log sql queries from file --- store/datastore/logs.go | 28 ++--- store/datastore/logs_test.go | 102 ++++++++++--------- store/datastore/sql/mysql/files/logs.sql | 9 ++ store/datastore/sql/mysql/files/users.sql | 53 ++++++++++ store/datastore/sql/mysql/sql_gen.go | 69 +++++++++++++ store/datastore/sql/postgres/files/logs.sql | 9 ++ store/datastore/sql/postgres/files/users.sql | 53 ++++++++++ store/datastore/sql/postgres/sql_gen.go | 69 +++++++++++++ store/datastore/sql/sqlite/files/logs.sql | 9 ++ store/datastore/sql/sqlite/files/user.sql | 53 ++++++++++ store/datastore/sql/sqlite/sql_gen.go | 69 +++++++++++++ store/datastore/users.go | 43 +++----- 12 files changed, 473 insertions(+), 93 deletions(-) create mode 100644 store/datastore/sql/mysql/files/logs.sql create mode 100644 store/datastore/sql/mysql/files/users.sql create mode 100644 store/datastore/sql/postgres/files/logs.sql create mode 100644 store/datastore/sql/postgres/files/users.sql create mode 100644 store/datastore/sql/sqlite/files/logs.sql create mode 100644 store/datastore/sql/sqlite/files/user.sql diff --git a/store/datastore/logs.go b/store/datastore/logs.go index b39ad3b6a..841ef7de3 100644 --- a/store/datastore/logs.go +++ b/store/datastore/logs.go @@ -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 -` diff --git a/store/datastore/logs_test.go b/store/datastore/logs_test.go index cd57b762d..664178708 100644 --- a/store/datastore/logs_test.go +++ b/store/datastore/logs_test.go @@ -6,55 +6,65 @@ 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() { + proc := model.Proc{ + ID: 1, + } + 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 - // table data from the database. - g.BeforeEach(func() { - db.Exec("DELETE FROM logs") - }) + rc, err := s.LogFind(&proc) + if err != nil { + t.Errorf("Unexpected error: log create: %s", err) + } - 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() - - rc, err := s.LogFind(&proc) - g.Assert(err == nil).IsTrue() - defer rc.Close() - out, _ := ioutil.ReadAll(rc) - g.Assert(string(out)).Equal("echo hi") - }) - - g.It("Should update a log", func() { - proc := model.Proc{ - ID: 1, - } - buf1 := bytes.NewBufferString("echo hi") - 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() - - rc, err := s.LogFind(&proc) - g.Assert(err == nil).IsTrue() - defer rc.Close() - out, _ := ioutil.ReadAll(rc) - g.Assert(string(out)).Equal("echo allo?") - }) - - }) + defer rc.Close() + out, _ := ioutil.ReadAll(rc) + 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() + }() + + proc := model.Proc{ + ID: 1, + } + buf1 := bytes.NewBufferString("echo hi") + buf2 := bytes.NewBufferString("echo allo?") + err1 := s.LogSave(&proc, buf1) + err2 := s.LogSave(&proc, buf2) + 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) + if err != nil { + 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) + } } diff --git a/store/datastore/sql/mysql/files/logs.sql b/store/datastore/sql/mysql/files/logs.sql new file mode 100644 index 000000000..7473d7dcc --- /dev/null +++ b/store/datastore/sql/mysql/files/logs.sql @@ -0,0 +1,9 @@ +-- name: logs-find-proc + +SELECT + log_id +,log_job_id +,log_data +FROM logs +WHERE log_job_id = ? +LIMIT 1 diff --git a/store/datastore/sql/mysql/files/users.sql b/store/datastore/sql/mysql/files/users.sql new file mode 100644 index 000000000..62c98eb99 --- /dev/null +++ b/store/datastore/sql/mysql/files/users.sql @@ -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 = ? diff --git a/store/datastore/sql/mysql/sql_gen.go b/store/datastore/sql/mysql/sql_gen.go index 7225f31f8..37797e2e0 100644 --- a/store/datastore/sql/mysql/sql_gen.go +++ b/store/datastore/sql/mysql/sql_gen.go @@ -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 = ? +` diff --git a/store/datastore/sql/postgres/files/logs.sql b/store/datastore/sql/postgres/files/logs.sql new file mode 100644 index 000000000..fe209c672 --- /dev/null +++ b/store/datastore/sql/postgres/files/logs.sql @@ -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 diff --git a/store/datastore/sql/postgres/files/users.sql b/store/datastore/sql/postgres/files/users.sql new file mode 100644 index 000000000..4255c53da --- /dev/null +++ b/store/datastore/sql/postgres/files/users.sql @@ -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 diff --git a/store/datastore/sql/postgres/sql_gen.go b/store/datastore/sql/postgres/sql_gen.go index 44356a447..3c11d1679 100644 --- a/store/datastore/sql/postgres/sql_gen.go +++ b/store/datastore/sql/postgres/sql_gen.go @@ -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 +` diff --git a/store/datastore/sql/sqlite/files/logs.sql b/store/datastore/sql/sqlite/files/logs.sql new file mode 100644 index 000000000..7473d7dcc --- /dev/null +++ b/store/datastore/sql/sqlite/files/logs.sql @@ -0,0 +1,9 @@ +-- name: logs-find-proc + +SELECT + log_id +,log_job_id +,log_data +FROM logs +WHERE log_job_id = ? +LIMIT 1 diff --git a/store/datastore/sql/sqlite/files/user.sql b/store/datastore/sql/sqlite/files/user.sql new file mode 100644 index 000000000..62c98eb99 --- /dev/null +++ b/store/datastore/sql/sqlite/files/user.sql @@ -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 = ? diff --git a/store/datastore/sql/sqlite/sql_gen.go b/store/datastore/sql/sqlite/sql_gen.go index 1728b2cc3..5ac34dbd1 100644 --- a/store/datastore/sql/sqlite/sql_gen.go +++ b/store/datastore/sql/sqlite/sql_gen.go @@ -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 = ? +` diff --git a/store/datastore/users.go b/store/datastore/users.go index 41df6ca44..ba432c346 100644 --- a/store/datastore/users.go +++ b/store/datastore/users.go @@ -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=? -`