forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb_migration.go
67 lines (51 loc) · 1.24 KB
/
db_migration.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
package connection
import (
"chat/globals"
"database/sql"
"strings"
)
func validSqlError(err error) bool {
if err == nil {
return false
}
content := err.Error()
// Error 1060: Duplicate column name
// Error 1050: Table already exists
return !(strings.Contains(content, "Error 1060") || strings.Contains(content, "Error 1050"))
}
func checkSqlError(_ sql.Result, err error) error {
if validSqlError(err) {
return err
}
return nil
}
func execSql(db *sql.DB, sql string, args ...interface{}) error {
return checkSqlError(globals.ExecDb(db, sql, args...))
}
func doMigration(db *sql.DB) error {
if globals.SqliteEngine {
return doSqliteMigration(db)
}
// v3.10 migration
// update `quota`, `used` field in `quota` table
// migrate `DECIMAL(16, 4)` to `DECIMAL(24, 6)`
if err := execSql(db, `
ALTER TABLE quota
MODIFY COLUMN quota DECIMAL(24, 6),
MODIFY COLUMN used DECIMAL(24, 6);
`); err != nil {
return err
}
// add new field `is_banned` in `auth` table
if err := execSql(db, `
ALTER TABLE auth
ADD COLUMN is_banned BOOLEAN DEFAULT FALSE;
`); err != nil {
return err
}
return nil
}
func doSqliteMigration(db *sql.DB) error {
// v3.10 added sqlite support, no migration needed before this version
return nil
}