mirror of
https://github.com/mindoc-org/mindoc.git
synced 2025-09-24 21:23:35 +08:00
实现数据库迁移
This commit is contained in:
162
commands/migrate/migrate.go
Normal file
162
commands/migrate/migrate.go
Normal file
@@ -0,0 +1,162 @@
|
||||
// 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 migrate
|
||||
|
||||
import (
|
||||
"os"
|
||||
|
||||
"log"
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"container/list"
|
||||
"fmt"
|
||||
"github.com/astaxie/beego"
|
||||
"github.com/astaxie/beego/orm"
|
||||
)
|
||||
|
||||
var (
|
||||
migrationList = &migrationCache{ }
|
||||
)
|
||||
|
||||
|
||||
type MigrationDatabase interface {
|
||||
//获取当前的版本
|
||||
Version() int64
|
||||
//校验当前是否可更新
|
||||
ValidUpdate(version int64) error
|
||||
//校验并备份表结构
|
||||
ValidForBackupTableSchema() error
|
||||
//校验并更新表结构
|
||||
ValidForUpdateTableSchema() error
|
||||
//恢复旧数据
|
||||
MigrationOldTableData() error
|
||||
//插入新数据
|
||||
MigrationNewTableData() error
|
||||
//增加迁移记录
|
||||
AddMigrationRecord(version int64) error
|
||||
//最后的清理工作
|
||||
MigrationCleanup() error
|
||||
//回滚本次迁移
|
||||
RollbackMigration() error
|
||||
}
|
||||
|
||||
type migrationCache struct {
|
||||
items *list.List
|
||||
}
|
||||
|
||||
func RunMigration() {
|
||||
|
||||
if len(os.Args) >= 2 && os.Args[1] == "migrate" {
|
||||
|
||||
migrate,err := models.NewMigration().FindFirst()
|
||||
|
||||
if err != nil {
|
||||
//log.Fatalf("migrations table %s", err)
|
||||
migrate = models.NewMigration()
|
||||
}
|
||||
fmt.Println("Start migration databae... ")
|
||||
|
||||
for el := migrationList.items.Front(); el != nil ; el = el.Next() {
|
||||
|
||||
//如果存在比当前版本大的版本,则依次升级
|
||||
if item,ok := el.Value.(MigrationDatabase); ok && item.Version() > migrate.Version {
|
||||
err := item.ValidUpdate(migrate.Version)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = item.ValidForBackupTableSchema()
|
||||
if err != nil {
|
||||
item.RollbackMigration()
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = item.ValidForUpdateTableSchema()
|
||||
if err != nil {
|
||||
item.RollbackMigration()
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = item.MigrationOldTableData()
|
||||
if err != nil {
|
||||
item.RollbackMigration()
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = item.MigrationNewTableData()
|
||||
if err != nil {
|
||||
item.RollbackMigration()
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = item.AddMigrationRecord(item.Version())
|
||||
if err != nil {
|
||||
item.RollbackMigration()
|
||||
log.Fatal(err)
|
||||
}
|
||||
err = item.MigrationCleanup()
|
||||
if err != nil {
|
||||
item.RollbackMigration()
|
||||
log.Fatal(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
fmt.Println("Migration successfull.")
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
||||
//导出数据库的表结构
|
||||
func ExportDatabaseTable() ([]string,error) {
|
||||
db_adapter := beego.AppConfig.String("db_adapter")
|
||||
db_database := beego.AppConfig.String("db_database")
|
||||
tables := make([]string,0)
|
||||
|
||||
o := orm.NewOrm()
|
||||
switch db_adapter {
|
||||
case "mysql":{
|
||||
var lists []orm.Params
|
||||
|
||||
_,err := o.Raw(fmt.Sprintf("SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_SCHEMA = '%s'",db_database)).Values(&lists)
|
||||
if err != nil {
|
||||
return tables,err
|
||||
}
|
||||
for _,table := range lists {
|
||||
var results []orm.Params
|
||||
|
||||
_,err = o.Raw(fmt.Sprintf("show create table %s",table["TABLE_NAME"])).Values(&results)
|
||||
if err != nil {
|
||||
return tables,err
|
||||
}
|
||||
tables = append(tables,results[0]["Create Table"].(string))
|
||||
}
|
||||
break;
|
||||
}
|
||||
case "sqlite3": {
|
||||
var results []orm.Params
|
||||
_,err := o.Raw("SELECT sql FROM sqlite_master WHERE sql IS NOT NULL ORDER BY rootpage ASC").Values(&results)
|
||||
if err != nil {
|
||||
return tables,err
|
||||
}
|
||||
for _,item := range results {
|
||||
if sql,ok := item["sql"]; ok {
|
||||
tables = append(tables,sql.(string))
|
||||
}
|
||||
}
|
||||
break
|
||||
}
|
||||
|
||||
}
|
||||
return tables,nil
|
||||
}
|
||||
|
||||
func RegisterMigration() {
|
||||
migrationList.items = list.New()
|
||||
|
||||
migrationList.items.PushBack(NewMigrationVersion03())
|
||||
}
|
139
commands/migrate/migrate_v03.go
Normal file
139
commands/migrate/migrate_v03.go
Normal file
@@ -0,0 +1,139 @@
|
||||
package migrate
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"github.com/astaxie/beego/orm"
|
||||
"github.com/lifei6671/godoc/models"
|
||||
"time"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type MigrationVersion03 struct {
|
||||
isValid bool
|
||||
tables []string
|
||||
|
||||
}
|
||||
|
||||
func NewMigrationVersion03() *MigrationVersion03 {
|
||||
return &MigrationVersion03{ isValid: false, tables: make([]string,0)}
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) Version() int64 {
|
||||
return 201705271114
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) ValidUpdate(version int64) error {
|
||||
if m.Version() > version {
|
||||
m.isValid = true
|
||||
return nil
|
||||
}
|
||||
m.isValid = false
|
||||
return errors.New("The target version is higher than the current version.")
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) ValidForBackupTableSchema() error {
|
||||
if !m.isValid {
|
||||
return errors.New("The current version failed to verify.")
|
||||
}
|
||||
var err error
|
||||
m.tables,err = ExportDatabaseTable()
|
||||
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) ValidForUpdateTableSchema() error {
|
||||
if !m.isValid {
|
||||
return errors.New("The current version failed to verify.")
|
||||
}
|
||||
|
||||
err := orm.RunSyncdb("default", false, true)
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
//_,err = o.Raw("ALTER TABLE md_members ADD auth_method VARCHAR(50) DEFAULT 'local' NULL").Exec()
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) MigrationOldTableData() error {
|
||||
if !m.isValid {
|
||||
return errors.New("The current version failed to verify.")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) MigrationNewTableData() error {
|
||||
if !m.isValid {
|
||||
return errors.New("The current version failed to verify.")
|
||||
}
|
||||
o := orm.NewOrm()
|
||||
|
||||
_,err := o.Raw("UPDATE md_members SET auth_method = 'local'").Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_,err = o.Raw("INSERT INTO md_options (option_title, option_name, option_value) SELECT '是否启用文档历史','ENABLE_DOCUMENT_HISTORY','true' WHERE NOT exists(SELECT * FROM md_options WHERE option_name = 'ENABLE_DOCUMENT_HISTORY');").Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) AddMigrationRecord(version int64) error {
|
||||
o := orm.NewOrm()
|
||||
tables,err := ExportDatabaseTable()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
migration := models.NewMigration()
|
||||
migration.Version = version
|
||||
migration.Status = "update"
|
||||
migration.CreateTime = time.Now()
|
||||
migration.Name = fmt.Sprintf("update_%d",version)
|
||||
migration.Statements = strings.Join(tables,"\r\n")
|
||||
|
||||
_, err = o.Insert(migration)
|
||||
|
||||
return err
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) MigrationCleanup() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *MigrationVersion03) RollbackMigration() error {
|
||||
if !m.isValid {
|
||||
return errors.New("The current version failed to verify.")
|
||||
}
|
||||
o := orm.NewOrm()
|
||||
_,err := o.Raw("ALTER TABLE md_members DROP COLUMN auth_method").Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
_,err = o.Raw("DROP TABLE md_document_history").Exec()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_,err = o.Raw("DELETE md_options WHERE option_name = 'ENABLE_DOCUMENT_HISTORY'").Exec()
|
||||
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
Reference in New Issue
Block a user