mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2024-11-19 08:21:01 +00:00
183 lines
4.4 KiB
Go
183 lines
4.4 KiB
Go
package migrate
|
|
|
|
import (
|
|
"github.com/drone/drone/Godeps/_workspace/src/github.com/BurntSushi/migration"
|
|
)
|
|
|
|
// 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,
|
|
agentTable,
|
|
}
|
|
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)
|
|
,user_avatar VARCHAR(255)
|
|
,user_admin BOOLEAN
|
|
,user_active BOOLEAN
|
|
,UNIQUE(user_login)
|
|
);
|
|
`
|
|
|
|
var repoTable = `
|
|
CREATE TABLE IF NOT EXISTS repos (
|
|
repo_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
,repo_user_id INTEGER
|
|
,repo_owner VARCHAR(255)
|
|
,repo_name VARCHAR(255)
|
|
,repo_full_name VARCHAR(1024)
|
|
,repo_self VARCHAR(1024)
|
|
,repo_link VARCHAR(1024)
|
|
,repo_clone VARCHAR(1024)
|
|
,repo_branch VARCHAR(255)
|
|
,repo_private BOOLEAN
|
|
,repo_trusted BOOLEAN
|
|
,repo_timeout INTEGER
|
|
,repo_keys_public BLOB
|
|
,repo_keys_private BLOB
|
|
,repo_hooks_pull_request BOOLEAN
|
|
,repo_hooks_push BOOLEAN
|
|
,repo_hooks_tags BOOLEAN
|
|
,repo_params BLOB
|
|
|
|
,UNIQUE(repo_owner, repo_name)
|
|
,UNIQUE(repo_full_name)
|
|
);
|
|
`
|
|
|
|
var repoUserIndex = `
|
|
CREATE INDEX repos_user_idx ON repos (repo_user_id);
|
|
`
|
|
|
|
var starTable = `
|
|
CREATE TABLE IF NOT EXISTS stars (
|
|
star_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
,star_user_id INTEGER
|
|
,star_repo_id INTEGER
|
|
,UNIQUE (star_repo_id, star_user_id)
|
|
);
|
|
`
|
|
|
|
var commitTable = `
|
|
CREATE TABLE IF NOT EXISTS commits (
|
|
commit_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
,commit_repo_id INTEGER
|
|
,commit_sequence INTEGER
|
|
,commit_state VARCHAR(255)
|
|
,commit_started INTEGER
|
|
,commit_finished INTEGER
|
|
,commit_sha VARCHAR(255)
|
|
,commit_ref VARCHAR(255)
|
|
,commit_branch VARCHAR(255)
|
|
,commit_pull_request VARCHAR(255)
|
|
,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
|
|
,UNIQUE(commit_repo_id, commit_sequence)
|
|
);
|
|
`
|
|
|
|
var commitRepoIndex = `
|
|
CREATE INDEX commits_repo_idx ON commits (commit_repo_id);
|
|
`
|
|
|
|
var tokenTable = `
|
|
CREATE TABLE IF NOT EXISTS tokens (
|
|
token_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
,token_user_id INTEGER
|
|
,token_kind VARCHAR(255)
|
|
,token_label VARCHAR(255)
|
|
,token_expiry INTEGER
|
|
,token_issued INTEGER
|
|
,UNIQUE(token_user_id, token_label)
|
|
);
|
|
`
|
|
|
|
var tokenUserIndex = `
|
|
CREATE INDEX tokens_user_idx ON tokens (token_user_id);
|
|
`
|
|
|
|
var buildTable = `
|
|
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)
|
|
);
|
|
`
|
|
|
|
var buildCommitIndex = `
|
|
CREATE INDEX ix_job_build_id ON jobs (job_build_id);
|
|
`
|
|
|
|
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)
|
|
);
|
|
`
|
|
|
|
var agentTable = `
|
|
CREATE TABLE IF NOT EXISTS agents (
|
|
agent_id INTEGER PRIMARY KEY AUTOINCREMENT
|
|
,agent_commit_id INTEGER
|
|
,agent_addr VARCHAR(2000)
|
|
,UNIQUE(agent_commit_id)
|
|
);
|
|
`
|