From 3850e2f070bb3c1227b78dcd1e8a34a89a9e9906 Mon Sep 17 00:00:00 2001 From: Ali Golzar <57574919+aliglzr@users.noreply.github.com> Date: Wed, 21 May 2025 13:34:38 +0330 Subject: feat: Add MySQL database support (#3024) * feat: Add MySQL database support - Add MySQL database support with environment-based configuration - Fix MySQL compatibility issue with 'key' column name - Maintain SQLite as default database - Add proper validation for MySQL configuration - Test and verify compatibility with existing database - Replaced raw SQL queries using JSON_EACH functions with standard GORM queries - Modified functions to handle JSON parsing in Go code instead of database since JSON_EACH is not available on MySQL or MariaDB: - getAllEmails() - GetClientTrafficByID() - getFallbackMaster() - MigrationRemoveOrphanedTraffics() The system now supports both MySQL and SQLite databases, with SQLite remaining as the default option. MySQL connection is only used when explicitly configured through environment variables. * refactor: prefix env variables of database with XUI_ to support direct environment usage without .env file All database configuration environment variables now start with the XUI_ prefix to avoid conflicts and allow configuration via system-level environment variables, not just the .env file. --- web/service/setting.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'web/service/setting.go') diff --git a/web/service/setting.go b/web/service/setting.go index 62d66c11..8fe3ec01 100644 --- a/web/service/setting.go +++ b/web/service/setting.go @@ -88,7 +88,7 @@ func (s *SettingService) GetDefaultJsonConfig() (any, error) { func (s *SettingService) GetAllSetting() (*entity.AllSetting, error) { db := database.GetDB() settings := make([]*model.Setting, 0) - err := db.Model(model.Setting{}).Not("key = ?", "xrayTemplateConfig").Find(&settings).Error + err := db.Model(model.Setting{}).Not("`key` = ?", "xrayTemplateConfig").Find(&settings).Error if err != nil { return nil, err } @@ -173,7 +173,7 @@ func (s *SettingService) ResetSettings() error { func (s *SettingService) getSetting(key string) (*model.Setting, error) { db := database.GetDB() setting := &model.Setting{} - err := db.Model(model.Setting{}).Where("key = ?", key).First(setting).Error + err := db.Model(model.Setting{}).Where("`key` = ?", key).First(setting).Error if err != nil { return nil, err } -- cgit v1.2.3