zitadel/internal/query/projection/member.go
Silvan 77b4fc5487
feat(database): support for postgres (#3998)
* beginning with postgres statements

* try pgx

* use pgx

* database

* init works for postgres

* arrays working

* init for cockroach

* init

* start tests

* tests

* TESTS

* ch

* ch

* chore: use go 1.18

* read stmts

* fix typo

* tests

* connection string

* add missing error handler

* cleanup

* start all apis

* go mod tidy

* old update

* switch back to minute

* on conflict

* replace string slice with `database.StringArray` in db models

* fix tests and start

* update go version in dockerfile

* setup go

* clean up

* remove notification migration

* update

* docs: add deploy guide for postgres

* fix: revert sonyflake

* use `database.StringArray` for daos

* use `database.StringArray` every where

* new tables

* index naming,
metadata primary key,
project grant role key type

* docs(postgres): change to beta

* chore: correct compose

* fix(defaults): add empty postgres config

* refactor: remove unused code

* docs: add postgres to self hosted

* fix broken link

* so?

* change title

* add mdx to link

* fix stmt

* update goreleaser in test-code

* docs: improve postgres example

* update more projections

* fix: add beta log for postgres

* revert index name change

* prerelease

* fix: add sequence to v1 "reduce paniced"

* log if nil

* add logging

* fix: log output

* fix(import): check if org exists and user

* refactor: imports

* fix(user): ignore malformed events

* refactor: method naming

* fix: test

* refactor: correct errors.Is call

* ci: don't build dev binaries on main

* fix(go releaser): update version to 1.11.0

* fix(user): projection should not break

* fix(user): handle error properly

* docs: correct config example

* Update .releaserc.js

* Update .releaserc.js

Co-authored-by: Livio Amstutz <livio.a@gmail.com>
Co-authored-by: Elio Bischof <eliobischof@gmail.com>
2022-08-31 07:52:43 +00:00

114 lines
3.4 KiB
Go

package projection
import (
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/eventstore/handler"
"github.com/zitadel/zitadel/internal/eventstore/handler/crdb"
"github.com/zitadel/zitadel/internal/repository/member"
)
const (
MemberUserIDCol = "user_id"
MemberRolesCol = "roles"
MemberCreationDate = "creation_date"
MemberChangeDate = "change_date"
MemberSequence = "sequence"
MemberResourceOwner = "resource_owner"
MemberInstanceID = "instance_id"
)
var (
memberColumns = []*crdb.Column{
crdb.NewColumn(MemberCreationDate, crdb.ColumnTypeTimestamp),
crdb.NewColumn(MemberChangeDate, crdb.ColumnTypeTimestamp),
crdb.NewColumn(MemberUserIDCol, crdb.ColumnTypeText),
crdb.NewColumn(MemberRolesCol, crdb.ColumnTypeTextArray, crdb.Nullable()),
crdb.NewColumn(MemberSequence, crdb.ColumnTypeInt64),
crdb.NewColumn(MemberResourceOwner, crdb.ColumnTypeText),
crdb.NewColumn(MemberInstanceID, crdb.ColumnTypeText),
}
)
type reduceMemberConfig struct {
cols []handler.Column
conds []handler.Condition
}
type reduceMemberOpt func(reduceMemberConfig) reduceMemberConfig
func withMemberCol(col string, value interface{}) reduceMemberOpt {
return func(opt reduceMemberConfig) reduceMemberConfig {
opt.cols = append(opt.cols, handler.NewCol(col, value))
return opt
}
}
func withMemberCond(cond string, value interface{}) reduceMemberOpt {
return func(opt reduceMemberConfig) reduceMemberConfig {
opt.conds = append(opt.conds, handler.NewCond(cond, value))
return opt
}
}
func reduceMemberAdded(e member.MemberAddedEvent, opts ...reduceMemberOpt) (*handler.Statement, error) {
config := reduceMemberConfig{
cols: []handler.Column{
handler.NewCol(MemberUserIDCol, e.UserID),
handler.NewCol(MemberRolesCol, database.StringArray(e.Roles)),
handler.NewCol(MemberCreationDate, e.CreationDate()),
handler.NewCol(MemberChangeDate, e.CreationDate()),
handler.NewCol(MemberSequence, e.Sequence()),
handler.NewCol(MemberResourceOwner, e.Aggregate().ResourceOwner),
handler.NewCol(MemberInstanceID, e.Aggregate().InstanceID),
}}
for _, opt := range opts {
config = opt(config)
}
return crdb.NewCreateStatement(&e, config.cols), nil
}
func reduceMemberChanged(e member.MemberChangedEvent, opts ...reduceMemberOpt) (*handler.Statement, error) {
config := reduceMemberConfig{
cols: []handler.Column{
handler.NewCol(MemberRolesCol, database.StringArray(e.Roles)),
handler.NewCol(MemberChangeDate, e.CreationDate()),
handler.NewCol(MemberSequence, e.Sequence()),
},
conds: []handler.Condition{
handler.NewCond(MemberUserIDCol, e.UserID),
}}
for _, opt := range opts {
config = opt(config)
}
return crdb.NewUpdateStatement(&e, config.cols, config.conds), nil
}
func reduceMemberCascadeRemoved(e member.MemberCascadeRemovedEvent, opts ...reduceMemberOpt) (*handler.Statement, error) {
config := reduceMemberConfig{
conds: []handler.Condition{
handler.NewCond(MemberUserIDCol, e.UserID),
}}
for _, opt := range opts {
config = opt(config)
}
return crdb.NewDeleteStatement(&e, config.conds), nil
}
func reduceMemberRemoved(e eventstore.Event, opts ...reduceMemberOpt) (*handler.Statement, error) {
config := reduceMemberConfig{
conds: []handler.Condition{},
}
for _, opt := range opts {
config = opt(config)
}
return crdb.NewDeleteStatement(e, config.conds), nil
}