Move database schema to migration script.

This commit is contained in:
Nurahmadie 2014-03-10 20:28:04 +07:00
parent 76ed21c40f
commit 92dfcbfca9
6 changed files with 268 additions and 11 deletions

View file

@ -25,6 +25,7 @@ var db *sql.DB
// Init will just bail out and returns error if driver name
// is not listed, no fallback nor default driver sets here.
func Init(name, datasource string) error {
var err error
driver := map[string]struct {
Md *meddler.Database
Mg migrate.DriverBuilder
@ -46,13 +47,11 @@ func Init(name, datasource string) error {
return fmt.Errorf("%s driver not found", name)
}
db, err := sql.Open(name, datasource)
db, err = sql.Open(name, datasource)
if err != nil {
return err
}
Set(db)
migration := migrate.New(db)
migration.All().Migrate()
return nil
@ -70,3 +69,7 @@ func Set(database *sql.DB) {
log.Fatal(err)
}
}
func Close() {
db.Close()
}

View file

@ -0,0 +1,152 @@
package migrate
type rev1st struct{}
var SetupTables = &rev1st{}
func (r *rev1st) Revision() int64 {
return 1
}
func (r *rev1st) Up(mg *MigrationDriver) error {
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)",
}); 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",
}); err != nil {
return err
}
if _, err := mg.CreateTable("members", []string{
"id INTEGER PRIMARY KEY AUTOINCREMENT",
"team_id INTEGER",
"user_id INTEGER",
"role INTEGER",
}); 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",
}); 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",
}); 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",
}); 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)",
})
return err
}
func (r *rev1st) Down(mg *MigrationDriver) error {
if _, err := mg.DropTable("settings"); err != nil {
return err
}
if _, err := mg.DropTable("builds"); err != nil {
return err
}
if _, err := mg.DropTable("commits"); err != nil {
return err
}
if _, err := mg.DropTable("repos"); err != nil {
return err
}
if _, err := mg.DropTable("members"); err != nil {
return err
}
if _, err := mg.DropTable("teams"); err != nil {
return err
}
_, err := mg.DropTable("users")
return err
}

View file

@ -0,0 +1,22 @@
package migrate
type rev20140310104446 struct{}
var AddOpenInvitationColumn = &rev20140310104446{}
func (r *rev20140310104446) Revision() int64 {
return 20140310104446
}
func (r *rev20140310104446) Up(mg *MigrationDriver) error {
if _, err := mg.AddColumn("settings", "open_invitations BOOLEAN"); err != nil {
return err
}
_, err := mg.Tx.Exec("UPDATE settings SET open_invitations=0 WHERE open_invitations IS NULL")
return err
}
func (r *rev20140310104446) Down(mg *MigrationDriver) error {
_, err := mg.DropColumns("settings", []string{"open_invitations"})
return err
}

View file

@ -0,0 +1,83 @@
package migrate
type rev2nd struct{}
var SetupIndices = &rev2nd{}
func (r *rev2nd) Revision() int64 {
return 2
}
func (r *rev2nd) Up(mg *MigrationDriver) error {
if _, err := mg.AddIndex("members", []string{"team_id", "user_id"}, "unique"); err != nil {
return err
}
if _, err := mg.AddIndex("members", []string{"team_id"}, ""); err != nil {
return err
}
if _, err := mg.AddIndex("members", []string{"user_id"}, ""); err != nil {
return err
}
if _, err := mg.AddIndex("commits", []string{"repo_id", "hash", "branch"}, "unique"); err != nil {
return err
}
if _, err := mg.AddIndex("commits", []string{"repo_id"}, ""); err != nil {
return err
}
if _, err := mg.AddIndex("commits", []string{"repo_id", "branch"}, ""); err != nil {
return err
}
if _, err := mg.AddIndex("repos", []string{"team_id"}, ""); err != nil {
return err
}
if _, err := mg.AddIndex("repos", []string{"user_id"}, ""); err != nil {
return err
}
if _, err := mg.AddIndex("builds", []string{"commit_id"}, ""); err != nil {
return err
}
_, err := mg.AddIndex("builds", []string{"commit_id", "slug"}, "")
return err
}
func (r *rev2nd) Down(mg *MigrationDriver) error {
if _, err := mg.DropIndex("builds", []string{"commit_id", "slug"}); err != nil {
return err
}
if _, err := mg.DropIndex("builds", []string{"commit_id"}); err != nil {
return err
}
if _, err := mg.DropIndex("repos", []string{"user_id"}); err != nil {
return err
}
if _, err := mg.DropIndex("repos", []string{"team_id"}); err != nil {
return err
}
if _, err := mg.DropIndex("commits", []string{"repo_id", "branch"}); err != nil {
return err
}
if _, err := mg.DropIndex("commits", []string{"repo_id"}); err != nil {
return err
}
if _, err := mg.DropIndex("commits", []string{"repo_id", "hash", "branch"}); err != nil {
return err
}
if _, err := mg.DropIndex("members", []string{"user_id"}); err != nil {
return err
}
if _, err := mg.DropIndex("members", []string{"team_id"}); err != nil {
return err
}
_, err := mg.DropIndex("members", []string{"team_id", "user_id"})
return err
}

View file

@ -3,8 +3,11 @@ package migrate
func (m *Migration) All() *Migration {
// List all migrations here
m.Add(SetupTables)
m.Add(SetupIndices)
m.Add(RenamePrivelegedToPrivileged)
m.Add(GitHubEnterpriseSupport)
m.Add(AddOpenInvitationColumn)
// m.Add(...)
// ...

View file

@ -37,13 +37,7 @@ func init() {
func Setup() {
// create an in-memory database
db, _ = sql.Open("sqlite3", ":memory:")
// make sure all the tables and indexes are created
database.Set(db)
migration := migrate.New(db)
migration.All().Migrate()
database.Init("sqlite3", ":memory:")
// create dummy user data
user1 := User{
@ -208,5 +202,5 @@ func Setup() {
}
func Teardown() {
db.Close()
database.Close()
}