mirror of
https://github.com/zitadel/zitadel.git
synced 2025-12-23 13:46:47 +00:00
# Which Problems Are Solved Relational table's primary keys are part of the SQL table definition. However, by abstraction through the repository interfaces callers (service layer) have no clear understanding of the primary key definion. This might lead to confusion during implementation: - The repository makes a sanity check on required conditions before query execution and returns an error if the condition was not met. - When a Get method is executed, an error is returned if not exactly one row is found. Get replaces the legacy GetXxxByID methods we have on projections. However, a list of flexible conditions can be passed. - Operations like UPDATE and DELETE should often only affect a single row, identified by the primary key. If the primary key conditions are missing an error is returned. Because there is no "type safety" enforced here, the errors are returned during runtime. When all possible combinations of API arguments are not properly tested such errors are actually bugs in the experience of the end-user. (The API should do it's own input validation, but a programmer's error is easily made). # How the Problems Are Solved Add `PrimaryKeyColumns` and `PrimaryKeyCondition` to existing repository intefaces. # Additional Changes - Where reducers already used repositories, the PrimaryKeyConditions are used where possible. # Additional Context - Part of https://github.com/zitadel/zitadel/wiki/Decision-Log#expose-primary-key-definition-in-repository-interfaces --------- Co-authored-by: Marco A. <marco@zitadel.com>
169 lines
6.0 KiB
Go
169 lines
6.0 KiB
Go
package projection
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/muhlemmer/gu"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/domain"
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
v3_sql "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql"
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database/repository"
|
|
"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/zerrors"
|
|
)
|
|
|
|
type instanceDomainRelationalProjection struct{}
|
|
|
|
func newInstanceDomainRelationalProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
|
return handler.NewHandler(ctx, &config, new(instanceDomainRelationalProjection))
|
|
}
|
|
|
|
func (*instanceDomainRelationalProjection) Name() string {
|
|
return "zitadel.instance_domains"
|
|
}
|
|
|
|
func (p *instanceDomainRelationalProjection) Reducers() []handler.AggregateReducer {
|
|
return []handler.AggregateReducer{
|
|
{
|
|
Aggregate: instance.AggregateType,
|
|
EventReducers: []handler.EventReducer{
|
|
{
|
|
Event: instance.InstanceDomainAddedEventType,
|
|
Reduce: p.reduceCustomDomainAdded,
|
|
},
|
|
{
|
|
Event: instance.InstanceDomainPrimarySetEventType,
|
|
Reduce: p.reduceDomainPrimarySet,
|
|
},
|
|
{
|
|
Event: instance.InstanceDomainRemovedEventType,
|
|
Reduce: p.reduceCustomDomainRemoved,
|
|
},
|
|
{
|
|
Event: instance.TrustedDomainAddedEventType,
|
|
Reduce: p.reduceTrustedDomainAdded,
|
|
},
|
|
{
|
|
Event: instance.TrustedDomainRemovedEventType,
|
|
Reduce: p.reduceTrustedDomainRemoved,
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *instanceDomainRelationalProjection) reduceCustomDomainAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*instance.DomainAddedEvent)
|
|
if !ok {
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-DU0xF", "reduce.wrong.event.type %s", instance.InstanceDomainAddedEventType)
|
|
}
|
|
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
|
tx, ok := ex.(*sql.Tx)
|
|
if !ok {
|
|
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-bXCa6", "reduce.wrong.db.pool %T", ex)
|
|
}
|
|
return repository.InstanceDomainRepository().Add(ctx, v3_sql.SQLTx(tx), &domain.AddInstanceDomain{
|
|
InstanceID: e.Aggregate().InstanceID,
|
|
Domain: e.Domain,
|
|
IsPrimary: gu.Ptr(false),
|
|
IsGenerated: &e.Generated,
|
|
Type: domain.DomainTypeCustom,
|
|
CreatedAt: e.CreationDate(),
|
|
UpdatedAt: e.CreationDate(),
|
|
})
|
|
}), nil
|
|
}
|
|
|
|
func (p *instanceDomainRelationalProjection) reduceDomainPrimarySet(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*instance.DomainPrimarySetEvent)
|
|
if !ok {
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-TdEWA", "reduce.wrong.event.type %s", instance.InstanceDomainPrimarySetEventType)
|
|
}
|
|
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
|
tx, ok := ex.(*sql.Tx)
|
|
if !ok {
|
|
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-QnjHo", "reduce.wrong.db.pool %T", ex)
|
|
}
|
|
domainRepo := repository.InstanceDomainRepository()
|
|
|
|
_, err := domainRepo.Update(ctx, v3_sql.SQLTx(tx),
|
|
database.And(
|
|
domainRepo.PrimaryKeyCondition(e.Domain),
|
|
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
|
domainRepo.TypeCondition(domain.DomainTypeCustom),
|
|
),
|
|
domainRepo.SetPrimary(),
|
|
domainRepo.SetUpdatedAt(e.CreationDate()),
|
|
)
|
|
return err
|
|
}), nil
|
|
}
|
|
|
|
func (p *instanceDomainRelationalProjection) reduceCustomDomainRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*instance.DomainRemovedEvent)
|
|
if !ok {
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Hhcdl", "reduce.wrong.event.type %s", instance.InstanceDomainRemovedEventType)
|
|
}
|
|
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
|
tx, ok := ex.(*sql.Tx)
|
|
if !ok {
|
|
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-58ghE", "reduce.wrong.db.pool %T", ex)
|
|
}
|
|
domainRepo := repository.InstanceDomainRepository()
|
|
_, err := domainRepo.Remove(ctx, v3_sql.SQLTx(tx),
|
|
database.And(
|
|
domainRepo.PrimaryKeyCondition(e.Domain),
|
|
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
|
domainRepo.TypeCondition(domain.DomainTypeCustom),
|
|
),
|
|
)
|
|
return err
|
|
}), nil
|
|
}
|
|
|
|
func (p *instanceDomainRelationalProjection) reduceTrustedDomainAdded(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*instance.TrustedDomainAddedEvent)
|
|
if !ok {
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-svHDh", "reduce.wrong.event.type %s", instance.TrustedDomainAddedEventType)
|
|
}
|
|
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
|
tx, ok := ex.(*sql.Tx)
|
|
if !ok {
|
|
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-gx7tQ", "reduce.wrong.db.pool %T", ex)
|
|
}
|
|
return repository.InstanceDomainRepository().Add(ctx, v3_sql.SQLTx(tx), &domain.AddInstanceDomain{
|
|
InstanceID: e.Aggregate().InstanceID,
|
|
Domain: e.Domain,
|
|
Type: domain.DomainTypeTrusted,
|
|
CreatedAt: e.CreationDate(),
|
|
UpdatedAt: e.CreationDate(),
|
|
})
|
|
}), nil
|
|
}
|
|
|
|
func (p *instanceDomainRelationalProjection) reduceTrustedDomainRemoved(event eventstore.Event) (*handler.Statement, error) {
|
|
e, ok := event.(*instance.TrustedDomainRemovedEvent)
|
|
if !ok {
|
|
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-4K74E", "reduce.wrong.event.type %s", instance.TrustedDomainRemovedEventType)
|
|
}
|
|
return handler.NewStatement(e, func(ctx context.Context, ex handler.Executer, projectionName string) error {
|
|
tx, ok := ex.(*sql.Tx)
|
|
if !ok {
|
|
return zerrors.ThrowInvalidArgumentf(nil, "HANDL-D68ap", "reduce.wrong.db.pool %T", ex)
|
|
}
|
|
domainRepo := repository.InstanceDomainRepository()
|
|
_, err := domainRepo.Remove(ctx, v3_sql.SQLTx(tx),
|
|
database.And(
|
|
domainRepo.PrimaryKeyCondition(e.Domain),
|
|
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
|
domainRepo.TypeCondition(domain.DomainTypeTrusted),
|
|
),
|
|
)
|
|
return err
|
|
}), nil
|
|
}
|