zitadel/internal/query/projection/iam.go
Livio Amstutz 56b916a2b0
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
2022-03-23 09:02:39 +01:00

150 lines
4.7 KiB
Go

package projection
import (
"context"
"github.com/caos/zitadel/internal/errors"
"github.com/caos/zitadel/internal/eventstore"
"github.com/caos/zitadel/internal/eventstore/handler"
"github.com/caos/zitadel/internal/eventstore/handler/crdb"
"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
}
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
}
func (p *IAMProjection) reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: iam.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: iam.GlobalOrgSetEventType,
Reduce: p.reduceGlobalOrgSet,
},
{
Event: iam.ProjectSetEventType,
Reduce: p.reduceIAMProjectSet,
},
{
Event: iam.DefaultLanguageSetEventType,
Reduce: p.reduceDefaultLanguageSet,
},
{
Event: iam.SetupStartedEventType,
Reduce: p.reduceSetupEvent,
},
{
Event: iam.SetupDoneEventType,
Reduce: p.reduceSetupEvent,
},
},
},
}
}
func (p *IAMProjection) reduceGlobalOrgSet(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.GlobalOrgSetEvent)
if !ok {
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().InstanceID),
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
handler.NewCol(IAMColumnSequence, e.Sequence()),
handler.NewCol(IAMColumnGlobalOrgID, e.OrgID),
},
), nil
}
func (p *IAMProjection) reduceIAMProjectSet(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.ProjectSetEvent)
if !ok {
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().InstanceID),
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
handler.NewCol(IAMColumnSequence, e.Sequence()),
handler.NewCol(IAMColumnProjectID, e.ProjectID),
},
), nil
}
func (p *IAMProjection) reduceDefaultLanguageSet(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.DefaultLanguageSetEvent)
if !ok {
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().InstanceID),
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
handler.NewCol(IAMColumnSequence, e.Sequence()),
handler.NewCol(IAMColumnDefaultLanguage, e.Language.String()),
},
), nil
}
func (p *IAMProjection) reduceSetupEvent(event eventstore.Event) (*handler.Statement, error) {
e, ok := event.(*iam.SetupStepEvent)
if !ok {
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().InstanceID),
handler.NewCol(IAMColumnChangeDate, e.CreationDate()),
handler.NewCol(IAMColumnSequence, e.Sequence()),
}
if e.EventType == iam.SetupStartedEventType {
columns = append(columns, handler.NewCol(IAMColumnSetUpStarted, e.Step))
} else {
columns = append(columns, handler.NewCol(IAMColumnSetUpDone, e.Step))
}
return crdb.NewUpsertStatement(
e,
columns,
), nil
}