mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-17 19:05:13 +00:00
start sqlite migrations
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
package repository
|
||||
|
||||
//AggregateType is the object name
|
||||
type AggregateType string
|
||||
|
||||
// //Aggregate represents an object
|
||||
// type Aggregate struct {
|
||||
// //ID id is the unique identifier of the aggregate
|
||||
// // the client must generate it by it's own
|
||||
// ID string
|
||||
// //Type describes the meaning of this aggregate
|
||||
// // it could an object like user
|
||||
// Type AggregateType
|
||||
|
||||
// //ResourceOwner is the organisation which owns this aggregate
|
||||
// // an aggregate can only be managed by one organisation
|
||||
// // use the ID of the org
|
||||
// ResourceOwner string
|
||||
|
||||
// //Events describe all the changes made on an aggregate
|
||||
// Events []*Event
|
||||
// }
|
||||
@@ -0,0 +1,60 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"time"
|
||||
)
|
||||
|
||||
//Event represents all information about a manipulation of an aggregate
|
||||
type Event struct {
|
||||
//ID is a generated uuid for this event
|
||||
ID string
|
||||
|
||||
//Sequence is the sequence of the event
|
||||
Sequence uint64
|
||||
//PreviousSequence is the sequence of the previous sequence
|
||||
// if it's 0 then it's the first event of this aggregate
|
||||
PreviousSequence uint64
|
||||
|
||||
//PreviousEvent is needed in push to update PreviousSequence
|
||||
// it implements a linked list
|
||||
PreviousEvent *Event
|
||||
|
||||
//CreationDate is the time the event is created
|
||||
// it's used for human readability.
|
||||
// Don't use it for event ordering,
|
||||
// time drifts in different services could cause integrity problems
|
||||
CreationDate time.Time
|
||||
|
||||
//Type describes the cause of the event (e.g. user.added)
|
||||
// it should always be in past-form
|
||||
Type EventType
|
||||
|
||||
//Data describe the changed fields (e.g. userName = "hodor")
|
||||
// data must always a pointer to a struct, a struct or a byte array containing json bytes
|
||||
Data interface{}
|
||||
|
||||
//EditorService should be a unique identifier for the service which created the event
|
||||
// it's meant for maintainability
|
||||
EditorService string
|
||||
//EditorUser should be a unique identifier for the user which created the event
|
||||
// it's meant for maintainability.
|
||||
// It's recommend to use the aggregate id of the user
|
||||
EditorUser string
|
||||
|
||||
//Version describes the definition of the aggregate at a certain point in time
|
||||
// it's used in read models to reduce the events in the correct definition
|
||||
Version Version
|
||||
//AggregateID id is the unique identifier of the aggregate
|
||||
// the client must generate it by it's own
|
||||
AggregateID string
|
||||
//AggregateType describes the meaning of the aggregate for this event
|
||||
// it could an object like user
|
||||
AggregateType AggregateType
|
||||
//ResourceOwner is the organisation which owns this aggregate
|
||||
// an aggregate can only be managed by one organisation
|
||||
// use the ID of the org
|
||||
ResourceOwner string
|
||||
}
|
||||
|
||||
//EventType is the description of the change
|
||||
type EventType string
|
||||
@@ -0,0 +1,82 @@
|
||||
package repository
|
||||
|
||||
import "context"
|
||||
|
||||
type InMemory struct {
|
||||
events []*Event
|
||||
}
|
||||
|
||||
func (repo *InMemory) Health(ctx context.Context) error { return nil }
|
||||
|
||||
// PushEvents adds all events of the given aggregates to the eventstreams of the aggregates.
|
||||
// This call is transaction save. The transaction will be rolled back if one event fails
|
||||
func (repo *InMemory) Push(ctx context.Context, events ...*Event) error {
|
||||
repo.events = append(repo.events, events...)
|
||||
return nil
|
||||
}
|
||||
|
||||
// Filter returns all events matching the given search query
|
||||
func (repo *InMemory) Filter(ctx context.Context, searchQuery *SearchQuery) (events []*Event, err error) {
|
||||
indexes := repo.filter(searchQuery)
|
||||
events = make([]*Event, len(indexes))
|
||||
for i, index := range indexes {
|
||||
events[i] = repo.events[index]
|
||||
}
|
||||
|
||||
return events, nil
|
||||
}
|
||||
|
||||
func (repo *InMemory) filter(query *SearchQuery) []int {
|
||||
foundIndex := make([]int, 0, query.Limit)
|
||||
events:
|
||||
for i, event := range repo.events {
|
||||
if query.Limit > 0 && uint64(len(foundIndex)) < query.Limit {
|
||||
return foundIndex
|
||||
}
|
||||
for _, filter := range query.Filters {
|
||||
var value interface{}
|
||||
switch filter.field {
|
||||
case Field_AggregateID:
|
||||
value = event.AggregateID
|
||||
case Field_EditorService:
|
||||
value = event.EditorService
|
||||
case Field_EventType:
|
||||
value = event.Type
|
||||
case Field_AggregateType:
|
||||
value = event.AggregateType
|
||||
case Field_EditorUser:
|
||||
value = event.EditorUser
|
||||
case Field_ResourceOwner:
|
||||
value = event.ResourceOwner
|
||||
case Field_LatestSequence:
|
||||
value = event.Sequence
|
||||
}
|
||||
switch filter.operation {
|
||||
case Operation_Equals:
|
||||
if filter.value == value {
|
||||
foundIndex = append(foundIndex, i)
|
||||
}
|
||||
case Operation_Greater:
|
||||
fallthrough
|
||||
case Operation_Less:
|
||||
|
||||
return nil
|
||||
case Operation_In:
|
||||
values := filter.Value().([]interface{})
|
||||
for _, val := range values {
|
||||
if val == value {
|
||||
foundIndex = append(foundIndex, i)
|
||||
continue events
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return foundIndex
|
||||
}
|
||||
|
||||
//LatestSequence returns the latests sequence found by the the search query
|
||||
func (repo *InMemory) LatestSequence(ctx context.Context, queryFactory *SearchQuery) (uint64, error) {
|
||||
return 0, nil
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
)
|
||||
|
||||
type Repository interface {
|
||||
Health(ctx context.Context) error
|
||||
|
||||
// PushEvents adds all events of the given aggregates to the eventstreams of the aggregates.
|
||||
// This call is transaction save. The transaction will be rolled back if one event fails
|
||||
Push(ctx context.Context, events ...*Event) error
|
||||
// Filter returns all events matching the given search query
|
||||
Filter(ctx context.Context, searchQuery *SearchQuery) (events []*Event, err error)
|
||||
//LatestSequence returns the latests sequence found by the the search query
|
||||
LatestSequence(ctx context.Context, queryFactory *SearchQuery) (uint64, error)
|
||||
}
|
||||
@@ -0,0 +1,81 @@
|
||||
package repository
|
||||
|
||||
import "github.com/caos/zitadel/internal/errors"
|
||||
|
||||
type SearchQuery struct {
|
||||
Columns Columns
|
||||
Limit uint64
|
||||
Desc bool
|
||||
Filters []*Filter
|
||||
}
|
||||
|
||||
type Columns int32
|
||||
|
||||
const (
|
||||
Columns_Event = iota
|
||||
Columns_Max_Sequence
|
||||
//insert new columns-types above this ColumnsCount because count is needed for validation
|
||||
ColumnsCount
|
||||
)
|
||||
|
||||
type Filter struct {
|
||||
field Field
|
||||
value interface{}
|
||||
operation Operation
|
||||
}
|
||||
|
||||
type Operation int32
|
||||
|
||||
const (
|
||||
Operation_Equals Operation = 1 + iota
|
||||
Operation_Greater
|
||||
Operation_Less
|
||||
Operation_In
|
||||
)
|
||||
|
||||
type Field int32
|
||||
|
||||
const (
|
||||
Field_AggregateType Field = 1 + iota
|
||||
Field_AggregateID
|
||||
Field_LatestSequence
|
||||
Field_ResourceOwner
|
||||
Field_EditorService
|
||||
Field_EditorUser
|
||||
Field_EventType
|
||||
)
|
||||
|
||||
//NewFilter is used in tests. Use searchQuery.*Filter() instead
|
||||
func NewFilter(field Field, value interface{}, operation Operation) *Filter {
|
||||
return &Filter{
|
||||
field: field,
|
||||
value: value,
|
||||
operation: operation,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *Filter) Field() Field {
|
||||
return f.field
|
||||
}
|
||||
func (f *Filter) Operation() Operation {
|
||||
return f.operation
|
||||
}
|
||||
func (f *Filter) Value() interface{} {
|
||||
return f.value
|
||||
}
|
||||
|
||||
func (f *Filter) Validate() error {
|
||||
if f == nil {
|
||||
return errors.ThrowPreconditionFailed(nil, "REPO-z6KcG", "filter is nil")
|
||||
}
|
||||
if f.field <= 0 {
|
||||
return errors.ThrowPreconditionFailed(nil, "REPO-zw62U", "field not definded")
|
||||
}
|
||||
if f.value == nil {
|
||||
return errors.ThrowPreconditionFailed(nil, "REPO-GJ9ct", "no value definded")
|
||||
}
|
||||
if f.operation <= 0 {
|
||||
return errors.ThrowPreconditionFailed(nil, "REPO-RrQTy", "operation not definded")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestNewFilter(t *testing.T) {
|
||||
type args struct {
|
||||
field Field
|
||||
value interface{}
|
||||
operation Operation
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
want *Filter
|
||||
}{
|
||||
{
|
||||
name: "aggregateID equals",
|
||||
args: args{
|
||||
field: Field_AggregateID,
|
||||
value: "hodor",
|
||||
operation: Operation_Equals,
|
||||
},
|
||||
want: &Filter{field: Field_AggregateID, operation: Operation_Equals, value: "hodor"},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if got := NewFilter(tt.args.field, tt.args.value, tt.args.operation); !reflect.DeepEqual(got, tt.want) {
|
||||
t.Errorf("NewFilter() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestFilter_Validate(t *testing.T) {
|
||||
type fields struct {
|
||||
field Field
|
||||
value interface{}
|
||||
operation Operation
|
||||
isNil bool
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
fields fields
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
name: "correct filter",
|
||||
fields: fields{
|
||||
field: Field_LatestSequence,
|
||||
operation: Operation_Greater,
|
||||
value: uint64(235),
|
||||
},
|
||||
wantErr: false,
|
||||
},
|
||||
{
|
||||
name: "filter is nil",
|
||||
fields: fields{isNil: true},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no field error",
|
||||
fields: fields{
|
||||
operation: Operation_Greater,
|
||||
value: uint64(235),
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no value error",
|
||||
fields: fields{
|
||||
field: Field_LatestSequence,
|
||||
operation: Operation_Greater,
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
{
|
||||
name: "no operation error",
|
||||
fields: fields{
|
||||
field: Field_LatestSequence,
|
||||
value: uint64(235),
|
||||
},
|
||||
wantErr: true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
var f *Filter
|
||||
if !tt.fields.isNil {
|
||||
f = &Filter{
|
||||
field: tt.fields.field,
|
||||
value: tt.fields.value,
|
||||
operation: tt.fields.operation,
|
||||
}
|
||||
}
|
||||
if err := f.Validate(); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Filter.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"regexp"
|
||||
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
)
|
||||
|
||||
var versionRegexp = regexp.MustCompile(`^v[0-9]+(\.[0-9]+){0,2}$`)
|
||||
|
||||
type Version string
|
||||
|
||||
func (v Version) Validate() error {
|
||||
if !versionRegexp.MatchString(string(v)) {
|
||||
return errors.ThrowPreconditionFailed(nil, "MODEL-luDuS", "version is not semver")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package repository
|
||||
|
||||
import "testing"
|
||||
|
||||
func TestVersion_Validate(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
v Version
|
||||
wantErr bool
|
||||
}{
|
||||
{
|
||||
"correct version",
|
||||
"v1.23.23",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"no v prefix",
|
||||
"1.2.2",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"letters in version",
|
||||
"v1.as.3",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"no version",
|
||||
"",
|
||||
true,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
if err := tt.v.Validate(); (err != nil) != tt.wantErr {
|
||||
t.Errorf("Version.Validate() error = %v, wantErr %v", err, tt.wantErr)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user