mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:27:42 +00:00
Merge branch 'main' into clean-transactional-propsal
This commit is contained in:
@@ -99,7 +99,7 @@ func assertReduce(t *testing.T, stmt *handler.Statement, err error, projection s
|
||||
want.executer.Validate(t)
|
||||
return
|
||||
}
|
||||
err = stmt.Execute(want.executer, projection)
|
||||
err = stmt.Execute(t.Context(), want.executer, projection)
|
||||
if err != nil {
|
||||
t.Errorf("unexpected error: %v", err)
|
||||
}
|
||||
|
141
internal/query/projection/organization_settings.go
Normal file
141
internal/query/projection/organization_settings.go
Normal file
@@ -0,0 +1,141 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"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"
|
||||
settings "github.com/zitadel/zitadel/internal/repository/organization_settings"
|
||||
)
|
||||
|
||||
const (
|
||||
OrganizationSettingsTable = "projections.organization_settings"
|
||||
OrganizationSettingsIDCol = "id"
|
||||
OrganizationSettingsCreationDateCol = "creation_date"
|
||||
OrganizationSettingsChangeDateCol = "change_date"
|
||||
OrganizationSettingsResourceOwnerCol = "resource_owner"
|
||||
OrganizationSettingsInstanceIDCol = "instance_id"
|
||||
OrganizationSettingsSequenceCol = "sequence"
|
||||
OrganizationSettingsOrganizationScopedUsernamesCol = "organization_scoped_usernames"
|
||||
)
|
||||
|
||||
type organizationSettingsProjection struct{}
|
||||
|
||||
func newOrganizationSettingsProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, new(organizationSettingsProjection))
|
||||
}
|
||||
|
||||
func (*organizationSettingsProjection) Name() string {
|
||||
return OrganizationSettingsTable
|
||||
}
|
||||
|
||||
func (*organizationSettingsProjection) Init() *old_handler.Check {
|
||||
return handler.NewTableCheck(
|
||||
handler.NewTable([]*handler.InitColumn{
|
||||
handler.NewColumn(OrganizationSettingsIDCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(OrganizationSettingsCreationDateCol, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(OrganizationSettingsChangeDateCol, handler.ColumnTypeTimestamp),
|
||||
handler.NewColumn(OrganizationSettingsResourceOwnerCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(OrganizationSettingsInstanceIDCol, handler.ColumnTypeText),
|
||||
handler.NewColumn(OrganizationSettingsSequenceCol, handler.ColumnTypeInt64),
|
||||
handler.NewColumn(OrganizationSettingsOrganizationScopedUsernamesCol, handler.ColumnTypeBool),
|
||||
},
|
||||
handler.NewPrimaryKey(OrganizationSettingsInstanceIDCol, OrganizationSettingsResourceOwnerCol, OrganizationSettingsIDCol),
|
||||
handler.WithIndex(handler.NewIndex("resource_owner", []string{OrganizationSettingsResourceOwnerCol})),
|
||||
),
|
||||
)
|
||||
}
|
||||
|
||||
func (p *organizationSettingsProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: settings.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: settings.OrganizationSettingsSetEventType,
|
||||
Reduce: p.reduceOrganizationSettingsSet,
|
||||
},
|
||||
{
|
||||
Event: settings.OrganizationSettingsRemovedEventType,
|
||||
Reduce: p.reduceOrganizationSettingsRemoved,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Aggregate: org.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: org.OrgRemovedEventType,
|
||||
Reduce: p.reduceOrgRemoved,
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
Aggregate: instance.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: instance.InstanceRemovedEventType,
|
||||
Reduce: reduceInstanceRemovedHelper(OrganizationSettingsInstanceIDCol),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *organizationSettingsProjection) reduceOrganizationSettingsSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*settings.OrganizationSettingsSetEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return handler.NewUpsertStatement(e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(OrganizationSettingsInstanceIDCol, e.Aggregate().InstanceID),
|
||||
handler.NewCol(OrganizationSettingsResourceOwnerCol, e.Aggregate().ResourceOwner),
|
||||
handler.NewCol(OrganizationSettingsIDCol, e.Aggregate().ID),
|
||||
},
|
||||
[]handler.Column{
|
||||
handler.NewCol(OrganizationSettingsInstanceIDCol, e.Aggregate().InstanceID),
|
||||
handler.NewCol(OrganizationSettingsResourceOwnerCol, e.Aggregate().ResourceOwner),
|
||||
handler.NewCol(OrganizationSettingsIDCol, e.Aggregate().ID),
|
||||
handler.NewCol(OrganizationSettingsCreationDateCol, handler.OnlySetValueOnInsert(OrganizationSettingsTable, e.CreationDate())),
|
||||
handler.NewCol(OrganizationSettingsChangeDateCol, e.CreationDate()),
|
||||
handler.NewCol(OrganizationSettingsSequenceCol, e.Sequence()),
|
||||
handler.NewCol(OrganizationSettingsOrganizationScopedUsernamesCol, e.OrganizationScopedUsernames),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *organizationSettingsProjection) reduceOrganizationSettingsRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*settings.OrganizationSettingsRemovedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return handler.NewDeleteStatement(e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(OrganizationSettingsInstanceIDCol, e.Aggregate().InstanceID),
|
||||
handler.NewCond(OrganizationSettingsResourceOwnerCol, e.Aggregate().ResourceOwner),
|
||||
handler.NewCond(OrganizationSettingsIDCol, e.Aggregate().ID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *organizationSettingsProjection) reduceOrgRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, err := assertEvent[*org.OrgRemovedEvent](event)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return handler.NewDeleteStatement(
|
||||
e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(OrganizationSettingsInstanceIDCol, e.Aggregate().InstanceID),
|
||||
handler.NewCond(OrganizationSettingsResourceOwnerCol, e.Aggregate().ResourceOwner),
|
||||
handler.NewCond(OrganizationSettingsIDCol, e.Aggregate().ID),
|
||||
},
|
||||
), nil
|
||||
}
|
154
internal/query/projection/organization_settings_test.go
Normal file
154
internal/query/projection/organization_settings_test.go
Normal file
@@ -0,0 +1,154 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
||||
"github.com/zitadel/zitadel/internal/repository/instance"
|
||||
"github.com/zitadel/zitadel/internal/repository/org"
|
||||
settings "github.com/zitadel/zitadel/internal/repository/organization_settings"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
func TestOrganizationSettingsProjection_reduces(t *testing.T) {
|
||||
type args struct {
|
||||
event func(t *testing.T) eventstore.Event
|
||||
}
|
||||
tests := []struct {
|
||||
name string
|
||||
args args
|
||||
reduce func(event eventstore.Event) (*handler.Statement, error)
|
||||
want wantReduce
|
||||
}{
|
||||
{
|
||||
name: "reduce organization settings set",
|
||||
args: args{
|
||||
event: getEvent(
|
||||
testEvent(
|
||||
settings.OrganizationSettingsSetEventType,
|
||||
settings.AggregateType,
|
||||
[]byte(`{"organizationScopedUsernames": true}`),
|
||||
), eventstore.GenericEventMapper[settings.OrganizationSettingsSetEvent],
|
||||
),
|
||||
},
|
||||
reduce: (&organizationSettingsProjection{}).reduceOrganizationSettingsSet,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("organization_settings"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "INSERT INTO projections.organization_settings (instance_id, resource_owner, id, creation_date, change_date, sequence, organization_scoped_usernames) VALUES ($1, $2, $3, $4, $5, $6, $7) ON CONFLICT (instance_id, resource_owner, id) DO UPDATE SET (creation_date, change_date, sequence, organization_scoped_usernames) = (projections.organization_settings.creation_date, EXCLUDED.change_date, EXCLUDED.sequence, EXCLUDED.organization_scoped_usernames)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
"ro-id",
|
||||
"agg-id",
|
||||
anyArg{},
|
||||
anyArg{},
|
||||
uint64(15),
|
||||
true,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduce organization settings removed",
|
||||
args: args{
|
||||
event: getEvent(
|
||||
testEvent(
|
||||
settings.OrganizationSettingsRemovedEventType,
|
||||
settings.AggregateType,
|
||||
[]byte(`{}`),
|
||||
), eventstore.GenericEventMapper[settings.OrganizationSettingsRemovedEvent],
|
||||
),
|
||||
},
|
||||
reduce: (&organizationSettingsProjection{}).reduceOrganizationSettingsRemoved,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("organization_settings"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM projections.organization_settings WHERE (instance_id = $1) AND (resource_owner = $2) AND (id = $3)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
"ro-id",
|
||||
"agg-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "reduceOrgRemoved",
|
||||
args: args{
|
||||
event: getEvent(
|
||||
testEvent(
|
||||
org.OrgRemovedEventType,
|
||||
org.AggregateType,
|
||||
nil,
|
||||
), org.OrgRemovedEventMapper),
|
||||
},
|
||||
reduce: (&organizationSettingsProjection{}).reduceOrgRemoved,
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("org"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM projections.organization_settings WHERE (instance_id = $1) AND (resource_owner = $2) AND (id = $3)",
|
||||
expectedArgs: []interface{}{
|
||||
"instance-id",
|
||||
"ro-id",
|
||||
"agg-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
{
|
||||
name: "instance reduceInstanceRemoved",
|
||||
args: args{
|
||||
event: getEvent(
|
||||
testEvent(
|
||||
instance.InstanceRemovedEventType,
|
||||
instance.AggregateType,
|
||||
nil,
|
||||
), instance.InstanceRemovedEventMapper),
|
||||
},
|
||||
reduce: reduceInstanceRemovedHelper(OrganizationSettingsInstanceIDCol),
|
||||
want: wantReduce{
|
||||
aggregateType: eventstore.AggregateType("instance"),
|
||||
sequence: 15,
|
||||
executer: &testExecuter{
|
||||
executions: []execution{
|
||||
{
|
||||
expectedStmt: "DELETE FROM projections.organization_settings WHERE (instance_id = $1)",
|
||||
expectedArgs: []interface{}{
|
||||
"agg-id",
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
event := baseEvent(t)
|
||||
got, err := tt.reduce(event)
|
||||
if ok := zerrors.IsErrorInvalidArgument(err); !ok {
|
||||
t.Errorf("no wrong event mapping: %v, got: %v", err, got)
|
||||
}
|
||||
|
||||
event = tt.args.event(t)
|
||||
got, err = tt.reduce(event)
|
||||
assertReduce(t, got, err, OrganizationSettingsTable, tt.want)
|
||||
})
|
||||
}
|
||||
}
|
@@ -89,6 +89,7 @@ var (
|
||||
WebKeyProjection *handler.Handler
|
||||
DebugEventsProjection *handler.Handler
|
||||
HostedLoginTranslationProjection *handler.Handler
|
||||
OrganizationSettingsProjection *handler.Handler
|
||||
|
||||
ProjectGrantFields *handler.FieldHandler
|
||||
OrgDomainVerifiedFields *handler.FieldHandler
|
||||
@@ -185,6 +186,7 @@ func Create(ctx context.Context, sqlClient *database.DB, es handler.EventStore,
|
||||
WebKeyProjection = newWebKeyProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["web_keys"]))
|
||||
DebugEventsProjection = newDebugEventsProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["debug_events"]))
|
||||
HostedLoginTranslationProjection = newHostedLoginTranslationProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["hosted_login_translation"]))
|
||||
OrganizationSettingsProjection = newOrganizationSettingsProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["organization_settings"]))
|
||||
|
||||
ProjectGrantFields = newFillProjectGrantFields(applyCustomConfig(projectionConfig, config.Customizations[fieldsProjectGrant]))
|
||||
OrgDomainVerifiedFields = newFillOrgDomainVerifiedFields(applyCustomConfig(projectionConfig, config.Customizations[fieldsOrgDomainVerified]))
|
||||
@@ -366,5 +368,6 @@ func newProjectionsList() {
|
||||
WebKeyProjection,
|
||||
DebugEventsProjection,
|
||||
HostedLoginTranslationProjection,
|
||||
OrganizationSettingsProjection,
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user