From ffd42a1a0e8c82536635bafdbb83a978b617e1cf Mon Sep 17 00:00:00 2001 From: Brad Rydzewski Date: Mon, 19 Oct 2015 11:45:53 -0700 Subject: [PATCH] use helper function to create test db connection for planned pg support --- model/build_test.go | 2 +- model/job_test.go | 2 +- model/key_test.go | 2 +- model/log_test.go | 2 +- model/node_test.go | 2 +- model/repo_test.go | 2 +- model/user_test.go | 2 +- remote/context.go | 30 +++++++++++------------------ shared/database/database.go | 13 +++++++++++++ shared/database/postgres/1_init.sql | 30 ++++++++++++++--------------- 10 files changed, 46 insertions(+), 41 deletions(-) diff --git a/model/build_test.go b/model/build_test.go index 10ff5314a..3e040cf67 100644 --- a/model/build_test.go +++ b/model/build_test.go @@ -8,7 +8,7 @@ import ( ) func TestBuild(t *testing.T) { - db := database.Open("sqlite3", ":memory:") + db := database.OpenTest() defer db.Close() g := goblin.Goblin(t) diff --git a/model/job_test.go b/model/job_test.go index 229176037..527450280 100644 --- a/model/job_test.go +++ b/model/job_test.go @@ -8,7 +8,7 @@ import ( ) func TestJob(t *testing.T) { - db := database.Open("sqlite3", ":memory:") + db := database.OpenTest() defer db.Close() g := goblin.Goblin(t) diff --git a/model/key_test.go b/model/key_test.go index 398e9f609..19615bf4f 100644 --- a/model/key_test.go +++ b/model/key_test.go @@ -8,7 +8,7 @@ import ( ) func TestKey(t *testing.T) { - db := database.Open("sqlite3", ":memory:") + db := database.OpenTest() defer db.Close() g := goblin.Goblin(t) diff --git a/model/log_test.go b/model/log_test.go index af50189e0..9b12851b0 100644 --- a/model/log_test.go +++ b/model/log_test.go @@ -10,7 +10,7 @@ import ( ) func TestLog(t *testing.T) { - db := database.Open("sqlite3", ":memory:") + db := database.OpenTest() defer db.Close() g := goblin.Goblin(t) diff --git a/model/node_test.go b/model/node_test.go index 9de6ec4aa..782c785ff 100644 --- a/model/node_test.go +++ b/model/node_test.go @@ -8,7 +8,7 @@ import ( ) func TestNode(t *testing.T) { - db := database.Open("sqlite3", ":memory:") + db := database.OpenTest() defer db.Close() g := goblin.Goblin(t) diff --git a/model/repo_test.go b/model/repo_test.go index 09270ba57..721e8a8b3 100644 --- a/model/repo_test.go +++ b/model/repo_test.go @@ -8,7 +8,7 @@ import ( ) func TestRepostore(t *testing.T) { - db := database.Open("sqlite3", ":memory:") + db := database.OpenTest() defer db.Close() g := goblin.Goblin(t) diff --git a/model/user_test.go b/model/user_test.go index cca270ac1..810b57b27 100644 --- a/model/user_test.go +++ b/model/user_test.go @@ -8,7 +8,7 @@ import ( ) func TestUserstore(t *testing.T) { - db := database.Open("sqlite3", ":memory:") + db := database.OpenTest() defer db.Close() g := goblin.Goblin(t) diff --git a/remote/context.go b/remote/context.go index 19eeb895d..a34e2ab68 100644 --- a/remote/context.go +++ b/remote/context.go @@ -4,28 +4,20 @@ import ( "code.google.com/p/go.net/context" ) -const reqkey = "remote" +const key = "remote" -// NewContext returns a Context whose Value method returns -// the applications Remote instance. -func NewContext(parent context.Context, v Remote) context.Context { - return &wrapper{parent, v} -} - -type wrapper struct { - context.Context - v Remote -} - -// Value returns the named key from the context. -func (c *wrapper) Value(key interface{}) interface{} { - if key == reqkey { - return c.v - } - return c.Context.Value(key) +// Setter defines a context that enables setting values. +type Setter interface { + Set(string, interface{}) } // FromContext returns the Remote associated with this context. func FromContext(c context.Context) Remote { - return c.Value(reqkey).(Remote) + return c.Value(key).(Remote) +} + +// ToContext adds the Remote to this context if it supports +// the Setter interface. +func ToContext(c Setter, r Remote) { + c.Set(key, r) } diff --git a/shared/database/database.go b/shared/database/database.go index 6b74d34e7..3b0d415ac 100644 --- a/shared/database/database.go +++ b/shared/database/database.go @@ -4,6 +4,7 @@ package database import ( "database/sql" + "os" "github.com/drone/drone/shared/envconfig" @@ -49,3 +50,15 @@ func Open(driver, config string) *sql.DB { } return db } + +func OpenTest() *sql.DB { + var ( + driver = "sqlite3" + config = ":memory:" + ) + if os.Getenv("DATABASE_DRIVER") != "" { + driver = os.Getenv("DATABASE_DRIVER") + config = os.Getenv("DATABASE_CONFIG") + } + return Open(driver, config) +} diff --git a/shared/database/postgres/1_init.sql b/shared/database/postgres/1_init.sql index 740070b07..0f243c158 100644 --- a/shared/database/postgres/1_init.sql +++ b/shared/database/postgres/1_init.sql @@ -2,15 +2,15 @@ CREATE TABLE users ( user_id SERIAL PRIMARY KEY -,user_login VARCHAR(500) -,user_token VARCHAR(500) -,user_secret VARCHAR(500) +,user_login VARCHAR(40) +,user_token VARCHAR(128) +,user_secret VARCHAR(128) ,user_expiry INTEGER -,user_email VARCHAR(500) -,user_avatar VARCHAR(500) +,user_email VARCHAR(256) +,user_avatar VARCHAR(256) ,user_active BOOLEAN ,user_admin BOOLEAN -,user_hash VARCHAR(500) +,user_hash VARCHAR(128) ,UNIQUE(user_login) ); @@ -60,21 +60,21 @@ CREATE TABLE builds ( build_id SERIAL PRIMARY KEY ,build_repo_id INTEGER ,build_number INTEGER -,build_event VARCHAR(500) -,build_status VARCHAR(500) +,build_event VARCHAR(25) +,build_status VARCHAR(25) ,build_enqueued INTEGER ,build_created INTEGER ,build_started INTEGER ,build_finished INTEGER -,build_commit VARCHAR(500) -,build_branch VARCHAR(500) -,build_ref VARCHAR(500) -,build_refspec VARCHAR(1000) -,build_remote VARCHAR(500) +,build_commit VARCHAR(40) +,build_branch VARCHAR(256) +,build_ref VARCHAR(512) +,build_refspec VARCHAR(512) +,build_remote VARCHAR(512) ,build_title VARCHAR(1000) ,build_message VARCHAR(2000) ,build_timestamp INTEGER -,build_author VARCHAR(500) +,build_author VARCHAR(40) ,build_avatar VARCHAR(1000) ,build_email VARCHAR(500) ,build_link VARCHAR(1000) @@ -90,7 +90,7 @@ CREATE TABLE jobs ( ,job_node_id INTEGER ,job_build_id INTEGER ,job_number INTEGER -,job_status VARCHAR(500) +,job_status VARCHAR(25) ,job_exit_code INTEGER ,job_started INTEGER ,job_enqueued INTEGER