mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-11 21:17:32 +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/domain"
|
||||
"github.com/caos/zitadel/internal/errors"
|
||||
"github.com/caos/zitadel/internal/eventstore"
|
||||
@@ -12,19 +11,57 @@ import (
|
||||
"github.com/caos/zitadel/internal/repository/iam"
|
||||
)
|
||||
|
||||
const (
|
||||
SMSConfigProjectionTable = "projections.sms_configs"
|
||||
SMSTwilioTable = SMSConfigProjectionTable + "_" + smsTwilioTableSuffix
|
||||
|
||||
SMSColumnID = "id"
|
||||
SMSColumnAggregateID = "aggregate_id"
|
||||
SMSColumnCreationDate = "creation_date"
|
||||
SMSColumnChangeDate = "change_date"
|
||||
SMSColumnSequence = "sequence"
|
||||
SMSColumnState = "state"
|
||||
SMSColumnResourceOwner = "resource_owner"
|
||||
SMSColumnInstanceID = "instance_id"
|
||||
|
||||
smsTwilioTableSuffix = "twilio"
|
||||
SMSTwilioConfigColumnSMSID = "sms_id"
|
||||
SMSTwilioConfigColumnSID = "sid"
|
||||
SMSTwilioConfigColumnSenderNumber = "sender_number"
|
||||
SMSTwilioConfigColumnToken = "token"
|
||||
)
|
||||
|
||||
type SMSConfigProjection struct {
|
||||
crdb.StatementHandler
|
||||
}
|
||||
|
||||
const (
|
||||
SMSConfigProjectionTable = "zitadel.projections.sms_configs"
|
||||
SMSTwilioTable = SMSConfigProjectionTable + "_" + smsTwilioTableSuffix
|
||||
)
|
||||
|
||||
func NewSMSConfigProjection(ctx context.Context, config crdb.StatementHandlerConfig) *SMSConfigProjection {
|
||||
p := new(SMSConfigProjection)
|
||||
config.ProjectionName = SMSConfigProjectionTable
|
||||
config.Reducers = p.reducers()
|
||||
config.InitCheck = crdb.NewMultiTableCheck(
|
||||
crdb.NewTable([]*crdb.Column{
|
||||
crdb.NewColumn(SMSColumnID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(SMSColumnAggregateID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(SMSColumnCreationDate, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(SMSColumnChangeDate, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(SMSColumnSequence, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(SMSColumnState, crdb.ColumnTypeEnum),
|
||||
crdb.NewColumn(SMSColumnResourceOwner, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(SMSColumnInstanceID, crdb.ColumnTypeText),
|
||||
},
|
||||
crdb.NewPrimaryKey(SMSColumnInstanceID, SMSColumnID),
|
||||
),
|
||||
crdb.NewSuffixedTable([]*crdb.Column{
|
||||
crdb.NewColumn(SMSTwilioConfigColumnSMSID, crdb.ColumnTypeText, crdb.Default(SMSColumnID)),
|
||||
crdb.NewColumn(SMSTwilioConfigColumnSID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(SMSTwilioConfigColumnSenderNumber, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(SMSTwilioConfigColumnToken, crdb.ColumnTypeJSONB),
|
||||
},
|
||||
crdb.NewPrimaryKey(SMSTwilioConfigColumnSMSID),
|
||||
smsTwilioTableSuffix,
|
||||
),
|
||||
)
|
||||
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
||||
return p
|
||||
}
|
||||
@@ -59,27 +96,10 @@ func (p *SMSConfigProjection) reducers() []handler.AggregateReducer {
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
SMSColumnID = "id"
|
||||
SMSColumnAggregateID = "aggregate_id"
|
||||
SMSColumnCreationDate = "creation_date"
|
||||
SMSColumnChangeDate = "change_date"
|
||||
SMSColumnResourceOwner = "resource_owner"
|
||||
SMSColumnState = "state"
|
||||
SMSColumnSequence = "sequence"
|
||||
|
||||
smsTwilioTableSuffix = "twilio"
|
||||
SMSTwilioConfigColumnSMSID = "sms_id"
|
||||
SMSTwilioConfigColumnSID = "sid"
|
||||
SMSTwilioConfigColumnToken = "token"
|
||||
SMSTwilioConfigColumnSenderNumber = "sender_number"
|
||||
)
|
||||
|
||||
func (p *SMSConfigProjection) reduceSMSConfigTwilioAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.SMSConfigTwilioAddedEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-9jiWf", "seq", event.Sequence(), "expectedType", iam.SMSConfigTwilioAddedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-s8efs", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-s8efs", "reduce.wrong.event.type %s", iam.SMSConfigTwilioAddedEventType)
|
||||
}
|
||||
|
||||
return crdb.NewMultiStatement(
|
||||
@@ -91,6 +111,7 @@ func (p *SMSConfigProjection) reduceSMSConfigTwilioAdded(event eventstore.Event)
|
||||
handler.NewCol(SMSColumnCreationDate, e.CreationDate()),
|
||||
handler.NewCol(SMSColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(SMSColumnResourceOwner, e.Aggregate().ResourceOwner),
|
||||
handler.NewCol(SMSColumnInstanceID, e.Aggregate().InstanceID),
|
||||
handler.NewCol(SMSColumnState, domain.SMSConfigStateInactive),
|
||||
handler.NewCol(SMSColumnSequence, e.Sequence()),
|
||||
},
|
||||
@@ -110,8 +131,7 @@ func (p *SMSConfigProjection) reduceSMSConfigTwilioAdded(event eventstore.Event)
|
||||
func (p *SMSConfigProjection) reduceSMSConfigTwilioChanged(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.SMSConfigTwilioChangedEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-fm9el", "seq", event.Sequence(), "expectedType", iam.SMSConfigTwilioChangedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-fi99F", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-fi99F", "reduce.wrong.event.type %s", iam.SMSConfigTwilioChangedEventType)
|
||||
}
|
||||
columns := make([]handler.Column, 0)
|
||||
if e.SID != nil {
|
||||
@@ -145,8 +165,7 @@ func (p *SMSConfigProjection) reduceSMSConfigTwilioChanged(event eventstore.Even
|
||||
func (p *SMSConfigProjection) reduceSMSConfigActivated(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.SMSConfigActivatedEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-fm03F", "seq", event.Sequence(), "expectedType", iam.SMSConfigActivatedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-fj9Ef", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-fj9Ef", "reduce.wrong.event.type %s", iam.SMSConfigActivatedEventType)
|
||||
}
|
||||
return crdb.NewUpdateStatement(
|
||||
e,
|
||||
@@ -164,8 +183,7 @@ func (p *SMSConfigProjection) reduceSMSConfigActivated(event eventstore.Event) (
|
||||
func (p *SMSConfigProjection) reduceSMSConfigDeactivated(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.SMSConfigDeactivatedEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-9fnHS", "seq", event.Sequence(), "expectedType", iam.SMSConfigDeactivatedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-dj9Js", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-dj9Js", "reduce.wrong.event.type %s", iam.SMSConfigDeactivatedEventType)
|
||||
}
|
||||
return crdb.NewUpdateStatement(
|
||||
e,
|
||||
@@ -183,8 +201,7 @@ func (p *SMSConfigProjection) reduceSMSConfigDeactivated(event eventstore.Event)
|
||||
func (p *SMSConfigProjection) reduceSMSConfigRemoved(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.SMSConfigRemovedEvent)
|
||||
if !ok {
|
||||
logging.LogWithFields("HANDL-0Opew", "seq", event.Sequence(), "expectedType", iam.SMSConfigRemovedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-s9JJf", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-s9JJf", "reduce.wrong.event.type %s", iam.SMSConfigRemovedEventType)
|
||||
}
|
||||
return crdb.NewDeleteStatement(
|
||||
e,
|
||||
|
Reference in New Issue
Block a user