feat(canvas): 模型选择 - 支持彩色展示标签并优化目录与积分交互

This commit is contained in:
ddCat
2026-09-21 11:11:10 +08:00
parent ea6f02b42f
commit 4f9e79f67f
31 changed files with 410 additions and 63 deletions

View File

@@ -0,0 +1,39 @@
package database
import (
"testing"
"infinite-canvas/backend/internal/model"
)
func TestChannelModelTagsMigration(t *testing.T) {
db, err := Open(Config{Driver: "sqlite", DSN: ":memory:"})
if err != nil {
t.Fatal(err)
}
if err := db.Exec(`CREATE TABLE channel_models (id TEXT PRIMARY KEY, model_key TEXT, unit_price_microcredits INTEGER)`).Error; err != nil {
t.Fatal(err)
}
if err := db.Exec(`INSERT INTO channel_models VALUES ('a', 'test', 300000)`).Error; err != nil {
t.Fatal(err)
}
for i := 0; i < 2; i++ {
if err := migrateChannelModelTags(db); err != nil {
t.Fatal(err)
}
}
var row struct {
ModelKey string
Tags string
UnitPriceMicrocredits int64
}
if err := db.Table("channel_models").Where("id = ?", "a").First(&row).Error; err != nil {
t.Fatal(err)
}
if row.Tags != "[]" || row.ModelKey != "test" || row.UnitPriceMicrocredits != 300000 {
t.Fatalf("migration changed data: %#v", row)
}
if !db.Migrator().HasColumn(&model.ChannelModel{}, "Tags") {
t.Fatal("tags column missing")
}
}

View File

@@ -11,7 +11,7 @@ import (
"gorm.io/gorm"
)
const CurrentSchemaVersion int64 = 31
const CurrentSchemaVersion int64 = 32
const baselineSchemaChecksum = "sha256:open-ai-canvas-schema-v1-20260830"
const schemaMigrationAppliedAtIndexChecksum = "sha256:schema-migrations-applied-at-index-v2-20260830"
@@ -105,6 +105,14 @@ var schemaMigrations = []migration{
{version: 31, name: "tool_favorites", checksum: "sha256:tool-favorites-v31", apply: func(tx *gorm.DB) error {
return tx.AutoMigrate(&model.ToolFavorite{})
}},
{version: 32, name: "channel_model_tags", checksum: "sha256:channel-model-tags-v32", apply: migrateChannelModelTags},
}
func migrateChannelModelTags(tx *gorm.DB) error {
if tx.Migrator().HasColumn(&model.ChannelModel{}, "Tags") {
return nil
}
return tx.Migrator().AddColumn(&model.ChannelModel{}, "Tags")
}
func migrateChannelCreditCost(tx *gorm.DB) error {

View File

@@ -11,6 +11,16 @@ import (
"gorm.io/gorm"
)
func TestCurrentSchemaVersionMatchesMigrationPlan(t *testing.T) {
if len(schemaMigrations) == 0 {
t.Fatal("migration plan is empty")
}
latest := schemaMigrations[len(schemaMigrations)-1].version
if CurrentSchemaVersion != latest {
t.Fatalf("supported schema version %d does not match latest migration %d", CurrentSchemaVersion, latest)
}
}
func TestMigrateSchemaRecordsAndValidatesVersion(t *testing.T) {
db, err := Open(Config{Driver: "sqlite", DSN: "file:migration-version?mode=memory&cache=shared"})
if err != nil {