refactored migrate into package one level up

This commit is contained in:
Brad Rydzewski 2014-09-28 20:20:35 -07:00
parent e5cc46b4dd
commit ce052eaaf4
4 changed files with 1 additions and 222 deletions

View file

@ -5,7 +5,7 @@ import (
"os"
"github.com/drone/drone/server/datastore"
"github.com/drone/drone/server/datastore/database/migrate"
"github.com/drone/drone/server/datastore/migrate"
"github.com/BurntSushi/migration"
_ "github.com/go-sql-driver/mysql"

View file

@ -1,46 +0,0 @@
package migrate
import (
"strconv"
"strings"
"github.com/russross/meddler"
)
// transform is a helper function that transforms sql
// statements to work with multiple database types.
func transform(stmt string) string {
switch meddler.Default {
case meddler.MySQL:
stmt = strings.Replace(stmt, "AUTOINCREMENT", "AUTO_INCREMENT", -1)
case meddler.PostgreSQL:
stmt = strings.Replace(stmt, "INTEGER PRIMARY KEY AUTOINCREMENT", "SERIAL PRIMARY KEY", -1)
stmt = strings.Replace(stmt, "BLOB", "BYTEA", -1)
}
return stmt
}
// rebind is a helper function that changes the sql
// bind type from ? to $ for postgres queries.
func rebind(query string) string {
if meddler.Default != meddler.PostgreSQL {
return query
}
qb := []byte(query)
// Add space enough for 10 params before we have to allocate
rqb := make([]byte, 0, len(qb)+10)
j := 1
for _, b := range qb {
if b == '?' {
rqb = append(rqb, '$')
for _, b := range strconv.Itoa(j) {
rqb = append(rqb, byte(b))
}
j++
} else {
rqb = append(rqb, b)
}
}
return string(rqb)
}

View file

@ -1,118 +0,0 @@
package migrate
import (
"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{
blobTable,
userTable,
repoTable,
permTable,
commitTable,
}
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_remote VARCHAR(255)
,user_login VARCHAR(255)
,user_access VARCHAR(255)
,user_secret VARCHAR(255)
,user_name VARCHAR(255)
,user_email VARCHAR(255)
,user_gravatar VARCHAR(255)
,user_token VARCHAR(255)
,user_admin BOOLEAN
,user_active BOOLEAN
,user_syncing BOOLEAN
,user_created INTEGER
,user_updated INTEGER
,user_synced INTEGER
,UNIQUE(user_token)
,UNIQUE(user_remote, user_login)
);
`
var permTable = `
CREATE TABLE IF NOT EXISTS perms (
perm_id INTEGER PRIMARY KEY AUTOINCREMENT
,user_id INTEGER
,repo_id INTEGER
,perm_read BOOLEAN
,perm_write BOOLEAN
,perm_admin BOOLEAN
,perm_created INTEGER
,perm_updated INTEGER
,UNIQUE (repo_id, user_id)
);
`
var repoTable = `
CREATE TABLE IF NOT EXISTS repos (
repo_id INTEGER PRIMARY KEY AUTOINCREMENT
,user_id INTEGER
,repo_remote VARCHAR(255)
,repo_host VARCHAR(255)
,repo_owner VARCHAR(255)
,repo_name VARCHAR(255)
,repo_url VARCHAR(1024)
,repo_clone_url VARCHAR(255)
,repo_git_url VARCHAR(255)
,repo_ssh_url VARCHAR(255)
,repo_active BOOLEAN
,repo_private BOOLEAN
,repo_privileged BOOLEAN
,repo_post_commit BOOLEAN
,repo_pull_request BOOLEAN
,repo_public_key BLOB
,repo_private_key BLOB
,repo_params BLOB
,repo_timeout INTEGER
,repo_created INTEGER
,repo_updated INTEGER
,UNIQUE(repo_host, repo_owner, repo_name)
);
`
var commitTable = `
CREATE TABLE IF NOT EXISTS commits (
commit_id INTEGER PRIMARY KEY AUTOINCREMENT
,repo_id INTEGER
,commit_status VARCHAR(255)
,commit_started INTEGER
,commit_finished INTEGER
,commit_duration INTEGER
,commit_sha VARCHAR(255)
,commit_branch VARCHAR(255)
,commit_pr VARCHAR(255)
,commit_author VARCHAR(255)
,commit_gravatar VARCHAR(255)
,commit_timestamp VARCHAR(255)
,commit_message VARCHAR(255)
,commit_yaml BLOB
,commit_created INTEGER
,commit_updated INTEGER
,UNIQUE(commit_sha, commit_branch, repo_id)
);
`
var blobTable = `
CREATE TABLE IF NOT EXISTS blobs (
blob_id INTEGER PRIMARY KEY AUTOINCREMENT
,blob_path VARCHAR(1024)
,blob_data BLOB
,UNIQUE(blob_path)
);
`

View file

@ -1,57 +0,0 @@
package migrate
import (
"github.com/BurntSushi/migration"
)
// GetVersion gets the migration version from the database,
// creating the migration table if it does not already exist.
func GetVersion(tx migration.LimitedTx) (int, error) {
v, err := getVersion(tx)
if err != nil {
if err := createVersionTable(tx); err != nil {
return 0, err
}
return getVersion(tx)
}
return v, nil
}
// SetVersion sets the migration version in the database,
// creating the migration table if it does not already exist.
func SetVersion(tx migration.LimitedTx, version int) error {
if err := setVersion(tx, version); err != nil {
if err := createVersionTable(tx); err != nil {
return err
}
return setVersion(tx, version)
}
return nil
}
// setVersion updates the migration version in the database.
func setVersion(tx migration.LimitedTx, version int) error {
_, err := tx.Exec(rebind("UPDATE migration_version SET version = ?"), version)
return err
}
// getVersion gets the migration version in the database.
func getVersion(tx migration.LimitedTx) (int, error) {
var version int
row := tx.QueryRow("SELECT version FROM migration_version")
if err := row.Scan(&version); err != nil {
return 0, err
}
return version, nil
}
// createVersionTable creates the version table and inserts the
// initial value (0) into the database.
func createVersionTable(tx migration.LimitedTx) error {
_, err := tx.Exec("CREATE TABLE migration_version ( version INTEGER )")
if err != nil {
return err
}
_, err = tx.Exec("INSERT INTO migration_version (version) VALUES (0)")
return err
}