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
f3d38ca1
authored
Feb 28, 2026
by
RedwindA
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
fix: enhance migrateTokenModelLimitsToText function to return errors and improve migration checks
parent
f1b36272
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
33 additions
and
25 deletions
+33
-25
model/main.go
+33
-25
No files found.
model/main.go
View file @
f3d38ca1
...
...
@@ -251,7 +251,9 @@ func migrateDB() error {
// Migrate price_amount column from float/double to decimal for existing tables
migrateSubscriptionPlanPriceAmount
()
// Migrate model_limits column from varchar to text for existing tables
migrateTokenModelLimitsToText
()
if
err
:=
migrateTokenModelLimitsToText
();
err
!=
nil
{
return
err
}
err
:=
DB
.
AutoMigrate
(
&
Channel
{},
...
...
@@ -449,52 +451,55 @@ PRIMARY KEY (` + "`id`" + `)
// migrateTokenModelLimitsToText migrates model_limits column from varchar(1024) to text
// This is safe to run multiple times - it checks the column type first
func
migrateTokenModelLimitsToText
()
{
func
migrateTokenModelLimitsToText
()
error
{
// SQLite uses type affinity, so TEXT and VARCHAR are effectively the same — no migration needed
if
common
.
UsingSQLite
{
return
return
nil
}
tableName
:=
"tokens"
columnName
:=
"model_limits"
if
!
DB
.
Migrator
()
.
HasTable
(
tableName
)
{
return
return
nil
}
if
!
DB
.
Migrator
()
.
HasColumn
(
&
Token
{},
columnName
)
{
return
return
nil
}
var
alterSQL
string
if
common
.
UsingPostgreSQL
{
var
dataType
string
DB
.
Raw
(
`SELECT data_type FROM information_schema.columns
WHERE table_name = ? AND column_name = ?`
,
tableName
,
columnName
)
.
Scan
(
&
dataType
)
if
dataType
==
"text"
{
return
if
err
:=
DB
.
Raw
(
`SELECT data_type FROM information_schema.columns
WHERE table_schema = current_schema() AND table_name = ? AND column_name = ?`
,
tableName
,
columnName
)
.
Scan
(
&
dataType
)
.
Error
;
err
!=
nil
{
common
.
SysLog
(
fmt
.
Sprintf
(
"Warning: failed to query metadata for %s.%s: %v"
,
tableName
,
columnName
,
err
))
}
else
if
dataType
==
"text"
{
return
nil
}
alterSQL
=
fmt
.
Sprintf
(
`ALTER TABLE %s ALTER COLUMN %s TYPE text`
,
tableName
,
columnName
)
}
else
if
common
.
UsingMySQL
{
var
columnType
string
DB
.
Raw
(
`SELECT COLUMN_TYPE FROM information_schema.columns
WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`
,
tableName
,
columnName
)
.
Scan
(
&
columnType
)
if
strings
.
ToLower
(
columnType
)
==
"text"
{
return
if
err
:=
DB
.
Raw
(
`SELECT COLUMN_TYPE FROM information_schema.columns
WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`
,
tableName
,
columnName
)
.
Scan
(
&
columnType
)
.
Error
;
err
!=
nil
{
common
.
SysLog
(
fmt
.
Sprintf
(
"Warning: failed to query metadata for %s.%s: %v"
,
tableName
,
columnName
,
err
))
}
else
if
strings
.
ToLower
(
columnType
)
==
"text"
{
return
nil
}
alterSQL
=
fmt
.
Sprintf
(
"ALTER TABLE %s MODIFY COLUMN %s text"
,
tableName
,
columnName
)
}
else
{
return
return
nil
}
if
alterSQL
!=
""
{
if
err
:=
DB
.
Exec
(
alterSQL
)
.
Error
;
err
!=
nil
{
common
.
SysLog
(
fmt
.
Sprintf
(
"Warning: failed to migrate %s.%s to text: %v"
,
tableName
,
columnName
,
err
))
}
else
{
common
.
SysLog
(
fmt
.
Sprintf
(
"Successfully migrated %s.%s to text"
,
tableName
,
columnName
))
return
fmt
.
Errorf
(
"failed to migrate %s.%s to text: %w"
,
tableName
,
columnName
,
err
)
}
common
.
SysLog
(
fmt
.
Sprintf
(
"Successfully migrated %s.%s to text"
,
tableName
,
columnName
))
}
return
nil
}
// migrateSubscriptionPlanPriceAmount migrates price_amount column from float/double to decimal(10,6)
...
...
@@ -523,9 +528,11 @@ func migrateSubscriptionPlanPriceAmount() {
if
common
.
UsingPostgreSQL
{
// PostgreSQL: Check if already decimal/numeric
var
dataType
string
DB
.
Raw
(
`SELECT data_type FROM information_schema.columns
WHERE table_name = ? AND column_name = ?`
,
tableName
,
columnName
)
.
Scan
(
&
dataType
)
if
dataType
==
"numeric"
{
if
err
:=
DB
.
Raw
(
`SELECT data_type FROM information_schema.columns
WHERE table_schema = current_schema() AND table_name = ? AND column_name = ?`
,
tableName
,
columnName
)
.
Scan
(
&
dataType
)
.
Error
;
err
!=
nil
{
common
.
SysLog
(
fmt
.
Sprintf
(
"Warning: failed to query metadata for %s.%s: %v"
,
tableName
,
columnName
,
err
))
}
else
if
dataType
==
"numeric"
{
return
// Already decimal/numeric
}
alterSQL
=
fmt
.
Sprintf
(
`ALTER TABLE %s ALTER COLUMN %s TYPE decimal(10,6) USING %s::decimal(10,6)`
,
...
...
@@ -533,10 +540,11 @@ func migrateSubscriptionPlanPriceAmount() {
}
else
if
common
.
UsingMySQL
{
// MySQL: Check if already decimal
var
columnType
string
DB
.
Raw
(
`SELECT COLUMN_TYPE FROM information_schema.columns
WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`
,
tableName
,
columnName
)
.
Scan
(
&
columnType
)
if
strings
.
HasPrefix
(
strings
.
ToLower
(
columnType
),
"decimal"
)
{
if
err
:=
DB
.
Raw
(
`SELECT COLUMN_TYPE FROM information_schema.columns
WHERE table_schema = DATABASE() AND table_name = ? AND column_name = ?`
,
tableName
,
columnName
)
.
Scan
(
&
columnType
)
.
Error
;
err
!=
nil
{
common
.
SysLog
(
fmt
.
Sprintf
(
"Warning: failed to query metadata for %s.%s: %v"
,
tableName
,
columnName
,
err
))
}
else
if
strings
.
HasPrefix
(
strings
.
ToLower
(
columnType
),
"decimal"
)
{
return
// Already decimal
}
alterSQL
=
fmt
.
Sprintf
(
"ALTER TABLE %s MODIFY COLUMN %s decimal(10,6) NOT NULL DEFAULT 0"
,
...
...
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