2020-06-25 06:01:13 +00:00
|
|
|
package repository
|
2020-04-14 16:20:20 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-18 15:47:45 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2020-04-14 16:20:20 +00:00
|
|
|
caos_errs "github.com/caos/zitadel/internal/errors"
|
2020-12-18 15:47:45 +00:00
|
|
|
int_model "github.com/caos/zitadel/internal/model"
|
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
|
|
|
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-12-18 15:47:45 +00:00
|
|
|
AggregateType string `gorm:"column:aggregate_type;primary_key"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type currentSequenceViewWithSequence struct {
|
|
|
|
ViewName string `gorm:"column:view_name;primary_key"`
|
|
|
|
CurrentSequence uint64 `gorm:"column:current_sequence"`
|
|
|
|
LastSuccessfulSpoolerRun time.Time `gorm:"column:last_successful_spooler_run"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type currentSequenceView struct {
|
|
|
|
ViewName string `gorm:"column:view_name;primary_key"`
|
|
|
|
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-12-18 15:47:45 +00:00
|
|
|
SequenceSearchKeyAggregateType
|
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"
|
2020-12-18 15:47:45 +00:00
|
|
|
case SequenceSearchKeyAggregateType:
|
|
|
|
return "aggregate_type"
|
2020-04-14 16:20:20 +00:00
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
type sequenceSearchQuery struct {
|
|
|
|
key sequenceSearchKey
|
|
|
|
value string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *sequenceSearchQuery) GetKey() ColumnKey {
|
|
|
|
return q.key
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *sequenceSearchQuery) GetMethod() int_model.SearchMethod {
|
|
|
|
return int_model.SearchMethodEquals
|
|
|
|
}
|
|
|
|
|
|
|
|
func (q *sequenceSearchQuery) GetValue() interface{} {
|
|
|
|
return q.value
|
|
|
|
}
|
|
|
|
|
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-12-18 15:47:45 +00:00
|
|
|
AggregateType: sequence.AggregateType,
|
2020-06-25 06:01:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func SaveCurrentSequence(db *gorm.DB, table, viewName, aggregateType string, sequence uint64, eventTimestamp time.Time) error {
|
|
|
|
return UpdateCurrentSequence(db, table, &CurrentSequence{viewName, sequence, eventTimestamp, time.Now(), aggregateType})
|
2020-12-02 07:50:59 +00:00
|
|
|
}
|
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
func UpdateCurrentSequence(db *gorm.DB, table string, currentSequence *CurrentSequence) (err error) {
|
|
|
|
var seq interface{} = currentSequence
|
|
|
|
if currentSequence.AggregateType == "" && currentSequence.CurrentSequence > 0 {
|
|
|
|
//spooler run
|
|
|
|
seq = ¤tSequenceView{ViewName: currentSequence.ViewName, LastSuccessfulSpoolerRun: currentSequence.LastSuccessfulSpoolerRun}
|
|
|
|
} else if currentSequence.AggregateType == "" {
|
|
|
|
//reset current sequence on view
|
|
|
|
seq = ¤tSequenceViewWithSequence{ViewName: currentSequence.ViewName, LastSuccessfulSpoolerRun: currentSequence.LastSuccessfulSpoolerRun, CurrentSequence: currentSequence.CurrentSequence}
|
|
|
|
}
|
2020-04-14 16:20:20 +00:00
|
|
|
|
2020-12-18 15:47:45 +00:00
|
|
|
save := PrepareSave(table)
|
|
|
|
err = save(db, seq)
|
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-12-18 15:47:45 +00:00
|
|
|
func LatestSequence(db *gorm.DB, table, viewName, aggregateType string) (*CurrentSequence, error) {
|
|
|
|
searchQueries := make([]SearchQuery, 0, 2)
|
|
|
|
searchQueries = append(searchQueries, &sequenceSearchQuery{key: sequenceSearchKey(SequenceSearchKeyViewName), value: viewName})
|
|
|
|
if aggregateType != "" {
|
|
|
|
searchQueries = append(searchQueries, &sequenceSearchQuery{key: sequenceSearchKey(SequenceSearchKeyAggregateType), value: aggregateType})
|
|
|
|
} else {
|
|
|
|
// ensure highest sequence of view
|
|
|
|
db = db.Order("current_sequence DESC")
|
|
|
|
}
|
|
|
|
|
|
|
|
query := PrepareGetByQuery(table, searchQueries...)
|
2020-07-15 11:24:36 +00:00
|
|
|
sequence := new(CurrentSequence)
|
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-18 15:47:45 +00:00
|
|
|
return SaveCurrentSequence(db, sequenceTable, truncateView, "", 0, time.Now())
|
2020-06-25 06:01:13 +00:00
|
|
|
}
|