zitadel/internal/eventstore/v1/models/event.go
Livio Amstutz 56b916a2b0
feat: projections auto create their tables (#3324)
* begin init checks for projections

* first projection checks

* debug notification providers with query fixes

* more projections and first index

* more projections

* more projections

* finish projections

* fix tests (remove db name)

* create tables in setup

* fix logging / error handling

* add tenant to views

* rename tenant to instance_id

* add instance_id to all projections

* add instance_id to all queries

* correct instance_id on projections

* add instance_id to failed_events

* use separate context for instance

* implement features projection

* implement features projection

* remove unique constraint from setup when migration failed

* add error to failed setup event

* add instance_id to primary keys

* fix IAM projection

* remove old migrations folder

* fix keysFromYAML test
2022-03-23 09:02:39 +01:00

91 lines
2.1 KiB
Go

package models
import (
"encoding/json"
"reflect"
"time"
"github.com/caos/zitadel/internal/errors"
)
type EventType string
func (et EventType) String() string {
return string(et)
}
type Event struct {
ID string
Sequence uint64
CreationDate time.Time
Type EventType
PreviousSequence uint64
Data []byte
AggregateID string
AggregateType AggregateType
AggregateVersion Version
EditorService string
EditorUser string
ResourceOwner string
InstanceID string
}
func eventData(i interface{}) ([]byte, error) {
switch v := i.(type) {
case []byte:
return v, nil
case map[string]interface{}:
bytes, err := json.Marshal(v)
if err != nil {
return nil, errors.ThrowInvalidArgument(err, "MODEL-s2fgE", "unable to marshal data")
}
return bytes, nil
case nil:
return nil, nil
default:
t := reflect.TypeOf(i)
if t.Kind() == reflect.Ptr {
t = t.Elem()
}
if t.Kind() != reflect.Struct {
return nil, errors.ThrowInvalidArgument(nil, "MODEL-rjWdN", "data is not valid")
}
bytes, err := json.Marshal(v)
if err != nil {
return nil, errors.ThrowInvalidArgument(err, "MODEL-Y2OpM", "unable to marshal data")
}
return bytes, nil
}
}
func (e *Event) Validate() error {
if e == nil {
return errors.ThrowPreconditionFailed(nil, "MODEL-oEAG4", "event is nil")
}
if string(e.Type) == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-R2sB0", "type not defined")
}
if e.AggregateID == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-A6WwL", "aggregate id not set")
}
if e.AggregateType == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-EzdyK", "aggregate type not set")
}
if err := e.AggregateVersion.Validate(); err != nil {
return err
}
if e.EditorService == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-4Yqik", "editor service not set")
}
if e.EditorUser == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-L3NHO", "editor user not set")
}
if e.ResourceOwner == "" {
return errors.ThrowPreconditionFailed(nil, "MODEL-omFVT", "resource ow")
}
return nil
}