2015-05-11 07:45:31 +00:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
2015-05-22 18:37:40 +00:00
|
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/BurntSushi/migration"
|
2015-05-11 07:45:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// Setup is the database migration function that
|
|
|
|
// will setup the initial SQL database structure.
|
|
|
|
func Setup(tx migration.LimitedTx) error {
|
|
|
|
var stmts = []string{
|
|
|
|
userTable,
|
|
|
|
starTable,
|
|
|
|
repoTable,
|
|
|
|
repoUserIndex,
|
|
|
|
commitTable,
|
|
|
|
commitRepoIndex,
|
|
|
|
tokenTable,
|
|
|
|
buildTable,
|
|
|
|
buildCommitIndex,
|
|
|
|
statusTable,
|
|
|
|
statusCommitIndex,
|
|
|
|
blobTable,
|
2015-05-18 22:47:13 +00:00
|
|
|
agentTable,
|
2015-05-11 07:45:31 +00:00
|
|
|
}
|
|
|
|
for _, stmt := range stmts {
|
|
|
|
_, err := tx.Exec(transform(stmt))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var userTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
|
|
user_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
|
|
,user_login VARCHAR(255)
|
|
|
|
,user_token VARCHAR(255)
|
|
|
|
,user_secret VARCHAR(255)
|
|
|
|
,user_email VARCHAR(255)
|
2015-06-18 23:37:40 +00:00
|
|
|
,user_avatar VARCHAR(255)
|
2015-05-11 07:45:31 +00:00
|
|
|
,user_admin BOOLEAN
|
|
|
|
,user_active BOOLEAN
|
|
|
|
,UNIQUE(user_login)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
var repoTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS repos (
|
|
|
|
repo_id INTEGER PRIMARY KEY AUTOINCREMENT
|
2015-06-13 23:48:06 +00:00
|
|
|
,repo_user_id INTEGER
|
2015-05-11 07:45:31 +00:00
|
|
|
,repo_owner VARCHAR(255)
|
|
|
|
,repo_name VARCHAR(255)
|
2015-06-13 23:48:06 +00:00
|
|
|
,repo_full_name VARCHAR(1024)
|
2015-05-11 07:45:31 +00:00
|
|
|
,repo_token VARCHAR(255)
|
2015-06-13 23:48:06 +00:00
|
|
|
,repo_language VARCHAR(255)
|
2015-05-11 07:45:31 +00:00
|
|
|
,repo_branch VARCHAR(255)
|
|
|
|
,repo_private BOOLEAN
|
|
|
|
,repo_trusted BOOLEAN
|
2015-05-13 06:58:30 +00:00
|
|
|
,repo_self VARCHAR(1024)
|
2015-05-11 07:45:31 +00:00
|
|
|
,repo_link VARCHAR(1024)
|
|
|
|
,repo_clone VARCHAR(1024)
|
2015-06-13 23:48:06 +00:00
|
|
|
,repo_post_commit BOOLEAN
|
|
|
|
,repo_pull_request BOOLEAN
|
2015-05-11 07:45:31 +00:00
|
|
|
,repo_public_key BLOB
|
|
|
|
,repo_private_key BLOB
|
|
|
|
,repo_params BLOB
|
|
|
|
,repo_timeout INTEGER
|
|
|
|
,repo_created INTEGER
|
|
|
|
,repo_updated INTEGER
|
|
|
|
,UNIQUE(repo_owner, repo_name)
|
2015-06-13 23:48:06 +00:00
|
|
|
,UNIQUE(repo_full_name)
|
2015-05-11 07:45:31 +00:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
var repoUserIndex = `
|
2015-06-13 23:48:06 +00:00
|
|
|
CREATE INDEX repos_user_idx ON repos (repo_user_id);
|
|
|
|
`
|
|
|
|
|
2015-05-11 07:45:31 +00:00
|
|
|
var starTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS stars (
|
2015-06-14 00:49:21 +00:00
|
|
|
star_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
|
|
,star_user_id INTEGER
|
|
|
|
,star_repo_id INTEGER
|
|
|
|
,UNIQUE (star_repo_id, star_user_id)
|
2015-05-11 07:45:31 +00:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
var commitTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS commits (
|
|
|
|
commit_id INTEGER PRIMARY KEY AUTOINCREMENT
|
2015-06-14 01:40:07 +00:00
|
|
|
,commit_repo_id INTEGER
|
|
|
|
,commit_sequence INTEGER
|
2015-05-11 07:45:31 +00:00
|
|
|
,commit_state VARCHAR(255)
|
|
|
|
,commit_started INTEGER
|
|
|
|
,commit_finished INTEGER
|
|
|
|
,commit_sha VARCHAR(255)
|
|
|
|
,commit_ref VARCHAR(255)
|
|
|
|
,commit_branch VARCHAR(255)
|
2015-06-14 01:40:07 +00:00
|
|
|
,commit_pull_request VARCHAR(255)
|
2015-05-11 07:45:31 +00:00
|
|
|
,commit_author VARCHAR(255)
|
|
|
|
,commit_gravatar VARCHAR(255)
|
|
|
|
,commit_timestamp VARCHAR(255)
|
|
|
|
,commit_message VARCHAR(1000)
|
|
|
|
,commit_source_remote VARCHAR(255)
|
|
|
|
,commit_source_branch VARCHAR(255)
|
|
|
|
,commit_source_sha VARCHAR(255)
|
|
|
|
,commit_created INTEGER
|
|
|
|
,commit_updated INTEGER
|
2015-06-14 01:40:07 +00:00
|
|
|
,UNIQUE(commit_repo_id, commit_sequence)
|
2015-05-11 07:45:31 +00:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
var commitRepoIndex = `
|
2015-06-14 01:40:07 +00:00
|
|
|
CREATE INDEX commits_repo_idx ON commits (commit_repo_id);
|
2015-05-11 07:45:31 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
var tokenTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS tokens (
|
|
|
|
token_id INTEGER PRIMARY KEY AUTOINCREMENT
|
2015-06-13 23:48:06 +00:00
|
|
|
,token_user_id INTEGER
|
2015-05-11 07:45:31 +00:00
|
|
|
,token_kind VARCHAR(255)
|
|
|
|
,token_label VARCHAR(255)
|
|
|
|
,token_expiry INTEGER
|
|
|
|
,token_issued INTEGER
|
2015-06-13 23:48:06 +00:00
|
|
|
,UNIQUE(token_user_id, token_label)
|
2015-05-11 07:45:31 +00:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
var tokenUserIndex = `
|
2015-06-13 23:48:06 +00:00
|
|
|
CREATE INDEX tokens_user_idx ON tokens (token_user_id);
|
2015-05-11 07:45:31 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
var buildTable = `
|
2015-06-19 00:36:52 +00:00
|
|
|
CREATE TABLE IF NOT EXISTS jobs (
|
|
|
|
job_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
|
|
,job_build_id INTEGER
|
|
|
|
,job_number INTEGER
|
|
|
|
,job_status VARCHAR(255)
|
|
|
|
,job_exit_code INTEGER
|
|
|
|
,job_started INTEGER
|
|
|
|
,job_finished INTEGER
|
|
|
|
,job_environment VARCHAR(2000)
|
|
|
|
,UNIQUE(job_build_id, job_number)
|
2015-05-11 07:45:31 +00:00
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
var buildCommitIndex = `
|
2015-06-19 00:36:52 +00:00
|
|
|
CREATE INDEX ix_job_build_id ON jobs (job_build_id);
|
2015-05-11 07:45:31 +00:00
|
|
|
`
|
|
|
|
|
|
|
|
var statusTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS status (
|
|
|
|
status_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
|
|
,commit_id INTEGER
|
|
|
|
,status_state VARCHAR(255)
|
|
|
|
,status_desc VARCHAR(2000)
|
|
|
|
,status_link VARCHAR(2000)
|
|
|
|
,status_context INTEGER
|
|
|
|
,status_attachment BOOL
|
|
|
|
,UNIQUE(commit_id, status_context)
|
|
|
|
);
|
|
|
|
`
|
|
|
|
|
|
|
|
var statusCommitIndex = `
|
|
|
|
CREATE INDEX status_commit_idx ON status (commit_id);
|
|
|
|
`
|
|
|
|
|
|
|
|
var blobTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS blobs (
|
|
|
|
blob_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
|
|
,blob_path VARCHAR(255)
|
|
|
|
,blob_data BLOB
|
|
|
|
,UNIQUE(blob_path)
|
|
|
|
);
|
|
|
|
`
|
2015-05-18 22:47:13 +00:00
|
|
|
|
|
|
|
var agentTable = `
|
|
|
|
CREATE TABLE IF NOT EXISTS agents (
|
|
|
|
agent_id INTEGER PRIMARY KEY AUTOINCREMENT
|
2015-06-14 00:37:36 +00:00
|
|
|
,agent_commit_id INTEGER
|
2015-05-18 22:47:13 +00:00
|
|
|
,agent_addr VARCHAR(2000)
|
2015-06-14 00:37:36 +00:00
|
|
|
,UNIQUE(agent_commit_id)
|
2015-05-18 22:47:13 +00:00
|
|
|
);
|
|
|
|
`
|