2017-05-27 17:53:35 +08:00
package migrate
import (
"errors"
2018-01-26 17:17:38 +08:00
"fmt"
2017-05-27 17:53:35 +08:00
"strings"
2018-01-26 17:17:38 +08:00
"time"
2021-03-23 15:09:17 +08:00
2021-03-26 11:34:02 +08:00
"github.com/beego/beego/v2/client/orm"
2021-03-23 15:09:17 +08:00
"github.com/mindoc-org/mindoc/models"
2017-05-27 17:53:35 +08:00
)
type MigrationVersion03 struct {
isValid bool
2018-01-26 17:17:38 +08:00
tables [ ] string
2017-05-27 17:53:35 +08:00
}
func NewMigrationVersion03 ( ) * MigrationVersion03 {
2018-01-26 17:17:38 +08:00
return & MigrationVersion03 { isValid : false , tables : make ( [ ] string , 0 ) }
2017-05-27 17:53:35 +08:00
}
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
2018-01-26 17:17:38 +08:00
m . tables , err = ExportDatabaseTable ( )
2017-05-27 17:53:35 +08:00
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 ( )
2018-01-26 17:17:38 +08:00
_ , err := o . Raw ( "UPDATE md_members SET auth_method = 'local'" ) . Exec ( )
2017-05-27 17:53:35 +08:00
if err != nil {
return err
}
2018-01-26 17:17:38 +08:00
_ , 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 ( )
2017-05-27 17:53:35 +08:00
if err != nil {
return err
}
return nil
}
func ( m * MigrationVersion03 ) AddMigrationRecord ( version int64 ) error {
o := orm . NewOrm ( )
2018-01-26 17:17:38 +08:00
tables , err := ExportDatabaseTable ( )
2017-05-27 17:53:35 +08:00
if err != nil {
2018-01-26 17:17:38 +08:00
return err
2017-05-27 17:53:35 +08:00
}
migration := models . NewMigration ( )
migration . Version = version
migration . Status = "update"
migration . CreateTime = time . Now ( )
2018-01-26 17:17:38 +08:00
migration . Name = fmt . Sprintf ( "update_%d" , version )
migration . Statements = strings . Join ( tables , "\r\n" )
2017-05-27 17:53:35 +08:00
_ , err = o . Insert ( migration )
return err
}
2018-01-26 17:17:38 +08:00
func ( m * MigrationVersion03 ) MigrationCleanup ( ) error {
2017-05-27 17:53:35 +08:00
return nil
}
func ( m * MigrationVersion03 ) RollbackMigration ( ) error {
if ! m . isValid {
return errors . New ( "The current version failed to verify." )
}
o := orm . NewOrm ( )
2018-01-26 17:17:38 +08:00
_ , err := o . Raw ( "ALTER TABLE md_members DROP COLUMN auth_method" ) . Exec ( )
2017-05-27 17:53:35 +08:00
if err != nil {
return err
}
2018-01-26 17:17:38 +08:00
_ , err = o . Raw ( "DROP TABLE md_document_history" ) . Exec ( )
2017-05-27 17:53:35 +08:00
if err != nil {
return err
}
2018-01-26 17:17:38 +08:00
_ , err = o . Raw ( "DELETE md_options WHERE option_name = 'ENABLE_DOCUMENT_HISTORY'" ) . Exec ( )
2017-05-27 17:53:35 +08:00
if err != nil {
return err
}
return nil
}