mirror of
https://github.com/zitadel/zitadel.git
synced 2024-12-15 04:18:01 +00:00
77b4fc5487
* 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>
86 lines
2.8 KiB
Go
86 lines
2.8 KiB
Go
package crdb
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"strconv"
|
|
"strings"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
|
"github.com/zitadel/zitadel/internal/errors"
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
)
|
|
|
|
const (
|
|
currentSequenceStmtFormat = `SELECT current_sequence, aggregate_type, instance_id FROM %s WHERE projection_name = $1 AND instance_id = ANY ($2) FOR UPDATE`
|
|
updateCurrentSequencesStmtFormat = `INSERT INTO %s (projection_name, aggregate_type, current_sequence, instance_id, timestamp) VALUES `
|
|
updateCurrentSequencesConflictStmt = ` ON CONFLICT (projection_name, aggregate_type, instance_id) DO UPDATE SET current_sequence = EXCLUDED.current_sequence, timestamp = EXCLUDED.timestamp`
|
|
)
|
|
|
|
type currentSequences map[eventstore.AggregateType][]*instanceSequence
|
|
|
|
type instanceSequence struct {
|
|
instanceID string
|
|
sequence uint64
|
|
}
|
|
|
|
func (h *StatementHandler) currentSequences(ctx context.Context, query func(context.Context, string, ...interface{}) (*sql.Rows, error), instanceIDs database.StringArray) (currentSequences, error) {
|
|
rows, err := query(ctx, h.currentSequenceStmt, h.ProjectionName, instanceIDs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
defer rows.Close()
|
|
|
|
sequences := make(currentSequences, len(h.aggregates))
|
|
for rows.Next() {
|
|
var (
|
|
aggregateType eventstore.AggregateType
|
|
sequence uint64
|
|
instanceID string
|
|
)
|
|
|
|
err = rows.Scan(&sequence, &aggregateType, &instanceID)
|
|
if err != nil {
|
|
return nil, errors.ThrowInternal(err, "CRDB-dbatK", "scan failed")
|
|
}
|
|
|
|
sequences[aggregateType] = append(sequences[aggregateType], &instanceSequence{
|
|
sequence: sequence,
|
|
instanceID: instanceID,
|
|
})
|
|
}
|
|
|
|
if err = rows.Close(); err != nil {
|
|
return nil, errors.ThrowInternal(err, "CRDB-h5i5m", "close rows failed")
|
|
}
|
|
|
|
if err = rows.Err(); err != nil {
|
|
return nil, errors.ThrowInternal(err, "CRDB-O8zig", "errors in scanning rows")
|
|
}
|
|
|
|
return sequences, nil
|
|
}
|
|
|
|
func (h *StatementHandler) updateCurrentSequences(tx *sql.Tx, sequences currentSequences) error {
|
|
valueQueries := make([]string, 0, len(sequences))
|
|
valueCounter := 0
|
|
values := make([]interface{}, 0, len(sequences)*3)
|
|
for aggregate, instanceSequence := range sequences {
|
|
for _, sequence := range instanceSequence {
|
|
valueQueries = append(valueQueries, "($"+strconv.Itoa(valueCounter+1)+", $"+strconv.Itoa(valueCounter+2)+", $"+strconv.Itoa(valueCounter+3)+", $"+strconv.Itoa(valueCounter+4)+", NOW())")
|
|
valueCounter += 4
|
|
values = append(values, h.ProjectionName, aggregate, sequence.sequence, sequence.instanceID)
|
|
}
|
|
}
|
|
|
|
res, err := tx.Exec(h.updateSequencesBaseStmt+strings.Join(valueQueries, ", ")+updateCurrentSequencesConflictStmt, values...)
|
|
if err != nil {
|
|
return errors.ThrowInternal(err, "CRDB-TrH2Z", "unable to exec update sequence")
|
|
}
|
|
if rows, _ := res.RowsAffected(); rows < 1 {
|
|
return errSeqNotUpdated
|
|
}
|
|
return nil
|
|
}
|