// Copyright 2021 The Gitea Authors. All rights reserved. // SPDX-License-Identifier: MIT package db import ( "context" "errors" "fmt" ) // ResourceIndex represents a resource index which could be used as issue/release and others // We can create different tables i.e. issue_index, release_index, etc. type ResourceIndex struct { GroupID int64 `xorm:"pk"` MaxIndex int64 `xorm:"index"` } var ( // ErrResouceOutdated represents an error when request resource outdated ErrResouceOutdated = errors.New("resource outdated") // ErrGetResourceIndexFailed represents an error when resource index retries 3 times ErrGetResourceIndexFailed = errors.New("get resource index failed") ) const ( // MaxDupIndexAttempts max retry times to create index MaxDupIndexAttempts = 3 ) // SyncMaxResourceIndex sync the max index with the resource func SyncMaxResourceIndex(ctx context.Context, tableName string, groupID, maxIndex int64) (err error) { e := GetEngine(ctx) // try to update the max_index and acquire the write-lock for the record res, err := e.Exec(fmt.Sprintf("UPDATE %s SET max_index=? WHERE group_id=? AND max_index