zitadel/internal/view/sequence.go
Fabi 34e2f1bcdd
feat: View (#27)
* feat: eventstore repository

* fix: remove gorm

* version

* feat: pkg

* feat: eventstore without eventstore-lib

* rename files

* gnueg

* fix: add object

* fix: global model

* feat: add global view functions

* feat(eventstore): sdk

* fix(eventstore): search query

* fix(eventstore): rename app to eventstore

* delete empty test

* remove unused func

* merge master

* fix(eventstore): tests

* fix(models): delete unused struct

* feat(eventstore): implemented push events

* feat(eventstore): overwrite context data

* feat(types): SQL-config

* feat(eventstore): options to overwrite editor

* fix: use global sql config

* fix: changes from mr

* fix: add some tests

* Update internal/eventstore/models/field.go

Co-Authored-By: livio-a <livio.a@gmail.com>

* fix(eventstore): code quality

* fix: try tests

* fix: query tests

* fix: use prepare funcs

* fix: go mod

* fix: go tests

* fix: better error func testing

* fix: merge master

* fix: changes for mr

* fix: change value to interface

* fix: searchmethods

* fix: check if value is string on equal ignore case

Co-authored-by: adlerhurst <silvan.reusser@gmail.com>
Co-authored-by: livio-a <livio.a@gmail.com>
2020-04-14 18:20:20 +02:00

59 lines
1.4 KiB
Go

package view
import (
caos_errs "github.com/caos/zitadel/internal/errors"
"github.com/jinzhu/gorm"
)
type actualSequece struct {
ActualSequence uint64 `gorm:"column:current_sequence"`
}
type currentSequence struct {
ViewName string `gorm:"column:view_name;primary_key"`
CurrentSequence uint64 `gorm:"column:current_sequence`
}
type SequenceSearchKey int32
const (
SEQUENCESEARCHKEY_UNDEFINED SequenceSearchKey = iota
SEQUENCESEARCHKEY_VIEW_NAME
)
type sequenceSearchKey SequenceSearchKey
func (key sequenceSearchKey) ToColumnName() string {
switch SequenceSearchKey(key) {
case SEQUENCESEARCHKEY_VIEW_NAME:
return "view_name"
default:
return ""
}
}
func SaveCurrentSequence(db *gorm.DB, table, viewName string, sequence uint64) error {
save := PrepareSave(table)
err := save(db, &currentSequence{viewName, sequence})
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) (uint64, error) {
sequence := new(actualSequece)
query := PrepareGetByKey(table, sequenceSearchKey(SEQUENCESEARCHKEY_VIEW_NAME), viewName)
err := query(db, sequence)
if err == nil {
return sequence.ActualSequence, nil
}
if gorm.IsRecordNotFoundError(err) {
return 0, nil
}
return 0, caos_errs.ThrowInternalf(err, "VIEW-9LyCB", "unable to get latest sequence of %s", viewName)
}