feat: manually mirror opencoze's code from bytedance
Change-Id: I09a73aadda978ad9511264a756b2ce51f5761adf
This commit is contained in:
180
backend/domain/knowledge/internal/dal/dao/knowledge.go
Normal file
180
backend/domain/knowledge/internal/dal/dao/knowledge.go
Normal file
@@ -0,0 +1,180 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/model"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/query"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
)
|
||||
|
||||
type KnowledgeDAO struct {
|
||||
DB *gorm.DB
|
||||
Query *query.Query
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDAO) Create(ctx context.Context, knowledge *model.Knowledge) error {
|
||||
return dao.Query.Knowledge.WithContext(ctx).Create(knowledge)
|
||||
}
|
||||
func (dao *KnowledgeDAO) Upsert(ctx context.Context, knowledge *model.Knowledge) error {
|
||||
return dao.Query.Knowledge.WithContext(ctx).Clauses(clause.OnConflict{UpdateAll: true}).Create(knowledge)
|
||||
}
|
||||
func (dao *KnowledgeDAO) Update(ctx context.Context, knowledge *model.Knowledge) error {
|
||||
k := dao.Query.Knowledge
|
||||
knowledge.UpdatedAt = time.Now().UnixMilli()
|
||||
err := k.WithContext(ctx).Where(k.ID.Eq(knowledge.ID)).Save(knowledge)
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDAO) Delete(ctx context.Context, id int64) error {
|
||||
k := dao.Query.Knowledge
|
||||
_, err := k.WithContext(ctx).Where(k.ID.Eq(id)).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDAO) MGetByID(ctx context.Context, ids []int64) ([]*model.Knowledge, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
k := dao.Query.Knowledge
|
||||
pos, err := k.WithContext(ctx).Where(k.ID.In(ids...)).Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pos, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDAO) FilterEnableKnowledge(ctx context.Context, knowledgeIDs []int64) ([]*model.Knowledge, error) {
|
||||
if len(knowledgeIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
k := dao.Query.Knowledge
|
||||
knowledgeModels, err := k.WithContext(ctx).
|
||||
Select(k.ID, k.FormatType).
|
||||
Where(k.ID.In(knowledgeIDs...)).
|
||||
Where(k.Status.Eq(int32(entity.DocumentStatusEnable))).
|
||||
Find()
|
||||
|
||||
return knowledgeModels, err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDAO) InitTx() (tx *gorm.DB, err error) {
|
||||
tx = dao.DB.Begin()
|
||||
if tx.Error != nil {
|
||||
return nil, err
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDAO) UpdateWithTx(ctx context.Context, tx *gorm.DB, knowledgeID int64, updateMap map[string]interface{}) error {
|
||||
return tx.WithContext(ctx).Model(&model.Knowledge{}).Where("id = ?", knowledgeID).Updates(updateMap).Error
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDAO) FindKnowledgeByCondition(ctx context.Context, opts *entity.WhereKnowledgeOption) (knowledge []*model.Knowledge, total int64, err error) {
|
||||
k := dao.Query.Knowledge
|
||||
do := k.WithContext(ctx).Debug()
|
||||
if opts == nil {
|
||||
return nil, 0, nil
|
||||
}
|
||||
if opts.Query != nil && len(*opts.Query) > 0 {
|
||||
do = do.Where(k.Name.Like("%" + *opts.Query + "%"))
|
||||
}
|
||||
if opts.Name != nil && len(*opts.Name) > 0 {
|
||||
do = do.Where(k.Name.Eq(*opts.Name))
|
||||
}
|
||||
|
||||
if len(opts.KnowledgeIDs) > 0 {
|
||||
do = do.Where(k.ID.In(opts.KnowledgeIDs...))
|
||||
}
|
||||
if ptr.From(opts.AppID) != 0 {
|
||||
do = do.Where(k.AppID.Eq(ptr.From(opts.AppID)))
|
||||
} else {
|
||||
if len(opts.KnowledgeIDs) == 0 {
|
||||
do = do.Where(k.AppID.Eq(0))
|
||||
}
|
||||
}
|
||||
if ptr.From(opts.SpaceID) != 0 {
|
||||
do = do.Where(k.SpaceID.Eq(*opts.SpaceID))
|
||||
}
|
||||
if len(opts.Status) > 0 {
|
||||
do = do.Where(k.Status.In(opts.Status...))
|
||||
}
|
||||
if opts.UserID != nil && ptr.From(opts.UserID) != 0 {
|
||||
do = do.Where(k.CreatorID.Eq(*opts.UserID))
|
||||
}
|
||||
if opts.FormatType != nil {
|
||||
do = do.Where(k.FormatType.Eq(int32(*opts.FormatType)))
|
||||
}
|
||||
if opts.Order != nil {
|
||||
if *opts.Order == entity.OrderCreatedAt {
|
||||
if opts.OrderType != nil {
|
||||
if *opts.OrderType == entity.OrderTypeAsc {
|
||||
do = do.Order(k.CreatedAt.Asc())
|
||||
} else {
|
||||
do = do.Order(k.CreatedAt.Desc())
|
||||
}
|
||||
} else {
|
||||
do = do.Order(k.CreatedAt.Desc())
|
||||
}
|
||||
} else if *opts.Order == entity.OrderUpdatedAt {
|
||||
if opts.OrderType != nil {
|
||||
if *opts.OrderType == entity.OrderTypeAsc {
|
||||
do = do.Order(k.UpdatedAt.Asc())
|
||||
} else {
|
||||
do = do.Order(k.UpdatedAt.Desc())
|
||||
}
|
||||
} else {
|
||||
do = do.Order(k.UpdatedAt.Desc())
|
||||
}
|
||||
}
|
||||
}
|
||||
if opts.Page != nil && opts.PageSize != nil {
|
||||
offset := (*opts.Page - 1) * (*opts.PageSize)
|
||||
do = do.Limit(*opts.PageSize).Offset(offset)
|
||||
}
|
||||
knowledge, err = do.Find()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total, err = do.Limit(-1).Offset(-1).Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return knowledge, total, err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDAO) GetByID(ctx context.Context, id int64) (*model.Knowledge, error) {
|
||||
k := dao.Query.Knowledge
|
||||
knowledge, err := k.WithContext(ctx).Where(k.ID.Eq(id)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return knowledge, nil
|
||||
}
|
||||
197
backend/domain/knowledge/internal/dal/dao/knowledge_document.go
Normal file
197
backend/domain/knowledge/internal/dal/dao/knowledge_document.go
Normal file
@@ -0,0 +1,197 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strconv"
|
||||
"time"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/model"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/query"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
)
|
||||
|
||||
type KnowledgeDocumentDAO struct {
|
||||
DB *gorm.DB
|
||||
Query *query.Query
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) Create(ctx context.Context, document *model.KnowledgeDocument) error {
|
||||
return dao.Query.KnowledgeDocument.WithContext(ctx).Create(document)
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) Update(ctx context.Context, document *model.KnowledgeDocument) error {
|
||||
document.UpdatedAt = time.Now().UnixMilli()
|
||||
err := dao.Query.KnowledgeDocument.WithContext(ctx).Save(document)
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) Delete(ctx context.Context, id int64) error {
|
||||
k := dao.Query.KnowledgeDocument
|
||||
_, err := k.WithContext(ctx).Where(k.ID.Eq(id)).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) MGetByID(ctx context.Context, ids []int64) ([]*model.KnowledgeDocument, error) {
|
||||
if len(ids) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
k := dao.Query.KnowledgeDocument
|
||||
pos, err := k.WithContext(ctx).Where(k.ID.In(ids...)).Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pos, err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) fromCursor(cursor string) (id int64, err error) {
|
||||
id, err = strconv.ParseInt(cursor, 10, 64)
|
||||
return
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) FindDocumentByCondition(ctx context.Context, opts *entity.WhereDocumentOpt) ([]*model.KnowledgeDocument, int64, error) {
|
||||
k := dao.Query.KnowledgeDocument
|
||||
do := k.WithContext(ctx)
|
||||
if opts == nil {
|
||||
return nil, 0, nil
|
||||
}
|
||||
if len(opts.IDs) == 0 && len(opts.KnowledgeIDs) == 0 {
|
||||
return nil, 0, errors.New("need ids or knowledge_ids")
|
||||
}
|
||||
if opts.CreatorID > 0 {
|
||||
do = do.Where(k.CreatorID.Eq(opts.CreatorID))
|
||||
}
|
||||
if len(opts.IDs) > 0 {
|
||||
do = do.Where(k.ID.In(opts.IDs...))
|
||||
}
|
||||
if len(opts.KnowledgeIDs) > 0 {
|
||||
do = do.Where(k.KnowledgeID.In(opts.KnowledgeIDs...))
|
||||
}
|
||||
if len(opts.StatusIn) > 0 {
|
||||
do = do.Where(k.Status.In(opts.StatusIn...))
|
||||
}
|
||||
if len(opts.StatusNotIn) > 0 {
|
||||
do = do.Where(k.Status.NotIn(opts.StatusNotIn...))
|
||||
}
|
||||
if opts.SelectAll {
|
||||
do = do.Limit(-1)
|
||||
} else {
|
||||
if opts.Limit != 0 {
|
||||
do = do.Limit(opts.Limit)
|
||||
}
|
||||
if opts.Offset != nil {
|
||||
do = do.Offset(ptr.From(opts.Offset))
|
||||
}
|
||||
}
|
||||
if opts.Cursor != nil {
|
||||
id, err := dao.fromCursor(ptr.From(opts.Cursor))
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
do = do.Where(k.ID.Lt(id)).Order(k.ID.Desc())
|
||||
}
|
||||
resp, err := do.Find()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total, err := do.Limit(-1).Offset(-1).Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return resp, total, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) DeleteDocuments(ctx context.Context, ids []int64) error {
|
||||
tx := dao.DB.Begin()
|
||||
var err error
|
||||
defer func() {
|
||||
if err != nil {
|
||||
tx.Rollback()
|
||||
} else {
|
||||
tx.Commit()
|
||||
}
|
||||
}()
|
||||
// 删除document
|
||||
err = tx.WithContext(ctx).Model(&model.KnowledgeDocument{}).Where("id in ?", ids).Delete(&model.KnowledgeDocument{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// 删除document_slice
|
||||
err = tx.WithContext(ctx).Model(&model.KnowledgeDocumentSlice{}).Where("document_id in?", ids).Delete(&model.KnowledgeDocumentSlice{}).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) SetStatus(ctx context.Context, documentID int64, status int32, reason string) error {
|
||||
k := dao.Query.KnowledgeDocument
|
||||
d := &model.KnowledgeDocument{Status: status, FailReason: reason, UpdatedAt: time.Now().UnixMilli()}
|
||||
_, err := k.WithContext(ctx).Debug().Where(k.ID.Eq(documentID)).Updates(d)
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) CreateWithTx(ctx context.Context, tx *gorm.DB, documents []*model.KnowledgeDocument) error {
|
||||
if len(documents) == 0 {
|
||||
return nil
|
||||
}
|
||||
tx = tx.WithContext(ctx).Debug().CreateInBatches(documents, len(documents))
|
||||
return tx.Error
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) GetByID(ctx context.Context, id int64) (*model.KnowledgeDocument, error) {
|
||||
k := dao.Query.KnowledgeDocument
|
||||
document, err := k.WithContext(ctx).Where(k.ID.Eq(id)).First()
|
||||
if err != nil {
|
||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, err
|
||||
}
|
||||
return document, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentDAO) UpdateDocumentSliceInfo(ctx context.Context, documentID int64) error {
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
var err error
|
||||
var sliceCount int64
|
||||
var totalSize *int64
|
||||
sliceCount, err = s.WithContext(ctx).Debug().Where(s.DocumentID.Eq(documentID)).Count()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
err = dao.DB.Raw("SELECT SUM(CHAR_LENGTH(content)) FROM knowledge_document_slice WHERE document_id = ? AND deleted_at IS NULL", documentID).Scan(&totalSize).Error
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
k := dao.Query.KnowledgeDocument
|
||||
updates := map[string]any{}
|
||||
updates[k.SliceCount.ColumnName().String()] = sliceCount
|
||||
if totalSize != nil {
|
||||
updates[k.Size.ColumnName().String()] = ptr.From(totalSize)
|
||||
}
|
||||
updates[k.UpdatedAt.ColumnName().String()] = time.Now().UnixMilli()
|
||||
_, err = k.WithContext(ctx).Debug().Where(k.ID.Eq(documentID)).Updates(updates)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,47 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/model"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/query"
|
||||
)
|
||||
|
||||
type KnowledgeDocumentReviewDAO struct {
|
||||
DB *gorm.DB
|
||||
Query *query.Query
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentReviewDAO) CreateInBatches(ctx context.Context, reviews []*model.KnowledgeDocumentReview) error {
|
||||
return dao.Query.KnowledgeDocumentReview.WithContext(ctx).Debug().CreateInBatches(reviews, len(reviews))
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentReviewDAO) MGetByIDs(ctx context.Context, reviewIDs []int64) ([]*model.KnowledgeDocumentReview, error) {
|
||||
return dao.Query.KnowledgeDocumentReview.WithContext(ctx).Debug().Where(dao.Query.KnowledgeDocumentReview.ID.In(reviewIDs...)).Find()
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentReviewDAO) GetByID(ctx context.Context, reviewID int64) (*model.KnowledgeDocumentReview, error) {
|
||||
return dao.Query.KnowledgeDocumentReview.WithContext(ctx).Debug().Where(dao.Query.KnowledgeDocumentReview.ID.Eq(reviewID)).First()
|
||||
}
|
||||
func (dao *KnowledgeDocumentReviewDAO) UpdateReview(ctx context.Context, reviewID int64, mp map[string]interface{}) error {
|
||||
_, err := dao.Query.KnowledgeDocumentReview.WithContext(ctx).Debug().Where(dao.Query.KnowledgeDocumentReview.ID.Eq(reviewID)).Updates(mp)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,323 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"golang.org/x/sync/errgroup"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/model"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/query"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/errorx"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/logs"
|
||||
"github.com/coze-dev/coze-studio/backend/types/errno"
|
||||
)
|
||||
|
||||
type KnowledgeDocumentSliceDAO struct {
|
||||
DB *gorm.DB
|
||||
Query *query.Query
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) Create(ctx context.Context, slice *model.KnowledgeDocumentSlice) error {
|
||||
return dao.Query.KnowledgeDocumentSlice.WithContext(ctx).Create(slice)
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) Update(ctx context.Context, slice *model.KnowledgeDocumentSlice) error {
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
slice.UpdatedAt = time.Now().UnixMilli()
|
||||
err := s.WithContext(ctx).Save(slice)
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) BatchCreate(ctx context.Context, slices []*model.KnowledgeDocumentSlice) error {
|
||||
return dao.Query.KnowledgeDocumentSlice.WithContext(ctx).CreateInBatches(slices, 100)
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) BatchSetStatus(ctx context.Context, ids []int64, status int32, reason string) error {
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
updates := map[string]any{s.Status.ColumnName().String(): status}
|
||||
if reason != "" {
|
||||
updates[s.FailReason.ColumnName().String()] = reason
|
||||
}
|
||||
updates[s.UpdatedAt.ColumnName().String()] = time.Now().UnixMilli()
|
||||
_, err := s.WithContext(ctx).Where(s.ID.In(ids...)).Updates(updates)
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) Delete(ctx context.Context, slice *model.KnowledgeDocumentSlice) error {
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
_, err := s.WithContext(ctx).Where(s.ID.Eq(slice.ID)).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) DeleteByDocument(ctx context.Context, documentID int64) error {
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
_, err := s.WithContext(ctx).Where(s.DocumentID.Eq(documentID)).Delete()
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) List(ctx context.Context, knowledgeID int64, documentID int64, limit int) (
|
||||
pos []*model.KnowledgeDocumentSlice, hasMore bool, err error) {
|
||||
|
||||
do, err := dao.listDo(ctx, knowledgeID, documentID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if limit == -1 {
|
||||
var (
|
||||
lastID int64 = 0
|
||||
batchSize = 100
|
||||
)
|
||||
for {
|
||||
sliceArr, _, err := dao.listBatch(ctx, knowledgeID, documentID, batchSize, lastID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
if len(sliceArr) == 0 {
|
||||
break
|
||||
}
|
||||
pos = append(pos, sliceArr...)
|
||||
lastID = sliceArr[len(sliceArr)-1].ID
|
||||
}
|
||||
return pos, false, nil
|
||||
} else {
|
||||
pos, err = do.Limit(limit).Find()
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if len(pos) == 0 {
|
||||
return nil, false, nil
|
||||
}
|
||||
|
||||
hasMore = len(pos) == limit
|
||||
|
||||
return pos, hasMore, err
|
||||
}
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) listBatch(ctx context.Context, knowledgeID int64, documentID int64, batchSize int, lastID int64) (
|
||||
pos []*model.KnowledgeDocumentSlice, hasMore bool, err error) {
|
||||
|
||||
if batchSize <= 0 {
|
||||
batchSize = 100 // 默认批量大小
|
||||
}
|
||||
|
||||
do, err := dao.listDo(ctx, knowledgeID, documentID)
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
if lastID > 0 {
|
||||
do = do.Where(dao.Query.KnowledgeDocumentSlice.ID.Gt(lastID))
|
||||
}
|
||||
|
||||
pos, err = do.Debug().Limit(batchSize).Order(dao.Query.KnowledgeDocumentSlice.ID.Asc()).Find()
|
||||
if err != nil {
|
||||
return nil, false, err
|
||||
}
|
||||
|
||||
hasMore = len(pos) == batchSize
|
||||
|
||||
return pos, hasMore, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) listDo(ctx context.Context, knowledgeID int64, documentID int64) (
|
||||
query.IKnowledgeDocumentSliceDo, error) {
|
||||
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
do := s.WithContext(ctx)
|
||||
if documentID != 0 {
|
||||
do = do.Where(s.DocumentID.Eq(documentID))
|
||||
}
|
||||
if knowledgeID != 0 {
|
||||
do = do.Where(s.KnowledgeID.Eq(knowledgeID))
|
||||
}
|
||||
|
||||
return do, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) GetDocumentSliceIDs(ctx context.Context, docIDs []int64) (sliceIDs []int64, err error) {
|
||||
if len(docIDs) == 0 {
|
||||
return nil, errors.New("empty document ids")
|
||||
}
|
||||
// doc可能会有很多slice,所以批量处理
|
||||
sliceIDs = make([]int64, 0)
|
||||
var mu sync.Mutex
|
||||
errGroup, ctx := errgroup.WithContext(ctx)
|
||||
errGroup.SetLimit(10)
|
||||
for i := range docIDs {
|
||||
docID := docIDs[i]
|
||||
errGroup.Go(func() (err error) {
|
||||
defer func() {
|
||||
if panicErr := recover(); panicErr != nil {
|
||||
logs.CtxErrorf(ctx, "[getDocSliceIDs] routine error recover:%+v", panicErr)
|
||||
}
|
||||
}()
|
||||
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
logs.CtxErrorf(ctx, "[getDocSliceIDs] doc_id:%d canceled", docID)
|
||||
return ctx.Err()
|
||||
default:
|
||||
}
|
||||
|
||||
slices, _, dbErr := dao.List(ctx, 0, docID, -1)
|
||||
if dbErr != nil {
|
||||
logs.CtxErrorf(ctx, "[getDocSliceIDs] get deleted slice id err:%+v, doc_id:%v", dbErr, docID)
|
||||
return dbErr
|
||||
}
|
||||
mu.Lock()
|
||||
for _, slice := range slices {
|
||||
sliceIDs = append(sliceIDs, slice.ID)
|
||||
}
|
||||
mu.Unlock()
|
||||
return nil
|
||||
})
|
||||
}
|
||||
if err = errGroup.Wait(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return sliceIDs, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) MGetSlices(ctx context.Context, sliceIDs []int64) ([]*model.KnowledgeDocumentSlice, error) {
|
||||
if len(sliceIDs) == 0 {
|
||||
return nil, nil
|
||||
}
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
pos, err := s.WithContext(ctx).Where(s.ID.In(sliceIDs...)).Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pos, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) FindSliceByCondition(ctx context.Context, opts *entity.WhereSliceOpt) (
|
||||
[]*model.KnowledgeDocumentSlice, int64, error) {
|
||||
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
do := s.WithContext(ctx)
|
||||
if opts.DocumentID != 0 {
|
||||
do = do.Where(s.DocumentID.Eq(opts.DocumentID))
|
||||
}
|
||||
if len(opts.DocumentIDs) != 0 {
|
||||
do = do.Where(s.DocumentID.In(opts.DocumentIDs...))
|
||||
}
|
||||
if opts.KnowledgeID != 0 {
|
||||
do = do.Where(s.KnowledgeID.Eq(opts.KnowledgeID))
|
||||
}
|
||||
if opts.DocumentID == 0 && opts.KnowledgeID == 0 && len(opts.DocumentIDs) == 0 {
|
||||
return nil, 0, errors.New("documentID and knowledgeID cannot be empty at the same time")
|
||||
}
|
||||
if opts.Keyword != nil && len(*opts.Keyword) != 0 {
|
||||
do = do.Where(s.Content.Like(*opts.Keyword))
|
||||
}
|
||||
|
||||
if opts.PageSize != 0 {
|
||||
do = do.Limit(int(opts.PageSize))
|
||||
do = do.Offset(int(opts.Sequence)).Order(s.Sequence.Asc())
|
||||
}
|
||||
if opts.NotEmpty != nil {
|
||||
if ptr.From(opts.NotEmpty) {
|
||||
do = do.Where(s.Content.Neq(""))
|
||||
} else {
|
||||
do = do.Where(s.Content.Eq(""))
|
||||
}
|
||||
}
|
||||
pos, err := do.Find()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
total, err := do.Limit(-1).Offset(-1).Count()
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
}
|
||||
return pos, total, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) GetSliceBySequence(ctx context.Context, documentID, sequence int64) ([]*model.KnowledgeDocumentSlice, error) {
|
||||
if documentID == 0 {
|
||||
return nil, errors.New("documentID cannot be empty")
|
||||
}
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
var offset int
|
||||
if sequence >= 2 {
|
||||
offset = int(sequence - 2)
|
||||
}
|
||||
pos, err := s.WithContext(ctx).Where(s.DocumentID.Eq(documentID)).Offset(offset).Order(s.Sequence.Asc()).Limit(2).Find()
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return pos, nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) IncrementHitCount(ctx context.Context, sliceIDs []int64) error {
|
||||
if len(sliceIDs) == 0 {
|
||||
return nil
|
||||
}
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
_, err := s.WithContext(ctx).Debug().Where(s.ID.In(sliceIDs...)).Updates(map[string]interface{}{
|
||||
s.Hit.ColumnName().String(): gorm.Expr("hit +?", 1),
|
||||
s.UpdatedAt.ColumnName().String(): time.Now().UnixMilli(),
|
||||
})
|
||||
return err
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) GetSliceHitByKnowledgeID(ctx context.Context, knowledgeID int64) (int64, error) {
|
||||
if knowledgeID == 0 {
|
||||
return 0, errors.New("knowledgeID cannot be empty")
|
||||
}
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
var totalSliceHit *int64
|
||||
err := s.WithContext(ctx).Debug().Select(s.Hit.Sum()).Where(s.KnowledgeID.Eq(knowledgeID)).Scan(&totalSliceHit)
|
||||
if err != nil {
|
||||
return 0, err
|
||||
}
|
||||
return ptr.From(totalSliceHit), nil
|
||||
}
|
||||
|
||||
func (dao *KnowledgeDocumentSliceDAO) GetLastSequence(ctx context.Context, documentID int64) (float64, error) {
|
||||
if documentID == 0 {
|
||||
return 0, errors.New("[GetLastSequence] documentID cannot be empty")
|
||||
}
|
||||
s := dao.Query.KnowledgeDocumentSlice
|
||||
resp, err := s.WithContext(ctx).Debug().
|
||||
Select(s.Sequence).
|
||||
Where(s.DocumentID.Eq(documentID)).
|
||||
Order(s.Sequence.Desc()).
|
||||
First()
|
||||
if err == gorm.ErrRecordNotFound {
|
||||
return 0, nil
|
||||
}
|
||||
if err != nil {
|
||||
return 0, fmt.Errorf("[GetLastSequence] db exec err, document_id=%v, %w", documentID, err)
|
||||
}
|
||||
if resp == nil {
|
||||
return 0, errorx.New(errno.ErrKnowledgeNonRetryableCode,
|
||||
errorx.KVf("reason", "[GetLastSequence] resp is nil, document_id=%v", documentID))
|
||||
}
|
||||
return resp.Sequence, nil
|
||||
}
|
||||
@@ -0,0 +1,229 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
. "github.com/bytedance/mockey"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gorm.io/gorm"
|
||||
"gorm.io/gorm/clause"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/entity"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/model"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/query"
|
||||
"github.com/coze-dev/coze-studio/backend/internal/mock/infra/contract/orm"
|
||||
"github.com/coze-dev/coze-studio/backend/pkg/lang/ptr"
|
||||
)
|
||||
|
||||
func TestKnowledgeDocument(t *testing.T) {
|
||||
suite.Run(t, &DocumentTestSuite{})
|
||||
}
|
||||
|
||||
type DocumentTestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
dao *KnowledgeDocumentDAO
|
||||
}
|
||||
|
||||
func (suite *DocumentTestSuite) SetupSuite() {
|
||||
suite.ctx = context.Background()
|
||||
mockDB := orm.NewMockDB()
|
||||
mockDB.AddTable(&model.KnowledgeDocument{})
|
||||
mockDB.AddTable(&model.KnowledgeDocumentSlice{})
|
||||
db, err := mockDB.DB()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
suite.db = db
|
||||
suite.dao = &KnowledgeDocumentDAO{
|
||||
DB: db,
|
||||
Query: query.Use(db),
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *DocumentTestSuite) TearDownTest() {
|
||||
suite.clearDB()
|
||||
}
|
||||
|
||||
func (suite *DocumentTestSuite) clearDB() {
|
||||
suite.db.WithContext(suite.ctx).Unscoped().Delete(&model.KnowledgeDocument{})
|
||||
suite.db.WithContext(suite.ctx).Unscoped().Delete(&model.KnowledgeDocumentSlice{})
|
||||
}
|
||||
|
||||
func (suite *DocumentTestSuite) TestCRUD() {
|
||||
PatchConvey("test crud", suite.T(), func() {
|
||||
ctx := suite.ctx
|
||||
q := suite.dao.Query.KnowledgeDocument
|
||||
|
||||
err := suite.dao.Create(ctx, &model.KnowledgeDocument{ID: 123, KnowledgeID: 456})
|
||||
So(err, ShouldBeNil)
|
||||
first, err := q.WithContext(ctx).Where(q.ID.Eq(123)).First()
|
||||
So(err, ShouldBeNil)
|
||||
So(first, ShouldNotBeNil)
|
||||
So(first.KnowledgeID, ShouldEqual, int64(456))
|
||||
|
||||
err = suite.dao.Update(ctx, &model.KnowledgeDocument{ID: 123, KnowledgeID: 654})
|
||||
So(err, ShouldBeNil)
|
||||
first, err = q.WithContext(ctx).Where(q.ID.Eq(123)).First()
|
||||
So(err, ShouldBeNil)
|
||||
So(first, ShouldNotBeNil)
|
||||
So(first.KnowledgeID, ShouldEqual, int64(654))
|
||||
|
||||
err = suite.dao.Delete(ctx, 123)
|
||||
So(err, ShouldBeNil)
|
||||
first, err = q.WithContext(ctx).Where(q.ID.Eq(123)).First()
|
||||
So(err, ShouldNotBeNil)
|
||||
So(errors.Is(err, gorm.ErrRecordNotFound), ShouldBeTrue)
|
||||
So(first, ShouldBeNil)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *DocumentTestSuite) TestMGetByID() {
|
||||
PatchConvey("test MGetByID", suite.T(), func() {
|
||||
ctx := suite.ctx
|
||||
resp, err := suite.dao.MGetByID(ctx, nil)
|
||||
So(err, ShouldBeNil)
|
||||
So(resp, ShouldBeNil)
|
||||
|
||||
suite.db.Create([]*model.KnowledgeDocument{
|
||||
{
|
||||
ID: 666,
|
||||
KnowledgeID: 123,
|
||||
}, {
|
||||
ID: 667,
|
||||
KnowledgeID: 123,
|
||||
},
|
||||
})
|
||||
resp, err = suite.dao.MGetByID(ctx, []int64{666, 667})
|
||||
So(err, ShouldBeNil)
|
||||
So(len(resp), ShouldEqual, 2)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *DocumentTestSuite) TestUpdateDocumentSliceInfo() {
|
||||
PatchConvey("test UpdateDocumentSliceInfo", suite.T(), func() {
|
||||
ctx := suite.ctx
|
||||
suite.db.Create([]*model.KnowledgeDocumentSlice{
|
||||
{
|
||||
ID: 1,
|
||||
KnowledgeID: 123,
|
||||
DocumentID: 456,
|
||||
Content: "hello",
|
||||
},
|
||||
{
|
||||
ID: 2,
|
||||
KnowledgeID: 123,
|
||||
DocumentID: 456,
|
||||
Content: "world",
|
||||
},
|
||||
})
|
||||
|
||||
suite.db.Create(&model.KnowledgeDocument{
|
||||
ID: 456,
|
||||
KnowledgeID: 123,
|
||||
})
|
||||
|
||||
Mock(GetMethod(suite.db, "Raw")).To(func(sql string, values ...interface{}) (tx *gorm.DB) {
|
||||
tx = suite.db.WithContext(suite.ctx)
|
||||
tx.Statement.SQL = strings.Builder{}
|
||||
newSQL := strings.Replace(sql, "CHAR_LENGTH", "LENGTH", 1)
|
||||
if strings.Contains(newSQL, "@") {
|
||||
clause.NamedExpr{SQL: newSQL, Vars: values}.Build(tx.Statement)
|
||||
} else {
|
||||
clause.Expr{SQL: newSQL, Vars: values}.Build(tx.Statement)
|
||||
}
|
||||
return tx
|
||||
}).Build()
|
||||
|
||||
err := suite.dao.UpdateDocumentSliceInfo(ctx, 456)
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
q := suite.dao.Query.KnowledgeDocument
|
||||
d, err := q.WithContext(ctx).Where(q.ID.Eq(456)).First()
|
||||
So(err, ShouldBeNil)
|
||||
So(d, ShouldNotBeNil)
|
||||
So(d.SliceCount, ShouldEqual, 2)
|
||||
So(d.Size, ShouldEqual, 10)
|
||||
})
|
||||
}
|
||||
|
||||
func (suite *DocumentTestSuite) TestFindDocumentByCondition() {
|
||||
PatchConvey("test FindDocumentByCondition", suite.T(), func() {
|
||||
ctx := context.Background()
|
||||
mockDB := orm.NewMockDB()
|
||||
mockDB.AddTable(&model.KnowledgeDocument{})
|
||||
db, err := mockDB.DB()
|
||||
So(err, ShouldBeNil)
|
||||
|
||||
dao := &KnowledgeDocumentDAO{
|
||||
DB: db,
|
||||
Query: query.Use(db),
|
||||
}
|
||||
|
||||
db.Create([]*model.KnowledgeDocument{
|
||||
{
|
||||
ID: 666,
|
||||
KnowledgeID: 123,
|
||||
}, {
|
||||
ID: 667,
|
||||
KnowledgeID: 123,
|
||||
},
|
||||
})
|
||||
|
||||
PatchConvey("test paging", func() {
|
||||
resp, total, err := dao.FindDocumentByCondition(ctx, &entity.WhereDocumentOpt{
|
||||
IDs: []int64{666, 667},
|
||||
KnowledgeIDs: []int64{123},
|
||||
Limit: 1,
|
||||
Offset: ptr.Of(0),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(total, ShouldEqual, 2)
|
||||
So(len(resp), ShouldEqual, 1)
|
||||
So(resp[0].ID, ShouldEqual, int64(666))
|
||||
|
||||
resp, total, err = dao.FindDocumentByCondition(ctx, &entity.WhereDocumentOpt{
|
||||
IDs: []int64{666, 667},
|
||||
KnowledgeIDs: []int64{123},
|
||||
Limit: 1,
|
||||
Offset: ptr.Of(1),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(total, ShouldEqual, 2)
|
||||
So(len(resp), ShouldEqual, 1)
|
||||
So(resp[0].ID, ShouldEqual, int64(667))
|
||||
|
||||
resp, total, err = dao.FindDocumentByCondition(ctx, &entity.WhereDocumentOpt{
|
||||
IDs: []int64{666, 667},
|
||||
KnowledgeIDs: []int64{123},
|
||||
Limit: 1,
|
||||
Offset: ptr.Of(2),
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
So(total, ShouldEqual, 2)
|
||||
So(len(resp), ShouldEqual, 0)
|
||||
})
|
||||
})
|
||||
}
|
||||
87
backend/domain/knowledge/internal/dal/dao/knowledge_test.go
Normal file
87
backend/domain/knowledge/internal/dal/dao/knowledge_test.go
Normal file
@@ -0,0 +1,87 @@
|
||||
/*
|
||||
* Copyright 2025 coze-dev Authors
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package dao
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
. "github.com/bytedance/mockey"
|
||||
. "github.com/smartystreets/goconvey/convey"
|
||||
"github.com/stretchr/testify/suite"
|
||||
"gorm.io/gorm"
|
||||
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/model"
|
||||
"github.com/coze-dev/coze-studio/backend/domain/knowledge/internal/dal/query"
|
||||
"github.com/coze-dev/coze-studio/backend/internal/mock/infra/contract/orm"
|
||||
)
|
||||
|
||||
func TestKnowledgeSuite(t *testing.T) {
|
||||
suite.Run(t, new(KnowledgeSuite))
|
||||
}
|
||||
|
||||
type KnowledgeSuite struct {
|
||||
suite.Suite
|
||||
|
||||
ctx context.Context
|
||||
db *gorm.DB
|
||||
dao *KnowledgeDAO
|
||||
}
|
||||
|
||||
func (suite *KnowledgeSuite) SetupSuite() {
|
||||
suite.ctx = context.Background()
|
||||
mockDB := orm.NewMockDB()
|
||||
mockDB.AddTable(&model.Knowledge{})
|
||||
db, err := mockDB.DB()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
suite.db = db
|
||||
suite.dao = &KnowledgeDAO{
|
||||
DB: db,
|
||||
Query: query.Use(db),
|
||||
}
|
||||
}
|
||||
|
||||
func (suite *KnowledgeSuite) TearDownTest() {
|
||||
suite.db.WithContext(suite.ctx).Unscoped().Delete(&model.Knowledge{})
|
||||
}
|
||||
|
||||
func (suite *KnowledgeSuite) TestCRUD() {
|
||||
PatchConvey("test crud", suite.T(), func() {
|
||||
ctx := suite.ctx
|
||||
q := suite.dao.Query.Knowledge
|
||||
|
||||
err := suite.dao.Create(ctx, &model.Knowledge{
|
||||
ID: 123,
|
||||
Name: "test",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
k, err := q.WithContext(ctx).Where(q.ID.Eq(123)).First()
|
||||
So(err, ShouldBeNil)
|
||||
So(k.Name, ShouldEqual, "test")
|
||||
|
||||
err = suite.dao.Upsert(ctx, &model.Knowledge{
|
||||
ID: 123,
|
||||
Name: "testtest",
|
||||
})
|
||||
So(err, ShouldBeNil)
|
||||
k, err = q.WithContext(ctx).Where(q.ID.Eq(123)).First()
|
||||
So(err, ShouldBeNil)
|
||||
So(k.Name, ShouldEqual, "testtest")
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user