2020-05-26 16:46:16 +02:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2023-04-28 16:28:13 +02:00
|
|
|
"context"
|
2020-12-02 08:50:59 +01:00
|
|
|
"time"
|
2020-12-18 16:47:45 +01:00
|
|
|
|
2022-04-27 01:01:45 +02:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/zitadel/internal/view/repository"
|
2020-05-26 16:46:16 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-07 16:53:54 +02:00
|
|
|
sequencesTable = "adminapi.current_sequences"
|
2020-05-26 16:46:16 +02:00
|
|
|
)
|
|
|
|
|
2020-12-18 16:47:45 +01:00
|
|
|
func (v *View) saveCurrentSequence(viewName string, event *models.Event) error {
|
2022-04-19 08:26:12 +02:00
|
|
|
return repository.SaveCurrentSequence(v.Db, sequencesTable, viewName, event.InstanceID, event.Sequence, event.CreationDate)
|
2020-05-26 16:46:16 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:28:13 +02:00
|
|
|
func (v *View) latestSequence(ctx context.Context, viewName, instanceID string) (*repository.CurrentSequence, error) {
|
|
|
|
return repository.LatestSequence(v.Db, v.TimeTravel(ctx, sequencesTable), viewName, instanceID)
|
2022-04-19 08:26:12 +02:00
|
|
|
}
|
|
|
|
|
2023-04-28 16:28:13 +02:00
|
|
|
func (v *View) latestSequences(ctx context.Context, viewName string, instanceIDs []string) ([]*repository.CurrentSequence, error) {
|
|
|
|
return repository.LatestSequences(v.Db, v.TimeTravel(ctx, sequencesTable), viewName, instanceIDs)
|
2020-06-25 08:01:13 +02:00
|
|
|
}
|
|
|
|
|
2022-11-18 13:49:38 +01:00
|
|
|
func (v *View) AllCurrentSequences(db, instanceID string) ([]*repository.CurrentSequence, error) {
|
|
|
|
return repository.AllCurrentSequences(v.Db, db+".current_sequences", instanceID)
|
2020-06-25 08:01:13 +02:00
|
|
|
}
|
|
|
|
|
2022-11-22 07:36:48 +01:00
|
|
|
func (v *View) updateSpoolerRunSequence(viewName string, instanceIDs []string) error {
|
|
|
|
currentSequences, err := repository.LatestSequences(v.Db, sequencesTable, viewName, instanceIDs)
|
2020-12-02 08:50:59 +01:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-04-19 08:26:12 +02:00
|
|
|
for _, currentSequence := range currentSequences {
|
|
|
|
if currentSequence.ViewName == "" {
|
|
|
|
currentSequence.ViewName = viewName
|
|
|
|
}
|
|
|
|
currentSequence.LastSuccessfulSpoolerRun = time.Now()
|
2020-12-02 08:50:59 +01:00
|
|
|
}
|
2022-04-19 08:26:12 +02:00
|
|
|
return repository.UpdateCurrentSequences(v.Db, sequencesTable, currentSequences)
|
2020-12-02 08:50:59 +01:00
|
|
|
}
|
|
|
|
|
2020-06-25 08:01:13 +02: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 16:46:16 +02:00
|
|
|
}
|