From e8fccf28b80941a3a5f5157abdb76c0cc8eed42c Mon Sep 17 00:00:00 2001 From: Iraq Jaber Date: Tue, 27 May 2025 15:39:59 +0200 Subject: [PATCH] fixup! feat(db): adding relational instance table --- .../migration/001_instance_table/up.sql | 14 +- .../query/projection/instance_relational.go | 257 +++++++++--------- internal/query/projection/projection.go | 3 + 3 files changed, 138 insertions(+), 136 deletions(-) diff --git a/backend/v3/storage/database/dialect/postgres/migration/001_instance_table/up.sql b/backend/v3/storage/database/dialect/postgres/migration/001_instance_table/up.sql index 1a65e16e67..fb71ec8e86 100644 --- a/backend/v3/storage/database/dialect/postgres/migration/001_instance_table/up.sql +++ b/backend/v3/storage/database/dialect/postgres/migration/001_instance_table/up.sql @@ -1,11 +1,15 @@ +-- the projection for instances happens before md/setup/54.go is run, +-- hence why the zitadel schema is added below +CREATE SCHEMA IF NOT EXISTS zitadel; + CREATE TABLE IF NOT EXISTS zitadel.instances( id TEXT NOT NULL PRIMARY KEY, name TEXT NOT NULL, - default_org_id TEXT NOT NULL, - iam_project_id TEXT NOT NULL, - console_client_id TEXT NOT NULL, - console_app_id TEXT NOT NULL, - default_language TEXT REFERENCES languages(code), + default_org_id TEXT, -- NOT NULL, + iam_project_id TEXT, -- NOT NULL, + console_client_id TEXT, -- NOT NULL, + console_app_id TEXT, -- NOT NULL, + default_language TEXT, -- NOT NULL, created_at TIMESTAMPTZ DEFAULT NOW(), updated_at TIMESTAMPTZ DEFAULT NOW(), deleted_at TIMESTAMPTZ DEFAULT NULL diff --git a/internal/query/projection/instance_relational.go b/internal/query/projection/instance_relational.go index b61a8efefc..5e383a4ea3 100644 --- a/internal/query/projection/instance_relational.go +++ b/internal/query/projection/instance_relational.go @@ -10,7 +10,7 @@ import ( "github.com/zitadel/zitadel/internal/zerrors" ) -const InstanceRelationalTable = "zitadel.instances" +const InstanceRelationalProjectionTable = "zitadel.instances" type instanceRelationalProjection struct{} @@ -19,7 +19,7 @@ func newInstanceRelationalProjection(ctx context.Context, config handler.Config) } func (*instanceRelationalProjection) Name() string { - return InstanceRelationalTable + return InstanceRelationalProjectionTable } func (*instanceRelationalProjection) Init() *old_handler.Check { @@ -47,38 +47,38 @@ func (p *instanceRelationalProjection) Reducers() []handler.AggregateReducer { EventReducers: []handler.EventReducer{ { Event: instance.InstanceAddedEventType, - Reduce: p.reduceInstanceRelationalAdded, + 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, }, - // { - // Event: instance.InstanceChangedEventType, - // Reduce: p.reduceInstanceChanged, - // }, - // { - // Event: instance.InstanceRemovedEventType, - // Reduce: reduceInstanceRemovedHelper(InstanceColumnID), - // }, - // { - // 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) reduceInstanceRelationalAdded(event eventstore.Event) (*handler.Statement, error) { +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) @@ -87,116 +87,111 @@ func (p *instanceRelationalProjection) reduceInstanceRelationalAdded(event event e, []handler.Column{ handler.NewCol(InstanceColumnID, e.Aggregate().InstanceID), - handler.NewCol(InstanceColumnCreationDate, e.CreationDate()), - handler.NewCol(InstanceColumnChangeDate, e.CreationDate()), - handler.NewCol(InstanceColumnSequence, e.Sequence()), handler.NewCol(InstanceColumnName, e.Name), + handler.NewCol(CreatedAt, e.CreationDate()), + handler.NewCol(UpdatedAt, e.CreationDate()), }, ), nil } -// func reduceInstanceRemovedHelper(instanceIDCol string) func(event eventstore.Event) (*handler.Statement, error) { -// return func(event eventstore.Event) (*handler.Statement, error) { -// e, ok := event.(*instance.InstanceRemovedEvent) -// if !ok { -// return nil, zerrors.ThrowInvalidArgumentf(nil, "HANDL-22nlS", "reduce.wrong.event.type %s", instance.InstanceRemovedEventType) -// } -// return handler.NewDeleteStatement( -// e, -// []handler.Condition{ -// handler.NewCond(instanceIDCol, e.Aggregate().ID), -// }, -// ), 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.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(InstanceColumnName, e.Name), + handler.NewCol(UpdatedAt, e.CreationDate()), + }, + []handler.Condition{ + handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), + }, + ), 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.NewUpdateStatement( -// e, -// []handler.Column{ -// handler.NewCol(InstanceColumnName, e.Name), -// handler.NewCol(InstanceColumnChangeDate, e.CreationDate()), -// handler.NewCol(InstanceColumnSequence, e.Sequence()), -// }, -// []handler.Condition{ -// handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), -// }, -// ), nil -// } +func (p *instanceRelationalProjection) reduceInstanceDelete(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.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(DeletedAt, e.CreationDate()), + }, + []handler.Condition{ + handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), + }, + ), 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.NewUpdateStatement( -// e, -// []handler.Column{ -// handler.NewCol(InstanceColumnChangeDate, e.CreationDate()), -// handler.NewCol(InstanceColumnSequence, e.Sequence()), -// handler.NewCol(InstanceColumnDefaultOrgID, e.OrgID), -// }, -// []handler.Condition{ -// handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), -// }, -// ), 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.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(UpdatedAt, e.CreationDate()), + handler.NewCol(InstanceColumnDefaultOrgID, e.OrgID), + }, + []handler.Condition{ + handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), + }, + ), 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.NewUpdateStatement( -// e, -// []handler.Column{ -// handler.NewCol(InstanceColumnChangeDate, e.CreationDate()), -// handler.NewCol(InstanceColumnSequence, e.Sequence()), -// handler.NewCol(InstanceColumnProjectID, e.ProjectID), -// }, -// []handler.Condition{ -// handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), -// }, -// ), 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.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(UpdatedAt, e.CreationDate()), + handler.NewCol(InstanceColumnProjectID, e.ProjectID), + }, + []handler.Condition{ + handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), + }, + ), 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.NewUpdateStatement( -// e, -// []handler.Column{ -// handler.NewCol(InstanceColumnChangeDate, e.CreationDate()), -// handler.NewCol(InstanceColumnSequence, e.Sequence()), -// handler.NewCol(InstanceColumnConsoleID, e.ClientID), -// handler.NewCol(InstanceColumnConsoleAppID, e.AppID), -// }, -// []handler.Condition{ -// handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), -// }, -// ), 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.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(UpdatedAt, e.CreationDate()), + handler.NewCol(InstanceColumnConsoleID, e.ClientID), + handler.NewCol(InstanceColumnConsoleAppID, e.AppID), + }, + []handler.Condition{ + handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), + }, + ), 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.NewUpdateStatement( -// e, -// []handler.Column{ -// handler.NewCol(InstanceColumnChangeDate, e.CreationDate()), -// handler.NewCol(InstanceColumnSequence, e.Sequence()), -// handler.NewCol(InstanceColumnDefaultLanguage, e.Language.String()), -// }, -// []handler.Condition{ -// handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), -// }, -// ), 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.NewUpdateStatement( + e, + []handler.Column{ + handler.NewCol(UpdatedAt, e.CreationDate()), + handler.NewCol(InstanceColumnDefaultLanguage, e.Language.String()), + }, + []handler.Condition{ + handler.NewCond(InstanceColumnID, e.Aggregate().InstanceID), + }, + ), nil +} diff --git a/internal/query/projection/projection.go b/internal/query/projection/projection.go index f4e3bbe0d4..7b700750f8 100644 --- a/internal/query/projection/projection.go +++ b/internal/query/projection/projection.go @@ -55,6 +55,7 @@ var ( UserMetadataProjection *handler.Handler UserAuthMethodProjection *handler.Handler InstanceProjection *handler.Handler + InstanceRelationalProjection *handler.Handler SecretGeneratorProjection *handler.Handler SMTPConfigProjection *handler.Handler SMSConfigProjection *handler.Handler @@ -150,6 +151,7 @@ func Create(ctx context.Context, sqlClient *database.DB, es handler.EventStore, UserMetadataProjection = newUserMetadataProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["user_metadata"])) UserAuthMethodProjection = newUserAuthMethodProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["user_auth_method"])) InstanceProjection = newInstanceProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["instances"])) + InstanceRelationalProjection = newInstanceRelationalProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["instances_relational"])) SecretGeneratorProjection = newSecretGeneratorProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["secret_generators"])) SMTPConfigProjection = newSMTPConfigProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["smtp_configs"])) SMSConfigProjection = newSMSConfigProjection(ctx, applyCustomConfig(projectionConfig, config.Customizations["sms_config"])) @@ -304,6 +306,7 @@ func newProjectionsList() { UserMetadataProjection, UserAuthMethodProjection, InstanceProjection, + InstanceRelationalProjection, SecretGeneratorProjection, SMTPConfigProjection, SMSConfigProjection,