mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-21 23:37:31 +00:00
b5564572bc
This implementation increases parallel write capabilities of the eventstore. Please have a look at the technical advisories: [05](https://zitadel.com/docs/support/advisory/a10005) and [06](https://zitadel.com/docs/support/advisory/a10006). The implementation of eventstore.push is rewritten and stored events are migrated to a new table `eventstore.events2`. If you are using cockroach: make sure that the database user of ZITADEL has `VIEWACTIVITY` grant. This is used to query events.
174 lines
5.9 KiB
Go
174 lines
5.9 KiB
Go
package projection
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
old_handler "github.com/zitadel/zitadel/internal/eventstore/handler"
|
|
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
|
"github.com/zitadel/zitadel/internal/repository/instance"
|
|
"github.com/zitadel/zitadel/internal/repository/org"
|
|
"github.com/zitadel/zitadel/internal/repository/user"
|
|
)
|
|
|
|
const (
|
|
UserMetadataProjectionTable = "projections.user_metadata4"
|
|
|
|
UserMetadataColumnUserID = "user_id"
|
|
UserMetadataColumnCreationDate = "creation_date"
|
|
UserMetadataColumnChangeDate = "change_date"
|
|
UserMetadataColumnSequence = "sequence"
|
|
UserMetadataColumnResourceOwner = "resource_owner"
|
|
UserMetadataColumnInstanceID = "instance_id"
|
|
UserMetadataColumnKey = "key"
|
|
UserMetadataColumnValue = "value"
|
|
UserMetadataColumnOwnerRemoved = "owner_removed"
|
|
)
|
|
|
|
type userMetadataProjection struct{}
|
|
|
|
func newUserMetadataProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
return handler.NewHandler(ctx, &config, new(userMetadataProjection))
|
|
}
|
|
|
|
func (*userMetadataProjection) Name() string {
|
|
return UserMetadataProjectionTable
|
|
}
|
|
|
|
func (*userMetadataProjection) Init() *old_handler.Check {
|
|
return handler.NewTableCheck(
|
|
handler.NewTable([]*handler.InitColumn{
|
|
handler.NewColumn(UserMetadataColumnUserID, handler.ColumnTypeText),
|
|
handler.NewColumn(UserMetadataColumnCreationDate, handler.ColumnTypeTimestamp),
|
|
handler.NewColumn(UserMetadataColumnChangeDate, handler.ColumnTypeTimestamp),
|
|
handler.NewColumn(UserMetadataColumnSequence, handler.ColumnTypeInt64),
|
|
handler.NewColumn(UserMetadataColumnResourceOwner, handler.ColumnTypeText),
|
|
handler.NewColumn(UserMetadataColumnInstanceID, handler.ColumnTypeText),
|
|
handler.NewColumn(UserMetadataColumnKey, handler.ColumnTypeText),
|
|
handler.NewColumn(UserMetadataColumnValue, handler.ColumnTypeBytes, handler.Nullable()),
|
|
handler.NewColumn(UserMetadataColumnOwnerRemoved, handler.ColumnTypeBool, handler.Default(false)),
|
|
},
|
|
handler.NewPrimaryKey(UserMetadataColumnInstanceID, UserMetadataColumnUserID, UserMetadataColumnKey),
|
|
handler.WithIndex(handler.NewIndex("resource_owner", []string{UserGrantResourceOwner})),
|
|
handler.WithIndex(handler.NewIndex("owner_removed", []string{UserMetadataColumnOwnerRemoved})),
|
|
),
|
|
)
|
|
}
|
|
|
|
func (p *userMetadataProjection) Reducers() []handler.AggregateReducer {
|
|
return []handler.AggregateReducer{
|
|
{
|
|
Aggregate: user.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: user.MetadataSetType,
|
|
Reduce: p.reduceMetadataSet,
|
|
},
|
|
{
|
|
Event: user.MetadataRemovedType,
|
|
Reduce: p.reduceMetadataRemoved,
|
|
},
|
|
{
|
|
Event: user.MetadataRemovedAllType,
|
|
Reduce: p.reduceMetadataRemovedAll,
|
|
},
|
|
{
|
|
Event: user.UserRemovedType,
|
|
Reduce: p.reduceMetadataRemovedAll,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: org.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: org.OrgRemovedEventType,
|
|
Reduce: p.reduceOwnerRemoved,
|
|
},
|
|
},
|
|
},
|
|
{
|
|
Aggregate: instance.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: instance.InstanceRemovedEventType,
|
|
Reduce: reduceInstanceRemovedHelper(UserMetadataColumnInstanceID),
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *userMetadataProjection) reduceMetadataSet(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*user.MetadataSetEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Ghn52", "reduce.wrong.event.type %s", user.MetadataSetType)
|
|
}
|
|
return handler.NewUpsertStatement(
|
|
e,
|
|
[]handler.Column{
|
|
handler.NewCol(UserMetadataColumnInstanceID, nil),
|
|
handler.NewCol(UserMetadataColumnUserID, nil),
|
|
handler.NewCol(UserMetadataColumnKey, e.Key),
|
|
},
|
|
[]handler.Column{
|
|
handler.NewCol(UserMetadataColumnInstanceID, e.Aggregate().InstanceID),
|
|
handler.NewCol(UserMetadataColumnUserID, e.Aggregate().ID),
|
|
handler.NewCol(UserMetadataColumnKey, e.Key),
|
|
handler.NewCol(UserMetadataColumnResourceOwner, e.Aggregate().ResourceOwner),
|
|
handler.NewCol(UserMetadataColumnCreationDate, e.CreationDate()),
|
|
handler.NewCol(UserMetadataColumnChangeDate, e.CreationDate()),
|
|
handler.NewCol(UserMetadataColumnSequence, e.Sequence()),
|
|
handler.NewCol(UserMetadataColumnValue, e.Value),
|
|
},
|
|
), nil
|
|
}
|
|
|
|
func (p *userMetadataProjection) reduceMetadataRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*user.MetadataRemovedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Bm542", "reduce.wrong.event.type %s", user.MetadataRemovedType)
|
|
}
|
|
return handler.NewDeleteStatement(
|
|
e,
|
|
[]handler.Condition{
|
|
handler.NewCond(UserMetadataColumnUserID, e.Aggregate().ID),
|
|
handler.NewCond(UserMetadataColumnKey, e.Key),
|
|
handler.NewCond(UserAuthMethodInstanceIDCol, e.Aggregate().InstanceID),
|
|
},
|
|
), nil
|
|
}
|
|
|
|
func (p *userMetadataProjection) reduceMetadataRemovedAll(event eventstore.Event) (*handler.Statement, error) {
|
|
switch event.(type) {
|
|
case *user.MetadataRemovedAllEvent,
|
|
*user.UserRemovedEvent:
|
|
//ok
|
|
default:
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-Bmnf2", "reduce.wrong.event.type %v", []eventstore.EventType{user.MetadataRemovedAllType, user.UserRemovedType})
|
|
}
|
|
return handler.NewDeleteStatement(
|
|
event,
|
|
[]handler.Condition{
|
|
handler.NewCond(UserMetadataColumnUserID, event.Aggregate().ID),
|
|
handler.NewCond(UserAuthMethodInstanceIDCol, event.Aggregate().InstanceID),
|
|
},
|
|
), nil
|
|
}
|
|
|
|
func (p *userMetadataProjection) reduceOwnerRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*org.OrgRemovedEvent)
|
|
if !ok {
|
|
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-oqwul", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
|
}
|
|
|
|
return handler.NewDeleteStatement(
|
|
e,
|
|
[]handler.Condition{
|
|
handler.NewCond(UserMetadataColumnInstanceID, e.Aggregate().InstanceID),
|
|
handler.NewCond(UserMetadataColumnResourceOwner, e.Aggregate().ID),
|
|
},
|
|
), nil
|
|
}
|