Skip to content

Commit

Permalink
unit testing postgres, mysql. temporary workaround for backticks
Browse files Browse the repository at this point in the history
  • Loading branch information
bradrydzewski committed Oct 20, 2015
1 parent 2dd4d66 commit 7f9ef94
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 83 deletions.
2 changes: 1 addition & 1 deletion .drone.sec
Original file line number Diff line number Diff line change
@@ -1 +1 @@
eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhHQ00ifQ.O4xkUcxY3P5xF8HpcChCbQUBJ7R-BC9wFrF698qHUvhbNmSZo9-jkrBvTG6Q4X73DHEmq9re-K6_coLIwrwjA8DW3EUny5cPpj1P0NccWwv1_HhLMfc7GToHbf2C4Sq7bDX-Omm3ghwLiw4-Gw04N5XfAxGWI0g99LsrnrhClRp6jR3pssyu0F_igZty-wgbZBc_zpTVbEEtvQoWJbF9CLIjuElFJc62BZVbqh1aNsVeYrIMJGI1vaYAWf9kGwKC4WSy9QehwggBpr3z-dNq6PG-FMuKVFSOyJubQMYTCSXM7fDoJR6497aQTKsHNncehREkJFrU4uwOAPa3QMD15A.qq6f_tSOp6Ybi46D.az1uQ5Moe9fzjckNl7wVg23LDVubURZ2hKa_rv2WnSbqPVZ89K7_m94KwDRGdSOvVrL5RJdWyk-7jWYF4RgoOkbHnhTesF18NHq4hGSan_EMagps_ZgJoBKMYsNoJaHZrr5_Jn-DJBjdRi8pjqf7RThWIpRyCtBTvlEQgBUcQqyuBZ6FA-vpBcu1VPD_7pUHVApIG3LY5ggDB4OvtJhcuYeZX76qLOMo1JAp3qRsVsFxXW1IeuIgEIKKe_bp7SOYR8L1KjZs5gDWjx9R0Q88jqAET-LHMBxedk0feb2yiRPbVEpCRfWfMbdqmHlY8mwWY71GGlYfeKvB8Q59WONsuacxCp9v.esSbar0HdGCrAjzoXapIyg
eyJhbGciOiJSU0EtT0FFUCIsImVuYyI6IkExMjhHQ00ifQ.KaxqyEtYnsyjK0qVgG9mfliQQJsTTrzWhJkv5gvHXClcMSQ7WF8ASonjDDMvQ9cfdgfb9GY9ZljfRYwKG1pkrTOzquXya_j6rPfYA0NxpJ5CQcv6w97NqW_7ZoHR3HLEqtMUomReYEFr_AGMfZJUYLCOWQZwzFQ-U1oSgpxNO0H1DaqhjcEkuPwcXnUvfKMJ1dRdvSFr0mHZAfIidkS-n1Jct5UsqAtgzORL_9g4WP7WmPDQuwqhOhxwYRd3eSg-nhhJv7B337Nnq1OcImS5WOgXf_7C_Hg_KzL6Ng4JPDF9fhXzAYCJ4-od1jlUH8-55BOPW81mG2cSe_CDUXoVgQ.kbk5v-POj3XudQhp.niD-kndAAwkNAbW_1Ew7EJTS8aKOpQ4WJ1-eS6rFLnLk0Z9my7uribsjnR-B-CzCs1AGpg2CBvWjEIFWoYuBBHqFi0_jXrDkh9JgqQM21_6hgd1FFMIsmFViuROZM2sCmai7XTKhFGs0sRT320F_-lE0lXTa7FLnwqHTzCRzjDG8wrYLuXSH0nIVbzTRUAhfiH8RzxI3n6YFqavoDfLve9d-HhJ11NsEIOzHDyCD0Lp41WBMz8BZ4-Vv5aoHLqgyn_AC03kPBweXskvXgRAM0TOTE0ko9JRVhok2CqNKW76JNveQwgZe08JIX-PHpLKkjghjeWFgtAD44FwObG7_FIb_n3gO.g_SaQlLuXLRCvfXZujgljw
14 changes: 13 additions & 1 deletion .drone.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,21 @@ build:
commands:
- /bin/bash contrib/setup-sqlite.sh 1> /dev/null
- /bin/bash contrib/setup-sassc.sh 1> /dev/null
- make deps gen test
- make deps gen
- make test test_postgres test_mysql
- make build build_static deb docs

compose:
postgres:
image: postgres:9.4.5
environment:
- POSTGRES_USER=postgres
mysql:
image: mysql:5.6.27
environment:
- MYSQL_DATABASE=test
- MYSQL_ALLOW_EMPTY_PASSWORD=yes

publish:
s3:
acl: public-read
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,14 @@ build_static:
test:
go test -cover $(PACKAGES)

# docker run --publish=3306:3306 -e MYSQL_DATABASE=test -e MYSQL_ALLOW_EMPTY_PASSWORD=yes mysql:5.6.27
test_mysql:
DATABASE_DRIVER="mysql" DATABASE_CONFIG="root@tcp(127.0.0.1:3306)/test?parseTime=true" go test github.com/drone/drone/model

# docker run --publish=5432:5432 postgres:9.4.5
test_postgres:
DATABASE_DRIVER="postgres" DATABASE_CONFIG="host=127.0.0.1 user=postgres dbname=postgres sslmode=disable" go test github.com/drone/drone/model

deb:
mkdir -p contrib/debian/drone/usr/local/bin
mkdir -p contrib/debian/drone/var/lib/drone
Expand Down
2 changes: 1 addition & 1 deletion model/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func GetBuildList(db meddler.DB, repo *Repo) ([]*Build, error) {

func CreateBuild(db meddler.DB, build *Build, jobs ...*Job) error {
var number int
db.QueryRow(buildNumberLast, build.RepoID).Scan(&number)
db.QueryRow(database.Rebind(buildNumberLast), build.RepoID).Scan(&number)
build.Number = number + 1
build.Created = time.Now().UTC().Unix()
build.Enqueued = build.Created
Expand Down
14 changes: 3 additions & 11 deletions model/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,6 @@ func DeleteKey(db meddler.DB, repo *Repo) error {

const keyTable = "keys"

const keyQuery = `
SELECT *
FROM keys
WHERE key_repo_id=?
LIMIT 1
`

const keyDeleteStmt = `
DELETE FROM keys
WHERE key_repo_id=?
`
const keyQuery = "SELECT * FROM `keys` WHERE key_repo_id=? LIMIT 1"

const keyDeleteStmt = "DELETE FROM `keys` WHERE key_repo_id=?"
2 changes: 1 addition & 1 deletion model/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestKey(t *testing.T) {
// before each test be sure to purge the package
// table data from the database.
g.BeforeEach(func() {
db.Exec("DELETE FROM keys")
db.Exec(database.Rebind("DELETE FROM `keys`"))
})

g.It("Should create a key", func() {
Expand Down
9 changes: 0 additions & 9 deletions model/repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,15 +100,6 @@ WHERE repo_full_name = ?
LIMIT 1;
`

const repoStarsQuery = `
SELECT r.*
FROM
repos r
,stars s
WHERE r.repo_id = s.star_repo_id
AND s.star_user_id = ?
`

const repoListQuery = `
SELECT *
FROM repos
Expand Down
30 changes: 0 additions & 30 deletions model/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,36 +86,6 @@ DELETE FROM users
WHERE user_id=?
`

// this query was referenced from
// http://stackoverflow.com/questions/2111384/sql-join-selecting-the-last-records-in-a-one-to-many-relationship/2111420#2111420
// const userRepoLatestQuery = `
// SELECT
// r.repo_owner
// ,r.repo_name
// ,r.repo_full_name
// ,r.repo_avatar
// ,b.build_number
// ,b.build_event
// ,b.build_status
// ,b.build_created
// ,b.build_started
// ,b.build_finished
// ,b.build_commit
// ,b.build_branch
// ,b.build_ref
// ,b.build_refspec
// ,b.build_remote
// ,b.build_title
// ,b.build_message
// ,b.build_author
// ,b.build_email
// FROM repos r
// JOIN builds b ON (r.repo_id = b.build_repo_id)
// LEFT OUTER JOIN builds bb ON (r.repo_id = bb.build_repo_id AND
// (b.build_number < bb.build_number OR b.build_number = bb.build_number AND b.build_id < bb.build_id AND b.build_author=?))
// WHERE bb.build_id IS NULL;
// `

const userFeedQuery = `
SELECT
repo_owner
Expand Down
1 change: 0 additions & 1 deletion model/user_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ func TestUserstore(t *testing.T) {
// table data from the database.
g.BeforeEach(func() {
db.Exec("DELETE FROM users")
db.Exec("DELETE FROM stars")
db.Exec("DELETE FROM repos")
db.Exec("DELETE FROM builds")
db.Exec("DELETE FROM jobs")
Expand Down
7 changes: 7 additions & 0 deletions shared/database/database.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
_ "github.com/lib/pq"
_ "github.com/mattn/go-sqlite3"
"github.com/rubenv/sql-migrate"
"github.com/russross/meddler"
)

func Load(env envconfig.Env) *sql.DB {
Expand All @@ -36,6 +37,12 @@ func Open(driver, config string) *sql.DB {
log.Errorln(err)
log.Fatalln("database connection failed")
}
switch driver {
case "mysql":
meddler.Default = meddler.MySQL
case "postgres":
meddler.Default = meddler.PostgreSQL
}

var migrations = &migrate.AssetMigrationSource{
Asset: Asset,
Expand Down
13 changes: 1 addition & 12 deletions shared/database/mysql/1_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ CREATE TABLE repos (
,UNIQUE(repo_full_name)
);

CREATE TABLE stars (
star_id INTEGER PRIMARY KEY AUTO_INCREMENT
,star_repo_id INTEGER
,star_user_id INTEGER

,UNIQUE(star_repo_id, star_user_id)
);

CREATE INDEX ix_star_user ON stars (star_user_id);

CREATE TABLE `keys` (
key_id INTEGER PRIMARY KEY AUTO_INCREMENT
,key_repo_id INTEGER
Expand Down Expand Up @@ -82,8 +72,7 @@ CREATE TABLE builds (
,UNIQUE(build_number, build_repo_id)
);

CREATE INDEX ix_build_repo ON builds (build_repo_id);
CREATE INDEX ix_build_author ON builds (build_author);
CREATE INDEX ix_build_repo ON builds (build_repo_id);

CREATE TABLE jobs (
job_id INTEGER PRIMARY KEY AUTO_INCREMENT
Expand Down
18 changes: 4 additions & 14 deletions shared/database/postgres/1_init.sql
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,6 @@ CREATE TABLE repos (
,UNIQUE(repo_full_name)
);

CREATE TABLE stars (
star_id SERIAL PRIMARY KEY
,star_repo_id INTEGER
,star_user_id INTEGER

,UNIQUE(star_repo_id, star_user_id)
);

CREATE INDEX ix_star_user ON builds (star_user_id);

CREATE TABLE keys (
key_id SERIAL PRIMARY KEY
,key_repo_id INTEGER
Expand Down Expand Up @@ -82,8 +72,7 @@ CREATE TABLE builds (
,UNIQUE(build_number, build_repo_id)
);

CREATE INDEX ix_build_repo ON builds (build_repo_id);
CREATE INDEX ix_build_author ON builds (build_author);
CREATE INDEX ix_build_repo ON builds (build_repo_id);

CREATE TABLE jobs (
job_id SERIAL PRIMARY KEY
Expand Down Expand Up @@ -121,8 +110,9 @@ CREATE TABLE IF NOT EXISTS nodes (
);


INSERT INTO nodes VALUES(null, 'unix:///var/run/docker.sock', 'linux_amd64', '', '', '');
INSERT INTO nodes VALUES(null, 'unix:///var/run/docker.sock', 'linux_amd64', '', '', '');
INSERT INTO nodes (node_addr, node_arch, node_cert, node_key, node_ca) VALUES
('unix:///var/run/docker.sock', 'linux_amd64', '', '', ''),
('unix:///var/run/docker.sock', 'linux_amd64', '', '', '');

-- +migrate Down

Expand Down
7 changes: 5 additions & 2 deletions shared/database/rebind.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ func Rebind(query string) string {
rqb := make([]byte, 0, len(qb)+5)
j := 1
for _, b := range qb {
if b == '?' {
switch b {
case '?':
rqb = append(rqb, '$')
for _, b := range strconv.Itoa(j) {
rqb = append(rqb, byte(b))
}
j++
} else {
case '`':
rqb = append(rqb, ' ')
default:
rqb = append(rqb, b)
}
}
Expand Down

0 comments on commit 7f9ef94

Please sign in to comment.