2020-05-26 14:46:16 +00:00
|
|
|
package view
|
|
|
|
|
|
|
|
import (
|
2023-04-28 14:28:13 +00:00
|
|
|
"context"
|
2020-12-02 07:50:59 +00:00
|
|
|
"time"
|
2020-12-18 15:47:45 +00:00
|
|
|
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore/v1/models"
|
|
|
|
"github.com/zitadel/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
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00: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 06:26:12 +00:00
|
|
|
}
|
|
|
|
|
2023-04-28 14:28:13 +00: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 06:01:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-18 12:49:38 +00:00
|
|
|
func (v *View) AllCurrentSequences(db, instanceID string) ([]*repository.CurrentSequence, error) {
|
|
|
|
return repository.AllCurrentSequences(v.Db, db+".current_sequences", instanceID)
|
2020-06-25 06:01:13 +00:00
|
|
|
}
|
|
|
|
|
2022-11-22 06:36:48 +00:00
|
|
|
func (v *View) updateSpoolerRunSequence(viewName string, instanceIDs []string) error {
|
|
|
|
currentSequences, err := repository.LatestSequences(v.Db, sequencesTable, viewName, instanceIDs)
|
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
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|