zitadel/internal/v2/eventstore/push.go
Silvan 2243306ef6
feat(cmd): mirror (#7004)
# Which Problems Are Solved

Adds the possibility to mirror an existing database to a new one. 

For that a new command was added `zitadel mirror`. Including it's
subcommands for a more fine grained mirror of the data.

Sub commands:

* `zitadel mirror eventstore`: copies only events and their unique
constraints
* `zitadel mirror system`: mirrors the data of the `system`-schema
*  `zitadel mirror projections`: runs all projections
*  `zitadel mirror auth`: copies auth requests
* `zitadel mirror verify`: counts the amount of rows in the source and
destination database and prints the diff.

The command requires one of the following flags:
* `--system`: copies all instances of the system
* `--instance <instance-id>`, `--instance <comma separated list of
instance ids>`: copies only the defined instances

The command is save to execute multiple times by adding the
`--replace`-flag. This replaces currently existing data except of the
`events`-table

# Additional Changes

A `--for-mirror`-flag was added to `zitadel setup` to prepare the new
database. The flag skips the creation of the first instances and initial
run of projections.

It is now possible to skip the creation of the first instance during
setup by setting `FirstInstance.Skip` to true in the steps
configuration.

# Additional info

It is currently not possible to merge multiple databases. See
https://github.com/zitadel/zitadel/issues/7964 for more details.

It is currently not possible to use files. See
https://github.com/zitadel/zitadel/issues/7966 for more information.

closes https://github.com/zitadel/zitadel/issues/7586
closes https://github.com/zitadel/zitadel/issues/7486

### Definition of Ready

- [x] I am happy with the code
- [x] Short description of the feature/issue is added in the pr
description
- [x] PR is linked to the corresponding user story
- [x] Acceptance criteria are met
- [x] All open todos and follow ups are defined in a new ticket and
justified
- [x] Deviations from the acceptance criteria and design are agreed with
the PO and documented.
- [x] No debug or dead code
- [x] My code has no repetitions
- [x] Critical parts are tested automatically
- [ ] Where possible E2E tests are implemented
- [x] Documentation/examples are up-to-date
- [x] All non-functional requirements are met
- [x] Functionality of the acceptance criteria is checked manually on
the dev system.

---------

Co-authored-by: Livio Spring <livio.a@gmail.com>
2024-05-30 09:35:30 +00:00

173 lines
3.6 KiB
Go

package eventstore
import (
"context"
"database/sql"
)
type Pusher interface {
healthier
// Push writes the intents to the storage
// if an intent implements [PushReducerIntent] [PushReducerIntent.Reduce] is called after
// the intent was stored
Push(ctx context.Context, intent *PushIntent) error
}
func NewPushIntent(instance string, opts ...PushOpt) *PushIntent {
intent := &PushIntent{
instance: instance,
}
for _, opt := range opts {
opt(intent)
}
return intent
}
type PushIntent struct {
instance string
reducer Reducer
tx *sql.Tx
aggregates []*PushAggregate
}
func (pi *PushIntent) Instance() string {
return pi.instance
}
func (pi *PushIntent) Reduce(events ...*StorageEvent) error {
if pi.reducer == nil {
return nil
}
return pi.reducer.Reduce(events...)
}
func (pi *PushIntent) Tx() *sql.Tx {
return pi.tx
}
func (pi *PushIntent) Aggregates() []*PushAggregate {
return pi.aggregates
}
type PushOpt func(pi *PushIntent)
func PushReducer(reducer Reducer) PushOpt {
return func(pi *PushIntent) {
pi.reducer = reducer
}
}
func PushTx(tx *sql.Tx) PushOpt {
return func(pi *PushIntent) {
pi.tx = tx
}
}
func AppendAggregate(owner, typ, id string, opts ...PushAggregateOpt) PushOpt {
return AppendAggregates(NewPushAggregate(owner, typ, id, opts...))
}
func AppendAggregates(aggregates ...*PushAggregate) PushOpt {
return func(pi *PushIntent) {
for _, aggregate := range aggregates {
aggregate.parent = pi
}
pi.aggregates = append(pi.aggregates, aggregates...)
}
}
type PushAggregate struct {
parent *PushIntent
// typ of the aggregate
typ string
// id of the aggregate
id string
// owner of the aggregate
owner string
// Commands is an ordered list of changes on the aggregate
commands []*Command
// CurrentSequence checks the current state of the aggregate.
// The following types match the current sequence of the aggregate as described:
// * nil or [SequenceIgnore]: Not relevant to add the commands
// * [SequenceMatches]: Must exactly match
// * [SequenceAtLeast]: Must be >= the given sequence
currentSequence CurrentSequence
}
func NewPushAggregate(owner, typ, id string, opts ...PushAggregateOpt) *PushAggregate {
pa := &PushAggregate{
typ: typ,
id: id,
owner: owner,
}
for _, opt := range opts {
opt(pa)
}
return pa
}
func (pa *PushAggregate) Type() string {
return pa.typ
}
func (pa *PushAggregate) ID() string {
return pa.id
}
func (pa *PushAggregate) Owner() string {
return pa.owner
}
func (pa *PushAggregate) Commands() []*Command {
return pa.commands
}
func (pa *PushAggregate) Aggregate() *Aggregate {
return &Aggregate{
ID: pa.id,
Type: pa.typ,
Owner: pa.owner,
Instance: pa.parent.instance,
}
}
func (pa *PushAggregate) CurrentSequence() CurrentSequence {
return pa.currentSequence
}
type PushAggregateOpt func(pa *PushAggregate)
func SetCurrentSequence(currentSequence CurrentSequence) PushAggregateOpt {
return func(pa *PushAggregate) {
pa.currentSequence = currentSequence
}
}
func IgnoreCurrentSequence() PushAggregateOpt {
return func(pa *PushAggregate) {
pa.currentSequence = SequenceIgnore()
}
}
func CurrentSequenceMatches(sequence uint32) PushAggregateOpt {
return func(pa *PushAggregate) {
pa.currentSequence = SequenceMatches(sequence)
}
}
func CurrentSequenceAtLeast(sequence uint32) PushAggregateOpt {
return func(pa *PushAggregate) {
pa.currentSequence = SequenceAtLeast(sequence)
}
}
func AppendCommands(commands ...*Command) PushAggregateOpt {
return func(pa *PushAggregate) {
pa.commands = append(pa.commands, commands...)
}
}