mirror of
https://github.com/andwati/zitadel.git
synced 2026-11-20 03:48:47 +00:00
feat(backend): state persisted objects (#9870)
This PR initiates the rework of Zitadel's backend to state-persisted objects. This change is a step towards a more scalable and maintainable architecture. ## Changes * **New `/backend/v3` package**: A new package structure has been introduced to house the reworked backend logic. This includes: * `domain`: Contains the core business logic, commands, and repository interfaces. * `storage`: Implements the repository interfaces for database interactions with new transactional tables. * `telemetry`: Provides logging and tracing capabilities. * **Transactional Tables**: New database tables have been defined for `instances`, `instance_domains`, `organizations`, and `org_domains`. * **Projections**: New projections have been created to populate the new relational tables from the existing event store, ensuring data consistency during the migration. * **Repositories**: New repositories provide an abstraction layer for accessing and manipulating the data in the new tables. * **Setup**: A new setup step for `TransactionalTables` has been added to manage the database migrations for the new tables. This PR lays the foundation for future work to fully transition to state-persisted objects for these components, which will improve performance and simplify data access patterns. This PR initiates the rework of ZITADEL's backend to state-persisted objects. This is a foundational step towards a new architecture that will improve performance and maintainability. The following objects are migrated from event-sourced aggregates to state-persisted objects: * Instances * incl. Domains * Orgs * incl. Domains The structure of the new backend implementation follows the software architecture defined in this [wiki page](https://github.com/zitadel/zitadel/wiki/Software-Architecturel). This PR includes: * The initial implementation of the new transactional repositories for the objects listed above. * Projections to populate the new relational tables from the existing event store. * Adjustments to the build and test process to accommodate the new backend structure. This is a work in progress and further changes will be made to complete the migration. --------- Co-authored-by: Copilot <[email protected]> Co-authored-by: Iraq Jaber <[email protected]> Co-authored-by: Iraq <[email protected]> Co-authored-by: Tim Möhlmann <[email protected]>
This commit is contained in:
co-authored by
Copilot
Iraq Jaber
Iraq
Tim Möhlmann
parent
8cc79c1376
commit
61cab8878e
@@ -0,0 +1,178 @@
|
||||
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.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false).Add(ctx, &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.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false)
|
||||
|
||||
condition := database.And(
|
||||
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
||||
domainRepo.DomainCondition(database.TextOperationEqual, e.Domain),
|
||||
domainRepo.TypeCondition(domain.DomainTypeCustom),
|
||||
)
|
||||
|
||||
_, err := domainRepo.Update(ctx,
|
||||
condition,
|
||||
domainRepo.SetPrimary(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// we need to split the update into two statements because multiple events can have the same creation date
|
||||
// therefore we first do not set the updated_at timestamp
|
||||
_, err = domainRepo.Update(ctx,
|
||||
condition,
|
||||
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.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false)
|
||||
_, err := domainRepo.Remove(ctx,
|
||||
database.And(
|
||||
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
||||
domainRepo.DomainCondition(database.TextOperationEqual, e.Domain),
|
||||
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.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false).Add(ctx, &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.InstanceRepository(v3_sql.SQLTx(tx)).Domains(false)
|
||||
_, err := domainRepo.Remove(ctx,
|
||||
database.And(
|
||||
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
||||
domainRepo.DomainCondition(database.TextOperationEqual, e.Domain),
|
||||
domainRepo.TypeCondition(domain.DomainTypeTrusted),
|
||||
),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
@@ -0,0 +1,200 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"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"
|
||||
)
|
||||
|
||||
const InstanceRelationalProjectionTable = "zitadel.instances"
|
||||
|
||||
type instanceRelationalProjection struct{}
|
||||
|
||||
func newInstanceRelationalProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, new(instanceRelationalProjection))
|
||||
}
|
||||
|
||||
func (*instanceRelationalProjection) Name() string {
|
||||
return InstanceRelationalProjectionTable
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: instance.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: instance.InstanceAddedEventType,
|
||||
Reduce: p.reduceInstanceAdded,
|
||||
},
|
||||
{
|
||||
Event: instance.InstanceChangedEventType,
|
||||
Reduce: p.reduceInstanceChanged,
|
||||
},
|
||||
{
|
||||
Event: instance.InstanceRemovedEventType,
|
||||
Reduce: p.reduceInstanceDelete,
|
||||
},
|
||||
{
|
||||
Event: instance.DefaultOrgSetEventType,
|
||||
Reduce: p.reduceDefaultOrgSet,
|
||||
},
|
||||
{
|
||||
Event: instance.ProjectSetEventType,
|
||||
Reduce: p.reduceIAMProjectSet,
|
||||
},
|
||||
{
|
||||
Event: instance.ConsoleSetEventType,
|
||||
Reduce: p.reduceConsoleSet,
|
||||
},
|
||||
{
|
||||
Event: instance.DefaultLanguageSetEventType,
|
||||
Reduce: p.reduceDefaultLanguageSet,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) reduceInstanceAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*instance.InstanceAddedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-29nRr", "reduce.wrong.event.type %s", instance.InstanceAddedEventType)
|
||||
}
|
||||
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-rVUyy", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
return repository.InstanceRepository(v3_sql.SQLTx(tx)).Create(ctx, &domain.Instance{
|
||||
ID: e.Aggregate().ID,
|
||||
Name: e.Name,
|
||||
CreatedAt: e.CreationDate(),
|
||||
UpdatedAt: e.CreationDate(),
|
||||
})
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) reduceInstanceChanged(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*instance.InstanceChangedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-so2am1", "reduce.wrong.event.type %s", instance.InstanceChangedEventType)
|
||||
}
|
||||
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-rVUyy", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.InstanceRepository(v3_sql.SQLTx(tx))
|
||||
return p.updateInstance(ctx, event, repo, repo.SetName(e.Name))
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) reduceInstanceDelete(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*instance.InstanceRemovedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-so2am1", "reduce.wrong.event.type %s", instance.InstanceChangedEventType)
|
||||
}
|
||||
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-rVUyy", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
_, err := repository.InstanceRepository(v3_sql.SQLTx(tx)).Delete(ctx, e.Aggregate().ID)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) reduceDefaultOrgSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*instance.DefaultOrgSetEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-2n9f2", "reduce.wrong.event.type %s", instance.DefaultOrgSetEventType)
|
||||
}
|
||||
|
||||
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-rVUyy", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.InstanceRepository(v3_sql.SQLTx(tx))
|
||||
return p.updateInstance(ctx, event, repo, repo.SetDefaultOrg(e.OrgID))
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) reduceIAMProjectSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*instance.ProjectSetEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-30o0e", "reduce.wrong.event.type %s", instance.ProjectSetEventType)
|
||||
}
|
||||
|
||||
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-rVUyy", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.InstanceRepository(v3_sql.SQLTx(tx))
|
||||
return p.updateInstance(ctx, event, repo, repo.SetIAMProject(e.ProjectID))
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) reduceConsoleSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*instance.ConsoleSetEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Dgf11", "reduce.wrong.event.type %s", instance.ConsoleSetEventType)
|
||||
}
|
||||
|
||||
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-rVUyy", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.InstanceRepository(v3_sql.SQLTx(tx))
|
||||
return p.updateInstance(ctx, event, repo, repo.SetConsoleClientID(e.ClientID), repo.SetConsoleAppID(e.AppID))
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) reduceDefaultLanguageSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*instance.DefaultLanguageSetEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-30o0e", "reduce.wrong.event.type %s", instance.DefaultLanguageSetEventType)
|
||||
}
|
||||
|
||||
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-rVUyy", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
repo := repository.InstanceRepository(v3_sql.SQLTx(tx))
|
||||
return p.updateInstance(ctx, event, repo, repo.SetDefaultLanguage(e.Language))
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *instanceRelationalProjection) updateInstance(ctx context.Context, event eventstore.Event, repo domain.InstanceRepository, changes ...database.Change) error {
|
||||
_, err := repo.Update(ctx, event.Aggregate().ID, changes...)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
instance, err := repo.Get(ctx, database.WithCondition(repo.IDCondition(event.Aggregate().ID)))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if instance.UpdatedAt.Equal(event.CreatedAt()) {
|
||||
return nil
|
||||
}
|
||||
// we need to split the update into two statements because multiple events can have the same creation date
|
||||
// therefore we first do not set the updated_at timestamp
|
||||
_, err = repo.Update(ctx,
|
||||
event.Aggregate().ID,
|
||||
repo.SetUpdatedAt(event.CreatedAt()),
|
||||
)
|
||||
return err
|
||||
}
|
||||
@@ -0,0 +1,210 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
|
||||
"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"
|
||||
old_domain "github.com/zitadel/zitadel/internal/domain"
|
||||
"github.com/zitadel/zitadel/internal/eventstore"
|
||||
"github.com/zitadel/zitadel/internal/eventstore/handler/v2"
|
||||
"github.com/zitadel/zitadel/internal/repository/org"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
type orgDomainRelationalProjection struct{}
|
||||
|
||||
func newOrgDomainRelationalProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, new(orgDomainRelationalProjection))
|
||||
}
|
||||
|
||||
func (*orgDomainRelationalProjection) Name() string {
|
||||
return "zitadel.org_domains"
|
||||
}
|
||||
|
||||
func (p *orgDomainRelationalProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: org.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: org.OrgDomainAddedEventType,
|
||||
Reduce: p.reduceAdded,
|
||||
},
|
||||
{
|
||||
Event: org.OrgDomainPrimarySetEventType,
|
||||
Reduce: p.reducePrimarySet,
|
||||
},
|
||||
{
|
||||
Event: org.OrgDomainRemovedEventType,
|
||||
Reduce: p.reduceRemoved,
|
||||
},
|
||||
{
|
||||
Event: org.OrgDomainVerificationAddedEventType,
|
||||
Reduce: p.reduceVerificationAdded,
|
||||
},
|
||||
{
|
||||
Event: org.OrgDomainVerifiedEventType,
|
||||
Reduce: p.reduceVerified,
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *orgDomainRelationalProjection) reduceAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.DomainAddedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-ZX9Fw", "reduce.wrong.event.type %s", org.OrgDomainAddedEventType)
|
||||
}
|
||||
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-kGokE", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
return repository.OrganizationRepository(v3_sql.SQLTx(tx)).Domains(false).Add(ctx, &domain.AddOrganizationDomain{
|
||||
InstanceID: e.Aggregate().InstanceID,
|
||||
OrgID: e.Aggregate().ResourceOwner,
|
||||
Domain: e.Domain,
|
||||
CreatedAt: e.CreationDate(),
|
||||
UpdatedAt: e.CreationDate(),
|
||||
})
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *orgDomainRelationalProjection) reducePrimarySet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.DomainPrimarySetEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-dmFdb", "reduce.wrong.event.type %s", org.OrgDomainPrimarySetEventType)
|
||||
}
|
||||
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-h6xF0", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
domainRepo := repository.OrganizationRepository(v3_sql.SQLTx(tx)).Domains(false)
|
||||
condition := database.And(
|
||||
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
||||
domainRepo.OrgIDCondition(e.Aggregate().ResourceOwner),
|
||||
domainRepo.DomainCondition(database.TextOperationEqual, e.Domain),
|
||||
)
|
||||
_, err := domainRepo.Update(ctx,
|
||||
condition,
|
||||
domainRepo.SetPrimary(),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// we need to split the update into two statements because multiple events can have the same creation date
|
||||
// therefore we first do not set the updated_at timestamp
|
||||
_, err = domainRepo.Update(ctx,
|
||||
condition,
|
||||
domainRepo.SetUpdatedAt(e.CreationDate()),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *orgDomainRelationalProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.DomainRemovedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-MzC0n", "reduce.wrong.event.type %s", org.OrgDomainRemovedEventType)
|
||||
}
|
||||
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-X8oS8", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
domainRepo := repository.OrganizationRepository(v3_sql.SQLTx(tx)).Domains(false)
|
||||
_, err := domainRepo.Remove(ctx,
|
||||
database.And(
|
||||
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
||||
domainRepo.OrgIDCondition(e.Aggregate().ResourceOwner),
|
||||
domainRepo.DomainCondition(database.TextOperationEqual, e.Domain),
|
||||
),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *orgDomainRelationalProjection) reduceVerificationAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.DomainVerificationAddedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-oGzip", "reduce.wrong.event.type %s", org.OrgDomainVerificationAddedEventType)
|
||||
}
|
||||
var validationType domain.DomainValidationType
|
||||
switch e.ValidationType {
|
||||
case old_domain.OrgDomainValidationTypeDNS:
|
||||
validationType = domain.DomainValidationTypeDNS
|
||||
case old_domain.OrgDomainValidationTypeHTTP:
|
||||
validationType = domain.DomainValidationTypeHTTP
|
||||
case old_domain.OrgDomainValidationTypeUnspecified:
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-FJfKB", "reduce.unsupported.validation.type %v", e.ValidationType)
|
||||
}
|
||||
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-yF03i", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
domainRepo := repository.OrganizationRepository(v3_sql.SQLTx(tx)).Domains(false)
|
||||
condition := database.And(
|
||||
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
||||
domainRepo.OrgIDCondition(e.Aggregate().ResourceOwner),
|
||||
domainRepo.DomainCondition(database.TextOperationEqual, e.Domain),
|
||||
)
|
||||
|
||||
_, err := domainRepo.Update(ctx,
|
||||
condition,
|
||||
domainRepo.SetValidationType(validationType),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// we need to split the update into two statements because multiple events can have the same creation date
|
||||
// therefore we first do not set the updated_at timestamp
|
||||
_, err = domainRepo.Update(ctx,
|
||||
condition,
|
||||
domainRepo.SetUpdatedAt(e.CreationDate()),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
|
||||
func (p *orgDomainRelationalProjection) reduceVerified(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.DomainVerifiedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-7WrI2", "reduce.wrong.event.type %s", org.OrgDomainVerifiedEventType)
|
||||
}
|
||||
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-0ZGqC", "reduce.wrong.db.pool %T", ex)
|
||||
}
|
||||
domainRepo := repository.OrganizationRepository(v3_sql.SQLTx(tx)).Domains(false)
|
||||
|
||||
condition := database.And(
|
||||
domainRepo.InstanceIDCondition(e.Aggregate().InstanceID),
|
||||
domainRepo.OrgIDCondition(e.Aggregate().ResourceOwner),
|
||||
domainRepo.DomainCondition(database.TextOperationEqual, e.Domain),
|
||||
)
|
||||
|
||||
_, err := domainRepo.Update(ctx,
|
||||
condition,
|
||||
domainRepo.SetVerified(),
|
||||
domainRepo.SetUpdatedAt(e.CreationDate()),
|
||||
)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// we need to split the update into two statements because multiple events can have the same creation date
|
||||
// therefore we first do not set the updated_at timestamp
|
||||
_, err = domainRepo.Update(ctx,
|
||||
condition,
|
||||
domainRepo.SetUpdatedAt(e.CreationDate()),
|
||||
)
|
||||
return err
|
||||
}), nil
|
||||
}
|
||||
@@ -0,0 +1,181 @@
|
||||
package projection
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
repoDomain "github.com/zitadel/zitadel/backend/v3/domain"
|
||||
"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"
|
||||
"github.com/zitadel/zitadel/internal/zerrors"
|
||||
)
|
||||
|
||||
const (
|
||||
OrgRelationProjectionTable = "zitadel.organizations"
|
||||
)
|
||||
|
||||
type orgRelationalProjection struct{}
|
||||
|
||||
func (*orgRelationalProjection) Name() string {
|
||||
return OrgRelationProjectionTable
|
||||
}
|
||||
|
||||
func newOrgRelationalProjection(ctx context.Context, config handler.Config) *handler.Handler {
|
||||
return handler.NewHandler(ctx, &config, new(orgRelationalProjection))
|
||||
}
|
||||
|
||||
func (p *orgRelationalProjection) Reducers() []handler.AggregateReducer {
|
||||
return []handler.AggregateReducer{
|
||||
{
|
||||
Aggregate: org.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: org.OrgAddedEventType,
|
||||
Reduce: p.reduceOrgRelationalAdded,
|
||||
},
|
||||
{
|
||||
Event: org.OrgChangedEventType,
|
||||
Reduce: p.reduceOrgRelationalChanged,
|
||||
},
|
||||
{
|
||||
Event: org.OrgDeactivatedEventType,
|
||||
Reduce: p.reduceOrgRelationalDeactivated,
|
||||
},
|
||||
{
|
||||
Event: org.OrgReactivatedEventType,
|
||||
Reduce: p.reduceOrgRelationalReactivated,
|
||||
},
|
||||
{
|
||||
Event: org.OrgRemovedEventType,
|
||||
Reduce: p.reduceOrgRelationalRemoved,
|
||||
},
|
||||
// TODO
|
||||
// {
|
||||
// Event: org.OrgDomainPrimarySetEventType,
|
||||
// Reduce: p.reducePrimaryDomainSetRelational,
|
||||
// },
|
||||
},
|
||||
},
|
||||
{
|
||||
Aggregate: instance.AggregateType,
|
||||
EventReducers: []handler.EventReducer{
|
||||
{
|
||||
Event: instance.InstanceRemovedEventType,
|
||||
Reduce: reduceInstanceRemovedHelper(OrgColumnInstanceID),
|
||||
},
|
||||
},
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func (p *orgRelationalProjection) reduceOrgRelationalAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.OrgAddedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-uYq5R", "reduce.wrong.event.type %s", org.OrgAddedEventType)
|
||||
}
|
||||
|
||||
return handler.NewCreateStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(OrgColumnID, e.Aggregate().ID),
|
||||
handler.NewCol(OrgColumnName, e.Name),
|
||||
handler.NewCol(OrgColumnInstanceID, e.Aggregate().InstanceID),
|
||||
handler.NewCol(State, repoDomain.OrgStateActive),
|
||||
handler.NewCol(CreatedAt, e.CreationDate()),
|
||||
handler.NewCol(UpdatedAt, e.CreationDate()),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *orgRelationalProjection) reduceOrgRelationalChanged(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.OrgChangedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-Bg9om", "reduce.wrong.event.type %s", org.OrgChangedEventType)
|
||||
}
|
||||
if e.Name == "" {
|
||||
return handler.NewNoOpStatement(e), nil
|
||||
}
|
||||
return handler.NewUpdateStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(OrgColumnName, e.Name),
|
||||
handler.NewCol(UpdatedAt, e.CreationDate()),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(OrgColumnID, e.Aggregate().ID),
|
||||
handler.NewCond(OrgColumnInstanceID, e.Aggregate().InstanceID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *orgRelationalProjection) reduceOrgRelationalDeactivated(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.OrgDeactivatedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-BApK5", "reduce.wrong.event.type %s", org.OrgDeactivatedEventType)
|
||||
}
|
||||
|
||||
return handler.NewUpdateStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(State, repoDomain.OrgStateInactive),
|
||||
handler.NewCol(UpdatedAt, e.CreationDate()),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(OrgColumnID, e.Aggregate().ID),
|
||||
handler.NewCond(OrgColumnInstanceID, e.Aggregate().InstanceID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
func (p *orgRelationalProjection) reduceOrgRelationalReactivated(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.OrgReactivatedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-o38DE", "reduce.wrong.event.type %s", org.OrgReactivatedEventType)
|
||||
}
|
||||
return handler.NewUpdateStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(State, repoDomain.OrgStateActive),
|
||||
handler.NewCol(UpdatedAt, e.CreationDate()),
|
||||
},
|
||||
[]handler.Condition{
|
||||
handler.NewCond(OrgColumnID, e.Aggregate().ID),
|
||||
handler.NewCond(OrgColumnInstanceID, e.Aggregate().InstanceID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
|
||||
// TODO
|
||||
// func (p *orgRelationalProjection) reducePrimaryDomainSetRelational(event eventstore.Event) (*handler.Statement, error) {
|
||||
// e, ok := event.(*org.DomainPrimarySetEvent)
|
||||
// if !ok {
|
||||
// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-3Tbkt", "reduce.wrong.event.type %s", org.OrgDomainPrimarySetEventType)
|
||||
// }
|
||||
// return handler.NewUpdateStatement(
|
||||
// e,
|
||||
// []handler.Column{
|
||||
// handler.NewCol(OrgColumnChangeDate, e.CreationDate()),
|
||||
// handler.NewCol(OrgColumnSequence, e.Sequence()),
|
||||
// handler.NewCol(OrgColumnDomain, e.Domain),
|
||||
// },
|
||||
// []handler.Condition{
|
||||
// handler.NewCond(OrgColumnID, e.Aggregate().ID),
|
||||
// handler.NewCond(OrgColumnInstanceID, e.Aggregate().InstanceID),
|
||||
// },
|
||||
// ), nil
|
||||
// }
|
||||
|
||||
func (p *orgRelationalProjection) reduceOrgRelationalRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*org.OrgRemovedEvent)
|
||||
if !ok {
|
||||
return nil, zerrors.ThrowInvalidArgumentf(nil, "PROJE-DGm9g", "reduce.wrong.event.type %s", org.OrgRemovedEventType)
|
||||
}
|
||||
return handler.NewDeleteStatement(
|
||||
e,
|
||||
[]handler.Condition{
|
||||
handler.NewCond(OrgColumnID, e.Aggregate().ID),
|
||||
handler.NewCond(OrgColumnInstanceID, e.Aggregate().InstanceID),
|
||||
},
|
||||
), nil
|
||||
}
|
||||
@@ -89,6 +89,11 @@ var (
|
||||
HostedLoginTranslationProjection *handler.Handler
|
||||
OrganizationSettingsProjection *handler.Handler
|
||||
|
||||
InstanceRelationalProjection *handler.Handler
|
||||
OrganizationRelationalProjection *handler.Handler
|
||||
InstanceDomainRelationalProjection *handler.Handler
|
||||
OrganizationDomainRelationalProjection *handler.Handler
|
||||
|
||||
ProjectGrantFields *handler.FieldHandler
|
||||
OrgDomainVerifiedFields *handler.FieldHandler
|
||||
InstanceDomainFields *handler.FieldHandler
|
||||
@@ -199,6 +204,11 @@ func Create(ctx context.Context, sqlClient *database.DB, es handler.EventStore,
|
||||
PermissionFields = newFillPermissionFields(applyCustomConfig(projectionConfig, config.Customizations[fieldsPermission]))
|
||||
// Don't forget to add the new field handler to [ProjectInstanceFields]
|
||||
|
||||
InstanceRelationalProjection = newInstanceRelationalProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["instances_relational"]))
|
||||
OrganizationRelationalProjection = newOrgRelationalProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["organizations_relational"]))
|
||||
InstanceDomainRelationalProjection = newInstanceDomainRelationalProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["instance_domains_relational"]))
|
||||
OrganizationDomainRelationalProjection = newOrgDomainRelationalProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["organization_domains_relational"]))
|
||||
|
||||
newProjectionsList()
|
||||
newFieldsList()
|
||||
return nil
|
||||
@@ -380,5 +390,10 @@ func newProjectionsList() {
|
||||
DebugEventsProjection,
|
||||
HostedLoginTranslationProjection,
|
||||
OrganizationSettingsProjection,
|
||||
|
||||
InstanceRelationalProjection,
|
||||
OrganizationRelationalProjection,
|
||||
InstanceDomainRelationalProjection,
|
||||
OrganizationDomainRelationalProjection,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
package projection
|
||||
|
||||
const (
|
||||
State = "state"
|
||||
CreatedAt = "created_at"
|
||||
UpdatedAt = "updated_at"
|
||||
)
|
||||
Reference in New Issue
Block a user