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

93 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 (
MPath = "migrations"
2016-07-22 23:05:01 +00:00
MDateFormat = "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) {
w := NewColorWriter(os.Stdout)
2016-07-22 23:05:01 +00:00
migrationFilePath := path.Join(curpath, "database", MPath)
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
2016-07-22 23:05:01 +00:00
today := time.Now().Format(MDateFormat)
2014-08-07 05:52:28 +00:00
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 CloseFile(f)
2016-07-22 23:05:01 +00:00
content := strings.Replace(MigrationTPL, "{{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)
// Run 'gofmt' on the generated source code
2014-08-07 05:52:28 +00:00
formatSourceCode(fpath)
fmt.Fprintf(w, "\t%s%screate%s\t %s%s\n", "\x1b[32m", "\x1b[1m", "\x1b[21m", fpath, "\x1b[0m")
2014-08-07 05:52:28 +00:00
} else {
ColorLog("[ERRO] Could not create migration file: %s\n", err)
os.Exit(2)
}
}
2016-07-22 23:05:01 +00:00
const MigrationTPL = `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
`