1
0
mirror of https://github.com/beego/bee.git synced 2024-11-16 10:10:54 +00:00
bee/g_migration.go

92 lines
2.6 KiB
Go
Raw Normal View History

// Copyright 2013 bee authors
//
// Licensed under the Apache License, Version 2.0 (the "License"): you may
// not use this file except in compliance with the License. You may obtain
// a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
// License for the specific language governing permissions and limitations
// under the License.
package main
2014-08-07 05:52:28 +00:00
import (
"fmt"
"os"
"path"
"strings"
"time"
)
2014-08-07 05:52:28 +00:00
const (
M_PATH = "migrations"
2014-08-07 06:39:38 +00:00
M_DATE_FORMAT = "20060102_150405"
2014-08-07 05:52:28 +00:00
)
// generateMigration generates migration file template for database schema update.
// The generated file template consists of an up() method for updating schema and
// a down() method for reverting the update.
2014-08-18 03:51:57 +00:00
func generateMigration(mname, upsql, downsql, curpath string) {
migrationFilePath := path.Join(curpath, "database", M_PATH)
2014-08-07 05:52:28 +00:00
if _, err := os.Stat(migrationFilePath); os.IsNotExist(err) {
// create migrations directory
2014-08-08 17:37:55 +00:00
if err := os.MkdirAll(migrationFilePath, 0777); err != nil {
2014-08-07 05:52:28 +00:00
ColorLog("[ERRO] Could not create migration directory: %s\n", err)
os.Exit(2)
}
}
// create file
today := time.Now().Format(M_DATE_FORMAT)
fpath := path.Join(migrationFilePath, fmt.Sprintf("%s_%s.go", today, mname))
if f, err := os.OpenFile(fpath, os.O_CREATE|os.O_EXCL|os.O_RDWR, 0666); err == nil {
defer f.Close()
2014-08-07 07:29:45 +00:00
content := strings.Replace(MIGRATION_TPL, "{{StructName}}", camelCase(mname)+"_"+today, -1)
2014-08-07 05:52:28 +00:00
content = strings.Replace(content, "{{CurrTime}}", today, -1)
2014-08-18 03:51:57 +00:00
content = strings.Replace(content, "{{UpSQL}}", upsql, -1)
content = strings.Replace(content, "{{DownSQL}}", downsql, -1)
2014-08-07 05:52:28 +00:00
f.WriteString(content)
// gofmt generated source code
formatSourceCode(fpath)
ColorLog("[INFO] Migration file generated: %s\n", fpath)
} else {
// error creating file
ColorLog("[ERRO] Could not create migration file: %s\n", err)
os.Exit(2)
}
}
2014-08-11 09:23:52 +00:00
const MIGRATION_TPL = `package main
2014-08-07 05:52:28 +00:00
import (
"github.com/astaxie/beego/migration"
)
2014-08-07 07:29:45 +00:00
// DO NOT MODIFY
2014-08-07 06:39:38 +00:00
type {{StructName}} struct {
migration.Migration
}
2014-08-07 07:29:45 +00:00
// DO NOT MODIFY
2014-08-07 05:52:28 +00:00
func init() {
m := &{{StructName}}{}
2014-08-07 06:39:38 +00:00
m.Created = "{{CurrTime}}"
2014-08-07 07:29:45 +00:00
migration.Register("{{StructName}}", m)
2014-08-07 05:52:28 +00:00
}
// Run the migrations
2014-08-07 06:39:38 +00:00
func (m *{{StructName}}) Up() {
// use m.SQL("CREATE TABLE ...") to make schema update
2014-08-18 03:51:57 +00:00
{{UpSQL}}
2014-08-07 05:52:28 +00:00
}
// Reverse the migrations
2014-08-07 06:39:38 +00:00
func (m *{{StructName}}) Down() {
// use m.SQL("DROP TABLE ...") to reverse schema update
2014-08-18 03:51:57 +00:00
{{DownSQL}}
}
2014-08-07 05:52:28 +00:00
`