forked from mindoc-org/mindoc
-
Notifications
You must be signed in to change notification settings - Fork 0
/
migrate_v03.go
129 lines (104 loc) · 2.84 KB
/
migrate_v03.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
package migrate
import (
"errors"
"fmt"
"github.com/astaxie/beego/orm"
"github.com/lifei6671/mindoc/models"
"strings"
"time"
)
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
}