mirror of
https://github.com/andwati/zitadel.git
synced 2026-12-02 00:48:52 +00:00
feat: projections auto create their tables (#3324)
* begin init checks for projections * first projection checks * debug notification providers with query fixes * more projections and first index * more projections * more projections * finish projections * fix tests (remove db name) * create tables in setup * fix logging / error handling * add tenant to views * rename tenant to instance_id * add instance_id to all projections * add instance_id to all queries * correct instance_id on projections * add instance_id to failed_events * use separate context for instance * implement features projection * implement features projection * remove unique constraint from setup when migration failed * add error to failed setup event * add instance_id to primary keys * fix IAM projection * remove old migrations folder * fix keysFromYAML test
This commit is contained in:
@@ -3,7 +3,6 @@ package projection
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/caos/logging"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore"
|
||||
"github.com/caos/zitadel/internal/eventstore/handler"
|
||||
@@ -11,18 +10,41 @@ import (
|
||||
"github.com/caos/zitadel/internal/repository/iam"
|
||||
)
|
||||
|
||||
const (
|
||||
IAMProjectionTable = "projections.iam"
|
||||
|
||||
IAMColumnID = "id"
|
||||
IAMColumnChangeDate = "change_date"
|
||||
IAMColumnGlobalOrgID = "global_org_id"
|
||||
IAMColumnProjectID = "iam_project_id"
|
||||
IAMColumnSequence = "sequence"
|
||||
IAMColumnSetUpStarted = "setup_started"
|
||||
IAMColumnSetUpDone = "setup_done"
|
||||
IAMColumnDefaultLanguage = "default_language"
|
||||
)
|
||||
|
||||
type IAMProjection struct {
|
||||
crdb.StatementHandler
|
||||
}
|
||||
|
||||
const (
|
||||
IAMProjectionTable = "zitadel.projections.iam"
|
||||
)
|
||||
|
||||
func NewIAMProjection(ctx context.Context, config crdb.StatementHandlerConfig) *IAMProjection {
|
||||
p := new(IAMProjection)
|
||||
config.ProjectionName = IAMProjectionTable
|
||||
config.Reducers = p.reducers()
|
||||
config.InitCheck = crdb.NewTableCheck(
|
||||
crdb.NewTable([]*crdb.Column{
|
||||
crdb.NewColumn(IAMColumnID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(IAMColumnChangeDate, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(IAMColumnGlobalOrgID, crdb.ColumnTypeText, crdb.Default("")),
|
||||
crdb.NewColumn(IAMColumnProjectID, crdb.ColumnTypeText, crdb.Default("")),
|
||||
crdb.NewColumn(IAMColumnSequence, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(IAMColumnSetUpStarted, crdb.ColumnTypeInt64, crdb.Default(0)),
|
||||
crdb.NewColumn(IAMColumnSetUpDone, crdb.ColumnTypeInt64, crdb.Default(0)),
|
||||
crdb.NewColumn(IAMColumnDefaultLanguage, crdb.ColumnTypeText, crdb.Default("")),
|
||||
},
|
||||
crdb.NewPrimaryKey(IAMColumnID),
|
||||
),
|
||||
)
|
||||
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
||||
return p
|
||||
}
|
||||
@@ -57,29 +79,15 @@ func (p *IAMProjection) reducers() []handler.AggregateReducer {
|
||||
}
|
||||
}
|
||||
|
||||
type IAMColumn string
|
||||
|
||||
const (
|
||||
IAMColumnID = "id"
|
||||
IAMColumnChangeDate = "change_date"
|
||||
IAMColumnGlobalOrgID = "global_org_id"
|
||||
IAMColumnProjectID = "iam_project_id"
|
||||
IAMColumnSequence = "sequence"
|
||||
IAMColumnSetUpStarted = "setup_started"
|
||||
IAMColumnSetUpDone = "setup_done"
|
||||
IAMColumnDefaultLanguage = "default_language"
|
||||
)
|
||||
|
||||
func (p *IAMProjection) reduceGlobalOrgSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.GlobalOrgSetEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-3n89fs", "seq", event.Sequence(), "expectedType", iam.GlobalOrgSetEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-2n9f2", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-2n9f2", "reduce.wrong.event.type %s", iam.GlobalOrgSetEventType)
|
||||
}
|
||||
return crdb.NewUpsertStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().ID),
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().InstanceID),
|
||||
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(IAMColumnSequence, e.Sequence()),
|
||||
handler.NewCol(IAMColumnGlobalOrgID, e.OrgID),
|
||||
@@ -90,13 +98,12 @@ func (p *IAMProjection) reduceGlobalOrgSet(event eventstore.Event) (*handler.Sta
|
||||
func (p *IAMProjection) reduceIAMProjectSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.ProjectSetEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-2j9fw", "seq", event.Sequence(), "expectedType", iam.ProjectSetEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-30o0e", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-30o0e", "reduce.wrong.event.type %s", iam.ProjectSetEventType)
|
||||
}
|
||||
return crdb.NewUpsertStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().ID),
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().InstanceID),
|
||||
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(IAMColumnSequence, e.Sequence()),
|
||||
handler.NewCol(IAMColumnProjectID, e.ProjectID),
|
||||
@@ -107,13 +114,12 @@ func (p *IAMProjection) reduceIAMProjectSet(event eventstore.Event) (*handler.St
|
||||
func (p *IAMProjection) reduceDefaultLanguageSet(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.DefaultLanguageSetEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-3n9le", "seq", event.Sequence(), "expectedType", iam.DefaultLanguageSetEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-30o0e", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-30o0e", "reduce.wrong.event.type %s", iam.DefaultLanguageSetEventType)
|
||||
}
|
||||
return crdb.NewUpsertStatement(
|
||||
e,
|
||||
[]handler.Column{
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().ID),
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().InstanceID),
|
||||
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(IAMColumnSequence, e.Sequence()),
|
||||
handler.NewCol(IAMColumnDefaultLanguage, e.Language.String()),
|
||||
@@ -124,11 +130,10 @@ func (p *IAMProjection) reduceDefaultLanguageSet(event eventstore.Event) (*handl
|
||||
func (p *IAMProjection) reduceSetupEvent(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.SetupStepEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-39fjw", "seq", event.Sequence(), "expectedTypes", []eventstore.EventType{iam.SetupDoneEventType, iam.SetupStartedEventType}).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-d9nfw", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-d9nfw", "reduce.wrong.event.type %v", []eventstore.EventType{iam.SetupDoneEventType, iam.SetupStartedEventType})
|
||||
}
|
||||
columns := []handler.Column{
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().ID),
|
||||
handler.NewCol(IAMColumnID, e.Aggregate().InstanceID),
|
||||
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(IAMColumnSequence, e.Sequence()),
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user