-
Notifications
You must be signed in to change notification settings - Fork 0
/
seqdbinit.go
79 lines (61 loc) · 1.63 KB
/
seqdbinit.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
// Copyright 2020 @thiinbit([email protected]). All rights reserved.
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file
package idgen
import (
"sync"
)
import (
_ "github.com/go-sql-driver/mysql"
)
var (
seqMu sync.Mutex
)
const (
dbNamePrefix = "d_seq"
tbNamePrefix = "t_seq"
dbSize = 4
tbSize = 8
)
func InitSeqDB() {
initDB(dbNamePrefix, tbNamePrefix, dbSize, tbSize)
}
func DropSeqDB() {
dropAllDB(dbNamePrefix, dbSize)
}
func initDB(dbNamePrefix string, tableNamePrefix string, dbSize int, tableSize int) {
seqMu.Lock()
defer seqMu.Unlock()
for i := 0; i < dbSize; i++ {
dbName := DBName(dbNamePrefix, i)
createSql := "CREATE DATABASE IF NOT EXISTS " + dbName
execSql(createSql)
for j := 0; j < tableSize; j++ {
tableName := TBName(dbName, tableNamePrefix, i*tableSize+j)
createTableSql :=
"CREATE TABLE IF NOT EXISTS " + tableName + " ( " +
"`section` bigint(20) unsigned NOT NULL AUTO_INCREMENT, " +
"`seq_step` bigint(20) unsigned NOT NULL, " +
"`seq_max` bigint(20) unsigned NOT NULL, " +
"`gmt_modify` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, " +
"PRIMARY KEY (`section`) " +
") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci; "
execSql(createTableSql)
}
}
}
func dropAllDB(dbNamePrefix string, dbSize int) {
seqMu.Lock()
defer seqMu.Unlock()
for i := 0; i < dbSize; i++ {
dbName := DBName(dbNamePrefix, i)
createSql := "DROP DATABASE " + dbName
execSql(createSql)
}
}
func execSql(sql string) {
_, err := Exec(sql)
if err != nil {
panic(err)
}
}