2020-05-26 14:46:16 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2020-12-02 07:50:59 +00:00
|
|
|
"time"
|
2020-12-18 15:47:45 +00:00
|
|
|
|
|
|
|
"github.com/caos/zitadel/internal/eventstore/models"
|
|
|
|
"github.com/caos/zitadel/internal/view/repository"
|
2020-05-26 14:46:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-07 14:53:54 +00:00
|
|
|
sequencesTable = "adminapi.current_sequences"
|
2020-05-26 14:46:16 +00:00
|
|
|
)
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) saveCurrentSequence(viewName string, event *models.Event) error {
|
|
|
|
return repository.SaveCurrentSequence(v.Db, sequencesTable, viewName, string(event.AggregateType), event.Sequence, event.CreationDate)
|
2020-05-26 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) latestSequence(viewName, aggregateType string) (*repository.CurrentSequence, error) {
|
|
|
|
return repository.LatestSequence(v.Db, sequencesTable, viewName, aggregateType)
|
2020-06-25 06:01:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) AllCurrentSequences(db string) ([]*repository.CurrentSequence, error) {
|
|
|
|
return repository.AllCurrentSequences(v.Db, db+".current_sequences")
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func (v *View) updateSpoolerRunSequence(viewName string) error {
|
2020-12-18 15:47:45 +00:00
|
|
|
currentSequence, err := repository.LatestSequence(v.Db, sequencesTable, viewName, "")
|
2020-12-02 07:50:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if currentSequence.ViewName == "" {
|
|
|
|
currentSequence.ViewName = viewName
|
|
|
|
}
|
|
|
|
currentSequence.LastSuccessfulSpoolerRun = time.Now()
|
2020-12-18 15:47:45 +00:00
|
|
|
//update all aggregate types
|
|
|
|
//TODO: not sure if all scenarios work as expected
|
|
|
|
currentSequence.AggregateType = ""
|
2020-12-02 07:50:59 +00:00
|
|
|
return repository.UpdateCurrentSequence(v.Db, sequencesTable, currentSequence)
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func (v *View) GetCurrentSequence(db, viewName, aggregateType string) (*repository.CurrentSequence, error) {
|
2020-12-02 07:50:59 +00:00
|
|
|
sequenceTable := db + ".current_sequences"
|
|
|
|
fullView := db + "." + viewName
|
2020-12-18 15:47:45 +00:00
|
|
|
return repository.LatestSequence(v.Db, sequenceTable, fullView, aggregateType)
|
2020-12-02 07:50:59 +00:00
|
|
|
}
|
|
|
|
|
2020-06-25 06:01:13 +00:00
|
|
|
func (v *View) ClearView(db, viewName string) error {
|
|
|
|
truncateView := db + "." + viewName
|
|
|
|
sequenceTable := db + ".current_sequences"
|
|
|
|
return repository.ClearView(v.Db, truncateView, sequenceTable)
|
2020-05-26 14:46:16 +00:00
|
|
|
}
|