zitadel/internal/query/projection/custom_text.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

164 lines
5.9 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"
"github.com/caos/zitadel/internal/repository/org"
"github.com/caos/zitadel/internal/repository/policy"
)
const (
CustomTextTable = "projections.custom_texts"
CustomTextAggregateIDCol = "aggregate_id"
CustomTextInstanceIDCol = "instance_id"
CustomTextCreationDateCol = "creation_date"
CustomTextChangeDateCol = "change_date"
CustomTextSequenceCol = "sequence"
CustomTextIsDefaultCol = "is_default"
CustomTextTemplateCol = "template"
CustomTextLanguageCol = "language"
CustomTextKeyCol = "key"
CustomTextTextCol = "text"
)
type CustomTextProjection struct {
crdb.StatementHandler
}
func NewCustomTextProjection(ctx context.Context, config crdb.StatementHandlerConfig) *CustomTextProjection {
p := new(CustomTextProjection)
config.ProjectionName = CustomTextTable
config.Reducers = p.reducers()
config.InitCheck = crdb.NewTableCheck(
crdb.NewTable([]*crdb.Column{
crdb.NewColumn(CustomTextAggregateIDCol, crdb.ColumnTypeText),
crdb.NewColumn(CustomTextInstanceIDCol, crdb.ColumnTypeText),
crdb.NewColumn(CustomTextCreationDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(CustomTextChangeDateCol, crdb.ColumnTypeTimestamp),
crdb.NewColumn(CustomTextSequenceCol, crdb.ColumnTypeInt64),
crdb.NewColumn(CustomTextIsDefaultCol, crdb.ColumnTypeBool),
crdb.NewColumn(CustomTextTemplateCol, crdb.ColumnTypeText),
crdb.NewColumn(CustomTextLanguageCol, crdb.ColumnTypeText),
crdb.NewColumn(CustomTextKeyCol, crdb.ColumnTypeText),
crdb.NewColumn(CustomTextTextCol, crdb.ColumnTypeText),
},
crdb.NewPrimaryKey(CustomTextInstanceIDCol, CustomTextAggregateIDCol, CustomTextTemplateCol, CustomTextKeyCol, CustomTextLanguageCol),
),
)
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
return p
}
func (p *CustomTextProjection) reducers() []handler.AggregateReducer {
return []handler.AggregateReducer{
{
Aggregate: org.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: org.CustomTextSetEventType,
Reduce: p.reduceSet,
},
{
Event: org.CustomTextRemovedEventType,
Reduce: p.reduceRemoved,
},
{
Event: org.CustomTextTemplateRemovedEventType,
Reduce: p.reduceTemplateRemoved,
},
},
},
{
Aggregate: iam.AggregateType,
EventRedusers: []handler.EventReducer{
{
Event: iam.CustomTextSetEventType,
Reduce: p.reduceSet,
},
{
Event: iam.CustomTextRemovedEventType,
Reduce: p.reduceRemoved,
},
{
Event: iam.CustomTextTemplateRemovedEventType,
Reduce: p.reduceTemplateRemoved,
},
},
},
}
}
func (p *CustomTextProjection) reduceSet(event eventstore.Event) (*handler.Statement, error) {
var customTextEvent policy.CustomTextSetEvent
var isDefault bool
switch e := event.(type) {
case *org.CustomTextSetEvent:
customTextEvent = e.CustomTextSetEvent
isDefault = false
case *iam.CustomTextSetEvent:
customTextEvent = e.CustomTextSetEvent
isDefault = true
default:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-KKfw4", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextSetEventType, iam.CustomTextSetEventType})
}
return crdb.NewUpsertStatement(
&customTextEvent,
[]handler.Column{
handler.NewCol(CustomTextAggregateIDCol, customTextEvent.Aggregate().ID),
handler.NewCol(CustomTextInstanceIDCol, customTextEvent.Aggregate().InstanceID),
handler.NewCol(CustomTextCreationDateCol, customTextEvent.CreationDate()),
handler.NewCol(CustomTextChangeDateCol, customTextEvent.CreationDate()),
handler.NewCol(CustomTextSequenceCol, customTextEvent.Sequence()),
handler.NewCol(CustomTextIsDefaultCol, isDefault),
handler.NewCol(CustomTextTemplateCol, customTextEvent.Template),
handler.NewCol(CustomTextLanguageCol, customTextEvent.Language.String()),
handler.NewCol(CustomTextKeyCol, customTextEvent.Key),
handler.NewCol(CustomTextTextCol, customTextEvent.Text),
}), nil
}
func (p *CustomTextProjection) reduceRemoved(event eventstore.Event) (*handler.Statement, error) {
var customTextEvent policy.CustomTextRemovedEvent
switch e := event.(type) {
case *org.CustomTextRemovedEvent:
customTextEvent = e.CustomTextRemovedEvent
case *iam.CustomTextRemovedEvent:
customTextEvent = e.CustomTextRemovedEvent
default:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-n9wJg", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextRemovedEventType, iam.CustomTextRemovedEventType})
}
return crdb.NewDeleteStatement(
&customTextEvent,
[]handler.Condition{
handler.NewCond(CustomTextAggregateIDCol, customTextEvent.Aggregate().ID),
handler.NewCond(CustomTextTemplateCol, customTextEvent.Template),
handler.NewCond(CustomTextKeyCol, customTextEvent.Key),
handler.NewCond(CustomTextLanguageCol, customTextEvent.Language.String()),
}), nil
}
func (p *CustomTextProjection) reduceTemplateRemoved(event eventstore.Event) (*handler.Statement, error) {
var customTextEvent policy.CustomTextTemplateRemovedEvent
switch e := event.(type) {
case *org.CustomTextTemplateRemovedEvent:
customTextEvent = e.CustomTextTemplateRemovedEvent
case *iam.CustomTextTemplateRemovedEvent:
customTextEvent = e.CustomTextTemplateRemovedEvent
default:
return nil, errors.ThrowInvalidArgumentf(nil, "PROJE-29iPf", "reduce.wrong.event.type %v", []eventstore.EventType{org.CustomTextTemplateRemovedEventType, iam.CustomTextTemplateRemovedEventType})
}
return crdb.NewDeleteStatement(
&customTextEvent,
[]handler.Condition{
handler.NewCond(CustomTextAggregateIDCol, customTextEvent.Aggregate().ID),
handler.NewCond(CustomTextTemplateCol, customTextEvent.Template),
handler.NewCond(CustomTextLanguageCol, customTextEvent.Language.String()),
}), nil
}