zitadel/internal/view/repository/sequence.go

94 lines
2.6 KiB
Go
Raw Normal View History

package repository
import (
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/view/model"
"github.com/jinzhu/gorm"
"strings"
"time"
)
type CurrentSequence struct {
ViewName string `gorm:"column:view_name;primary_key"`
CurrentSequence uint64 `gorm:"column:current_sequence"`
EventTimestamp time.Time `gorm:"column:event_timestamp"`
LastSuccessfulSpoolerRun time.Time `gorm:"column:last_successful_spooler_run"`
}
type SequenceSearchKey int32
const (
SequenceSearchKeyUndefined SequenceSearchKey = iota
SequenceSearchKeyViewName
)
type sequenceSearchKey SequenceSearchKey
func (key sequenceSearchKey) ToColumnName() string {
switch SequenceSearchKey(key) {
case SequenceSearchKeyViewName:
return "view_name"
default:
return ""
}
}
func CurrentSequenceToModel(sequence *CurrentSequence) *model.View {
dbView := strings.Split(sequence.ViewName, ".")
return &model.View{
Database: dbView[0],
ViewName: dbView[1],
CurrentSequence: sequence.CurrentSequence,
EventTimestamp: sequence.EventTimestamp,
LastSuccessfulSpoolerRun: sequence.LastSuccessfulSpoolerRun,
}
}
func SaveCurrentSequence(db *gorm.DB, table, viewName string, sequence uint64, eventTimestamp time.Time) error {
return UpdateCurrentSequence(db, table, &CurrentSequence{viewName, sequence, eventTimestamp, time.Now()})
}
func UpdateCurrentSequence(db *gorm.DB, table string, currentSequence *CurrentSequence) error {
save := PrepareSave(table)
err := save(db, currentSequence)
if err != nil {
return caos_errs.ThrowInternal(err, "VIEW-5kOhP", "unable to updated processed sequence")
}
return nil
}
func LatestSequence(db *gorm.DB, table, viewName string) (*CurrentSequence, error) {
sequence := new(CurrentSequence)
query := PrepareGetByKey(table, sequenceSearchKey(SequenceSearchKeyViewName), viewName)
err := query(db, sequence)
if err == nil {
return sequence, nil
}
feat: project view (#90) * init for views (spooler, handler) * init for views (spooler, handler) * start view in management * granted project * implement granted project view * search granted projects * fix search column * update all projects on project change * search roles * filter org * project members * project grant members * fix tests * application view * project grant search * mock * test appendevents * test appendevents * Update internal/view/query.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * Update internal/eventstore/spooler/spooler.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * Update internal/view/query.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * merge request changes * Update internal/project/repository/view/model/application.go Co-authored-by: Livio Amstutz <livio.a@gmail.com> * merge request changes * Project view sql (#92) * sql and configs * error handling * sql start in eventstore * on error handling, config * read user on members * Update internal/project/repository/view/application_view.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/application_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_grant_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_grant_member_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_grant_member_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_member_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_member_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/granted_project.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * return caos errors * Update internal/project/repository/view/model/granted_project_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_grant_member.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_grant_member_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_member.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_member_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_role.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update internal/project/repository/view/model/project_role_query.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/application_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/application_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * Update pkg/management/api/grpc/project_converter.go Co-authored-by: Silvan <silvan.reusser@gmail.com> * converter fix Co-authored-by: Livio Amstutz <livio.a@gmail.com> Co-authored-by: Silvan <silvan.reusser@gmail.com>
2020-05-11 10:16:29 +00:00
if caos_errs.IsNotFound(err) {
return sequence, nil
}
return nil, caos_errs.ThrowInternalf(err, "VIEW-9LyCB", "unable to get latest sequence of %s", viewName)
}
func AllCurrentSequences(db *gorm.DB, table string) ([]*CurrentSequence, error) {
sequences := make([]*CurrentSequence, 0)
query := PrepareSearchQuery(table, GeneralSearchRequest{})
_, err := query(db, &sequences)
if err != nil {
return nil, err
}
return sequences, nil
}
func ClearView(db *gorm.DB, truncateView, sequenceTable string) error {
truncate := PrepareTruncate(truncateView)
err := truncate(db)
if err != nil {
return err
}
return SaveCurrentSequence(db, sequenceTable, truncateView, 0, time.Now())
}