Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
phsl
/
new-api
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Members
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
4371717c
authored
Jun 14, 2025
by
CaIon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: Resolving conflicts caused by mixing multiple databases
parent
d26ae9aa
Hide whitespace changes
Inline
Side-by-side
Showing
7 changed files
with
129 additions
and
88 deletions
+129
-88
common/database.go
+7
-0
model/ability.go
+6
-6
model/channel.go
+10
-10
model/log.go
+19
-9
model/main.go
+82
-58
model/token.go
+2
-2
model/user.go
+3
-3
No files found.
common/database.go
View file @
4371717c
package
common
package
common
const
(
DatabaseTypeMySQL
=
"mysql"
DatabaseTypeSQLite
=
"sqlite"
DatabaseTypePostgreSQL
=
"postgres"
)
var
UsingSQLite
=
false
var
UsingSQLite
=
false
var
UsingPostgreSQL
=
false
var
UsingPostgreSQL
=
false
var
LogSqlType
=
DatabaseTypeSQLite
// Default to SQLite for logging SQL queries
var
UsingMySQL
=
false
var
UsingMySQL
=
false
var
UsingClickHouse
=
false
var
UsingClickHouse
=
false
...
...
model/ability.go
View file @
4371717c
...
@@ -24,7 +24,7 @@ type Ability struct {
...
@@ -24,7 +24,7 @@ type Ability struct {
func
GetGroupModels
(
group
string
)
[]
string
{
func
GetGroupModels
(
group
string
)
[]
string
{
var
models
[]
string
var
models
[]
string
// Find distinct models
// Find distinct models
DB
.
Table
(
"abilities"
)
.
Where
(
g
roupCol
+
" = ? and enabled = ?"
,
group
,
true
)
.
Distinct
(
"model"
)
.
Pluck
(
"model"
,
&
models
)
DB
.
Table
(
"abilities"
)
.
Where
(
commonG
roupCol
+
" = ? and enabled = ?"
,
group
,
true
)
.
Distinct
(
"model"
)
.
Pluck
(
"model"
,
&
models
)
return
models
return
models
}
}
...
@@ -50,8 +50,8 @@ func getPriority(group string, model string, retry int) (int, error) {
...
@@ -50,8 +50,8 @@ func getPriority(group string, model string, retry int) (int, error) {
var
priorities
[]
int
var
priorities
[]
int
err
:=
DB
.
Model
(
&
Ability
{})
.
err
:=
DB
.
Model
(
&
Ability
{})
.
Select
(
"DISTINCT(priority)"
)
.
Select
(
"DISTINCT(priority)"
)
.
Where
(
g
roupCol
+
" = ? and model = ? and enabled = "
+
trueVal
,
group
,
model
)
.
Where
(
commonG
roupCol
+
" = ? and model = ? and enabled = "
+
trueVal
,
group
,
model
)
.
Order
(
"priority DESC"
)
.
// 按优先级降序排序
Order
(
"priority DESC"
)
.
// 按优先级降序排序
Pluck
(
"priority"
,
&
priorities
)
.
Error
// Pluck用于将查询的结果直接扫描到一个切片中
Pluck
(
"priority"
,
&
priorities
)
.
Error
// Pluck用于将查询的结果直接扫描到一个切片中
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -80,14 +80,14 @@ func getChannelQuery(group string, model string, retry int) *gorm.DB {
...
@@ -80,14 +80,14 @@ func getChannelQuery(group string, model string, retry int) *gorm.DB {
if
common
.
UsingPostgreSQL
{
if
common
.
UsingPostgreSQL
{
trueVal
=
"true"
trueVal
=
"true"
}
}
maxPrioritySubQuery
:=
DB
.
Model
(
&
Ability
{})
.
Select
(
"MAX(priority)"
)
.
Where
(
g
roupCol
+
" = ? and model = ? and enabled = "
+
trueVal
,
group
,
model
)
maxPrioritySubQuery
:=
DB
.
Model
(
&
Ability
{})
.
Select
(
"MAX(priority)"
)
.
Where
(
commonG
roupCol
+
" = ? and model = ? and enabled = "
+
trueVal
,
group
,
model
)
channelQuery
:=
DB
.
Where
(
g
roupCol
+
" = ? and model = ? and enabled = "
+
trueVal
+
" and priority = (?)"
,
group
,
model
,
maxPrioritySubQuery
)
channelQuery
:=
DB
.
Where
(
commonG
roupCol
+
" = ? and model = ? and enabled = "
+
trueVal
+
" and priority = (?)"
,
group
,
model
,
maxPrioritySubQuery
)
if
retry
!=
0
{
if
retry
!=
0
{
priority
,
err
:=
getPriority
(
group
,
model
,
retry
)
priority
,
err
:=
getPriority
(
group
,
model
,
retry
)
if
err
!=
nil
{
if
err
!=
nil
{
common
.
SysError
(
fmt
.
Sprintf
(
"Get priority failed: %s"
,
err
.
Error
()))
common
.
SysError
(
fmt
.
Sprintf
(
"Get priority failed: %s"
,
err
.
Error
()))
}
else
{
}
else
{
channelQuery
=
DB
.
Where
(
g
roupCol
+
" = ? and model = ? and enabled = "
+
trueVal
+
" and priority = ?"
,
group
,
model
,
priority
)
channelQuery
=
DB
.
Where
(
commonG
roupCol
+
" = ? and model = ? and enabled = "
+
trueVal
+
" and priority = ?"
,
group
,
model
,
priority
)
}
}
}
}
...
...
model/channel.go
View file @
4371717c
...
@@ -145,7 +145,7 @@ func SearchChannels(keyword string, group string, model string, idSort bool) ([]
...
@@ -145,7 +145,7 @@ func SearchChannels(keyword string, group string, model string, idSort bool) ([]
}
}
// 构造基础查询
// 构造基础查询
baseQuery
:=
DB
.
Model
(
&
Channel
{})
.
Omit
(
k
eyCol
)
baseQuery
:=
DB
.
Model
(
&
Channel
{})
.
Omit
(
commonK
eyCol
)
// 构造WHERE子句
// 构造WHERE子句
var
whereClause
string
var
whereClause
string
...
@@ -153,15 +153,15 @@ func SearchChannels(keyword string, group string, model string, idSort bool) ([]
...
@@ -153,15 +153,15 @@ func SearchChannels(keyword string, group string, model string, idSort bool) ([]
if
group
!=
""
&&
group
!=
"null"
{
if
group
!=
""
&&
group
!=
"null"
{
var
groupCondition
string
var
groupCondition
string
if
common
.
UsingMySQL
{
if
common
.
UsingMySQL
{
groupCondition
=
`CONCAT(',', `
+
g
roupCol
+
`, ',') LIKE ?`
groupCondition
=
`CONCAT(',', `
+
commonG
roupCol
+
`, ',') LIKE ?`
}
else
{
}
else
{
// sqlite, PostgreSQL
// sqlite, PostgreSQL
groupCondition
=
`(',' || `
+
g
roupCol
+
` || ',') LIKE ?`
groupCondition
=
`(',' || `
+
commonG
roupCol
+
` || ',') LIKE ?`
}
}
whereClause
=
"(id = ? OR name LIKE ? OR "
+
k
eyCol
+
" = ? OR "
+
baseURLCol
+
" LIKE ?) AND "
+
modelsCol
+
` LIKE ? AND `
+
groupCondition
whereClause
=
"(id = ? OR name LIKE ? OR "
+
commonK
eyCol
+
" = ? OR "
+
baseURLCol
+
" LIKE ?) AND "
+
modelsCol
+
` LIKE ? AND `
+
groupCondition
args
=
append
(
args
,
common
.
String2Int
(
keyword
),
"%"
+
keyword
+
"%"
,
keyword
,
"%"
+
keyword
+
"%"
,
"%"
+
model
+
"%"
,
"%,"
+
group
+
",%"
)
args
=
append
(
args
,
common
.
String2Int
(
keyword
),
"%"
+
keyword
+
"%"
,
keyword
,
"%"
+
keyword
+
"%"
,
"%"
+
model
+
"%"
,
"%,"
+
group
+
",%"
)
}
else
{
}
else
{
whereClause
=
"(id = ? OR name LIKE ? OR "
+
k
eyCol
+
" = ? OR "
+
baseURLCol
+
" LIKE ?) AND "
+
modelsCol
+
" LIKE ?"
whereClause
=
"(id = ? OR name LIKE ? OR "
+
commonK
eyCol
+
" = ? OR "
+
baseURLCol
+
" LIKE ?) AND "
+
modelsCol
+
" LIKE ?"
args
=
append
(
args
,
common
.
String2Int
(
keyword
),
"%"
+
keyword
+
"%"
,
keyword
,
"%"
+
keyword
+
"%"
,
"%"
+
model
+
"%"
)
args
=
append
(
args
,
common
.
String2Int
(
keyword
),
"%"
+
keyword
+
"%"
,
keyword
,
"%"
+
keyword
+
"%"
,
"%"
+
model
+
"%"
)
}
}
...
@@ -478,7 +478,7 @@ func SearchTags(keyword string, group string, model string, idSort bool) ([]*str
...
@@ -478,7 +478,7 @@ func SearchTags(keyword string, group string, model string, idSort bool) ([]*str
}
}
// 构造基础查询
// 构造基础查询
baseQuery
:=
DB
.
Model
(
&
Channel
{})
.
Omit
(
k
eyCol
)
baseQuery
:=
DB
.
Model
(
&
Channel
{})
.
Omit
(
commonK
eyCol
)
// 构造WHERE子句
// 构造WHERE子句
var
whereClause
string
var
whereClause
string
...
@@ -486,15 +486,15 @@ func SearchTags(keyword string, group string, model string, idSort bool) ([]*str
...
@@ -486,15 +486,15 @@ func SearchTags(keyword string, group string, model string, idSort bool) ([]*str
if
group
!=
""
&&
group
!=
"null"
{
if
group
!=
""
&&
group
!=
"null"
{
var
groupCondition
string
var
groupCondition
string
if
common
.
UsingMySQL
{
if
common
.
UsingMySQL
{
groupCondition
=
`CONCAT(',', `
+
g
roupCol
+
`, ',') LIKE ?`
groupCondition
=
`CONCAT(',', `
+
commonG
roupCol
+
`, ',') LIKE ?`
}
else
{
}
else
{
// sqlite, PostgreSQL
// sqlite, PostgreSQL
groupCondition
=
`(',' || `
+
g
roupCol
+
` || ',') LIKE ?`
groupCondition
=
`(',' || `
+
commonG
roupCol
+
` || ',') LIKE ?`
}
}
whereClause
=
"(id = ? OR name LIKE ? OR "
+
k
eyCol
+
" = ? OR "
+
baseURLCol
+
" LIKE ?) AND "
+
modelsCol
+
` LIKE ? AND `
+
groupCondition
whereClause
=
"(id = ? OR name LIKE ? OR "
+
commonK
eyCol
+
" = ? OR "
+
baseURLCol
+
" LIKE ?) AND "
+
modelsCol
+
` LIKE ? AND `
+
groupCondition
args
=
append
(
args
,
common
.
String2Int
(
keyword
),
"%"
+
keyword
+
"%"
,
keyword
,
"%"
+
keyword
+
"%"
,
"%"
+
model
+
"%"
,
"%,"
+
group
+
",%"
)
args
=
append
(
args
,
common
.
String2Int
(
keyword
),
"%"
+
keyword
+
"%"
,
keyword
,
"%"
+
keyword
+
"%"
,
"%"
+
model
+
"%"
,
"%,"
+
group
+
",%"
)
}
else
{
}
else
{
whereClause
=
"(id = ? OR name LIKE ? OR "
+
k
eyCol
+
" = ? OR "
+
baseURLCol
+
" LIKE ?) AND "
+
modelsCol
+
" LIKE ?"
whereClause
=
"(id = ? OR name LIKE ? OR "
+
commonK
eyCol
+
" = ? OR "
+
baseURLCol
+
" LIKE ?) AND "
+
modelsCol
+
" LIKE ?"
args
=
append
(
args
,
common
.
String2Int
(
keyword
),
"%"
+
keyword
+
"%"
,
keyword
,
"%"
+
keyword
+
"%"
,
"%"
+
model
+
"%"
)
args
=
append
(
args
,
common
.
String2Int
(
keyword
),
"%"
+
keyword
+
"%"
,
keyword
,
"%"
+
keyword
+
"%"
,
"%"
+
model
+
"%"
)
}
}
...
...
model/log.go
View file @
4371717c
...
@@ -63,7 +63,7 @@ func formatUserLogs(logs []*Log) {
...
@@ -63,7 +63,7 @@ func formatUserLogs(logs []*Log) {
func
GetLogByKey
(
key
string
)
(
logs
[]
*
Log
,
err
error
)
{
func
GetLogByKey
(
key
string
)
(
logs
[]
*
Log
,
err
error
)
{
if
os
.
Getenv
(
"LOG_SQL_DSN"
)
!=
""
{
if
os
.
Getenv
(
"LOG_SQL_DSN"
)
!=
""
{
var
tk
Token
var
tk
Token
if
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
k
eyCol
+
"=?"
,
strings
.
TrimPrefix
(
key
,
"sk-"
))
.
First
(
&
tk
)
.
Error
;
err
!=
nil
{
if
err
=
DB
.
Model
(
&
Token
{})
.
Where
(
logK
eyCol
+
"=?"
,
strings
.
TrimPrefix
(
key
,
"sk-"
))
.
First
(
&
tk
)
.
Error
;
err
!=
nil
{
return
nil
,
err
return
nil
,
err
}
}
err
=
LOG_DB
.
Model
(
&
Log
{})
.
Where
(
"token_id=?"
,
tk
.
Id
)
.
Find
(
&
logs
)
.
Error
err
=
LOG_DB
.
Model
(
&
Log
{})
.
Where
(
"token_id=?"
,
tk
.
Id
)
.
Find
(
&
logs
)
.
Error
...
@@ -122,8 +122,13 @@ func RecordErrorLog(c *gin.Context, userId int, channelId int, modelName string,
...
@@ -122,8 +122,13 @@ func RecordErrorLog(c *gin.Context, userId int, channelId int, modelName string,
UseTime
:
useTimeSeconds
,
UseTime
:
useTimeSeconds
,
IsStream
:
isStream
,
IsStream
:
isStream
,
Group
:
group
,
Group
:
group
,
Ip
:
func
()
string
{
if
needRecordIp
{
return
c
.
ClientIP
()
};
return
""
}(),
Ip
:
func
()
string
{
Other
:
otherStr
,
if
needRecordIp
{
return
c
.
ClientIP
()
}
return
""
}(),
Other
:
otherStr
,
}
}
err
:=
LOG_DB
.
Create
(
log
)
.
Error
err
:=
LOG_DB
.
Create
(
log
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -165,8 +170,13 @@ func RecordConsumeLog(c *gin.Context, userId int, channelId int, promptTokens in
...
@@ -165,8 +170,13 @@ func RecordConsumeLog(c *gin.Context, userId int, channelId int, promptTokens in
UseTime
:
useTimeSeconds
,
UseTime
:
useTimeSeconds
,
IsStream
:
isStream
,
IsStream
:
isStream
,
Group
:
group
,
Group
:
group
,
Ip
:
func
()
string
{
if
needRecordIp
{
return
c
.
ClientIP
()
};
return
""
}(),
Ip
:
func
()
string
{
Other
:
otherStr
,
if
needRecordIp
{
return
c
.
ClientIP
()
}
return
""
}(),
Other
:
otherStr
,
}
}
err
:=
LOG_DB
.
Create
(
log
)
.
Error
err
:=
LOG_DB
.
Create
(
log
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -206,7 +216,7 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName
...
@@ -206,7 +216,7 @@ func GetAllLogs(logType int, startTimestamp int64, endTimestamp int64, modelName
tx
=
tx
.
Where
(
"logs.channel_id = ?"
,
channel
)
tx
=
tx
.
Where
(
"logs.channel_id = ?"
,
channel
)
}
}
if
group
!=
""
{
if
group
!=
""
{
tx
=
tx
.
Where
(
"logs."
+
g
roupCol
+
" = ?"
,
group
)
tx
=
tx
.
Where
(
"logs."
+
logG
roupCol
+
" = ?"
,
group
)
}
}
err
=
tx
.
Model
(
&
Log
{})
.
Count
(
&
total
)
.
Error
err
=
tx
.
Model
(
&
Log
{})
.
Count
(
&
total
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -264,7 +274,7 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int
...
@@ -264,7 +274,7 @@ func GetUserLogs(userId int, logType int, startTimestamp int64, endTimestamp int
tx
=
tx
.
Where
(
"logs.created_at <= ?"
,
endTimestamp
)
tx
=
tx
.
Where
(
"logs.created_at <= ?"
,
endTimestamp
)
}
}
if
group
!=
""
{
if
group
!=
""
{
tx
=
tx
.
Where
(
"logs."
+
g
roupCol
+
" = ?"
,
group
)
tx
=
tx
.
Where
(
"logs."
+
logG
roupCol
+
" = ?"
,
group
)
}
}
err
=
tx
.
Model
(
&
Log
{})
.
Count
(
&
total
)
.
Error
err
=
tx
.
Model
(
&
Log
{})
.
Count
(
&
total
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
...
@@ -325,8 +335,8 @@ func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelNa
...
@@ -325,8 +335,8 @@ func SumUsedQuota(logType int, startTimestamp int64, endTimestamp int64, modelNa
rpmTpmQuery
=
rpmTpmQuery
.
Where
(
"channel_id = ?"
,
channel
)
rpmTpmQuery
=
rpmTpmQuery
.
Where
(
"channel_id = ?"
,
channel
)
}
}
if
group
!=
""
{
if
group
!=
""
{
tx
=
tx
.
Where
(
g
roupCol
+
" = ?"
,
group
)
tx
=
tx
.
Where
(
logG
roupCol
+
" = ?"
,
group
)
rpmTpmQuery
=
rpmTpmQuery
.
Where
(
g
roupCol
+
" = ?"
,
group
)
rpmTpmQuery
=
rpmTpmQuery
.
Where
(
logG
roupCol
+
" = ?"
,
group
)
}
}
tx
=
tx
.
Where
(
"type = ?"
,
LogTypeConsume
)
tx
=
tx
.
Where
(
"type = ?"
,
LogTypeConsume
)
...
...
model/main.go
View file @
4371717c
package
model
package
model
import
(
import
(
"fmt"
"log"
"log"
"one-api/common"
"one-api/common"
"one-api/constant"
"one-api/constant"
...
@@ -15,18 +16,33 @@ import (
...
@@ -15,18 +16,33 @@ import (
"gorm.io/gorm"
"gorm.io/gorm"
)
)
var
groupCol
string
var
commonGroupCol
string
var
keyCol
string
var
commonKeyCol
string
var
logKeyCol
string
var
logGroupCol
string
func
initCol
()
{
func
initCol
()
{
// init common column names
if
common
.
UsingPostgreSQL
{
if
common
.
UsingPostgreSQL
{
groupCol
=
`"group"`
commonGroupCol
=
`"group"`
keyCol
=
`"key"`
commonKeyCol
=
`"key"`
}
else
{
}
else
{
groupCol
=
"`group`"
commonGroupCol
=
"`group`"
keyCol
=
"`key`"
commonKeyCol
=
"`key`"
}
if
DB
!=
LOG_DB
{
switch
common
.
LogSqlType
{
case
common
.
DatabaseTypePostgreSQL
:
logGroupCol
=
`"group"`
logKeyCol
=
`"key"`
default
:
logGroupCol
=
commonGroupCol
logKeyCol
=
commonKeyCol
}
}
}
// log sql type and database type
common
.
SysLog
(
"Using Log SQL Type: "
+
common
.
LogSqlType
)
}
}
var
DB
*
gorm
.
DB
var
DB
*
gorm
.
DB
...
@@ -83,7 +99,7 @@ func CheckSetup() {
...
@@ -83,7 +99,7 @@ func CheckSetup() {
}
}
}
}
func
chooseDB
(
envName
string
)
(
*
gorm
.
DB
,
error
)
{
func
chooseDB
(
envName
string
,
isLog
bool
)
(
*
gorm
.
DB
,
error
)
{
defer
func
()
{
defer
func
()
{
initCol
()
initCol
()
}()
}()
...
@@ -92,7 +108,11 @@ func chooseDB(envName string) (*gorm.DB, error) {
...
@@ -92,7 +108,11 @@ func chooseDB(envName string) (*gorm.DB, error) {
if
strings
.
HasPrefix
(
dsn
,
"postgres://"
)
||
strings
.
HasPrefix
(
dsn
,
"postgresql://"
)
{
if
strings
.
HasPrefix
(
dsn
,
"postgres://"
)
||
strings
.
HasPrefix
(
dsn
,
"postgresql://"
)
{
// Use PostgreSQL
// Use PostgreSQL
common
.
SysLog
(
"using PostgreSQL as database"
)
common
.
SysLog
(
"using PostgreSQL as database"
)
common
.
UsingPostgreSQL
=
true
if
!
isLog
{
common
.
UsingPostgreSQL
=
true
}
else
{
common
.
LogSqlType
=
common
.
DatabaseTypePostgreSQL
}
return
gorm
.
Open
(
postgres
.
New
(
postgres
.
Config
{
return
gorm
.
Open
(
postgres
.
New
(
postgres
.
Config
{
DSN
:
dsn
,
DSN
:
dsn
,
PreferSimpleProtocol
:
true
,
// disables implicit prepared statement usage
PreferSimpleProtocol
:
true
,
// disables implicit prepared statement usage
...
@@ -102,7 +122,11 @@ func chooseDB(envName string) (*gorm.DB, error) {
...
@@ -102,7 +122,11 @@ func chooseDB(envName string) (*gorm.DB, error) {
}
}
if
strings
.
HasPrefix
(
dsn
,
"local"
)
{
if
strings
.
HasPrefix
(
dsn
,
"local"
)
{
common
.
SysLog
(
"SQL_DSN not set, using SQLite as database"
)
common
.
SysLog
(
"SQL_DSN not set, using SQLite as database"
)
common
.
UsingSQLite
=
true
if
!
isLog
{
common
.
UsingSQLite
=
true
}
else
{
common
.
LogSqlType
=
common
.
DatabaseTypeSQLite
}
return
gorm
.
Open
(
sqlite
.
Open
(
common
.
SQLitePath
),
&
gorm
.
Config
{
return
gorm
.
Open
(
sqlite
.
Open
(
common
.
SQLitePath
),
&
gorm
.
Config
{
PrepareStmt
:
true
,
// precompile SQL
PrepareStmt
:
true
,
// precompile SQL
})
})
...
@@ -117,7 +141,11 @@ func chooseDB(envName string) (*gorm.DB, error) {
...
@@ -117,7 +141,11 @@ func chooseDB(envName string) (*gorm.DB, error) {
dsn
+=
"?parseTime=true"
dsn
+=
"?parseTime=true"
}
}
}
}
common
.
UsingMySQL
=
true
if
!
isLog
{
common
.
UsingMySQL
=
true
}
else
{
common
.
LogSqlType
=
common
.
DatabaseTypeMySQL
}
return
gorm
.
Open
(
mysql
.
Open
(
dsn
),
&
gorm
.
Config
{
return
gorm
.
Open
(
mysql
.
Open
(
dsn
),
&
gorm
.
Config
{
PrepareStmt
:
true
,
// precompile SQL
PrepareStmt
:
true
,
// precompile SQL
})
})
...
@@ -131,7 +159,7 @@ func chooseDB(envName string) (*gorm.DB, error) {
...
@@ -131,7 +159,7 @@ func chooseDB(envName string) (*gorm.DB, error) {
}
}
func
InitDB
()
(
err
error
)
{
func
InitDB
()
(
err
error
)
{
db
,
err
:=
chooseDB
(
"SQL_DSN"
)
db
,
err
:=
chooseDB
(
"SQL_DSN"
,
false
)
if
err
==
nil
{
if
err
==
nil
{
if
common
.
DebugEnabled
{
if
common
.
DebugEnabled
{
db
=
db
.
Debug
()
db
=
db
.
Debug
()
...
@@ -149,7 +177,7 @@ func InitDB() (err error) {
...
@@ -149,7 +177,7 @@ func InitDB() (err error) {
return
nil
return
nil
}
}
if
common
.
UsingMySQL
{
if
common
.
UsingMySQL
{
_
,
_
=
sqlDB
.
Exec
(
"ALTER TABLE channels MODIFY model_mapping TEXT;"
)
// TODO: delete this line when most users have upgraded
//
_, _ = sqlDB.Exec("ALTER TABLE channels MODIFY model_mapping TEXT;") // TODO: delete this line when most users have upgraded
}
}
common
.
SysLog
(
"database migration started"
)
common
.
SysLog
(
"database migration started"
)
err
=
migrateDB
()
err
=
migrateDB
()
...
@@ -165,7 +193,7 @@ func InitLogDB() (err error) {
...
@@ -165,7 +193,7 @@ func InitLogDB() (err error) {
LOG_DB
=
DB
LOG_DB
=
DB
return
return
}
}
db
,
err
:=
chooseDB
(
"LOG_SQL_DSN"
)
db
,
err
:=
chooseDB
(
"LOG_SQL_DSN"
,
true
)
if
err
==
nil
{
if
err
==
nil
{
if
common
.
DebugEnabled
{
if
common
.
DebugEnabled
{
db
=
db
.
Debug
()
db
=
db
.
Debug
()
...
@@ -198,54 +226,50 @@ func InitLogDB() (err error) {
...
@@ -198,54 +226,50 @@ func InitLogDB() (err error) {
}
}
func
migrateDB
()
error
{
func
migrateDB
()
error
{
err
:=
DB
.
AutoMigrate
(
&
Channel
{})
var
wg
sync
.
WaitGroup
if
err
!=
nil
{
errChan
:=
make
(
chan
error
,
12
)
// Buffer size matches number of migrations
return
err
}
migrations
:=
[]
struct
{
err
=
DB
.
AutoMigrate
(
&
Token
{})
model
interface
{}
if
err
!=
nil
{
name
string
return
err
}{
}
{
&
Channel
{},
"Channel"
},
err
=
DB
.
AutoMigrate
(
&
User
{})
{
&
Token
{},
"Token"
},
if
err
!=
nil
{
{
&
User
{},
"User"
},
return
err
{
&
Option
{},
"Option"
},
}
{
&
Redemption
{},
"Redemption"
},
err
=
DB
.
AutoMigrate
(
&
Option
{})
{
&
Ability
{},
"Ability"
},
if
err
!=
nil
{
{
&
Log
{},
"Log"
},
return
err
{
&
Midjourney
{},
"Midjourney"
},
}
{
&
TopUp
{},
"TopUp"
},
err
=
DB
.
AutoMigrate
(
&
Redemption
{})
{
&
QuotaData
{},
"QuotaData"
},
if
err
!=
nil
{
{
&
Task
{},
"Task"
},
return
err
{
&
Setup
{},
"Setup"
},
}
err
=
DB
.
AutoMigrate
(
&
Ability
{})
if
err
!=
nil
{
return
err
}
err
=
DB
.
AutoMigrate
(
&
Log
{})
if
err
!=
nil
{
return
err
}
err
=
DB
.
AutoMigrate
(
&
Midjourney
{})
if
err
!=
nil
{
return
err
}
err
=
DB
.
AutoMigrate
(
&
TopUp
{})
if
err
!=
nil
{
return
err
}
}
err
=
DB
.
AutoMigrate
(
&
QuotaData
{})
if
err
!=
nil
{
for
_
,
m
:=
range
migrations
{
return
err
wg
.
Add
(
1
)
go
func
(
model
interface
{},
name
string
)
{
defer
wg
.
Done
()
if
err
:=
DB
.
AutoMigrate
(
model
);
err
!=
nil
{
errChan
<-
fmt
.
Errorf
(
"failed to migrate %s: %v"
,
name
,
err
)
}
}(
m
.
model
,
m
.
name
)
}
}
err
=
DB
.
AutoMigrate
(
&
Task
{})
if
err
!=
nil
{
// Wait for all migrations to complete
return
err
wg
.
Wait
()
close
(
errChan
)
// Check for any errors
for
err
:=
range
errChan
{
if
err
!=
nil
{
return
err
}
}
}
err
=
DB
.
AutoMigrate
(
&
Setup
{})
common
.
SysLog
(
"database migrated"
)
common
.
SysLog
(
"database migrated"
)
//err = createRootAccountIfNeed()
return
nil
return
err
}
}
func
migrateLOGDB
()
error
{
func
migrateLOGDB
()
error
{
...
...
model/token.go
View file @
4371717c
...
@@ -66,7 +66,7 @@ func SearchUserTokens(userId int, keyword string, token string) (tokens []*Token
...
@@ -66,7 +66,7 @@ func SearchUserTokens(userId int, keyword string, token string) (tokens []*Token
if
token
!=
""
{
if
token
!=
""
{
token
=
strings
.
Trim
(
token
,
"sk-"
)
token
=
strings
.
Trim
(
token
,
"sk-"
)
}
}
err
=
DB
.
Where
(
"user_id = ?"
,
userId
)
.
Where
(
"name LIKE ?"
,
"%"
+
keyword
+
"%"
)
.
Where
(
k
eyCol
+
" LIKE ?"
,
"%"
+
token
+
"%"
)
.
Find
(
&
tokens
)
.
Error
err
=
DB
.
Where
(
"user_id = ?"
,
userId
)
.
Where
(
"name LIKE ?"
,
"%"
+
keyword
+
"%"
)
.
Where
(
commonK
eyCol
+
" LIKE ?"
,
"%"
+
token
+
"%"
)
.
Find
(
&
tokens
)
.
Error
return
tokens
,
err
return
tokens
,
err
}
}
...
@@ -161,7 +161,7 @@ func GetTokenByKey(key string, fromDB bool) (token *Token, err error) {
...
@@ -161,7 +161,7 @@ func GetTokenByKey(key string, fromDB bool) (token *Token, err error) {
// Don't return error - fall through to DB
// Don't return error - fall through to DB
}
}
fromDB
=
true
fromDB
=
true
err
=
DB
.
Where
(
k
eyCol
+
" = ?"
,
key
)
.
First
(
&
token
)
.
Error
err
=
DB
.
Where
(
commonK
eyCol
+
" = ?"
,
key
)
.
First
(
&
token
)
.
Error
return
token
,
err
return
token
,
err
}
}
...
...
model/user.go
View file @
4371717c
...
@@ -175,7 +175,7 @@ func SearchUsers(keyword string, group string, startIdx int, num int) ([]*User,
...
@@ -175,7 +175,7 @@ func SearchUsers(keyword string, group string, startIdx int, num int) ([]*User,
// 如果是数字,同时搜索ID和其他字段
// 如果是数字,同时搜索ID和其他字段
likeCondition
=
"id = ? OR "
+
likeCondition
likeCondition
=
"id = ? OR "
+
likeCondition
if
group
!=
""
{
if
group
!=
""
{
query
=
query
.
Where
(
"("
+
likeCondition
+
") AND "
+
g
roupCol
+
" = ?"
,
query
=
query
.
Where
(
"("
+
likeCondition
+
") AND "
+
commonG
roupCol
+
" = ?"
,
keywordInt
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
group
)
keywordInt
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
group
)
}
else
{
}
else
{
query
=
query
.
Where
(
likeCondition
,
query
=
query
.
Where
(
likeCondition
,
...
@@ -184,7 +184,7 @@ func SearchUsers(keyword string, group string, startIdx int, num int) ([]*User,
...
@@ -184,7 +184,7 @@ func SearchUsers(keyword string, group string, startIdx int, num int) ([]*User,
}
else
{
}
else
{
// 非数字关键字,只搜索字符串字段
// 非数字关键字,只搜索字符串字段
if
group
!=
""
{
if
group
!=
""
{
query
=
query
.
Where
(
"("
+
likeCondition
+
") AND "
+
g
roupCol
+
" = ?"
,
query
=
query
.
Where
(
"("
+
likeCondition
+
") AND "
+
commonG
roupCol
+
" = ?"
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
group
)
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
"%"
+
keyword
+
"%"
,
group
)
}
else
{
}
else
{
query
=
query
.
Where
(
likeCondition
,
query
=
query
.
Where
(
likeCondition
,
...
@@ -615,7 +615,7 @@ func GetUserGroup(id int, fromDB bool) (group string, err error) {
...
@@ -615,7 +615,7 @@ func GetUserGroup(id int, fromDB bool) (group string, err error) {
// Don't return error - fall through to DB
// Don't return error - fall through to DB
}
}
fromDB
=
true
fromDB
=
true
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Select
(
g
roupCol
)
.
Find
(
&
group
)
.
Error
err
=
DB
.
Model
(
&
User
{})
.
Where
(
"id = ?"
,
id
)
.
Select
(
commonG
roupCol
)
.
Find
(
&
group
)
.
Error
if
err
!=
nil
{
if
err
!=
nil
{
return
""
,
err
return
""
,
err
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment