2020-06-25 06:01:13 +00:00
|
|
|
package repository
|
2020-04-14 16:20:20 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-06-25 06:01:13 +00:00
|
|
|
"github.com/caos/zitadel/internal/view/model"
|
2020-04-14 16:20:20 +00:00
|
|
|
"github.com/jinzhu/gorm"
|
2020-06-25 06:01:13 +00:00
|
|
|
"strings"
|
2020-07-15 11:24:36 +00:00
|
|
|
"time"
|
2020-04-14 16:20:20 +00:00
|
|
|
)
|
|
|
|
|
2020-06-25 06:01:13 +00:00
|
|
|
type CurrentSequence struct {
|
2020-12-02 07:50:59 +00:00
|
|
|
ViewName string `gorm:"column:view_name;primary_key"`
|
|
|
|
CurrentSequence uint64 `gorm:"column:current_sequence"`
|
|
|
|
EventTimestamp time.Time `gorm:"column:event_timestamp"`
|
|
|
|
LastSuccessfulSpoolerRun time.Time `gorm:"column:last_successful_spooler_run"`
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type SequenceSearchKey int32
|
|
|
|
|
|
|
|
const (
|
2020-06-23 12:47:47 +00:00
|
|
|
SequenceSearchKeyUndefined SequenceSearchKey = iota
|
|
|
|
SequenceSearchKeyViewName
|
2020-04-14 16:20:20 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type sequenceSearchKey SequenceSearchKey
|
|
|
|
|
|
|
|
func (key sequenceSearchKey) ToColumnName() string {
|
|
|
|
switch SequenceSearchKey(key) {
|
2020-06-23 12:47:47 +00:00
|
|
|
case SequenceSearchKeyViewName:
|
2020-04-14 16:20:20 +00:00
|
|
|
return "view_name"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-25 06:01:13 +00:00
|
|
|
func CurrentSequenceToModel(sequence *CurrentSequence) *model.View {
|
|
|
|
dbView := strings.Split(sequence.ViewName, ".")
|
|
|
|
return &model.View{
|
2020-12-02 07:50:59 +00:00
|
|
|
Database: dbView[0],
|
|
|
|
ViewName: dbView[1],
|
|
|
|
CurrentSequence: sequence.CurrentSequence,
|
|
|
|
EventTimestamp: sequence.EventTimestamp,
|
|
|
|
LastSuccessfulSpoolerRun: sequence.LastSuccessfulSpoolerRun,
|
2020-06-25 06:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-02 07:50:59 +00:00
|
|
|
func SaveCurrentSequence(db *gorm.DB, table, viewName string, sequence uint64, eventTimestamp time.Time) error {
|
|
|
|
return UpdateCurrentSequence(db, table, &CurrentSequence{viewName, sequence, eventTimestamp, time.Now()})
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateCurrentSequence(db *gorm.DB, table string, currentSequence *CurrentSequence) error {
|
2020-04-14 16:20:20 +00:00
|
|
|
save := PrepareSave(table)
|
2020-12-02 07:50:59 +00:00
|
|
|
err := save(db, currentSequence)
|
2020-04-14 16:20:20 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return caos_errs.ThrowInternal(err, "VIEW-5kOhP", "unable to updated processed sequence")
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-07-15 11:24:36 +00:00
|
|
|
func LatestSequence(db *gorm.DB, table, viewName string) (*CurrentSequence, error) {
|
|
|
|
sequence := new(CurrentSequence)
|
2020-06-23 12:47:47 +00:00
|
|
|
query := PrepareGetByKey(table, sequenceSearchKey(SequenceSearchKeyViewName), viewName)
|
2020-04-14 16:20:20 +00:00
|
|
|
err := query(db, sequence)
|
|
|
|
|
|
|
|
if err == nil {
|
2020-07-15 11:24:36 +00:00
|
|
|
return sequence, nil
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
|
|
|
|
2020-05-11 10:16:29 +00:00
|
|
|
if caos_errs.IsNotFound(err) {
|
2020-07-15 11:24:36 +00:00
|
|
|
return sequence, nil
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
2020-07-15 11:24:36 +00:00
|
|
|
return nil, caos_errs.ThrowInternalf(err, "VIEW-9LyCB", "unable to get latest sequence of %s", viewName)
|
2020-04-14 16:20:20 +00:00
|
|
|
}
|
2020-06-25 06:01:13 +00:00
|
|
|
|
|
|
|
func AllCurrentSequences(db *gorm.DB, table string) ([]*CurrentSequence, error) {
|
|
|
|
sequences := make([]*CurrentSequence, 0)
|
|
|
|
query := PrepareSearchQuery(table, GeneralSearchRequest{})
|
|
|
|
_, err := query(db, &sequences)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return sequences, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func ClearView(db *gorm.DB, truncateView, sequenceTable string) error {
|
|
|
|
truncate := PrepareTruncate(truncateView)
|
|
|
|
err := truncate(db)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-12-02 07:50:59 +00:00
|
|
|
return SaveCurrentSequence(db, sequenceTable, truncateView, 0, time.Now())
|
2020-06-25 06:01:13 +00:00
|
|
|
}
|