Commit Graph

57 Commits

Author SHA1 Message Date
t0ng7u
067f3ce9ed 🔧 chore(db): drop legacy single-column UNIQUE indexes to prevent duplicate-key errors after soft-delete
Why
Previous versions created single-column UNIQUE constraints (`models.model_name`, `vendors.name`).
After introducing composite indexes on `(model_name, deleted_at)` and `(name, deleted_at)` for soft-delete support, those legacy constraints could still exist in user databases.
When a record was soft-deleted and re-inserted with the same name, MySQL raised `Error 1062 … for key 'models.model_name'`.

What
• In `migrateDB` and `migrateDBFast` paths of `model/main.go`, proactively drop:
  – `models.uk_model_name` and fallback `models.model_name`
  – `vendors.uk_vendor_name` and fallback `vendors.name`
• Keeps existing helper `dropIndexIfExists` to ensure the operation is MySQL-only and error-free when indexes are already absent.

Result
Startup migration now removes every possible legacy UNIQUE index, ensuring composite index strategy works correctly.
Users can soft-delete and recreate models/vendors with identical names without hitting duplicate-entry errors.
2025-08-11 01:25:13 +08:00
t0ng7u
1dd1ef862f 🐛 fix(db): allow re-adding models & vendors after soft delete; add safe index cleanup
Replace legacy single-column unique indexes with composite unique indexes on
(name, deleted_at) and introduce a safe index drop utility to eliminate
duplicate-key errors and noisy MySQL 1091 warnings.

WHAT
• model/model_meta.go
  - Model.ModelName  → `uniqueIndex:uk_model_name,priority:1`
  - Model.DeletedAt  → `index; uniqueIndex:uk_model_name,priority:2`
• model/vendor_meta.go
  - Vendor.Name      → `uniqueIndex:uk_vendor_name,priority:1`
  - Vendor.DeletedAt → `index; uniqueIndex:uk_vendor_name,priority:2`
• model/main.go
  - Add `dropIndexIfExists(table, index)`:
    • Checks `information_schema.statistics`
    • Drops index only when present (avoids Error 1091)
  - Invoke helper in `migrateDB` & `migrateDBFast`
  - Remove direct `ALTER TABLE … DROP INDEX …` calls

WHY
• Users received `Error 1062 (23000)` when re-creating a soft-deleted
  model/vendor because the old unique index enforced uniqueness on name alone.
• Directly dropping nonexistent indexes caused MySQL `Error 1091` noise.

HOW
• Composite unique indexes `(model_name, deleted_at)` / `(name, deleted_at)`
  respect GORM soft deletes.
• Safe helper ensures idempotent migrations across environments.

RESULT
• Users can now delete and re-add the same model or vendor without manual SQL.
• Startup migration runs quietly across MySQL, PostgreSQL, and SQLite.
• No behavior changes for existing data beyond index updates.

TEST
1. Add model “deepseek-chat” → delete (soft) → re-add → success.
2. Add vendor “DeepSeek”     → delete (soft) → re-add → success.
3. Restart service twice → no duplicate key or 1091 errors.
2025-08-09 15:44:08 +08:00
t0ng7u
e1796b92cd 🐛 fix(db): allow re-adding models/vendors after soft delete via composite unique indexes
Ensure models and vendors can be re-created after soft deletion by switching to composite unique indexes on (name, deleted_at) and cleaning up legacy single-column unique indexes on MySQL.

Why
- MySQL raised 1062 duplicate key errors when re-adding a soft-deleted model/vendor because the legacy unique index enforced uniqueness on the name column alone (uk_model_name / uk_vendor_name), despite soft deletes.
- Users encountered errors such as:
  - Error 1062 (23000): Duplicate entry 'deepseek-chat' for key 'models.uk_model_name'
  - Error 1062 (23000): Duplicate entry 'DeepSeek' for key 'vendors.uk_vendor_name'

How
- Model indices:
  - model/model_meta.go:
    - Model.ModelName → gorm: uniqueIndex:uk_model_name,priority:1
    - Model.DeletedAt → gorm: index; uniqueIndex:uk_model_name,priority:2
- Vendor indices:
  - model/vendor_meta.go:
    - Vendor.Name → gorm: uniqueIndex:uk_vendor_name,priority:1
    - Vendor.DeletedAt → gorm: index; uniqueIndex:uk_vendor_name,priority:2
- Migration (automatic, idempotent):
  - model/main.go (migrateDB, migrateDBFast):
    - On MySQL, drop legacy single-column unique indexes if present:
      - ALTER TABLE models DROP INDEX uk_model_name;
      - ALTER TABLE vendors DROP INDEX uk_vendor_name;
    - Then run AutoMigrate to create composite unique indexes.
  - Missing-index errors are ignored to keep the migration safe to run multiple times.

Result
- Users can delete and re-add the same model/vendor name without manual SQL.
- Migration runs automatically at startup; no user action required.
- PostgreSQL and SQLite remain unaffected.

Files changed
- model/model_meta.go
- model/vendor_meta.go
- model/main.go (migrateDB, migrateDBFast)

Testing
- Create model "deepseek-chat" → delete (soft) → re-create → succeeds.
- Create vendor "DeepSeek" → delete (soft) → re-create → succeeds.

Backward compatibility
- Data remains intact; only index definitions are updated.
- Behavior is unchanged except for fixing the uniqueness constraint with soft deletes.
2025-08-09 13:07:57 +08:00
t0ng7u
e64a7520cb Merge remote-tracking branch 'origin/alpha' into refactor/model-pricing 2025-08-04 21:37:38 +08:00
t0ng7u
663e25b311 feat: Add prefill group management system for models
- Add new PrefillGroup model with CRUD operations
  * Support for model, tag, and endpoint group types
  * JSON storage for group items with GORM datatypes
  * Automatic database migration support

- Implement backend API endpoints
  * GET /api/prefill_group - List groups by type with admin auth
  * POST /api/prefill_group - Create new groups
  * PUT /api/prefill_group - Update existing groups
  * DELETE /api/prefill_group/:id - Delete groups

- Add comprehensive frontend management interface
  * PrefillGroupManagement component for group listing
  * EditPrefillGroupModal for group creation/editing
  * Integration with EditModelModal for auto-filling
  * Responsive design with CardTable and SideSheet

- Enhance model editing workflow
  * Tag group selection with auto-fill functionality
  * Endpoint group selection with auto-fill functionality
  * Seamless integration with existing model forms

- Create reusable UI components
  * Extract common rendering utilities to models/ui/
  * Shared renderLimitedItems and renderDescription functions
  * Consistent styling across all model-related components

- Improve user experience
  * Empty state illustrations matching existing patterns
  * Fixed column positioning for operation buttons
  * Item content display with +x indicators for overflow
  * Tooltip support for long descriptions
2025-08-04 02:54:37 +08:00
Seefs
b48d3a6b40 feat: implement two-factor authentication (2FA) support with user login and settings integration 2025-08-02 14:53:28 +08:00
t0ng7u
1baad070d7 🚀 feat: Introduce full Model & Vendor Management suite (backend + frontend) and UI refinements
Backend
• Add `model/model_meta.go` and `model/vendor_meta.go` defining Model & Vendor entities with CRUD helpers, soft-delete and time stamps
• Create corresponding controllers `controller/model_meta.go`, `controller/vendor_meta.go` and register routes in `router/api-router.go`
• Auto-migrate new tables in DB startup logic

Frontend
• Build complete “Model Management” module under `/console/models`
  - New pages, tables, filters, actions, hooks (`useModelsData`) and dynamic vendor tabs
  - Modals `EditModelModal.jsx` & unified `EditVendorModal.jsx`; latter now uses default confirm/cancel footer and mobile-friendly modal sizing (`full-width` / `small`) via `useIsMobile`
• Update sidebar (`SiderBar.js`) and routing (`App.js`) to surface the feature
• Add helper updates (`render.js`) incl. `stringToColor`, dynamic LobeHub icon retrieval, and tag color palettes

Table UX improvements
• Replace separate status column with inline Enable / Disable buttons in operation column (matching channel table style)
• Limit visible tags to max 3; overflow represented as “+x” tag with padded `Popover` showing remaining tags
• Color all tags deterministically using `stringToColor` for consistent theming
• Change vendor column tag color to white for better contrast

Misc
• Minor layout tweaks, compact-mode toggle relocation, lint fixes and TypeScript/ESLint clean-up

These changes collectively deliver end-to-end model & vendor administration while unifying visual language across management tables.
2025-07-31 22:28:09 +08:00
Xyfacai
60d73d2a27 fix(db): 修复 db migration 报错过多可能卡住的隐患 2025-07-20 10:11:35 +08:00
CaIon
7ddee5ea2f Merge branch 'alpha' into mutil_key_channel
# Conflicts:
#	controller/channel.go
#	docker-compose.yml
#	web/src/components/table/ChannelsTable.js
#	web/src/pages/Channel/EditChannel.js
2025-07-06 10:33:48 +08:00
creamlike1024
4a725f8bb0 fix: 使用日志分组查询 2025-06-19 17:17:32 +08:00
CaIon
7d126706fc temp 2025-06-16 21:01:57 +08:00
CaIon
558e67d991 feat(channel): enhance AddChannel functionality with structured request handling 2025-06-16 00:37:22 +08:00
CaIon
c6226f3678 feat(database): implement database migration logic for PostgreSQL and add fast migration fallback 2025-06-14 19:47:44 +08:00
CaIon
a9ec62eea8 🐛 fix(ability, channel): standardize boolean value handling across database queries 2025-06-14 18:15:45 +08:00
CaIon
4371717c78 fix: Resolving conflicts caused by mixing multiple databases 2025-06-14 17:51:05 +08:00
CaIon
ac06e9a46f feat: Add CheckSetup function call in main to ensure proper initialization #942 2025-04-08 18:14:36 +08:00
CaIon
eca18ce25c fix: Improve setup check logic and logging for system initialization 2025-04-04 21:27:24 +08:00
CaIon
f33ebc8e2c feat: Implement system setup functionality 2025-04-03 18:57:15 +08:00
Seefs
884e25325f feat: Support postgresql:// dsn format 2025-03-12 21:08:47 +08:00
1808837298@qq.com
0be6c5ee69 chore: replace sqlite lib with prue go lib 2025-02-11 18:34:34 +08:00
1808837298@qq.com
c8a8526ff3 feat: modify channel model_mapping column type to TEXT
- Change `ModelMapping` column type from varchar(1024) to TEXT in channels table
- Add MySQL migration script to alter column type during database initialization
- Improve database schema flexibility for storing complex model mappings
2025-02-06 14:35:14 +08:00
CalciumIon
e0b7300239 fix: try to fix pgsql #682 2024-12-31 02:06:30 +08:00
CalciumIon
aefd53b683 refactor: token cache logic 2024-12-30 17:10:48 +08:00
1808837298@qq.com
05f35fd024 feat: 不自动生成系统访问令牌 2024-09-25 16:31:25 +08:00
liuzhifei
3a6a30a811 support log db 2024-08-13 10:29:55 +08:00
CalciumIon
eca52104cc chore: 重构 2024-06-27 19:30:17 +08:00
Xiangyuan Liu
05a4646b44 feat: suno api 支持
feat: 调试 suno

feat: 补充suno 文档
2024-06-13 10:35:48 +08:00
CaIon
9df64bcad4 fix: try to fix sqlite database migration (#231) 2024-05-16 16:10:25 +08:00
CaIon
38305d7703 fix: midjourneys table 2024-03-20 21:49:54 +08:00
CaIon
9cd256fc7f chore: drop idx_channels_key on start 2024-03-12 00:35:12 +08:00
1808837298@qq.com
5ddb37c12f feat: add "/api/status/test" 2024-03-04 19:32:59 +08:00
CaIon
dd0abc9447 feat: 新增数据看板 2024-01-07 18:31:14 +08:00
CaIon
7eeacd2c8f enable parseTime=true 2023-12-27 16:47:32 +08:00
CaIon
9c7456b0a6 enable mysql parseTime 2023-12-27 15:33:35 +08:00
Calcium-Ion
f439c4d553 Merge branch 'songquanpeng:main' into main 2023-10-30 16:12:09 +08:00
Bryan
58b6c88f7b fix: fix postgresql support (#606)
* fix postgresql support

fixes #517

* fix: fix pg support

* chore: delete useless code

---------

Co-authored-by: JustSong <songquanpeng@foxmail.com>
2023-10-22 18:38:29 +08:00
CaIon
ba7e6324d3 Merge remote-tracking branch 'origin/main'
# Conflicts:
#	README.md
#	go.mod
#	go.sum
#	model/log.go
#	web/src/App.js
#	web/src/components/ChannelsTable.js
2023-10-16 17:31:26 +08:00
JustSong
5ad4bcea7f chore: add database migration prompt 2023-10-02 12:13:30 +08:00
CaIon
e60239c93d Merge remote-tracking branch 'origin/main'
# Conflicts:
#	controller/relay-text.go
#	go.mod
#	web/src/components/PersonalSetting.js
#	web/src/components/TokensTable.js
#	web/src/pages/Home/index.js
2023-08-25 00:51:02 +08:00
CaIon
68b24da6a6 add epay 2023-08-14 22:16:32 +08:00
igophper
27b290c578 fix: fix baidu's embedding api (#398)
* fix:judge baidu embeddings input type

* chore: add back update all channels balance

* chore: update default configuration for sql connection

---------

Co-authored-by: JustSong <songquanpeng@foxmail.com>
2023-08-13 00:45:04 +08:00
JustSong
c563e1ed91 feat: support PostgreSQL now 2023-08-12 19:20:12 +08:00
JustSong
2586fe09eb perf: flush response after response handled (close #364) 2023-08-12 18:10:15 +08:00
JustSong
8a2f1b64ec fix: set connection limits for database 2023-08-12 10:05:25 +08:00
JustSong
b7a89340e1 chore: format logs 2023-06-22 10:59:01 +08:00
JustSong
b0b0ed2013 chore: print more logs 2023-06-22 01:12:28 +08:00
JustSong
64d026fc1a fix: only master node can migrate database 2023-06-22 00:52:27 +08:00
JustSong
532b60f753 feat: now user can check its topup & consume history (close #78, close #95) 2023-06-10 16:04:04 +08:00
JustSong
cbf339284c feat: support group now (close #17, close #72, close #85, close #104, close #136)
Co-authored-by: quzard <1191890118@qq.com>
2023-06-07 23:26:00 +08:00
JustSong
08a628a7f8 chore: set initial quota for root user 2023-05-21 10:05:34 +08:00