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
|
|
|
|
2021-02-23 14:13:04 +00:00
|
|
|
"github.com/caos/zitadel/internal/eventstore/v1/models"
|
2020-12-18 15:47:45 +00:00
|
|
|
"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 {
|
2022-04-19 06:26:12 +00:00
|
|
|
return repository.SaveCurrentSequence(v.Db, sequencesTable, viewName, event.InstanceID, event.Sequence, event.CreationDate)
|
2020-05-26 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) latestSequence(viewName, instanceID string) (*repository.CurrentSequence, error) {
|
|
|
|
return repository.LatestSequence(v.Db, sequencesTable, viewName, instanceID)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *View) latestSequences(viewName string) ([]*repository.CurrentSequence, error) {
|
|
|
|
return repository.LatestSequences(v.Db, sequencesTable, viewName)
|
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 {
|
2022-04-19 06:26:12 +00:00
|
|
|
currentSequences, err := repository.LatestSequences(v.Db, sequencesTable, viewName)
|
2020-12-02 07:50:59 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
for _, currentSequence := range currentSequences {
|
|
|
|
if currentSequence.ViewName == "" {
|
|
|
|
currentSequence.ViewName = viewName
|
|
|
|
}
|
|
|
|
currentSequence.LastSuccessfulSpoolerRun = time.Now()
|
2020-12-02 07:50:59 +00:00
|
|
|
}
|
2022-04-19 06:26:12 +00:00
|
|
|
return repository.UpdateCurrentSequences(v.Db, sequencesTable, currentSequences)
|
2020-12-02 07:50:59 +00:00
|
|
|
}
|
|
|
|
|
2022-04-19 06:26:12 +00:00
|
|
|
func (v *View) GetCurrentSequence(db, viewName string) ([]*repository.CurrentSequence, error) {
|
2020-12-02 07:50:59 +00:00
|
|
|
sequenceTable := db + ".current_sequences"
|
|
|
|
fullView := db + "." + viewName
|
2022-04-19 06:26:12 +00:00
|
|
|
return repository.LatestSequences(v.Db, sequenceTable, fullView)
|
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
|
|
|
}
|