fixup! feat(db): adding relational instance table

This commit is contained in:
Iraq Jaber
2025-05-27 15:39:59 +02:00
parent 11d691e190
commit e8fccf28b8
3 changed files with 138 additions and 136 deletions

View File

@@ -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

View File

@@ -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
}

View File

@@ -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,