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

View file

@ -28,8 +28,9 @@ type Operation interface {
} }
type MigrationDriver struct { type MigrationDriver struct {
Tx *sql.Tx
Operation Operation
T *columnType
Tx *sql.Tx
} }
type DriverBuilder func(tx *sql.Tx) *MigrationDriver 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{ return &MigrationDriver{
Tx: tx, Tx: tx,
Operation: &sqliteDriver{Tx: tx}, Operation: &sqliteDriver{Tx: tx},
T: &columnType{},
} }
} }