mirror of
https://github.com/zitadel/zitadel.git
synced 2025-08-12 00:27:31 +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"
|
||||
@@ -12,18 +11,46 @@ import (
|
||||
"github.com/caos/zitadel/internal/repository/project"
|
||||
)
|
||||
|
||||
const (
|
||||
OIDCSettingsProjectionTable = "projections.oidc_settings"
|
||||
|
||||
OIDCSettingsColumnAggregateID = "aggregate_id"
|
||||
OIDCSettingsColumnCreationDate = "creation_date"
|
||||
OIDCSettingsColumnChangeDate = "change_date"
|
||||
OIDCSettingsColumnResourceOwner = "resource_owner"
|
||||
OIDCSettingsColumnInstanceID = "instance_id"
|
||||
OIDCSettingsColumnSequence = "sequence"
|
||||
OIDCSettingsColumnAccessTokenLifetime = "access_token_lifetime"
|
||||
OIDCSettingsColumnIdTokenLifetime = "id_token_lifetime"
|
||||
OIDCSettingsColumnRefreshTokenIdleExpiration = "refresh_token_idle_expiration"
|
||||
OIDCSettingsColumnRefreshTokenExpiration = "refresh_token_expiration"
|
||||
)
|
||||
|
||||
type OIDCSettingsProjection struct {
|
||||
crdb.StatementHandler
|
||||
}
|
||||
|
||||
const (
|
||||
OIDCSettingsProjectionTable = "zitadel.projections.oidc_settings"
|
||||
)
|
||||
|
||||
func NewOIDCSettingsProjection(ctx context.Context, config crdb.StatementHandlerConfig) *OIDCSettingsProjection {
|
||||
p := new(OIDCSettingsProjection)
|
||||
config.ProjectionName = OIDCSettingsProjectionTable
|
||||
config.Reducers = p.reducers()
|
||||
config.InitCheck = crdb.NewTableCheck(
|
||||
crdb.NewTable([]*crdb.Column{
|
||||
crdb.NewColumn(OIDCSettingsColumnAggregateID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(OIDCSettingsColumnCreationDate, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(OIDCSettingsColumnChangeDate, crdb.ColumnTypeTimestamp),
|
||||
crdb.NewColumn(OIDCSettingsColumnResourceOwner, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(OIDCSettingsColumnInstanceID, crdb.ColumnTypeText),
|
||||
crdb.NewColumn(OIDCSettingsColumnSequence, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(OIDCSettingsColumnAccessTokenLifetime, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(ExternalLoginCheckLifetimeCol, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(OIDCSettingsColumnIdTokenLifetime, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(OIDCSettingsColumnRefreshTokenIdleExpiration, crdb.ColumnTypeInt64),
|
||||
crdb.NewColumn(OIDCSettingsColumnRefreshTokenExpiration, crdb.ColumnTypeInt64),
|
||||
},
|
||||
crdb.NewPrimaryKey(OIDCSettingsColumnInstanceID, OIDCSettingsColumnAggregateID),
|
||||
),
|
||||
)
|
||||
p.StatementHandler = crdb.NewStatementHandler(ctx, config)
|
||||
return p
|
||||
}
|
||||
@@ -46,23 +73,10 @@ func (p *OIDCSettingsProjection) reducers() []handler.AggregateReducer {
|
||||
}
|
||||
}
|
||||
|
||||
const (
|
||||
OIDCSettingsColumnAggregateID = "aggregate_id"
|
||||
OIDCSettingsColumnCreationDate = "creation_date"
|
||||
OIDCSettingsColumnChangeDate = "change_date"
|
||||
OIDCSettingsColumnResourceOwner = "resource_owner"
|
||||
OIDCSettingsColumnSequence = "sequence"
|
||||
OIDCSettingsColumnAccessTokenLifetime = "access_token_lifetime"
|
||||
OIDCSettingsColumnIdTokenLifetime = "id_token_lifetime"
|
||||
OIDCSettingsColumnRefreshTokenIdleExpiration = "refresh_token_idle_expiration"
|
||||
OIDCSettingsColumnRefreshTokenExpiration = "refresh_token_expiration"
|
||||
)
|
||||
|
||||
func (p *OIDCSettingsProjection) reduceOIDCSettingsAdded(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.OIDCSettingsAddedEvent)
|
||||
if !ok {
|
||||
logging.WithFields("seq", event.Sequence(), "expectedType", iam.OIDCSettingsAddedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-f9nwf", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-f9nwf", "reduce.wrong.event.type %s", iam.OIDCSettingsAddedEventType)
|
||||
}
|
||||
return crdb.NewCreateStatement(
|
||||
e,
|
||||
@@ -71,6 +85,7 @@ func (p *OIDCSettingsProjection) reduceOIDCSettingsAdded(event eventstore.Event)
|
||||
handler.NewCol(OIDCSettingsColumnCreationDate, e.CreationDate()),
|
||||
handler.NewCol(OIDCSettingsColumnChangeDate, e.CreationDate()),
|
||||
handler.NewCol(OIDCSettingsColumnResourceOwner, e.Aggregate().ResourceOwner),
|
||||
handler.NewCol(OIDCSettingsColumnInstanceID, e.Aggregate().InstanceID),
|
||||
handler.NewCol(OIDCSettingsColumnSequence, e.Sequence()),
|
||||
handler.NewCol(OIDCSettingsColumnAccessTokenLifetime, e.AccessTokenLifetime),
|
||||
handler.NewCol(OIDCSettingsColumnIdTokenLifetime, e.IdTokenLifetime),
|
||||
@@ -83,8 +98,7 @@ func (p *OIDCSettingsProjection) reduceOIDCSettingsAdded(event eventstore.Event)
|
||||
func (p *OIDCSettingsProjection) reduceOIDCSettingsChanged(event eventstore.Event) (*handler.Statement, error) {
|
||||
e, ok := event.(*iam.OIDCSettingsChangedEvent)
|
||||
if !ok {
|
||||
logging.WithFields("seq", event.Sequence(), "expected", iam.OIDCSettingsChangedEventType).Error("wrong event type")
|
||||
return nil, errors.ThrowInvalidArgument(nil, "HANDL-8JJ2d", "reduce.wrong.event.type")
|
||||
return nil, errors.ThrowInvalidArgumentf(nil, "HANDL-8JJ2d", "reduce.wrong.event.type %s", iam.OIDCSettingsChangedEventType)
|
||||
}
|
||||
|
||||
columns := make([]handler.Column, 0, 6)
|
||||
|
Reference in New Issue
Block a user