1
0
mirror of https://github.com/astaxie/beego.git synced 2024-11-01 01:30:56 +00:00

golint migration

This commit is contained in:
astaxie 2015-09-11 23:16:05 +08:00
parent 0a5fa04062
commit 542e143e55
2 changed files with 34 additions and 29 deletions

View File

@ -14,33 +14,40 @@
package migration package migration
// Table store the tablename and Column
type Table struct { type Table struct {
TableName string TableName string
Columns []*Column Columns []*Column
} }
// Create return the create sql
func (t *Table) Create() string { func (t *Table) Create() string {
return "" return ""
} }
// Drop return the drop sql
func (t *Table) Drop() string { func (t *Table) Drop() string {
return "" return ""
} }
// Column define the columns name type and Default
type Column struct { type Column struct {
Name string Name string
Type string Type string
Default interface{} Default interface{}
} }
// Create return create sql with the provided tbname and columns
func Create(tbname string, columns ...Column) string { func Create(tbname string, columns ...Column) string {
return "" return ""
} }
// Drop return the drop sql with the provided tbname and columns
func Drop(tbname string, columns ...Column) string { func Drop(tbname string, columns ...Column) string {
return "" return ""
} }
// TableDDL is still in think
func TableDDL(tbname string, columns ...Column) string { func TableDDL(tbname string, columns ...Column) string {
return "" return ""
} }

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and // See the License for the specific language governing permissions and
// limitations under the License. // limitations under the License.
// migration package for migration // Package migration is used for migration
// //
// The table structure is as follow: // The table structure is as follow:
// //
@ -39,8 +39,8 @@ import (
// const the data format for the bee generate migration datatype // const the data format for the bee generate migration datatype
const ( const (
M_DATE_FORMAT = "20060102_150405" DateFormat = "20060102_150405"
M_DB_DATE_FORMAT = "2006-01-02 15:04:05" DBDateFormat = "2006-01-02 15:04:05"
) )
// Migrationer is an interface for all Migration struct // Migrationer is an interface for all Migration struct
@ -60,24 +60,24 @@ func init() {
migrationMap = make(map[string]Migrationer) migrationMap = make(map[string]Migrationer)
} }
// the basic type which will implement the basic type // Migration the basic type which will implement the basic type
type Migration struct { type Migration struct {
sqls []string sqls []string
Created string Created string
} }
// implement in the Inheritance struct for upgrade // Up implement in the Inheritance struct for upgrade
func (m *Migration) Up() { func (m *Migration) Up() {
} }
// implement in the Inheritance struct for down // Down implement in the Inheritance struct for down
func (m *Migration) Down() { func (m *Migration) Down() {
} }
// add sql want to execute // SQL add sql want to execute
func (m *Migration) Sql(sql string) { func (m *Migration) SQL(sql string) {
m.sqls = append(m.sqls, sql) m.sqls = append(m.sqls, sql)
} }
@ -86,7 +86,7 @@ func (m *Migration) Reset() {
m.sqls = make([]string, 0) m.sqls = make([]string, 0)
} }
// execute the sql already add in the sql // Exec execute the sql already add in the sql
func (m *Migration) Exec(name, status string) error { func (m *Migration) Exec(name, status string) error {
o := orm.NewOrm() o := orm.NewOrm()
for _, s := range m.sqls { for _, s := range m.sqls {
@ -108,29 +108,28 @@ func (m *Migration) addOrUpdateRecord(name, status string) error {
if err != nil { if err != nil {
return nil return nil
} }
_, err = p.Exec(status, strings.Join(m.sqls, "; "), time.Now().Format(M_DB_DATE_FORMAT), name) _, err = p.Exec(status, strings.Join(m.sqls, "; "), time.Now().Format(DBDateFormat), name)
return err return err
} else { }
status = "update" status = "update"
p, err := o.Raw("insert into migrations(`name`, `created_at`, `statements`, `status`) values(?,?,?,?)").Prepare() p, err := o.Raw("insert into migrations(`name`, `created_at`, `statements`, `status`) values(?,?,?,?)").Prepare()
if err != nil { if err != nil {
return err return err
} }
_, err = p.Exec(name, time.Now().Format(M_DB_DATE_FORMAT), strings.Join(m.sqls, "; "), status) _, err = p.Exec(name, time.Now().Format(DBDateFormat), strings.Join(m.sqls, "; "), status)
return err return err
}
} }
// get the unixtime from the Created // GetCreated get the unixtime from the Created
func (m *Migration) GetCreated() int64 { func (m *Migration) GetCreated() int64 {
t, err := time.Parse(M_DATE_FORMAT, m.Created) t, err := time.Parse(DateFormat, m.Created)
if err != nil { if err != nil {
return 0 return 0
} }
return t.Unix() return t.Unix()
} }
// register the Migration in the map // Register register the Migration in the map
func Register(name string, m Migrationer) error { func Register(name string, m Migrationer) error {
if _, ok := migrationMap[name]; ok { if _, ok := migrationMap[name]; ok {
return errors.New("already exist name:" + name) return errors.New("already exist name:" + name)
@ -139,7 +138,7 @@ func Register(name string, m Migrationer) error {
return nil return nil
} }
// upgrate the migration from lasttime // Upgrade upgrate the migration from lasttime
func Upgrade(lasttime int64) error { func Upgrade(lasttime int64) error {
sm := sortMap(migrationMap) sm := sortMap(migrationMap)
i := 0 i := 0
@ -163,7 +162,7 @@ func Upgrade(lasttime int64) error {
return nil return nil
} }
//rollback the migration by the name // Rollback rollback the migration by the name
func Rollback(name string) error { func Rollback(name string) error {
if v, ok := migrationMap[name]; ok { if v, ok := migrationMap[name]; ok {
beego.Info("start rollback") beego.Info("start rollback")
@ -178,14 +177,13 @@ func Rollback(name string) error {
beego.Info("end rollback") beego.Info("end rollback")
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
return nil return nil
} else { }
beego.Error("not exist the migrationMap name:" + name) beego.Error("not exist the migrationMap name:" + name)
time.Sleep(2 * time.Second) time.Sleep(2 * time.Second)
return errors.New("not exist the migrationMap name:" + name) return errors.New("not exist the migrationMap name:" + name)
}
} }
// reset all migration // Reset reset all migration
// run all migration's down function // run all migration's down function
func Reset() error { func Reset() error {
sm := sortMap(migrationMap) sm := sortMap(migrationMap)
@ -214,7 +212,7 @@ func Reset() error {
return nil return nil
} }
// first Reset, then Upgrade // Refresh first Reset, then Upgrade
func Refresh() error { func Refresh() error {
err := Reset() err := Reset()
if err != nil { if err != nil {