forked from coaidev/coai
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.go
204 lines (187 loc) · 4.66 KB
/
database.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package connection
import (
"database/sql"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/spf13/viper"
"log"
)
var DB *sql.DB
func InitMySQLSafe() *sql.DB {
ConnectMySQL()
// using DB as a global variable to point to the latest db connection
MysqlWorker(DB)
return DB
}
func ConnectMySQL() *sql.DB {
// connect to MySQL
db, err := sql.Open("mysql", fmt.Sprintf(
"%s:%s@tcp(%s:%d)/%s",
viper.GetString("mysql.user"),
viper.GetString("mysql.password"),
viper.GetString("mysql.host"),
viper.GetInt("mysql.port"),
viper.GetString("mysql.db"),
))
if err != nil || db.Ping() != nil {
log.Println(fmt.Sprintf("[connection] failed to connect to mysql server: %s, will retry in 5 seconds", viper.GetString("mysql.host")))
return nil
} else {
log.Println(fmt.Sprintf("[connection] connected to mysql server (host: %s)", viper.GetString("mysql.host")))
}
CreateUserTable(db)
CreateConversationTable(db)
CreateSharingTable(db)
CreatePackageTable(db)
CreateQuotaTable(db)
CreateSubscriptionTable(db)
CreateApiKeyTable(db)
CreateInvitationTable(db)
CreateBroadcastTable(db)
DB = db
return db
}
func CreateUserTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS auth (
id INT PRIMARY KEY AUTO_INCREMENT,
bind_id INT UNIQUE,
username VARCHAR(24) UNIQUE,
token VARCHAR(255) NOT NULL,
password VARCHAR(64) NOT NULL,
is_admin BOOLEAN DEFAULT FALSE
);
`)
if err != nil {
fmt.Println(err)
}
}
func CreatePackageTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS package (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
type VARCHAR(255),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES auth(id),
UNIQUE KEY (user_id, type)
);
`)
if err != nil {
fmt.Println(err)
}
}
func CreateQuotaTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS quota (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT UNIQUE,
quota DECIMAL(10, 4),
used DECIMAL(10, 4),
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES auth(id)
);
`)
if err != nil {
fmt.Println(err)
}
}
func CreateConversationTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS conversation (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT,
conversation_id INT,
conversation_name VARCHAR(255),
data MEDIUMTEXT,
model VARCHAR(255) NOT NULL DEFAULT 'gpt-3.5-turbo-0613',
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY (user_id, conversation_id)
);
`)
if err != nil {
fmt.Println(err)
}
}
func CreateSharingTable(db *sql.DB) {
// refs is an array of message id, separated by comma (-1 means all messages)
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS sharing (
id INT PRIMARY KEY AUTO_INCREMENT,
hash CHAR(32) UNIQUE,
user_id INT,
conversation_id INT,
refs TEXT,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES auth(id)
);
`)
if err != nil {
fmt.Println(err)
}
}
func CreateSubscriptionTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS subscription (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT UNIQUE,
expired_at DATETIME,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
total_month INT DEFAULT 0,
enterprise BOOLEAN DEFAULT FALSE,
FOREIGN KEY (user_id) REFERENCES auth(id)
);
`)
if err != nil {
fmt.Println(err)
}
}
func CreateApiKeyTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS apikey (
id INT PRIMARY KEY AUTO_INCREMENT,
user_id INT UNIQUE,
api_key VARCHAR(255) UNIQUE,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES auth(id)
);
`)
if err != nil {
fmt.Println(err)
}
}
func CreateInvitationTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS invitation (
id INT PRIMARY KEY AUTO_INCREMENT,
code VARCHAR(255) UNIQUE,
quota DECIMAL(6, 4),
type VARCHAR(255),
used BOOLEAN DEFAULT FALSE,
used_id INT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
UNIQUE KEY (used_id, type),
FOREIGN KEY (used_id) REFERENCES auth(id)
);
`)
if err != nil {
fmt.Println(err)
}
}
func CreateBroadcastTable(db *sql.DB) {
_, err := db.Exec(`
CREATE TABLE IF NOT EXISTS broadcast (
id INT PRIMARY KEY AUTO_INCREMENT,
poster_id INT,
content TEXT,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (poster_id) REFERENCES auth(id)
);
`)
if err != nil {
fmt.Println(err)
}
}