Files
yawan f7fc992f57
Anydoc / anydoc parser engine (push) Canceled after 0s
App / Format, vet, test, and build (push) Canceled after 0s
CLI / test (macos-latest / go 1.26) (push) Canceled after 0s
CLI / test (ubuntu-latest / go 1.26) (push) Canceled after 0s
CLI / test (windows-latest / go 1.26) (push) Canceled after 0s
Build and Push Docker Image / build-ui (push) Canceled after 0s
Build and Push Docker Image / build-docreader (push) Canceled after 0s
Build and Push Docker Image / build-sandbox (push) Canceled after 0s
Build and Push Docker Image / build-app (amd64, linux/amd64, ubuntu-latest) (push) Canceled after 0s
Build and Push Docker Image / build-app (arm64, linux/arm64, ubuntu-24.04-arm) (push) Canceled after 0s
DocReader / Python tests and Go client tests (push) Canceled after 0s
Build & Publish (dsh-weknora) / Typecheck, test, pack (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (pinned) (push) Canceled after 0s
Build & Publish (dsh-weknora) / End-to-end inside dsh (latest, advisory) (push) Canceled after 0s
Frontend / Test, type-check, and build (push) Canceled after 0s
Go Lint Cache / warm lint cache (.) (push) Canceled after 0s
Go Lint Cache / warm lint cache (cli) (push) Canceled after 0s
Go Lint Cache / warm lint cache (client) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.10) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.11) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.12) (push) Canceled after 0s
Build & Publish (mcp-server) / Test (Python 3.13) (push) Canceled after 0s
Build and Push Docker Image / merge (push) Canceled after 0s
Build & Publish (dsh-weknora) / Publish to npm (push) Canceled after 0s
Build & Publish (mcp-server) / Build sdist + wheel (push) Canceled after 0s
Build & Publish (mcp-server) / Publish to PyPI (push) Canceled after 0s
WeKnora deployment source with the reviewed wiki identity, summary and recovery fixes
2026-09-22 16:19:06 +08:00

100 lines
2.8 KiB
Go

package repository
import (
"context"
"errors"
"github.com/Tencent/WeKnora/internal/types"
"github.com/Tencent/WeKnora/internal/types/interfaces"
"gorm.io/gorm"
)
// modelRepository implements the model repository interface
type modelRepository struct {
db *gorm.DB
}
// NewModelRepository creates a new model repository
func NewModelRepository(db *gorm.DB) interfaces.ModelRepository {
return &modelRepository{db: db}
}
// Create creates a new model
func (r *modelRepository) Create(ctx context.Context, m *types.Model) error {
return r.db.WithContext(ctx).Create(m).Error
}
// GetByID retrieves a model by ID
func (r *modelRepository) GetByID(ctx context.Context, tenantID uint64, id string) (*types.Model, error) {
var m types.Model
if err := r.db.WithContext(ctx).Where("id = ?", id).Where(
"(tenant_id = ? OR is_builtin = true)", tenantID,
).First(&m).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
return nil, nil
}
return nil, err
}
return &m, nil
}
// List lists models with optional filtering
func (r *modelRepository) List(
ctx context.Context, tenantID uint64, modelType types.ModelType, source types.ModelSource,
) ([]*types.Model, error) {
var models []*types.Model
query := r.db.WithContext(ctx).Where(
"(tenant_id = ? OR is_builtin = true)", tenantID,
)
if modelType != "" {
query = query.Where("type = ?", modelType)
}
if source != "" {
query = query.Where("source = ?", source)
}
if err := query.Find(&models).Error; err != nil {
return nil, err
}
return models, nil
}
// Update updates a model
func (r *modelRepository) Update(ctx context.Context, m *types.Model) error {
// Use Select to explicitly update all fields, including zero values like false
return r.db.WithContext(ctx).Debug().Model(&types.Model{}).Where(
"id = ? AND tenant_id = ?", m.ID, m.TenantID,
).Select("*").Updates(m).Error
}
// Delete deletes a model
func (r *modelRepository) Delete(ctx context.Context, tenantID uint64, id string) error {
return r.db.WithContext(ctx).Where(
"id = ? AND tenant_id = ?", id, tenantID,
).Delete(&types.Model{}).Error
}
// ClearDefaultByType clears the default flag for all models of a specific type
// This is a batch operation that updates all matching records in one query
func (r *modelRepository) ClearDefaultByType(
ctx context.Context,
tenantID uint,
modelType types.ModelType,
excludeID string,
) error {
query := r.db.WithContext(ctx).Model(&types.Model{}).Where(
"tenant_id = ? AND type = ? AND is_default = ?", tenantID, modelType, true,
)
// If excludeID is provided, exclude that model from the update
if excludeID != "" {
query = query.Where("id != ?", excludeID)
}
// Batch update: set is_default to false for all matching records
return query.Update("is_default", false).Error
}