Add wrapper for common column types for easier column type

transformation between databases.
This commit is contained in:
Nurahmadie 2014-03-13 18:53:52 +07:00
parent 97825cf6bf
commit 5903eb8f04
4 changed files with 163 additions and 85 deletions

View file

@ -9,121 +9,122 @@ func (r *rev1st) Revision() int64 {
}
func (r *rev1st) Up(mg *MigrationDriver) error {
t := mg.T
if _, err := mg.CreateTable("users", []string{
"id INTEGER PRIMARY KEY AUTOINCREMENT",
"email VARCHAR(255) UNIQUE",
"password VARCHAR(255)",
"token VARCHAR(255) UNIQUE",
"name VARCHAR(255)",
"gravatar VARCHAR(255)",
"created TIMESTAMP",
"updated TIMESTAMP",
"admin BOOLEAN",
"github_login VARCHAR(255)",
"github_token VARCHAR(255)",
"bitbucket_login VARCHAR(255)",
"bitbucket_token VARCHAR(255)",
"bitbucket_secret VARCHAR(255)",
t.Integer("id", PRIMARYKEY, AUTOINCREMENT),
t.String("email", UNIQUE),
t.String("password"),
t.String("token", UNIQUE),
t.String("name"),
t.String("gravatar"),
t.Timestamp("created"),
t.Timestamp("updated"),
t.Bool("admin"),
t.String("github_login"),
t.String("github_token"),
t.String("bitbucket_login"),
t.String("bitbucket_token"),
t.String("bitbucket_secret"),
}); err != nil {
return err
}
if _, err := mg.CreateTable("teams", []string{
"id INTEGER PRIMARY KEY AUTOINCREMENT",
"slug VARCHAR(255) UNIQUE",
"name VARCHAR(255)",
"email VARCHAR(255)",
"gravatar VARCHAR(255)",
"created TIMESTAMP",
"updated TIMESTAMP",
t.Integer("id", PRIMARYKEY, AUTOINCREMENT),
t.String("slug", UNIQUE),
t.String("name"),
t.String("email"),
t.String("gravatar"),
t.Timestamp("created"),
t.Timestamp("updated"),
}); err != nil {
return err
}
if _, err := mg.CreateTable("members", []string{
"id INTEGER PRIMARY KEY AUTOINCREMENT",
"team_id INTEGER",
"user_id INTEGER",
"role INTEGER",
t.Integer("id", PRIMARYKEY, AUTOINCREMENT),
t.Integer("team_id"),
t.Integer("user_id"),
t.Integer("role"),
}); err != nil {
return err
}
if _, err := mg.CreateTable("repos", []string{
"id INTEGER PRIMARY KEY AUTOINCREMENT",
"slug VARCHAR(1024) UNIQUE",
"host VARCHAR(255)",
"owner VARCHAR(255)",
"name VARCHAR(255)",
"private BOOLEAN",
"disabled BOOLEAN",
"disabled_pr BOOLEAN",
"priveleged BOOLEAN",
"timeout INTEGER",
"scm VARCHAR(25)",
"url VARCHAR(1024)",
"username VARCHAR(255)",
"password VARCHAR(255)",
"public_key VARCHAR(1024)",
"private_key VARCHAR(1024)",
"params VARCHAR(2000)",
"created TIMESTAMP",
"updated TIMESTAMP",
"user_id INTEGER",
"team_id INTEGER",
t.Integer("id", PRIMARYKEY, AUTOINCREMENT),
t.Varchar("slug", 1024, UNIQUE),
t.String("host"),
t.String("owner"),
t.String("name"),
t.Bool("private"),
t.Bool("disabled"),
t.Bool("disabled_pr"),
t.Bool("priveleged"),
t.Integer("timeout"),
t.Varchar("scm", 25),
t.Varchar("url", 1024),
t.String("username"),
t.String("password"),
t.Varchar("public_key", 1024),
t.Varchar("private_key", 1024),
t.Varchar("params", 2000),
t.Timestamp("created"),
t.Timestamp("updated"),
t.Integer("user_id"),
t.Integer("team_id"),
}); err != nil {
return err
}
if _, err := mg.CreateTable("commits", []string{
"id INTEGER PRIMARY KEY AUTOINCREMENT",
"repo_id INTEGER",
"status VARCHAR(255)",
"started TIMESTAMP",
"finished TIMESTAMP",
"duration INTEGER",
"attempts INTEGER",
"hash VARCHAR(255)",
"branch VARCHAR(255)",
"pull_request VARCHAR(255)",
"author VARCHAR(255)",
"gravatar VARCHAR(255)",
"timestamp VARCHAR(255)",
"message VARCHAR(255)",
"created TIMESTAMP",
"updated TIMESTAMP",
t.Integer("id", PRIMARYKEY, AUTOINCREMENT),
t.Integer("repo_id"),
t.String("status"),
t.Timestamp("started"),
t.Timestamp("finished"),
t.Integer("duration"),
t.Integer("attempts"),
t.String("hash"),
t.String("branch"),
t.String("pull_request"),
t.String("author"),
t.String("gravatar"),
t.String("timestamp"),
t.String("message"),
t.Timestamp("created"),
t.Timestamp("updated"),
}); err != nil {
return err
}
if _, err := mg.CreateTable("builds", []string{
"id INTEGER PRIMARY KEY AUTOINCREMENT",
"commit_id INTEGER",
"slug VARCHAR(255)",
"status VARCHAR(255)",
"started TIMESTAMP",
"finished TIMESTAMP",
"duration INTEGER",
"created TIMESTAMP",
"updated TIMESTAMP",
"stdout BLOB",
t.Integer("id", PRIMARYKEY, AUTOINCREMENT),
t.Integer("commit_id"),
t.String("slug"),
t.String("status"),
t.Timestamp("started"),
t.Timestamp("finished"),
t.Integer("duration"),
t.Timestamp("created"),
t.Timestamp("updated"),
t.Text("stdout"),
}); err != nil {
return err
}
_, err := mg.CreateTable("settings", []string{
"id INTEGER PRIMARY KEY",
"github_key VARCHAR(255)",
"github_secret VARCHAR(255)",
"bitbucket_key VARCHAR(255)",
"bitbucket_secret VARCHAR(255)",
"smtp_server VARCHAR(1024)",
"smtp_port VARCHAR(5)",
"smtp_address VARCHAR(1024)",
"smtp_username VARCHAR(1024)",
"smtp_password VARCHAR(1024)",
"hostname VARCHAR(1024)",
"scheme VARCHAR(5)",
t.Integer("id", PRIMARYKEY),
t.String("github_key"),
t.String("github_secret"),
t.String("bitbucket_key"),
t.String("bitbucket_secret"),
t.Varchar("smtp_server", 1024),
t.Varchar("smtp_port", 5),
t.Varchar("smtp_address", 1024),
t.Varchar("smtp_username", 1024),
t.Varchar("smtp_password", 1024),
t.Varchar("hostname", 1024),
t.Varchar("scheme", 5),
})
return err
}

View file

@ -28,8 +28,9 @@ type Operation interface {
}
type MigrationDriver struct {
Tx *sql.Tx
Operation
T *columnType
Tx *sql.Tx
}
type DriverBuilder func(tx *sql.Tx) *MigrationDriver

View file

@ -0,0 +1,75 @@
package migrate
import (
"fmt"
"reflect"
"strings"
)
const (
UNIQUE int = iota
PRIMARYKEY
AUTOINCREMENT
NULL
NOTNULL
TSTRING
TTEXT
)
type columnType struct {
Driver string
AttrMap map[int]string
}
var defaultMap = map[int]string{
UNIQUE: "UNIQUE",
PRIMARYKEY: "PRIMARY KEY",
AUTOINCREMENT: "AUTOINCREMENT",
NULL: "NULL",
NOTNULL: "NOT NULL",
}
func (c *columnType) Integer(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s INTEGER %s", colName, c.parseAttr(spec))
}
func (c *columnType) String(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s VARCHAR(255) %s", colName, c.parseAttr(spec))
}
func (c *columnType) Text(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s TEXT %s", colName, c.parseAttr(spec))
}
func (c *columnType) Timestamp(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s TIMESTAMP %s", colName, c.parseAttr(spec))
}
func (c *columnType) Bool(colName string, spec ...interface{}) string {
return fmt.Sprintf("%s BOOLEAN %s", colName, c.parseAttr(spec))
}
func (c *columnType) Varchar(colName string, length int, spec ...interface{}) string {
return fmt.Sprintf("%s VARCHAR(%d) %s", colName, length, c.parseAttr(spec))
}
func (c *columnType) attr(flag int) string {
if v, ok := c.AttrMap[flag]; ok {
return v
}
return defaultMap[flag]
}
func (c *columnType) parseAttr(spec []interface{}) string {
var attrs []string
for _, v := range spec {
switch reflect.ValueOf(v).Kind() {
case reflect.Int:
attrs = append(attrs, c.attr(v.(int)))
case reflect.String:
attrs = append(attrs, v.(string))
}
}
return strings.Join(attrs, " ")
}

View file

@ -14,6 +14,7 @@ func SQLite(tx *sql.Tx) *MigrationDriver {
return &MigrationDriver{
Tx: tx,
Operation: &sqliteDriver{Tx: tx},
T: &columnType{},
}
}