From 571f7d02b0d224466a1c2884f254a4f2b362a2aa Mon Sep 17 00:00:00 2001 From: Nurahmadie Date: Fri, 14 Mar 2014 11:35:38 +0000 Subject: [PATCH] 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. --- pkg/database/commits.go | 8 +++--- pkg/database/migrate/1_setup_tables.go | 4 +-- pkg/database/migrate/column_type.go | 4 +++ pkg/database/testing/commits_test.go | 40 +++++++++++++------------- pkg/database/testing/members_test.go | 6 ++-- pkg/database/testing/testing.go | 9 ------ 6 files changed, 33 insertions(+), 38 deletions(-) diff --git a/pkg/database/commits.go b/pkg/database/commits.go index 74e0d86b4..883d3d065 100644 --- a/pkg/database/commits.go +++ b/pkg/database/commits.go @@ -16,7 +16,7 @@ SELECT id, repo_id, status, started, finished, duration, hash, branch, pull_request, author, gravatar, timestamp, message, created, updated FROM commits WHERE repo_id = ? AND branch = ? -ORDER BY created DESC +ORDER BY created DESC, id DESC LIMIT 10 ` @@ -26,7 +26,7 @@ SELECT id, repo_id, status, started, finished, duration, hash, branch, pull_request, author, gravatar, timestamp, message, created, updated FROM commits WHERE repo_id = ? AND branch = ? -ORDER BY created DESC +ORDER BY created DESC, id DESC LIMIT 1 ` @@ -57,7 +57,7 @@ WHERE r.user_id = ? AND r.team_id = 0 AND r.id = c.repo_id AND c.status IN ('Success', 'Failure') -ORDER BY c.created desc +ORDER BY c.created desc, c.id desc LIMIT 10 ` @@ -70,7 +70,7 @@ FROM repos r, commits c WHERE r.team_id = ? AND r.id = c.repo_id AND c.status IN ('Success', 'Failure') -ORDER BY c.created desc +ORDER BY c.created desc, c.id desc LIMIT 10 ` diff --git a/pkg/database/migrate/1_setup_tables.go b/pkg/database/migrate/1_setup_tables.go index 92d62ab32..79dfcdf2f 100644 --- a/pkg/database/migrate/1_setup_tables.go +++ b/pkg/database/migrate/1_setup_tables.go @@ -45,7 +45,7 @@ func (r *rev1st) Up(mg *MigrationDriver) error { t.Integer("id", PRIMARYKEY, AUTOINCREMENT), t.Integer("team_id"), t.Integer("user_id"), - t.Integer("role"), + t.String("role"), }); err != nil { return err } @@ -67,7 +67,7 @@ func (r *rev1st) Up(mg *MigrationDriver) error { t.String("password"), t.Varchar("public_key", 1024), t.Varchar("private_key", 1024), - t.Varchar("params", 2000), + t.Blob("params"), t.Timestamp("created"), t.Timestamp("updated"), t.Integer("user_id"), diff --git a/pkg/database/migrate/column_type.go b/pkg/database/migrate/column_type.go index 02e44deb3..6a6286283 100644 --- a/pkg/database/migrate/column_type.go +++ b/pkg/database/migrate/column_type.go @@ -42,6 +42,10 @@ func (c *columnType) Text(colName string, spec ...interface{}) string { 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 { return fmt.Sprintf("%s TIMESTAMP %s", colName, c.parseAttr(spec)) } diff --git a/pkg/database/testing/commits_test.go b/pkg/database/testing/commits_test.go index 1f6b8b9de..59c7f53c2 100644 --- a/pkg/database/testing/commits_test.go +++ b/pkg/database/testing/commits_test.go @@ -16,31 +16,31 @@ func TestGetCommit(t *testing.T) { } 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" { - t.Errorf("Exepected Status %s, got %s", "Success", commit.Status) + t.Errorf("Expected Status %s, got %s", "Success", commit.Status) } 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" { - 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" { - 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" { - t.Errorf("Exepected Message %s, got %s", "master", commit.Message) + t.Errorf("Expected Message %s, got %s", "master", commit.Message) } 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 { - t.Errorf("Exepected ID %d, got %d", 1, commit.ID) + t.Errorf("Expected ID %d, got %d", 1, commit.ID) } 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" { - 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 { - 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" { - 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 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 } @@ -135,30 +135,30 @@ func TestListCommits(t *testing.T) { commit := commits[1] // TODO something strange is happening with ordering here 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" { - t.Errorf("Exepected Status %s, got %s", "Success", commit.Status) + t.Errorf("Expected Status %s, got %s", "Success", commit.Status) } 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" { - 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" { - 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" { - t.Errorf("Exepected Message %s, got %s", "master", commit.Message) + t.Errorf("Expected Message %s, got %s", "master", commit.Message) } if commit.Gravatar != "8c58a0be77ee441bb8f8595b7f1b4e87" { - t.Errorf("Exepected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar) + t.Errorf("Expected Gravatar %s, got %s", "8c58a0be77ee441bb8f8595b7f1b4e87", commit.Gravatar) } } diff --git a/pkg/database/testing/members_test.go b/pkg/database/testing/members_test.go index aeebcf98a..c23c1a115 100644 --- a/pkg/database/testing/members_test.go +++ b/pkg/database/testing/members_test.go @@ -64,21 +64,21 @@ func TestIsMemberAdmin(t *testing.T) { if ok, err := database.IsMemberAdmin(1, 1); err != nil { t.Error(err) } 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 if ok, err := database.IsMemberAdmin(2, 1); err != nil { t.Error(err) } 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) if ok, err := database.IsMemberAdmin(3, 1); err != nil { t.Error(err) } else if ok { - t.Errorf("Expected IsMemberAdmin to return false, returned true") + t.Errorf("Expected user id 3 IsMemberAdmin to return false, returned true") } } diff --git a/pkg/database/testing/testing.go b/pkg/database/testing/testing.go index 9c8e5c79a..57f08390b 100644 --- a/pkg/database/testing/testing.go +++ b/pkg/database/testing/testing.go @@ -2,22 +2,16 @@ package database import ( "crypto/aes" - "database/sql" "log" "github.com/drone/drone/pkg/database" "github.com/drone/drone/pkg/database/encrypt" - "github.com/drone/drone/pkg/database/migrate" . "github.com/drone/drone/pkg/model" _ "github.com/mattn/go-sqlite3" "github.com/russross/meddler" ) -// in-memory database used for -// unit testing purposes. -var db *sql.DB - func init() { // create a cipher for ecnrypting and decrypting // database fields @@ -30,9 +24,6 @@ func init() { // decrypt database fields. meddler.Register("gobencrypt", &encrypt.EncryptedField{cipher}) - // notify meddler that we are working with sqlite - meddler.Default = meddler.SQLite - migrate.Driver = migrate.SQLite } func Setup() {