Fix some column type to work with both mysql and sqlite.

Basically this is caused by sqlite actually doesn't have any
column type restriction. also save gob type column as blob to enforce
byte-string value, since varchar with utf8 collation type will take at
least 3 byte to store, this will break YAML-Gob decoding at `params`
column.

Also fix some typo, and clean up fixtures code.
This commit is contained in:
Nurahmadie 2014-03-14 11:35:38 +00:00
parent 2540a5fb3e
commit 571f7d02b0
6 changed files with 33 additions and 38 deletions

View file

@ -16,7 +16,7 @@ SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits FROM commits
WHERE repo_id = ? AND branch = ? WHERE repo_id = ? AND branch = ?
ORDER BY created DESC ORDER BY created DESC, id DESC
LIMIT 10 LIMIT 10
` `
@ -26,7 +26,7 @@ SELECT id, repo_id, status, started, finished, duration,
hash, branch, pull_request, author, gravatar, timestamp, message, created, updated hash, branch, pull_request, author, gravatar, timestamp, message, created, updated
FROM commits FROM commits
WHERE repo_id = ? AND branch = ? WHERE repo_id = ? AND branch = ?
ORDER BY created DESC ORDER BY created DESC, id DESC
LIMIT 1 LIMIT 1
` `
@ -57,7 +57,7 @@ WHERE r.user_id = ?
AND r.team_id = 0 AND r.team_id = 0
AND r.id = c.repo_id AND r.id = c.repo_id
AND c.status IN ('Success', 'Failure') AND c.status IN ('Success', 'Failure')
ORDER BY c.created desc ORDER BY c.created desc, c.id desc
LIMIT 10 LIMIT 10
` `
@ -70,7 +70,7 @@ FROM repos r, commits c
WHERE r.team_id = ? WHERE r.team_id = ?
AND r.id = c.repo_id AND r.id = c.repo_id
AND c.status IN ('Success', 'Failure') AND c.status IN ('Success', 'Failure')
ORDER BY c.created desc ORDER BY c.created desc, c.id desc
LIMIT 10 LIMIT 10
` `

View file

@ -45,7 +45,7 @@ func (r *rev1st) Up(mg *MigrationDriver) error {
t.Integer("id", PRIMARYKEY, AUTOINCREMENT), t.Integer("id", PRIMARYKEY, AUTOINCREMENT),
t.Integer("team_id"), t.Integer("team_id"),
t.Integer("user_id"), t.Integer("user_id"),
t.Integer("role"), t.String("role"),
}); err != nil { }); err != nil {
return err return err
} }
@ -67,7 +67,7 @@ func (r *rev1st) Up(mg *MigrationDriver) error {
t.String("password"), t.String("password"),
t.Varchar("public_key", 1024), t.Varchar("public_key", 1024),
t.Varchar("private_key", 1024), t.Varchar("private_key", 1024),
t.Varchar("params", 2000), t.Blob("params"),
t.Timestamp("created"), t.Timestamp("created"),
t.Timestamp("updated"), t.Timestamp("updated"),
t.Integer("user_id"), t.Integer("user_id"),

View file

@ -42,6 +42,10 @@ func (c *columnType) Text(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s TEXT %s", colName, c.parseAttr(spec)) return fmt.Sprintf("%s TEXT %s", colName, c.parseAttr(spec))
} }
func (c *columnType) Blob(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s BLOB %s", colName, c.parseAttr(spec))
}
func (c *columnType) Timestamp(colName string, spec ...interface{}) string { func (c *columnType) Timestamp(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s TIMESTAMP %s", colName, c.parseAttr(spec)) return fmt.Sprintf("%s TIMESTAMP %s", colName, c.parseAttr(spec))
} }

View file

@ -16,31 +16,31 @@ func TestGetCommit(t *testing.T) {
} }
if commit.ID != 1 { if commit.ID != 1 {
t.Errorf("Exepected ID %d, got %d", 1, commit.ID) t.Errorf("Expected ID %d, got %d", 1, commit.ID)
} }
if commit.Status != "Success" { if commit.Status != "Success" {
t.Errorf("Exepected Status %s, got %s", "Success", commit.Status) t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
} }
if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" { if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
t.Errorf("Exepected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash) t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
} }
if commit.Branch != "master" { if commit.Branch != "master" {
t.Errorf("Exepected Branch %s, got %s", "master", commit.Branch) t.Errorf("Expected Branch %s, got %s", "master", commit.Branch)
} }
if commit.Author != "brad.rydzewski@gmail.com" { if commit.Author != "brad.rydzewski@gmail.com" {
t.Errorf("Exepected Author %s, got %s", "master", commit.Author) t.Errorf("Expected Author %s, got %s", "master", commit.Author)
} }
if commit.Message != "commit message" { if commit.Message != "commit message" {
t.Errorf("Exepected Message %s, got %s", "master", commit.Message) t.Errorf("Expected Message %s, got %s", "master", commit.Message)
} }
if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" { if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" {
t.Errorf("Exepected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar) t.Errorf("Expected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar)
} }
} }
@ -54,15 +54,15 @@ func TestGetCommitHash(t *testing.T) {
} }
if commit.ID != 1 { if commit.ID != 1 {
t.Errorf("Exepected ID %d, got %d", 1, commit.ID) t.Errorf("Expected ID %d, got %d", 1, commit.ID)
} }
if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" { if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
t.Errorf("Exepected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash) t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
} }
if commit.Status != "Success" { if commit.Status != "Success" {
t.Errorf("Exepected Status %s, got %s", "Success", commit.Status) t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
} }
} }
@ -91,11 +91,11 @@ func TestSaveCommit(t *testing.T) {
} }
if commit.Hash != updatedCommit.Hash { if commit.Hash != updatedCommit.Hash {
t.Errorf("Exepected Hash %s, got %s", updatedCommit.Hash, commit.Hash) t.Errorf("Expected Hash %s, got %s", updatedCommit.Hash, commit.Hash)
} }
if commit.Status != "Failing" { if commit.Status != "Failing" {
t.Errorf("Exepected Status %s, got %s", updatedCommit.Status, commit.Status) t.Errorf("Expected Status %s, got %s", updatedCommit.Status, commit.Status)
} }
} }
@ -126,7 +126,7 @@ func TestListCommits(t *testing.T) {
// verify commit count // verify commit count
if len(commits) != 2 { if len(commits) != 2 {
t.Errorf("Exepected %d commits in database, got %d", 2, len(commits)) t.Errorf("Expected %d commits in database, got %d", 2, len(commits))
return return
} }
@ -135,30 +135,30 @@ func TestListCommits(t *testing.T) {
commit := commits[1] // TODO something strange is happening with ordering here commit := commits[1] // TODO something strange is happening with ordering here
if commit.ID != 1 { if commit.ID != 1 {
t.Errorf("Exepected ID %d, got %d", 1, commit.ID) t.Errorf("Expected ID %d, got %d", 1, commit.ID)
} }
if commit.Status != "Success" { if commit.Status != "Success" {
t.Errorf("Exepected Status %s, got %s", "Success", commit.Status) t.Errorf("Expected Status %s, got %s", "Success", commit.Status)
} }
if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" { if commit.Hash != "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608" {
t.Errorf("Exepected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash) t.Errorf("Expected Hash %s, got %s", "4f4c4594be6d6ddbc1c0dd521334f7ecba92b608", commit.Hash)
} }
if commit.Branch != "master" { if commit.Branch != "master" {
t.Errorf("Exepected Branch %s, got %s", "master", commit.Branch) t.Errorf("Expected Branch %s, got %s", "master", commit.Branch)
} }
if commit.Author != "brad.rydzewski@gmail.com" { if commit.Author != "brad.rydzewski@gmail.com" {
t.Errorf("Exepected Author %s, got %s", "master", commit.Author) t.Errorf("Expected Author %s, got %s", "master", commit.Author)
} }
if commit.Message != "commit message" { if commit.Message != "commit message" {
t.Errorf("Exepected Message %s, got %s", "master", commit.Message) t.Errorf("Expected Message %s, got %s", "master", commit.Message)
} }
if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" { if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" {
t.Errorf("Exepected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar) t.Errorf("Expected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar)
} }
} }

View file

@ -64,21 +64,21 @@ func TestIsMemberAdmin(t *testing.T) {
if ok, err := database.IsMemberAdmin(1, 1); err != nil { if ok, err := database.IsMemberAdmin(1, 1); err != nil {
t.Error(err) t.Error(err)
} else if !ok { } else if !ok {
t.Errorf("Expected IsMemberAdmin to return true, returned false") t.Errorf("Expected user id 1 IsMemberAdmin to return true, returned false")
} }
// expecting user is Admin // expecting user is Admin
if ok, err := database.IsMemberAdmin(2, 1); err != nil { if ok, err := database.IsMemberAdmin(2, 1); err != nil {
t.Error(err) t.Error(err)
} else if !ok { } else if !ok {
t.Errorf("Expected IsMemberAdmin to return true, returned false") t.Errorf("Expected user id 2 IsMemberAdmin to return true, returned false")
} }
// expecting user is NOT Admin (Write role) // expecting user is NOT Admin (Write role)
if ok, err := database.IsMemberAdmin(3, 1); err != nil { if ok, err := database.IsMemberAdmin(3, 1); err != nil {
t.Error(err) t.Error(err)
} else if ok { } else if ok {
t.Errorf("Expected IsMemberAdmin to return false, returned true") t.Errorf("Expected user id 3 IsMemberAdmin to return false, returned true")
} }
} }

View file

@ -2,22 +2,16 @@ package database
import ( import (
"crypto/aes" "crypto/aes"
"database/sql"
"log" "log"
"github.com/drone/drone/pkg/database" "github.com/drone/drone/pkg/database"
"github.com/drone/drone/pkg/database/encrypt" "github.com/drone/drone/pkg/database/encrypt"
"github.com/drone/drone/pkg/database/migrate"
. "github.com/drone/drone/pkg/model" . "github.com/drone/drone/pkg/model"
_ "github.com/mattn/go-sqlite3" _ "github.com/mattn/go-sqlite3"
"github.com/russross/meddler" "github.com/russross/meddler"
) )
// in-memory database used for
// unit testing purposes.
var db *sql.DB
func init() { func init() {
// create a cipher for ecnrypting and decrypting // create a cipher for ecnrypting and decrypting
// database fields // database fields
@ -30,9 +24,6 @@ func init() {
// decrypt database fields. // decrypt database fields.
meddler.Register("gobencrypt", &encrypt.EncryptedField{cipher}) meddler.Register("gobencrypt", &encrypt.EncryptedField{cipher})
// notify meddler that we are working with sqlite
meddler.Default = meddler.SQLite
migrate.Driver = migrate.SQLite
} }
func Setup() { func Setup() {