2024-07-23 14:48:37 +08:00
package migrations
import (
2025-09-05 17:39:36 +08:00
"encoding/json"
2026-02-05 16:54:55 +08:00
"errors"
2024-12-28 17:58:43 +08:00
"fmt"
2025-09-05 17:39:36 +08:00
"os"
"os/user"
2025-01-14 14:27:51 +08:00
"path"
2025-10-27 18:04:17 +08:00
"strconv"
2025-09-26 15:12:13 +08:00
"strings"
2025-03-04 17:58:57 +08:00
"time"
2024-12-28 17:58:43 +08:00
2025-09-05 17:39:36 +08:00
"github.com/1Panel-dev/1Panel/agent/app/dto"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/app/dto/request"
"github.com/1Panel-dev/1Panel/agent/app/model"
2026-07-17 13:47:49 +08:00
providercatalog "github.com/1Panel-dev/1Panel/agent/app/provider"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/app/service"
"github.com/1Panel-dev/1Panel/agent/constant"
"github.com/1Panel-dev/1Panel/agent/global"
2026-02-11 14:54:59 +08:00
migrationutils "github.com/1Panel-dev/1Panel/agent/init/migration/migrations/utils"
2026-09-02 14:49:20 +08:00
alertwebhook "github.com/1Panel-dev/1Panel/agent/utils/alert_webhook"
2024-07-23 14:48:37 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/common"
2025-09-05 17:39:36 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/copier"
2024-11-28 18:42:40 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/encrypt"
2026-09-16 16:48:48 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/firewall"
2025-09-05 17:39:36 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/ssh"
2024-08-21 15:14:50 +08:00
"github.com/1Panel-dev/1Panel/agent/utils/xpack"
2024-07-23 14:48:37 +08:00
"github.com/go-gormigrate/gormigrate/v2"
2026-09-02 14:49:20 +08:00
"github.com/google/uuid"
2024-07-23 14:48:37 +08:00
"gorm.io/gorm"
)
var AddTable = & gormigrate . Migration {
2025-10-02 20:33:20 +08:00
ID : "20250930-add-table" ,
2024-07-23 14:48:37 +08:00
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate (
& model . AppDetail {},
& model . AppInstallResource {},
& model . AppInstall {},
& model . AppTag {},
& model . Tag {},
& model . App {},
2024-12-26 14:13:41 +08:00
& model . AppLauncher {},
2025-05-13 15:17:08 +08:00
& model . OllamaModel {},
2024-12-26 14:13:41 +08:00
& model . BackupAccount {},
2024-07-23 14:48:37 +08:00
& model . BackupRecord {},
& model . Clam {},
& model . ComposeTemplate {},
& model . Compose {},
& model . Cronjob {},
& model . Database {},
& model . DatabaseMysql {},
2026-07-16 14:24:38 +08:00
& model . DatabaseUser {},
2026-07-21 19:08:09 +08:00
& model . DatabaseUserGrant {},
2026-04-14 18:40:44 +08:00
& model . DatabaseMongodb {},
2024-07-23 14:48:37 +08:00
& model . DatabasePostgresql {},
& model . Favorite {},
2026-04-09 17:38:38 +08:00
& model . FileShare {},
2026-03-19 15:25:19 +08:00
& model . Host {},
2024-07-23 14:48:37 +08:00
& model . Ftp {},
& model . ImageRepo {},
2025-05-06 18:09:37 +08:00
& model . ScriptLibrary {},
2024-07-23 14:48:37 +08:00
& model . JobRecords {},
& model . MonitorBase {},
& model . MonitorIO {},
& model . MonitorNetwork {},
& model . PHPExtensions {},
& model . Runtime {},
& model . Setting {},
& model . Snapshot {},
& model . Tag {},
& model . Website {},
& model . WebsiteAcmeAccount {},
& model . WebsiteCA {},
2026-07-31 10:01:20 +08:00
& model . WebsiteTemplate {},
& model . WebsiteTemplateOutput {},
2024-07-23 14:48:37 +08:00
& model . WebsiteDnsAccount {},
& model . WebsiteDomain {},
& model . WebsiteSSL {},
2025-03-03 16:18:47 +08:00
& model . Group {},
2025-04-18 15:44:28 +08:00
& model . AppIgnoreUpgrade {},
2025-04-28 15:21:47 +08:00
& model . McpServer {},
2025-08-01 10:43:20 +08:00
& model . RootCert {},
2025-09-04 17:13:32 +08:00
& model . ClamRecord {},
2024-07-23 14:48:37 +08:00
)
},
}
2024-08-14 10:15:55 +08:00
var AddMonitorTable = & gormigrate . Migration {
ID : "20240813-add-monitor-table" ,
Migrate : func ( tx * gorm . DB ) error {
return global . MonitorDB . AutoMigrate (
& model . MonitorBase {},
& model . MonitorIO {},
& model . MonitorNetwork {},
)
},
}
2024-07-23 14:48:37 +08:00
var InitSetting = & gormigrate . Migration {
ID : "20240722-init-setting" ,
Migrate : func ( tx * gorm . DB ) error {
2025-01-24 11:00:07 +08:00
global . CONF . Base . EncryptKey = common . RandStr ( 16 )
2026-04-23 17:00:50 +08:00
nodeInfo , err := xpack . MultiNodeProvider . LoadNodeInfo ( true )
2024-08-21 15:14:50 +08:00
if err != nil {
2024-07-25 18:13:57 +08:00
return err
}
2024-11-28 18:42:40 +08:00
if err := tx . Create ( & model . Setting { Key : "BaseDir" , Value : nodeInfo . BaseDir }). Error ; err != nil {
2024-11-26 14:34:47 +08:00
return err
}
2024-11-28 18:42:40 +08:00
itemKey , _ := encrypt . StringEncrypt ( nodeInfo . ServerKey )
if err := tx . Create ( & model . Setting { Key : "ServerKey" , Value : itemKey }). Error ; err != nil {
return err
}
itemCrt , _ := encrypt . StringEncrypt ( nodeInfo . ServerCrt )
if err := tx . Create ( & model . Setting { Key : "ServerCrt" , Value : itemCrt }). Error ; err != nil {
return err
}
2024-12-26 14:13:41 +08:00
if err := tx . Create ( & model . Setting { Key : "NodeScope" , Value : nodeInfo . Scope }). Error ; err != nil {
2024-11-28 18:42:40 +08:00
return err
}
2024-12-28 17:58:43 +08:00
if err := tx . Create ( & model . Setting { Key : "NodePort" , Value : fmt . Sprintf ( "%v" , nodeInfo . NodePort )}). Error ; err != nil {
return err
}
2024-11-28 18:42:40 +08:00
if err := tx . Create ( & model . Setting { Key : "SystemVersion" , Value : nodeInfo . Version }). Error ; err != nil {
2024-08-21 15:14:50 +08:00
return err
}
2024-07-25 18:13:57 +08:00
2025-02-10 11:18:32 +08:00
if err := tx . Create ( & model . Setting { Key : "EncryptKey" , Value : global . CONF . Base . EncryptKey }). Error ; err != nil {
2024-12-26 15:38:56 +08:00
return err
}
2024-07-23 14:48:37 +08:00
if err := tx . Create ( & model . Setting { Key : "DockerSockPath" , Value : "unix:///var/run/docker.sock" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "SystemStatus" , Value : "Free" }). Error ; err != nil {
return err
}
2026-02-28 17:17:53 +08:00
lang := common . LoadParamsWithoutPanic ( "LANGUAGE" )
if err := tx . Create ( & model . Setting { Key : "Language" , Value : lang }). Error ; err != nil {
2025-05-13 15:17:08 +08:00
return err
}
if err := tx . Create ( & model . Setting { Key : "SystemIP" , Value : "" }). Error ; err != nil {
return err
}
2024-07-23 14:48:37 +08:00
if err := tx . Create ( & model . Setting { Key : "LocalTime" , Value : "" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "TimeZone" , Value : common . LoadTimeZoneByCmd ()}). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "NtpSite" , Value : "pool.ntp.org" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "LastCleanTime" , Value : "" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "LastCleanSize" , Value : "" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "LastCleanData" , Value : "" }). Error ; err != nil {
return err
}
2024-08-05 18:22:25 +08:00
if err := tx . Create ( & model . Setting { Key : "DefaultNetwork" , Value : "all" }). Error ; err != nil {
return err
}
2025-05-13 15:17:08 +08:00
if err := tx . Create ( & model . Setting { Key : "MonitorStatus" , Value : constant . StatusEnable }). Error ; err != nil {
2024-07-23 14:48:37 +08:00
return err
}
if err := tx . Create ( & model . Setting { Key : "MonitorStoreDays" , Value : "7" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "MonitorInterval" , Value : "5" }). Error ; err != nil {
return err
}
2025-05-13 17:54:06 +08:00
if err := tx . Create ( & model . Setting { Key : "ProxyType" , Value : "" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "ProxyUrl" , Value : "" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "ProxyPort" , Value : "" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "ProxyUser" , Value : "" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "ProxyPasswd" , Value : "" }). Error ; err != nil {
return err
}
2024-07-23 14:48:37 +08:00
if err := tx . Create ( & model . Setting { Key : "AppStoreVersion" , Value : "" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "AppStoreSyncStatus" , Value : "SyncSuccess" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "AppStoreLastModified" , Value : "0" }). Error ; err != nil {
return err
}
2025-05-13 15:17:08 +08:00
if err := tx . Create ( & model . Setting { Key : "FileRecycleBin" , Value : constant . StatusEnable }). Error ; err != nil {
2024-07-23 14:48:37 +08:00
return err
}
2024-07-31 18:29:41 +08:00
2025-05-13 15:17:08 +08:00
if err := tx . Create ( & model . Setting { Key : "LocalSSHConn" , Value : "" }). Error ; err != nil {
return err
}
2024-07-23 14:48:37 +08:00
return nil
},
}
var InitImageRepo = & gormigrate . Migration {
ID : "20240722-init-imagerepo" ,
Migrate : func ( tx * gorm . DB ) error {
item := & model . ImageRepo {
Name : "Docker Hub" ,
Protocol : "https" ,
DownloadUrl : "docker.io" ,
Status : constant . StatusSuccess ,
}
if err := tx . Create ( item ). Error ; err != nil {
return err
}
return nil
},
}
var InitDefaultCA = & gormigrate . Migration {
ID : "20240722-init-default-ca" ,
Migrate : func ( tx * gorm . DB ) error {
caService := service . NewIWebsiteCAService ()
if _ , err := caService . Create ( request . WebsiteCACreate {
CommonName : "1Panel-CA" ,
Country : "CN" ,
2026-05-26 14:18:17 +08:00
KeyType : "EC256" ,
2024-07-23 14:48:37 +08:00
Name : "1Panel" ,
Organization : "FIT2CLOUD" ,
OrganizationUint : "1Panel" ,
Province : "Beijing" ,
City : "Beijing" ,
}); err != nil {
return err
}
return nil
},
}
var InitPHPExtensions = & gormigrate . Migration {
ID : "20240722-add-php-extensions" ,
Migrate : func ( tx * gorm . DB ) error {
2025-06-04 17:31:37 +08:00
phpExtensions := [] model . PHPExtensions {
{
Name : "Default" ,
Extensions : "bcmath,ftp,gd,gettext,intl,mysqli,pcntl,pdo_mysql,shmop,soap,sockets,sysvsem,xmlrpc,zip" ,
},
{
Name : "WordPress" ,
Extensions : "exif,igbinary,imagick,intl,zip,apcu,memcached,opcache,redis,shmop,mysqli,pdo_mysql,gd" ,
},
{
Name : "Flarum" ,
Extensions : "curl,gd,pdo_mysql,mysqli,bz2,exif,yaf,imap" ,
},
{
Name : "SeaCMS" ,
Extensions : "mysqli,pdo_mysql,gd,curl" ,
},
{
Name : "Dev" ,
Extensions : "bcmath,ftp,gd,gettext,intl,mysqli,pcntl,pdo_mysql,shmop,soap,sockets,sysvsem,xmlrpc,zip,exif,igbinary,imagick,apcu,memcached,opcache,redis,bc,image,dom,iconv,mbstring,mysqlnd,openssl,pdo,tokenizer,xml,curl,bz2,yaf,imap,xdebug,swoole,pdo_pgsql,fileinfo,pgsql,calendar,gmp" ,
},
2024-07-23 14:48:37 +08:00
}
2025-06-04 17:31:37 +08:00
for _ , ext := range phpExtensions {
if err := tx . Create ( & ext ). Error ; err != nil {
return err
}
2024-07-23 14:48:37 +08:00
}
return nil
},
}
2024-12-27 14:55:03 +08:00
var AddTaskTable = & gormigrate . Migration {
ID : "20241226-add-task" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate (
& model . Task {},
)
},
}
2024-12-28 17:58:43 +08:00
2025-01-14 14:27:51 +08:00
var InitBackup = & gormigrate . Migration {
ID : "20241226-init-backup" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . Create ( & model . BackupAccount {
Name : "localhost" ,
Type : "LOCAL" ,
2025-01-24 11:00:07 +08:00
BackupPath : path . Join ( global . Dir . DataDir , "backup" ),
2025-01-14 14:27:51 +08:00
}). Error ; err != nil {
return err
}
return nil
},
}
2025-02-06 18:15:06 +08:00
2025-03-03 16:18:47 +08:00
var InitDefault = & gormigrate . Migration {
ID : "20250301-init-default" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . Create ( & model . Group { Name : "Default" , Type : "website" , IsDefault : true }). Error ; err != nil {
return err
}
return nil
},
}
2025-03-04 17:58:57 +08:00
var UpdateWebsiteExpireDate = & gormigrate . Migration {
ID : "20250304-update-website" ,
Migrate : func ( tx * gorm . DB ) error {
targetDate := time . Date ( 9999 , 12 , 31 , 0 , 0 , 0 , 0 , time . UTC )
if err := tx . Model ( & model . Website {}).
Where ( "expire_date = ?" , time . Date ( 1970 , 1 , 1 , 0 , 0 , 0 , 0 , time . UTC )).
Update ( "expire_date" , targetDate ). Error ; err != nil {
return err
}
return nil
},
}
2025-06-23 18:37:37 +08:00
var UpdateRuntime = & gormigrate . Migration {
ID : "20250624-update-runtime" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate (
& model . Runtime {},
)
},
}
2025-06-27 16:47:26 +08:00
var AddSnapshotRule = & gormigrate . Migration {
2025-07-03 16:31:25 +08:00
ID : "20250703-add-snapshot-rule" ,
2025-06-27 16:47:26 +08:00
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate (
& model . Cronjob {},
)
},
}
2025-06-27 16:56:13 +08:00
var UpdatePHPRuntime = & gormigrate . Migration {
2025-07-02 12:23:35 +08:00
ID : "20250702-update-php-runtime" ,
2025-06-27 16:56:13 +08:00
Migrate : func ( tx * gorm . DB ) error {
service . HandleOldPHPRuntime ()
return nil
},
}
2025-06-27 17:25:23 +08:00
var AddSnapshotIgnore = & gormigrate . Migration {
2025-07-16 18:43:52 +08:00
ID : "20250716-add-snapshot-ignore" ,
2025-06-27 17:25:23 +08:00
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate (
& model . Snapshot {},
)
},
}
2025-07-02 17:41:36 +08:00
var InitAppLauncher = & gormigrate . Migration {
ID : "20250702-init-app-launcher" ,
Migrate : func ( tx * gorm . DB ) error {
launchers := [] string { "openresty" , "mysql" , "halo" , "redis" , "maxkb" , "wordpress" }
for _ , val := range launchers {
var item model . AppLauncher
_ = tx . Model ( & model . AppLauncher {}). Where ( "key = ?" , val ). First ( & item ). Error
if item . ID == 0 {
item . Key = val
_ = tx . Create ( & item ). Error
}
}
return nil
},
}
2025-07-18 11:44:20 +08:00
var AddTableAlert = & gormigrate . Migration {
ID : "20250122-add-table-alert" ,
Migrate : func ( tx * gorm . DB ) error {
return global . AlertDB . AutoMigrate ( & model . Alert {}, & model . AlertTask {}, model . AlertLog {}, model . AlertConfig {})
},
}
var InitAlertConfig = & gormigrate . Migration {
ID : "20250705-init-alert-config" ,
Migrate : func ( tx * gorm . DB ) error {
records := [] model . AlertConfig {
{
2026-06-02 22:35:21 +08:00
Type : "sms" ,
Title : "xpack.alert.smsConfig" ,
Status : "Enable" ,
Config : `{"alertDailyNum":50}` ,
CreateUser : "system" ,
UpdateUser : "system" ,
2025-07-18 11:44:20 +08:00
},
{
2026-06-02 22:35:21 +08:00
Type : "common" ,
Title : "xpack.alert.commonConfig" ,
Status : "Enable" ,
Config : `{"isOffline":"Disable","alertSendTimeRange":{"noticeAlert":{"sendTimeRange":"08:00:00 - 23:59:59","type":["ssl","siteEndTime","panelPwdEndTime","panelUpdate"]},"resourceAlert":{"sendTimeRange":"00:00:00 - 23:59:59","type":["clams","cronJob","cpu","memory","load","disk"]}}}` ,
CreateUser : "system" ,
UpdateUser : "system" ,
2025-07-18 11:44:20 +08:00
},
}
for _ , r := range records {
if err := global . AlertDB . Model ( & model . AlertConfig {}). Create ( & r ). Error ; err != nil {
return err
}
}
return nil
},
}
var AddMethodToAlertLog = & gormigrate . Migration {
ID : "20250713-add-method-to-alert_log" ,
Migrate : func ( tx * gorm . DB ) error {
if err := global . AlertDB . AutoMigrate ( & model . AlertLog {}); err != nil {
return err
}
if err := global . AlertDB . Model ( & model . AlertLog {}). Where ( "method IS NULL OR method = ''" ). Update ( "method" , "sms" ). Error ; err != nil {
return err
}
return nil
},
}
2025-07-23 16:58:24 +08:00
var AddMethodToAlertTask = & gormigrate . Migration {
ID : "20250723-add-method-to-alert_task" ,
Migrate : func ( tx * gorm . DB ) error {
if err := global . AlertDB . AutoMigrate ( & model . AlertTask {}); err != nil {
return err
}
if err := global . AlertDB . Model ( & model . AlertTask {}). Where ( "method IS NULL OR method = ''" ). Update ( "method" , "sms" ). Error ; err != nil {
return err
}
return nil
},
}
2025-07-30 18:54:51 +08:00
var UpdateMcpServer = & gormigrate . Migration {
ID : "20250729-update-mcp-server" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . McpServer {}); err != nil {
return err
}
if err := tx . Model ( & model . McpServer {}). Where ( "1=1" ). Update ( "output_transport" , "sse" ). Error ; err != nil {
return err
}
return nil
},
}
2025-08-05 23:15:03 +08:00
var InitCronjobGroup = & gormigrate . Migration {
ID : "20250805-init-cronjob-group" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . Cronjob {}); err != nil {
return err
}
if err := tx . Model ( & model . Cronjob {}). Where ( "1=1" ). Updates ( map [ string ] interface {}{ "group_id" : 0 }). Error ; err != nil {
return err
}
return nil
},
}
2025-08-19 13:41:15 +08:00
var AddColumnToAlert = & gormigrate . Migration {
ID : "20250729-add-column-to-alert" ,
Migrate : func ( tx * gorm . DB ) error {
if err := global . AlertDB . AutoMigrate ( & model . Alert {}); err != nil {
return err
}
if err := global . AlertDB . Model ( & model . Alert {}).
Where ( "advanced_params IS NULL" ).
Update ( "advanced_params" , "" ). Error ; err != nil {
return err
}
return nil
},
}
2025-08-20 16:41:19 +08:00
2026-06-02 22:35:21 +08:00
var MigrateAlertMethodConfigIDs = & gormigrate . Migration {
ID : "20251001-migrate-alert-method-config-ids" ,
Migrate : func ( tx * gorm . DB ) error {
2026-09-02 14:49:20 +08:00
if err := tx . AutoMigrate ( & model . Alert {}, & model . AlertLog {}, & model . AlertTask {}, & model . AlertConfig {}); err != nil {
2026-06-02 22:35:21 +08:00
return err
}
2026-06-08 21:47:47 +08:00
if err := migrateAlertMethodConfigIDs ( tx ); err != nil {
return err
}
return nil
},
}
var MigrateAlertLogTaskMethodConfigIDs = & gormigrate . Migration {
ID : "20260608-migrate-alert-log-task-method-config-ids" ,
Migrate : func ( tx * gorm . DB ) error {
2026-09-02 14:49:20 +08:00
if err := tx . AutoMigrate ( & model . AlertLog {}, & model . AlertTask {}, & model . AlertConfig {}); err != nil {
2026-06-08 21:47:47 +08:00
return err
}
if err := migrateAlertMethodRecords ( tx , & model . AlertLog {}); err != nil {
return err
}
if err := migrateAlertMethodRecords ( tx , & model . AlertTask {}); err != nil {
2026-06-02 22:35:21 +08:00
return err
}
return nil
},
}
var AddAlertAuditUser = & gormigrate . Migration {
ID : "20260602-add-alert-audit-user" ,
Migrate : func ( tx * gorm . DB ) error {
2026-09-02 14:49:20 +08:00
return tx . AutoMigrate ( & model . Alert {}, & model . AlertConfig {})
2026-06-02 22:35:21 +08:00
},
}
2026-09-02 14:49:20 +08:00
var AddAlertConfigUIDAndSecret = & gormigrate . Migration {
ID : "20260826-add-alert-config-uid-secret" ,
Migrate : func ( tx * gorm . DB ) error {
return migrateAlertConfigUIDAndSecret ( tx )
},
}
var AddAlertTaskDeliveryLogID = & gormigrate . Migration {
ID : "20260826-add-alert-task-delivery-log-id" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . AlertTask {})
},
}
func migrateAlertConfigUIDAndSecret ( tx * gorm . DB ) error {
if ! tx . Migrator (). HasTable ( & model . AlertConfig {}) {
return tx . AutoMigrate ( & model . AlertConfig {})
}
if ! tx . Migrator (). HasColumn ( & model . AlertConfig {}, "UID" ) {
if err := tx . Exec ( "ALTER TABLE alert_configs ADD COLUMN uid varchar(64)" ). Error ; err != nil {
return err
}
}
if ! tx . Migrator (). HasColumn ( & model . AlertConfig {}, "SecretConfig" ) {
if err := tx . Exec ( "ALTER TABLE alert_configs ADD COLUMN secret_config text NOT NULL DEFAULT ''" ). Error ; err != nil {
return err
}
}
var configs [] struct {
ID uint
UID string
}
if err := tx . Table ( "alert_configs" ). Select ( "id" , "uid" ). Find ( & configs ). Error ; err != nil {
return err
}
for _ , config := range configs {
if strings . TrimSpace ( config . UID ) != "" {
continue
}
if err := tx . Table ( "alert_configs" ). Where ( "id = ?" , config . ID ). Update ( "uid" , uuid . NewString ()). Error ; err != nil {
return err
}
}
if err := tx . AutoMigrate ( & model . AlertConfig {}); err != nil {
return err
}
var customConfigs [] model . AlertConfig
if err := tx . Where ( "type = ?" , constant . Custom ). Find ( & customConfigs ). Error ; err != nil {
return err
}
for _ , config := range customConfigs {
prepared , status , legacy , err := alertwebhook . NormalizeLegacy ( config . Config , config . Status , config . Title )
if err != nil {
return fmt . Errorf ( "normalize legacy custom webhook %d: %w" , config . ID , err )
}
if ! legacy {
if err := alertwebhook . ValidateStored ( config ); err != nil && config . Status != constant . AlertDisable {
if updateErr := tx . Model ( & model . AlertConfig {}). Where ( "id = ?" , config . ID ). Update ( "status" , constant . AlertDisable ). Error ; updateErr != nil {
return updateErr
}
}
continue
}
if err := tx . Model ( & model . AlertConfig {}). Where ( "id = ?" , config . ID ). Updates ( map [ string ] interface {}{
"config" : prepared . Config ,
"secret_config" : prepared . SecretConfig ,
"status" : status ,
}). Error ; err != nil {
return err
}
}
if tx . Migrator (). HasTable ( & model . Alert {}) {
if err := migrateAlertMethodConfigIDs ( tx ); err != nil {
return err
}
}
if tx . Migrator (). HasTable ( & model . AlertLog {}) {
if err := migrateAlertMethodRecords ( tx , & model . AlertLog {}); err != nil {
return err
}
}
if tx . Migrator (). HasTable ( & model . AlertTask {}) {
if err := migrateAlertMethodRecords ( tx , & model . AlertTask {}); err != nil {
return err
}
}
return nil
}
2026-06-02 22:35:21 +08:00
func migrateAlertMethodConfigIDs ( tx * gorm . DB ) error {
if err := tx . Model ( & model . AlertConfig {}). Where ( "type = ?" , "mail" ). Update ( "type" , constant . EmailConfig ). Error ; err != nil {
return err
}
2026-06-08 21:47:47 +08:00
configIDs , err := loadAlertConfigIDs ( tx )
if err != nil {
return err
2026-06-02 22:35:21 +08:00
}
var alerts [] model . Alert
if err := tx . Find ( & alerts ). Error ; err != nil {
return err
}
for _ , alert := range alerts {
2026-06-08 21:47:47 +08:00
method := migrateAlertMethodValue ( alert . Method , alertLegacyMethodTypeMap (), configIDs )
2026-06-02 22:35:21 +08:00
if method == alert . Method {
continue
}
if err := tx . Model ( & model . Alert {}). Where ( "id = ?" , alert . ID ). Update ( "method" , method ). Error ; err != nil {
return err
}
}
return nil
}
2026-06-08 21:47:47 +08:00
func migrateAlertMethodRecords ( tx * gorm . DB , modelValue interface {}) error {
configIDs , err := loadAlertConfigIDs ( tx )
if err != nil {
return err
}
switch modelValue .( type ) {
case * model . AlertLog :
var logs [] model . AlertLog
if err := tx . Find ( & logs ). Error ; err != nil {
return err
}
for _ , item := range logs {
method := migrateAlertMethodValue ( item . Method , alertLegacyMethodTypeMap (), configIDs )
if method == item . Method {
continue
}
if err := tx . Model ( & model . AlertLog {}). Where ( "id = ?" , item . ID ). Update ( "method" , method ). Error ; err != nil {
return err
}
}
case * model . AlertTask :
var tasks [] model . AlertTask
if err := tx . Find ( & tasks ). Error ; err != nil {
return err
}
for _ , item := range tasks {
method := migrateAlertMethodValue ( item . Method , alertLegacyMethodTypeMap (), configIDs )
if method == item . Method {
continue
}
if err := tx . Model ( & model . AlertTask {}). Where ( "id = ?" , item . ID ). Update ( "method" , method ). Error ; err != nil {
return err
}
}
}
return nil
}
func loadAlertConfigIDs ( tx * gorm . DB ) ( map [ string ] string , error ) {
configIDs := map [ string ] string {}
for _ , configType := range alertLegacyMethodTypeMap () {
if _ , ok := configIDs [ configType ]; ok {
continue
}
var config model . AlertConfig
if err := tx . Where ( "type = ?" , configType ). Order ( "id ASC" ). First ( & config ). Error ; err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
continue
}
return nil , err
}
configIDs [ configType ] = strconv . Itoa ( int ( config . ID ))
}
return configIDs , nil
}
func alertLegacyMethodTypeMap () map [ string ] string {
return map [ string ] string {
"mail" : constant . Email ,
constant . Email : constant . Email ,
constant . SMS : constant . SMS ,
constant . Bark : constant . Bark ,
constant . WeChat : constant . WeCom ,
constant . WeCom : constant . WeCom ,
constant . DingTalk : constant . DingTalk ,
constant . FeiShu : constant . FeiShu ,
2026-09-02 14:49:20 +08:00
constant . Custom : constant . Custom ,
2026-06-08 21:47:47 +08:00
}
}
2026-06-02 22:35:21 +08:00
func migrateAlertMethodValue ( method string , typeMap map [ string ] string , configIDs map [ string ] string ) string {
items := strings . Split ( method , "," )
next := make ([] string , 0 , len ( items ))
seen := map [ string ] struct {}{}
for _ , item := range items {
item = strings . TrimSpace ( item )
if item == "" {
continue
}
configType , ok := typeMap [ item ]
if ok {
if id , exists := configIDs [ configType ]; exists {
item = id
}
}
if _ , exists := seen [ item ]; exists {
continue
}
seen [ item ] = struct {}{}
next = append ( next , item )
}
return strings . Join ( next , "," )
}
2025-08-20 16:41:19 +08:00
var UpdateWebsiteSSL = & gormigrate . Migration {
ID : "20250819-update-website-ssl" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . WebsiteSSL {}); err != nil {
return err
}
return nil
},
}
2025-09-02 17:58:10 +08:00
var AddQuickJump = & gormigrate . Migration {
ID : "20250901-add-quick-jump" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . QuickJump {}); err != nil {
return err
}
if err := tx . Create ( & model . QuickJump { Name : "Website" , Title : "menu.website" , Recommend : 10 , IsShow : true , Router : "/websites" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . QuickJump { Name : "Database" , Title : "home.database" , Recommend : 30 , IsShow : true , Router : "/databases" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . QuickJump { Name : "Cronjob" , Title : "menu.cronjob" , Recommend : 50 , IsShow : true , Router : "/cronjobs" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . QuickJump { Name : "AppInstalled" , Title : "home.appInstalled" , Recommend : 70 , IsShow : true , Router : "/apps/installed" }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . QuickJump { Name : "File" , Detail : "/" , Title : "home.quickDir" , Recommend : 90 , IsShow : false , Router : "/hosts/files" }). Error ; err != nil {
return err
}
return nil
},
}
2025-09-04 15:21:33 +08:00
var UpdateMcpServerAddType = & gormigrate . Migration {
ID : "20250904-update-mcp-server" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . McpServer {}); err != nil {
return err
}
if err := tx . Model ( & model . McpServer {}). Where ( "1=1" ). Update ( "type" , "npx" ). Error ; err != nil {
return err
}
return nil
},
}
2025-09-05 17:39:36 +08:00
2026-06-16 16:47:27 +08:00
var UpdateMcpServerGatewayConfig = & gormigrate . Migration {
ID : "20260615-update-mcp-server-gateway-config" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . McpServer {}); err != nil {
return err
}
if err := tx . Model ( & model . McpServer {}). Where ( "gateway_image IS NULL OR gateway_image = ''" ). Where ( "type = ?" , "uvx" ). Update ( "gateway_image" , "supercorp/supergateway:uvx" ). Error ; err != nil {
return err
}
if err := tx . Model ( & model . McpServer {}). Where ( "gateway_image IS NULL OR gateway_image = ''" ). Where ( "type <> ? OR type IS NULL OR type = ''" , "uvx" ). Update ( "gateway_image" , "supercorp/supergateway:3.4.3" ). Error ; err != nil {
return err
}
if err := tx . Model ( & model . McpServer {}). Where ( "protocol_version IS NULL OR protocol_version = ''" ). Update ( "protocol_version" , "2025-06-18" ). Error ; err != nil {
return err
}
return nil
},
}
2025-09-05 17:39:36 +08:00
var InitLocalSSHConn = & gormigrate . Migration {
ID : "20250905-init-local-ssh" ,
Migrate : func ( tx * gorm . DB ) error {
itemPath := ""
currentInfo , _ := user . Current ()
if len ( currentInfo . HomeDir ) == 0 {
itemPath = "/root/.ssh/id_ed25519_1panel"
} else {
itemPath = path . Join ( currentInfo . HomeDir , ".ssh/id_ed25519_1panel" )
}
if _ , err := os . Stat ( itemPath ); err != nil {
2026-11-09 16:37:45 +08:00
if err := service . NewISSHService (). CreateRootCert ( dto . RootCertOperate { Mode : "generate" , EncryptionMode : "ed25519" , Name : "id_ed25519_1panel" , Description : "1Panel Terminal" }); err != nil {
global . LOG . Errorf ( "initialize local SSH key failed: %v" , err )
return nil
}
2025-09-05 17:39:36 +08:00
}
privateKey , _ := os . ReadFile ( itemPath )
connWithKey := ssh . ConnInfo {
Addr : "127.0.0.1" ,
User : "root" ,
Port : 22 ,
AuthMode : "key" ,
PrivateKey : privateKey ,
}
2026-11-09 16:37:45 +08:00
client , err := ssh . NewClient ( connWithKey )
if err != nil {
2025-09-05 17:39:36 +08:00
return nil
}
2026-11-09 16:37:45 +08:00
defer client . Close ()
2025-09-05 17:39:36 +08:00
var conn model . LocalConnInfo
_ = copier . Copy ( & conn , & connWithKey )
conn . PrivateKey = string ( privateKey )
conn . PassPhrase = ""
localConn , _ := json . Marshal ( & conn )
connAfterEncrypt , _ := encrypt . StringEncrypt ( string ( localConn ))
if err := tx . Model ( & model . Setting {}). Where ( "key = ?" , "LocalSSHConn" ). Updates ( map [ string ] interface {}{ "value" : connAfterEncrypt }). Error ; err != nil {
return err
}
return nil
},
}
2025-09-08 22:08:42 +08:00
var InitLocalSSHShow = & gormigrate . Migration {
ID : "20250908-init-local-ssh-show" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . Create ( & model . Setting { Key : "LocalSSHConnShow" , Value : constant . StatusEnable }). Error ; err != nil {
return err
}
return nil
},
}
2025-09-15 16:27:11 +08:00
var InitRecordStatus = & gormigrate . Migration {
ID : "20250910-init-record-status" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . BackupRecord {}); err != nil {
return err
}
if err := tx . Model ( & model . BackupRecord {}). Where ( "1 == 1" ). Updates ( map [ string ] interface {}{ "status" : constant . StatusSuccess }). Error ; err != nil {
return err
}
return nil
},
}
2025-09-17 17:51:59 +08:00
var AddShowNameForQuickJump = & gormigrate . Migration {
ID : "20250918-add-show-name-for-quick-jump" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . QuickJump {})
},
}
2025-09-23 11:56:04 +08:00
2026-03-12 17:31:33 +08:00
var AddAgentQuickJump = & gormigrate . Migration {
ID : "20260312-add-agent-quick-jump" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . QuickJump {}); err != nil {
return err
}
var quicks [] model . QuickJump
if err := tx . Find ( & quicks ). Error ; err != nil {
return err
}
var (
cronjob * model . QuickJump
database * model . QuickJump
showList [] * model . QuickJump
)
for i := range quicks {
switch quicks [ i ]. Name {
case "Cronjob" :
cronjob = & quicks [ i ]
case "Database" :
database = & quicks [ i ]
}
if quicks [ i ]. IsShow {
showList = append ( showList , & quicks [ i ])
}
}
showCount := len ( showList )
updatedIDs := make ( map [ uint ] struct {})
kickedCronjob := false
if showCount >= 4 && cronjob != nil && cronjob . IsShow {
cronjob . IsShow = false
updatedIDs [ cronjob . ID ] = struct {}{}
kickedCronjob = true
}
if ! kickedCronjob && showCount >= 4 && database != nil && database . IsShow {
database . IsShow = false
updatedIDs [ database . ID ] = struct {}{}
}
for _ , item := range quicks {
if _ , ok := updatedIDs [ item . ID ]; ! ok {
continue
}
if err := tx . Model ( & model . QuickJump {}). Where ( "id = ?" , item . ID ). Update ( "is_show" , item . IsShow ). Error ; err != nil {
return err
}
}
return tx . Create ( & model . QuickJump {
Name : "Agent" ,
2026-03-24 18:56:30 +08:00
Title : "aiTools.agents.agent" ,
2026-03-12 17:31:33 +08:00
Recommend : 1 ,
IsShow : true ,
Router : "/ai/agents/agent" ,
}). Error
},
}
2025-09-23 11:56:04 +08:00
var AddTimeoutForClam = & gormigrate . Migration {
ID : "20250922-add-timeout-for-clam" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . Clam {}); err != nil {
return err
}
if err := tx . Model ( & model . Clam {}). Where ( "1 == 1" ). Updates ( map [ string ] interface {}{ "timeout" : 18000 }). Error ; err != nil {
return err
}
return nil
},
}
2025-09-26 15:12:13 +08:00
2025-09-28 18:16:18 +08:00
var UpdateCronjobSpec = & gormigrate . Migration {
2025-09-26 15:12:13 +08:00
ID : "20250925-update-cronjob-spec" ,
Migrate : func ( tx * gorm . DB ) error {
var cronjobs [] model . Cronjob
if err := tx . Where ( "1 == 1" ). Find ( & cronjobs ). Error ; err != nil {
return err
}
for _ , item := range cronjobs {
if ! strings . Contains ( item . Spec , "," ) {
continue
}
if err := tx . Model ( & model . Cronjob {}). Where ( "id = ?" , item . ID ). Updates (
map [ string ] interface {}{ "spec" : strings . ReplaceAll ( item . Spec , "," , "&&" )}). Error ; err != nil {
return err
}
}
return nil
},
}
2025-09-28 18:16:18 +08:00
var UpdateWebsiteSSLAddColumn = & gormigrate . Migration {
ID : "20250928-update-website-ssl" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . WebsiteSSL {}); err != nil {
return err
}
return nil
},
}
2025-10-18 22:49:48 +08:00
2025-10-27 18:04:17 +08:00
var UpdateMonitorInterval = & gormigrate . Migration {
ID : "20251026-update-monitor-interval" ,
Migrate : func ( tx * gorm . DB ) error {
var monitorInterval model . Setting
if err := tx . Where ( "key = ?" , "MonitorInterval" ). First ( & monitorInterval ). Error ; err != nil {
return err
}
interval , _ := strconv . Atoi ( monitorInterval . Value )
if interval == 0 {
interval = 300
}
if err := tx . Model ( & model . Setting {}).
Where ( "key = ?" , "MonitorInterval" ).
Updates ( map [ string ] interface {}{ "value" : fmt . Sprintf ( "%v" , interval * 60 )}).
Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "DefaultIO" , Value : "all" }). Error ; err != nil {
return err
}
return nil
},
}
2025-10-30 16:48:18 +08:00
var AddMonitorProcess = & gormigrate . Migration {
ID : "20251030-add-monitor-process" ,
Migrate : func ( tx * gorm . DB ) error {
return global . MonitorDB . AutoMigrate ( & model . MonitorBase {})
},
}
2025-11-05 18:08:25 +08:00
var UpdateCronJob = & gormigrate . Migration {
ID : "20251105-update-cronjob" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . Cronjob {})
},
}
2025-11-10 16:40:37 +08:00
2025-11-24 15:55:14 +08:00
var AddCommonDescription = & gormigrate . Migration {
ID : "20251117-add-common-description" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . CommonDescription {})
2025-11-24 16:07:14 +08:00
},
2025-11-24 15:55:14 +08:00
}
2025-11-24 16:07:14 +08:00
2025-11-17 00:30:55 -08:00
var UpdateDatabase = & gormigrate . Migration {
ID : "20251117-update-database" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . Database {})
},
}
2025-11-24 15:59:15 +08:00
var AddGPUMonitor = & gormigrate . Migration {
2025-11-27 13:57:24 +08:00
ID : "20251122-add-gpu-monitor" ,
2025-11-24 15:59:15 +08:00
Migrate : func ( tx * gorm . DB ) error {
return global . GPUMonitorDB . AutoMigrate ( & model . MonitorGPU {})
},
}
2025-11-24 16:07:14 +08:00
var UpdateDatabaseMysql = & gormigrate . Migration {
2025-11-25 15:02:37 +08:00
ID : "20251126-update-database-mysql" ,
2025-11-24 16:07:14 +08:00
Migrate : func ( tx * gorm . DB ) error {
2025-11-25 15:02:37 +08:00
if err := tx . AutoMigrate ( & model . DatabaseMysql {}); err != nil {
return err
}
var data [] model . DatabaseMysql
_ = tx . Where ( "1 = 1" ). Find ( & data ). Error
for _ , item := range data {
if len ( item . Collation ) == 0 {
collation := ""
switch item . Format {
case "utf8" :
collation = "utf8_general_ci"
case "utf8mb4" :
collation = "utf8mb4_general_ci"
case "gbk" :
collation = "gbk_chinese_ci"
case "big5" :
collation = "big5_chinese_ci"
default :
collation = "utf8mb4_general_ci"
}
_ = tx . Model ( & model . DatabaseMysql {}). Where ( "id = ?" , item . ID ). Updates ( map [ string ] interface {}{ "collation" : collation }). Error
}
}
return nil
2025-11-24 16:07:14 +08:00
},
}
2025-12-01 14:33:38 +08:00
2026-04-14 18:40:44 +08:00
var AddDatabaseMongodb = & gormigrate . Migration {
ID : "20260413-add-database-mongodb" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . DatabaseMongodb {})
},
}
2025-12-01 14:33:38 +08:00
var InitIptablesStatus = & gormigrate . Migration {
ID : "20251201-init-iptables-status" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . Create ( & model . Setting { Key : "IptablesStatus" , Value : constant . StatusDisable }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "IptablesForwardStatus" , Value : constant . StatusDisable }). Error ; err != nil {
return err
}
2026-06-01 18:26:54 +08:00
return nil
},
}
var InitFirewallPortWhiteList = & gormigrate . Migration {
ID : "20260601-init-firewall-port-whitelist" ,
Migrate : func ( tx * gorm . DB ) error {
var setting model . Setting
if err := tx . Where ( "key = ?" , constant . FirewallPortWhiteList ). First ( & setting ). Error ; err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
return tx . Create ( & model . Setting { Key : constant . FirewallPortWhiteList , Value : constant . FirewallPortWhiteListValue }). Error
}
return err
}
2025-12-01 14:33:38 +08:00
return nil
},
}
2025-12-11 18:33:03 +08:00
var UpdateWebsite = & gormigrate . Migration {
ID : "20251203-update-website" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . Website {})
},
}
2025-12-23 13:35:38 +08:00
var AddisIPtoWebsiteSSL = & gormigrate . Migration {
ID : "20251223-update-website-ssl" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . WebsiteSSL {})
},
}
2025-12-29 17:35:36 +08:00
var InitPingStatus = & gormigrate . Migration {
ID : "20251201-init-ping-status" ,
Migrate : func ( tx * gorm . DB ) error {
2026-09-16 16:48:48 +08:00
status := firewall . LoadPingStatus ()
2025-12-29 17:35:36 +08:00
if err := tx . Create ( & model . Setting { Key : "BanPing" , Value : status }). Error ; err != nil {
return err
}
return nil
},
}
2026-01-04 13:43:40 +08:00
var UpdateApp = & gormigrate . Migration {
ID : "20251228-update-app" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . App {})
},
}
2026-01-06 16:41:55 +08:00
var AddCronjobArgs = & gormigrate . Migration {
ID : "20260106-add-cronjob-args" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . Cronjob {})
},
}
2026-01-21 18:02:19 +08:00
var AddWebsiteAcmeAccountColumn = & gormigrate . Migration {
ID : "20260110-add-website-acme-account" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . WebsiteAcmeAccount {})
},
}
2026-02-05 16:54:55 +08:00
var AddAgentTables = & gormigrate . Migration {
ID : "20260205-add-agent-tables" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate (
& model . Agent {},
& model . AgentAccount {},
)
},
}
2026-02-24 18:26:02 +08:00
var AddAgentCustomModelFields = & gormigrate . Migration {
ID : "20260224-add-agent-custom-model-fields" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . Agent {}, & model . AgentAccount {}); err != nil {
return err
}
if err := tx . Model ( & model . AgentAccount {}). Where ( "api_type = '' OR api_type IS NULL" ). Update ( "api_type" , "openai-completions" ). Error ; err != nil {
return err
}
if err := tx . Model ( & model . Agent {}). Where ( "api_type = '' OR api_type IS NULL" ). Update ( "api_type" , "openai-completions" ). Error ; err != nil {
return err
}
return nil
},
}
2026-02-25 10:35:29 +08:00
var AddAppInstallSortOrder = & gormigrate . Migration {
ID : "20260222-add-app-install-sort-order" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . AppInstall {})
},
}
2026-02-25 13:53:45 +08:00
var AddAgentAccountRememberAPIKey = & gormigrate . Migration {
ID : "20260225-add-agent-account-remember-api-key" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AgentAccount {}); err != nil {
return err
}
return tx . Model ( & model . AgentAccount {}). Where ( "remember_api_key IS NULL" ). Update ( "remember_api_key" , true ). Error
},
}
2026-02-28 10:38:35 +08:00
var AddEditionSetting = & gormigrate . Migration {
ID : "20260224-add-edition-setting" ,
Migrate : func ( tx * gorm . DB ) error {
var setting model . Setting
edition := common . LoadParamsWithoutPanic ( "PANEL_EDITION" )
if err := tx . Where ( "key = ?" , "Edition" ). First ( & setting ). Error ; err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
return tx . Create ( & model . Setting { Key : "Edition" , Value : edition }). Error
}
return err
}
if setting . Value == "" {
return tx . Model ( & model . Setting {}). Where ( "key = ?" , "Edition" ). Update ( "value" , edition ). Error
}
return nil
},
}
2026-03-02 14:53:58 +08:00
var AddAgentTypeForAgents = & gormigrate . Migration {
ID : "20260302-add-agent-type-for-agents" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . Agent {}); err != nil {
return err
}
if err := tx . Model ( & model . Agent {}). Where ( "agent_type = '' OR agent_type IS NULL" ). Update ( "agent_type" , constant . AppOpenclaw ). Error ; err != nil {
return err
}
return tx . Exec (
"UPDATE agents SET agent_type = ? WHERE app_install_id IN (SELECT ai.id FROM app_installs ai JOIN apps a ON ai.app_id = a.id WHERE a.key = ?)" ,
constant . AppCopaw ,
constant . AppCopaw ,
). Error
},
}
2026-03-03 11:00:53 +08:00
var NormalizeAgentAccountVerifiedStatus = & gormigrate . Migration {
ID : "20260303-normalize-agent-account-verified-status" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AgentAccount {}); err != nil {
return err
}
2026-11-16 14:49:32 +08:00
if ! tx . Migrator (). HasColumn ( & model . AgentAccount {}, "verified" ) {
return nil
}
2026-03-03 11:00:53 +08:00
return tx . Model ( & model . AgentAccount {}).
Where ( "provider IN ?" , [] string { "custom" , "ollama" , "kimi-coding" }).
Update ( "verified" , false ). Error
},
}
2026-03-03 11:57:17 +08:00
var NormalizeOllamaAccountAPIType = & gormigrate . Migration {
ID : "20260304-normalize-ollama-account-api-type" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AgentAccount {}); err != nil {
return err
}
return tx . Model ( & model . AgentAccount {}).
Where ( "provider = ?" , "ollama" ).
Update ( "api_type" , "openai-responses" ). Error
},
}
2026-03-18 15:38:01 +08:00
2026-03-18 18:36:11 +08:00
var InitAgentAccountModelPool = & gormigrate . Migration {
ID : "20260319-init-agent-account-model-pool" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AgentAccountModel {}); err != nil {
return err
}
return migrationutils . MigrateAgentAccountModelPool ( tx )
},
}
2026-03-19 15:25:19 +08:00
2026-06-05 14:48:53 +08:00
var AddAgentAccountMasterID = & gormigrate . Migration {
ID : "20260401-add-agent-account-master-id" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . AgentAccount {})
},
}
2026-07-17 13:47:49 +08:00
var NormalizeAgentAccountModelIDs = & gormigrate . Migration {
ID : "20260716-normalize-agent-account-model-ids" ,
Migrate : func ( tx * gorm . DB ) error {
return migrationutils . NormalizeAgentAccountModelIDs ( tx )
},
}
var AddAgentAccountVerifyModel = & gormigrate . Migration {
ID : "20260716-add-agent-account-verify-model" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AgentAccount {}); err != nil {
return err
}
var accounts [] model . AgentAccount
if err := tx . Where ( "verify_model = '' OR verify_model IS NULL" ). Find ( & accounts ). Error ; err != nil {
return err
}
for _ , account := range accounts {
var accountModel model . AgentAccountModel
err := tx . Where ( "account_id = ?" , account . ID ). Order ( "sort_order ASC, id ASC" ). First ( & accountModel ). Error
if errors . Is ( err , gorm . ErrRecordNotFound ) {
continue
}
if err != nil {
return err
}
if err := tx . Model ( & model . AgentAccount {}). Where ( "id = ?" , account . ID ). Update ( "verify_model" , accountModel . Model ). Error ; err != nil {
return err
}
}
return nil
},
}
var AddAgentAccountAuthMode = & gormigrate . Migration {
ID : "20260716-add-agent-account-auth-mode" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AgentAccount {}); err != nil {
return err
}
if err := tx . Model ( & model . AgentAccount {}).
Where ( "api_type = ? AND (auth_mode IS NULL OR auth_mode = '') AND provider IN ?" , "anthropic-messages" , [] string { "bailian-coding-plan" , "ark-coding-plan" , "xiaomi" }).
Update ( "auth_mode" , providercatalog . AuthModeBearer ). Error ; err != nil {
return err
}
return tx . Model ( & model . AgentAccount {}).
Where ( "api_type = ? AND (auth_mode IS NULL OR auth_mode = '')" , "anthropic-messages" ).
Update ( "auth_mode" , providercatalog . AuthModeXAPIKey ). Error
},
}
2026-03-19 15:25:19 +08:00
var AddHostTable = & gormigrate . Migration {
ID : "20260318-add-host-table" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . Host {}); err != nil {
return err
}
if global . CoreDB == nil || ! global . CoreDB . Migrator (). HasTable ( "hosts" ) {
if err := tx . Create ( & model . Group { Name : "Default" , Type : "host" , IsDefault : true }). Error ; err != nil {
return err
}
return nil
}
var encryptSetting model . Setting
if err := global . CoreDB . Where ( "key = ?" , "EncryptKey" ). First ( & encryptSetting ). Error ; err != nil {
global . LOG . Errorf ( "failed to get encrypt key from core db, err: %v" , err )
return nil
}
coreEncryptKey := strings . TrimSpace ( encryptSetting . Value )
if coreEncryptKey == "" {
global . LOG . Error ( "encrypt key from core db is empty" )
return nil
}
groupIDMap := make ( map [ uint ] uint )
defaultGroupID := uint ( 0 )
var coreGroups [] model . Group
if err := global . CoreDB . Where ( "type = ?" , "host" ). Order ( "id asc" ). Find ( & coreGroups ). Error ; err != nil {
return err
}
for _ , coreGroup := range coreGroups {
agentGroup := model . Group {
Name : coreGroup . Name ,
Type : "host" ,
IsDefault : coreGroup . IsDefault ,
}
if agentGroup . IsDefault {
defaultGroupID = coreGroup . ID
}
if err := tx . Create ( & agentGroup ). Error ; err != nil {
global . LOG . Errorf ( "failed to create group, group id: %v, err: %v" , coreGroup . ID , err )
continue
}
groupIDMap [ coreGroup . ID ] = agentGroup . ID
}
var coreHosts [] model . Host
if err := global . CoreDB . Order ( "id asc" ). Find ( & coreHosts ). Error ; err != nil {
return err
}
for _ , coreHost := range coreHosts {
password , err := encrypt . StringDecryptWithKey ( coreHost . Password , coreEncryptKey )
if err != nil {
global . LOG . Errorf ( "failed to decrypt host password, host id: %v, err: %v" , coreHost . ID , err )
continue
}
privateKey , err := encrypt . StringDecryptWithKey ( coreHost . PrivateKey , coreEncryptKey )
if err != nil {
global . LOG . Errorf ( "failed to decrypt host private key, host id: %v, err: %v" , coreHost . ID , err )
continue
}
passPhrase , err := encrypt . StringDecryptWithKey ( coreHost . PassPhrase , coreEncryptKey )
if err != nil {
global . LOG . Errorf ( "failed to decrypt host pass phrase, host id: %v, err: %v" , coreHost . ID , err )
continue
}
encryptedPassword , err := encrypt . StringEncrypt ( password )
if err != nil {
global . LOG . Errorf ( "failed to encrypt host password, host id: %v, err: %v" , coreHost . ID , err )
continue
}
encryptedPrivateKey , err := encrypt . StringEncrypt ( privateKey )
if err != nil {
global . LOG . Errorf ( "failed to encrypt host private key, host id: %v, err: %v" , coreHost . ID , err )
continue
}
encryptedPassPhrase , err := encrypt . StringEncrypt ( passPhrase )
if err != nil {
global . LOG . Errorf ( "failed to encrypt host pass phrase, host id: %v, err: %v" , coreHost . ID , err )
continue
}
groupID := defaultGroupID
if mappedGroupID , ok := groupIDMap [ coreHost . GroupID ]; ok && mappedGroupID != 0 {
groupID = mappedGroupID
}
host := model . Host {
GroupID : groupID ,
Name : coreHost . Name ,
Addr : coreHost . Addr ,
Port : coreHost . Port ,
User : coreHost . User ,
AuthMode : coreHost . AuthMode ,
Password : encryptedPassword ,
PrivateKey : encryptedPrivateKey ,
PassPhrase : encryptedPassPhrase ,
RememberPassword : coreHost . RememberPassword ,
Description : coreHost . Description ,
}
if err := tx . Create ( & host ). Error ; err != nil {
global . LOG . Errorf ( "failed to create host, host id: %v, err: %v" , coreHost . ID , err )
continue
}
}
return nil
},
}
var AddAITerminalSettings = & gormigrate . Migration {
ID : "20260318-add-ai-terminal-settings" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . Create ( & model . Setting { Key : "AIStatus" , Value : constant . StatusDisable }). Error ; err != nil {
return err
}
if err := tx . Create ( & model . Setting { Key : "AIAccountID" , Value : "" }). Error ; err != nil {
return err
}
2026-03-23 15:48:04 +08:00
if err := tx . Create ( & model . Setting { Key : "AIPrefix" , Value : constant . DefaultTerminalAIPrefix }). Error ; err != nil {
2026-03-19 15:25:19 +08:00
return err
}
return tx . Create ( & model . Setting {
Key : "AIRiskCommands" ,
2026-03-23 15:48:04 +08:00
Value : constant . DefaultTerminalAIRiskCommands ,
2026-03-19 15:25:19 +08:00
}). Error
},
}
2026-03-24 18:56:30 +08:00
var UpdateAgentQuickJumpTitle = & gormigrate . Migration {
ID : "20260324-update-agent-quick-jump-title" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . Model ( & model . QuickJump {}).
Where ( "title = ?" , "aiTools.agents.agents" ).
Update ( "title" , "aiTools.agents.agent" ). Error
},
}
2026-03-25 14:02:17 +08:00
var FixOpenclaw20260323HTTPPort = & gormigrate . Migration {
ID : "20260325-fix-openclaw-20260323-http-port" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . Exec (
`UPDATE app_installs
SET http_port = https_port,
https_port = 0
WHERE version = ?
AND https_port > 0
AND app_id IN (SELECT id FROM apps WHERE key = ?)` ,
2026-03-26 13:44:27 +08:00
"2026.3.24" ,
2026-03-25 14:02:17 +08:00
constant . AppOpenclaw ,
). Error
},
}
2026-03-30 14:53:05 +08:00
var AddAgentRemarkColumn = & gormigrate . Migration {
ID : "20260330-add-agent-remark-column" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . Agent {})
},
}
2026-04-03 17:15:54 +08:00
var AddAgentWebsiteBinding = & gormigrate . Migration {
ID : "20260403-add-agent-website-binding" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . Agent {}); err != nil {
return err
}
var agents [] model . Agent
if err := tx . Find ( & agents ). Error ; err != nil {
return err
}
if len ( agents ) == 0 {
return nil
}
var websites [] model . Website
if err := tx . Where ( "type = ? AND app_install_id > 0" , constant . Deployment ). Find ( & websites ). Error ; err != nil {
return err
}
2026-07-17 13:47:49 +08:00
websiteMap := service . UniqueDeploymentWebsiteMapByAppInstall ( websites )
2026-04-03 17:15:54 +08:00
for _ , agent := range agents {
if agent . WebsiteID != 0 || agent . AppInstallID == 0 {
continue
}
website , ok := websiteMap [ agent . AppInstallID ]
if ! ok {
continue
}
if err := tx . Model ( & model . Agent {}). Where ( "id = ?" , agent . ID ). Update ( "website_id" , website . ID ). Error ; err != nil {
return err
}
}
return nil
},
}
2026-04-03 22:11:01 +08:00
var AddFileManageAISettings = & gormigrate . Migration {
ID : "20260330-add-file-manage-ai-settings" ,
Migrate : func ( tx * gorm . DB ) error {
rows := [] model . Setting {
{ Key : "FileAIStatus" , Value : constant . StatusDisable },
{ Key : "FileAIAccountID" , Value : "" },
}
for i := range rows {
var exist model . Setting
if err := tx . Where ( "`key` = ?" , rows [ i ]. Key ). First ( & exist ). Error ; err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
if err := tx . Create ( & rows [ i ]). Error ; err != nil {
return err
}
} else {
return err
}
}
}
return nil
},
}
2026-04-09 17:38:38 +08:00
var AddFileShareTable = & gormigrate . Migration {
2026-04-10 13:36:10 +08:00
ID : "20260410-add-file-share-table" ,
2026-04-09 17:38:38 +08:00
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . FileShare {})
},
}
2026-04-16 19:11:05 +08:00
var AddFileHistoryTable = & gormigrate . Migration {
ID : "20260414-add-file-history-table" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . FileHistory {}); err != nil {
return err
}
defaultRows := [] model . Setting {
{ Key : "FileHistoryStatus" , Value : constant . StatusEnable },
{ Key : "FileHistoryMaxPerPath" , Value : "20" },
{ Key : "FileHistoryDiskQuotaMB" , Value : "1024" },
}
for i := range defaultRows {
var exist model . Setting
if err := tx . Where ( "`key` = ?" , defaultRows [ i ]. Key ). First ( & exist ). Error ; err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
if err := tx . Create ( & defaultRows [ i ]). Error ; err != nil {
return err
}
} else {
return err
}
}
}
return nil
},
}
2026-05-26 14:18:17 +08:00
2026-07-16 14:24:38 +08:00
func loadDatabaseUserTypesForMigration ( tx * gorm . DB , mysqlName string ) [] string {
var database model . Database
if err := tx . Where ( "name = ?" , mysqlName ). First ( & database ). Error ; err == nil && len ( database . Type ) != 0 {
return [] string { database . Type }
}
var appKey string
_ = tx . Table ( "app_installs" ).
Select ( "apps.`key`" ).
Joins ( "JOIN apps ON apps.id = app_installs.app_id" ).
Where ( "app_installs.name = ?" , mysqlName ).
Scan ( & appKey ). Error
switch appKey {
case constant . AppMariaDB , constant . AppMysqlCluster :
return [] string { appKey }
case constant . AppMysql :
return [] string { constant . AppMysql }
default :
return [] string { constant . AppMysql , constant . AppMariaDB }
}
}
func normalizeDatabaseUserHostsForMigration ( permission string ) [] string {
hostSet := make ( map [ string ] struct {})
for _ , host := range strings . Split ( permission , "," ) {
host = strings . TrimSpace ( host )
if len ( host ) == 0 {
continue
}
hostSet [ host ] = struct {}{}
}
if len ( hostSet ) == 0 {
return [] string { "%" }
}
hosts := make ([] string , 0 , len ( hostSet ))
for host := range hostSet {
hosts = append ( hosts , host )
}
return hosts
}
func isDatabaseSystemUserForMigration ( username string ) bool {
switch strings . ToLower ( username ) {
case "root" ,
"mysql.session" , "mysql.sys" , "mysql.infoschema" , "mysqlxsys" ,
"mariadb.sys" , "mariadb-sys" ,
"debian-sys-maint" :
return true
default :
return false
}
}
var AddDatabaseUserTable = & gormigrate . Migration {
ID : "20260703-add-database-user-table" ,
Migrate : func ( tx * gorm . DB ) error {
2026-07-21 19:08:09 +08:00
if err := tx . AutoMigrate ( & model . DatabaseUser {}, & model . DatabaseUserGrant {}); err != nil {
2026-07-16 14:24:38 +08:00
return err
}
2026-07-21 19:08:09 +08:00
var mysqls [] model . DatabaseMysql
if err := tx . Find ( & mysqls ). Error ; err != nil {
return err
}
for _ , item := range mysqls {
if len ( item . Username ) == 0 || isDatabaseSystemUserForMigration ( item . Username ) {
continue
}
for _ , dbType := range loadDatabaseUserTypesForMigration ( tx , item . MysqlName ) {
if len ( dbType ) == 0 {
continue
}
for _ , host := range normalizeDatabaseUserHostsForMigration ( item . Permission ) {
user := model . DatabaseUser {
Type : dbType ,
Database : item . MysqlName ,
Username : item . Username ,
Host : host ,
Password : item . Password ,
}
if err := tx . Where ( "`type` = ? AND database = ? AND username = ? AND host = ?" , dbType , item . MysqlName , item . Username , host ). FirstOrCreate ( & user ). Error ; err != nil {
return err
}
grant := model . DatabaseUserGrant {
Type : dbType ,
Database : item . MysqlName ,
DBName : item . Name ,
Username : item . Username ,
Host : host ,
}
if err := tx . Where ( "`type` = ? AND database = ? AND db_name = ? AND username = ? AND host = ?" , dbType , item . MysqlName , item . Name , item . Username , host ). FirstOrCreate ( & grant ). Error ; err != nil {
return err
}
}
}
}
return nil
2026-07-16 14:24:38 +08:00
},
}
2026-05-26 14:18:17 +08:00
// MigrateLegoV5 normalizes data persisted under lego v4 so that lego v5 can read it.
//
// Two things changed in lego v5 that affect existing rows:
//
// 1. certcrypto.KeyType string values were renamed:
// "P256"->"EC256", "P384"->"EC384",
// "2048"->"RSA2048", "3072"->"RSA3072", "4096"->"RSA4096", "8192"->"RSA8192".
// We update every key_type column we own to the new form.
//
// 2. DnsPod provider was removed from upstream lego v5; the frontend already
// marked it deprecated. Existing DnsPod website_dns_accounts rows are kept
// so the user can decide what to do, but any website_ssls still pointing
// at a DnsPod account would fail to renew. We log a warning row count and
// leave deletion to the operator.
var MigrateLegoV5 = & gormigrate . Migration {
ID : "20260523-migrate-lego-v5" ,
Migrate : func ( tx * gorm . DB ) error {
keyTypeMap := map [ string ] string {
"P256" : "EC256" ,
"P384" : "EC384" ,
"2048" : "RSA2048" ,
"3072" : "RSA3072" ,
"4096" : "RSA4096" ,
"8192" : "RSA8192" ,
}
for old , neu := range keyTypeMap {
if err := tx . Model ( & model . WebsiteAcmeAccount {}).
Where ( "key_type = ?" , old ).
Update ( "key_type" , neu ). Error ; err != nil {
return fmt . Errorf ( "migrate WebsiteAcmeAccount.key_type %s->%s: %w" , old , neu , err )
}
if err := tx . Model ( & model . WebsiteSSL {}).
Where ( "key_type = ?" , old ).
Update ( "key_type" , neu ). Error ; err != nil {
return fmt . Errorf ( "migrate WebsiteSSL.key_type %s->%s: %w" , old , neu , err )
}
if err := tx . Model ( & model . WebsiteCA {}).
Where ( "key_type = ?" , old ).
Update ( "key_type" , neu ). Error ; err != nil {
return fmt . Errorf ( "migrate WebsiteCA.key_type %s->%s: %w" , old , neu , err )
}
}
var dnsPodCount int64
_ = tx . Model ( & model . WebsiteDnsAccount {}). Where ( "type = ?" , "DnsPod" ). Count ( & dnsPodCount ). Error
if dnsPodCount > 0 {
global . LOG . Warnf ( "lego v5 removed the DnsPod provider; %d existing DnsPod DNS account(s) will not be usable for renewal -- please switch them to TencentCloud" , dnsPodCount )
}
return nil
},
}
2026-07-15 15:53:44 +08:00
var AddMcpServerGatewayArgs = & gormigrate . Migration {
ID : "20260714-add-mcp-server-gateway-args" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . McpServer {})
},
}
2026-07-29 10:42:21 +08:00
var AddBackupRecordArgs = & gormigrate . Migration {
ID : "20260729-add-backup-record-args" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . BackupRecord {})
},
}
2026-07-29 16:41:43 +08:00
var AddFtpIdentity = & gormigrate . Migration {
ID : "20260729-add-ftp-identity" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . Ftp {}); err != nil {
return err
}
return tx . Model ( & model . Ftp {}).
Where ( "1 = 1" ).
Updates ( map [ string ] interface {}{
"uid" : constant . WebsiteUID ,
"gid" : constant . WebsiteGID ,
}). Error
},
}
2026-07-30 09:39:47 +08:00
2026-07-31 10:01:20 +08:00
var AddWebsiteTemplateTable = & gormigrate . Migration {
ID : "20260728-add-website-template-table" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate (
& model . WebsiteTemplate {},
& model . WebsiteTemplateOutput {},
)
2026-07-31 11:05:23 +08:00
},
}
2026-07-30 09:39:47 +08:00
var AddComposePinned = & gormigrate . Migration {
ID : "20260729-add-compose-pinned" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . Compose {})
},
}
2026-08-24 12:51:34 +08:00
var AddFirewallRuleTable = & gormigrate . Migration {
ID : "20260819-add-firewall-v2-tables" ,
Migrate : func ( tx * gorm . DB ) error {
2026-10-08 18:19:30 +08:00
return tx . AutoMigrate ( & model . CommonDescription {})
2026-08-24 12:51:34 +08:00
},
}
var InitDockerPortGuardStatus = & gormigrate . Migration {
ID : "20260818-init-docker-port-guard-status" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . Where ( "key = ?" , "DockerPortGuardStatus" ). FirstOrCreate ( & model . Setting {
Key : "DockerPortGuardStatus" ,
Value : constant . StatusDisable ,
}). Error
},
}
2026-08-27 10:31:39 +08:00
var NormalizeFirewallBackendSelections = & gormigrate . Migration {
ID : "20260826-normalize-firewall-backend-selections" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . Transaction ( func ( tx * gorm . DB ) error {
2026-09-01 14:53:01 +08:00
result := tx . Model ( & model . Setting {}).
Where (
"key = ? AND value NOT IN ?" ,
constant . FirewallDockerBackendKey ,
[] string { constant . FirewallProviderIptables , constant . FirewallProviderNftables },
).
Update ( "value" , constant . FirewallProviderIptables )
if result . Error != nil {
return result . Error
}
2026-08-27 10:31:39 +08:00
if err := tx . Where ( "key = ?" , constant . FirewallDockerBackendKey ). FirstOrCreate ( & model . Setting {
2026-09-01 14:53:01 +08:00
Key : constant . FirewallDockerBackendKey , Value : constant . FirewallProviderIptables ,
2026-08-27 10:31:39 +08:00
}). Error ; err != nil {
return err
}
2026-09-01 14:53:01 +08:00
result = tx . Model ( & model . Setting {}).
2026-08-27 10:31:39 +08:00
Where (
"key = ? AND value NOT IN ?" ,
constant . FirewallForwardingBackendKey ,
[] string { constant . FirewallProviderIptables , constant . FirewallProviderNftables },
).
Update ( "value" , constant . FirewallProviderIptables )
if result . Error != nil {
return result . Error
}
if err := tx . Where ( "key = ?" , constant . FirewallForwardingBackendKey ). FirstOrCreate ( & model . Setting {
Key : constant . FirewallForwardingBackendKey , Value : constant . FirewallProviderIptables ,
}). Error ; err != nil {
return err
}
return nil
})
},
}
2026-10-08 18:19:30 +08:00
var MigrateFirewallDescriptions = & gormigrate . Migration {
ID : "20260921-migrate-firewall-descriptions" ,
2026-08-27 10:31:39 +08:00
Migrate : func ( tx * gorm . DB ) error {
2026-10-08 18:19:30 +08:00
return tx . Transaction ( migrationutils . MigrateHostFirewallDescriptions )
2026-09-02 14:48:59 +08:00
},
}
2026-09-28 18:32:12 +08:00
var AddAcceleratorMetrics = & gormigrate . Migration {
ID : "20260928-accelerator-vendor-metrics" ,
Migrate : func ( tx * gorm . DB ) error {
return global . GPUMonitorDB . AutoMigrate ( & model . MonitorGPU {})
},
}
2026-09-29 15:19:30 +08:00
var AddMonitorSettings = & gormigrate . Migration {
ID : "20260929-monitor-settings" ,
Migrate : func ( tx * gorm . DB ) error {
for _ , key := range [] string { "MonitorStatus" , "MonitorInterval" , "MonitorStoreDays" } {
var setting model . Setting
if err := tx . Where ( "key = ?" , key ). First ( & setting ). Error ; err != nil {
return err
}
2026-11-09 14:17:47 +08:00
if err := tx . Where ( "key = ?" , "GPU" + key ). FirstOrCreate ( & model . Setting { Key : "GPU" + key , Value : setting . Value }). Error ; err != nil {
return err
2026-09-29 15:19:30 +08:00
}
}
return nil
},
}
2026-10-12 15:54:42 +08:00
2026-10-21 16:21:07 +08:00
var AddQuickDirectory = & gormigrate . Migration {
ID : "20261016-add-quick-directory" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . QuickDirectory {})
},
}
2026-10-23 18:39:22 +08:00
var AddLocalModelAccountLink = & gormigrate . Migration {
ID : "20261023-add-local-model-account-link" ,
Migrate : func ( tx * gorm . DB ) error {
2026-10-26 14:03:40 +08:00
return tx . AutoMigrate ( & model . AgentAccount {})
},
}
var AddOllamaModelAccountSync = & gormigrate . Migration {
ID : "20261026-add-ollama-model-account-sync" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AppInstall {}); err != nil {
2026-10-23 18:39:22 +08:00
return err
}
2026-10-26 14:03:40 +08:00
var accounts [] model . AgentAccount
if err := tx . Where ( "provider = ? AND app_install_id > 0" , "ollama" ). Find ( & accounts ). Error ; err != nil {
return err
}
for _ , account := range accounts {
var install model . AppInstall
if err := tx . Preload ( "App" ). First ( & install , account . AppInstallID ). Error ; err != nil {
if errors . Is ( err , gorm . ErrRecordNotFound ) {
continue
}
return err
}
if install . App . Key != "ollama" {
continue
}
addressType := "custom"
if strings . TrimRight ( account . BaseURL , "/" ) == fmt . Sprintf ( "http://%s:11434/v1" , install . ContainerName ) {
addressType = "container"
}
if err := tx . Model ( & model . AppInstall {}). Where ( "id = ?" , account . AppInstallID ). Updates ( map [ string ] interface {}{
"sync_model_account" : true ,
"model_account_base_url_type" : addressType ,
"model_account_base_url" : account . BaseURL ,
}). Error ; err != nil {
return err
}
}
return nil
2026-10-23 18:39:22 +08:00
},
}
2026-10-30 22:46:20 +08:00
var AddModelQuickJump = & gormigrate . Migration {
ID : "20261030-add-model-quick-jump" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . Where ( "name = ?" , "ModelServices" ). FirstOrCreate ( & model . QuickJump {
Name : "ModelServices" ,
Title : "aiTools.model.model" ,
Recommend : 0 ,
IsShow : true ,
Router : "/ai/model" ,
}). Error
},
}
2026-11-05 17:08:15 +08:00
var UpdateModelServicesQuickJumpRouter = & gormigrate . Migration {
ID : "20261105-update-model-services-quick-jump-router" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . Model ( & model . QuickJump {}).
Where ( "name = ? AND router IN ?" , "ModelServices" , [] string { "/ai/model" , "/ai/model/account" }).
Update ( "router" , "/ai/model-services" ). Error
},
}
2026-11-06 00:44:50 +08:00
var AddAgentAccountVerificationResult = & gormigrate . Migration {
ID : "20261106-add-agent-account-verification-settings" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AgentAccount {}); err != nil {
return err
}
return tx . Model ( & model . AgentAccount {}). Where ( "validate_availability IS NULL" ). Update ( "validate_availability" , true ). Error
},
}
2026-11-09 09:54:02 +08:00
var AddGPUGroup = & gormigrate . Migration {
ID : "20261108-add-gpu-groups" ,
Migrate : func ( tx * gorm . DB ) error {
return tx . AutoMigrate ( & model . GPUGroup {})
},
}
2026-11-09 23:54:25 +08:00
var AddCronjobURLItems = & gormigrate . Migration {
ID : "20261109-cronjob-url-items" ,
Migrate : func ( tx * gorm . DB ) error {
if tx . Migrator (). HasColumn ( & model . Cronjob {}, "URLItemsJSON" ) {
return nil
}
return tx . Migrator (). AddColumn ( & model . Cronjob {}, "URLItemsJSON" )
},
}
2026-11-16 14:49:32 +08:00
var AddAgentAccountStatus = & gormigrate . Migration {
ID : "20261116-add-agent-account-status" ,
Migrate : func ( tx * gorm . DB ) error {
if err := tx . AutoMigrate ( & model . AgentAccount {}); err != nil {
return err
}
if err := tx . Model ( & model . AgentAccount {}). Where ( "status IS NULL OR status = ?" , "" ). Update ( "status" , constant . StatusPending ). Error ; err != nil {
return err
}
if tx . Migrator (). HasColumn ( & model . AgentAccount {}, "verified" ) {
if err := tx . Model ( & model . AgentAccount {}). Where ( "verified = ?" , true ). Update ( "status" , constant . StatusSuccess ). Error ; err != nil {
return err
}
}
if tx . Migrator (). HasColumn ( & model . AgentAccount {}, "verify_error" ) {
if err := tx . Model ( & model . AgentAccount {}). Where ( "verify_error <> ?" , "" ). Update ( "status" , constant . StatusFailed ). Error ; err != nil {
return err
}
}
if tx . Migrator (). HasColumn ( & model . AgentAccount {}, "verifying" ) {
return tx . Model ( & model . AgentAccount {}). Where ( "verifying = ?" , true ). Update ( "status" , constant . StatusWaiting ). Error
}
return nil
},
}