fix(eventstore): Make Eventstore Compatible with Relational Table Package (#10687)

Improves compatibility of eventstore and related database components
with the new relational table package.

## Which problems are solved

1. **Incompatible Database Interfaces**: The existing eventstore was
tightly coupled to the database package, which is incompatible with the
new, more abstract relational table package in v3. This prevented the
new command-side logic from pushing events to the legacy eventstore.
2. **Missing Health Checks**: The database interfaces in the new package
lacked a Ping method, making it impossible to perform health checks on
database connections.
3. **Event Publishing Logic**: The command handling logic in domain
needed a way to collect and push events to the legacy eventstore after a
command was successfully executed.

## How the problems are solved

1. **`LegacyEventstore` Interface**:
* A new `LegacyEventstore` interface is introduced in the new
`database/eventstore` . This interface exposes a `PushWithNewClient`
method that accepts the new `database.QueryExecutor` interface,
decoupling the v3 domain from the legacy implementation.
* The `internal/eventstore.Eventstore` now implements this interface. A
wrapper, PushWithClient, is added to convert the old database client
types (`*sql.DB`, `*sql.Tx`) into the new `QueryExecutor` types before
calling `PushWithNewClient`.
2. **Database Interface Updates**:
* The `database.Pool` and `database.Client` interfaces in
`storage/eventstore` have been updated to include a Ping method,
allowing for consistent health checks across different database
dialects.
* The `postgres` and `sql` dialect implementations have been updated to
support this new method.
3. **Command and Invoker Refactoring**:
* The `Commander` interface in domain now includes an `Events()
[]legacy_es.Command` method. This allows commands to declare which
events they will generate.
* The `eventCollector` in the invoker logic has been redesigned. It now
ensures a database transaction is started before executing a command.
After successful execution, it calls the `Events()` method on the
command to collect the generated events and appends them to a list.
* The `eventStoreInvoker` then pushes all collected events to the legacy
eventstore using the new `LegacyEventstore` interface, ensuring that
events are only pushed if the entire command (and any sub-commands)
executes successfully within the transaction.
4. **Testing**:
* New unit tests have been added for the invoker to verify that events
are correctly collected from single commands, batched commands, and
nested commands.

These changes create a clean bridge between the new v3 command-side
logic and the existing v1 eventstore, allowing for incremental adoption
of the new architecture while maintaining full functionality.

## Additional Information

closes https://github.com/zitadel/zitadel/issues/10442
This commit is contained in:
Silvan
2025-09-16 18:58:49 +02:00
committed by GitHub
parent 41d04ffa65
commit 22ef817d5c
28 changed files with 705 additions and 644 deletions
+18 -4
View File
@@ -2,6 +2,7 @@ package eventstore
import (
"context"
"database/sql"
"errors"
"sort"
"time"
@@ -10,6 +11,8 @@ import (
"github.com/shopspring/decimal"
"github.com/zitadel/logging"
new_db "github.com/zitadel/zitadel/backend/v3/storage/database"
new_sql "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/zerrors"
@@ -91,6 +94,19 @@ func (es *Eventstore) Push(ctx context.Context, cmds ...Command) ([]Event, error
// PushWithClient pushes the events in a single transaction using the provided database client
// an event needs at least an aggregate
func (es *Eventstore) PushWithClient(ctx context.Context, client database.ContextQueryExecuter, cmds ...Command) ([]Event, error) {
var dbClient new_db.QueryExecutor
switch client := client.(type) {
case *sql.DB:
dbClient = new_sql.SQLPool(client)
case *sql.Tx:
dbClient = new_sql.SQLTx(client)
case *sql.Conn:
dbClient = new_sql.SQLConn(client)
}
return es.PushWithNewClient(ctx, dbClient, cmds...)
}
func (es *Eventstore) PushWithNewClient(ctx context.Context, client new_db.QueryExecutor, cmds ...Command) ([]Event, error) {
if es.PushTimeout > 0 {
var cancel func()
ctx, cancel = context.WithTimeout(ctx, es.PushTimeout)
@@ -108,7 +124,7 @@ retry:
for i := 0; i <= es.maxRetries; i++ {
events, err = es.pusher.Push(ctx, client, cmds...)
// if there is a transaction passed the calling function needs to retry
if _, ok := client.(database.Tx); ok {
if _, ok := client.(new_db.Transaction); ok {
break retry
}
var pgErr *pgconn.PgError
@@ -284,9 +300,7 @@ type Pusher interface {
// Health checks if the connection to the storage is available
Health(ctx context.Context) error
// Push stores the actions
Push(ctx context.Context, client database.ContextQueryExecuter, commands ...Command) (_ []Event, err error)
// Client returns the underlying database connection
Client() *database.DB
Push(ctx context.Context, client new_db.QueryExecutor, commands ...Command) (_ []Event, err error)
}
type FillFieldsEvent interface {
@@ -1,162 +0,0 @@
package eventstore_test
import (
"context"
_ "embed"
"fmt"
"strconv"
"testing"
"github.com/zitadel/zitadel/internal/eventstore"
)
//go:embed bench_payload.txt
var text string
func Benchmark_Push_SameAggregate(b *testing.B) {
ctx := context.Background()
smallPayload := struct {
Username string
Firstname string
Lastname string
}{
Username: "username",
Firstname: "firstname",
Lastname: "lastname",
}
bigPayload := struct {
Username string
Firstname string
Lastname string
Text string
}{
Username: "username",
Firstname: "firstname",
Lastname: "lastname",
Text: text,
}
commands := map[string][]eventstore.Command{
"no payload one command": {
generateCommand(eventstore.AggregateType(b.Name()), "id"),
},
"small payload one command": {
generateCommand(eventstore.AggregateType(b.Name()), "id", withTestData(smallPayload)),
},
"big payload one command": {
generateCommand(eventstore.AggregateType(b.Name()), "id", withTestData(bigPayload)),
},
"no payload multiple commands": {
generateCommand(eventstore.AggregateType(b.Name()), "id"),
generateCommand(eventstore.AggregateType(b.Name()), "id"),
generateCommand(eventstore.AggregateType(b.Name()), "id"),
},
"mixed payload multiple command": {
generateCommand(eventstore.AggregateType(b.Name()), "id", withTestData(smallPayload)),
generateCommand(eventstore.AggregateType(b.Name()), "id", withTestData(bigPayload)),
generateCommand(eventstore.AggregateType(b.Name()), "id", withTestData(smallPayload)),
generateCommand(eventstore.AggregateType(b.Name()), "id", withTestData(bigPayload)),
},
}
for cmdsKey, cmds := range commands {
for pusherKey, store := range pushers {
b.Run(fmt.Sprintf("Benchmark_Push_SameAggregate-%s-%s", pusherKey, cmdsKey), func(b *testing.B) {
b.StopTimer()
cleanupEventstore(clients[pusherKey])
b.StartTimer()
for n := 0; n < b.N; n++ {
_, err := store.Push(ctx, store.Client().DB, cmds...)
if err != nil {
b.Error(err)
}
}
})
}
}
}
func Benchmark_Push_MultipleAggregate_Parallel(b *testing.B) {
smallPayload := struct {
Username string
Firstname string
Lastname string
}{
Username: "username",
Firstname: "firstname",
Lastname: "lastname",
}
bigPayload := struct {
Username string
Firstname string
Lastname string
Text string
}{
Username: "username",
Firstname: "firstname",
Lastname: "lastname",
Text: text,
}
commandCreators := map[string]func(id string) []eventstore.Command{
"no payload one command": func(id string) []eventstore.Command {
return []eventstore.Command{
generateCommand(eventstore.AggregateType(b.Name()), id),
}
},
"small payload one command": func(id string) []eventstore.Command {
return []eventstore.Command{
generateCommand(eventstore.AggregateType(b.Name()), id, withTestData(smallPayload)),
}
},
"big payload one command": func(id string) []eventstore.Command {
return []eventstore.Command{
generateCommand(eventstore.AggregateType(b.Name()), id, withTestData(bigPayload)),
}
},
"no payload multiple commands": func(id string) []eventstore.Command {
return []eventstore.Command{
generateCommand(eventstore.AggregateType(b.Name()), id),
generateCommand(eventstore.AggregateType(b.Name()), id),
generateCommand(eventstore.AggregateType(b.Name()), id),
}
},
"mixed payload multiple command": func(id string) []eventstore.Command {
return []eventstore.Command{
generateCommand(eventstore.AggregateType(b.Name()), id, withTestData(smallPayload)),
generateCommand(eventstore.AggregateType(b.Name()), id, withTestData(bigPayload)),
generateCommand(eventstore.AggregateType(b.Name()), id, withTestData(smallPayload)),
generateCommand(eventstore.AggregateType(b.Name()), id, withTestData(bigPayload)),
}
},
}
for cmdsKey, commandCreator := range commandCreators {
for pusherKey, store := range pushers {
b.Run(fmt.Sprintf("Benchmark_Push_DifferentAggregate-%s-%s", cmdsKey, pusherKey), func(b *testing.B) {
b.StopTimer()
cleanupEventstore(clients[pusherKey])
ctx, cancel := context.WithCancel(context.Background())
b.StartTimer()
i := 0
b.RunParallel(func(p *testing.PB) {
for p.Next() {
i++
_, err := store.Push(ctx, store.Client().DB, commandCreator(strconv.Itoa(i))...)
if err != nil {
b.Error(err)
}
}
})
cancel()
})
}
}
}
+2 -1
View File
@@ -11,6 +11,7 @@ import (
"github.com/jackc/pgx/v5/pgconn"
"github.com/shopspring/decimal"
new_db "github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/api/service"
"github.com/zitadel/zitadel/internal/database"
@@ -348,7 +349,7 @@ func (repo *testPusher) Health(ctx context.Context) error {
return nil
}
func (repo *testPusher) Push(_ context.Context, _ database.ContextQueryExecuter, commands ...Command) (events []Event, err error) {
func (repo *testPusher) Push(_ context.Context, _ new_db.QueryExecutor, commands ...Command) (events []Event, err error) {
if len(repo.errs) != 0 {
err, repo.errs = repo.errs[0], repo.errs[1:]
return nil, err
@@ -14,7 +14,8 @@ import (
reflect "reflect"
decimal "github.com/shopspring/decimal"
database "github.com/zitadel/zitadel/internal/database"
database "github.com/zitadel/zitadel/backend/v3/storage/database"
database0 "github.com/zitadel/zitadel/internal/database"
eventstore "github.com/zitadel/zitadel/internal/eventstore"
gomock "go.uber.org/mock/gomock"
)
@@ -43,10 +44,10 @@ func (m *MockQuerier) EXPECT() *MockQuerierMockRecorder {
}
// Client mocks base method.
func (m *MockQuerier) Client() *database.DB {
func (m *MockQuerier) Client() *database0.DB {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Client")
ret0, _ := ret[0].(*database.DB)
ret0, _ := ret[0].(*database0.DB)
return ret0
}
@@ -137,20 +138,6 @@ func (m *MockPusher) EXPECT() *MockPusherMockRecorder {
return m.recorder
}
// Client mocks base method.
func (m *MockPusher) Client() *database.DB {
m.ctrl.T.Helper()
ret := m.ctrl.Call(m, "Client")
ret0, _ := ret[0].(*database.DB)
return ret0
}
// Client indicates an expected call of Client.
func (mr *MockPusherMockRecorder) Client() *gomock.Call {
mr.mock.ctrl.T.Helper()
return mr.mock.ctrl.RecordCallWithMethodType(mr.mock, "Client", reflect.TypeOf((*MockPusher)(nil).Client))
}
// Health mocks base method.
func (m *MockPusher) Health(arg0 context.Context) error {
m.ctrl.T.Helper()
@@ -166,7 +153,7 @@ func (mr *MockPusherMockRecorder) Health(arg0 any) *gomock.Call {
}
// Push mocks base method.
func (m *MockPusher) Push(arg0 context.Context, arg1 database.ContextQueryExecuter, arg2 ...eventstore.Command) ([]eventstore.Event, error) {
func (m *MockPusher) Push(arg0 context.Context, arg1 database.QueryExecutor, arg2 ...eventstore.Command) ([]eventstore.Event, error) {
m.ctrl.T.Helper()
varargs := []any{arg0, arg1}
for _, a := range arg2 {
@@ -15,6 +15,7 @@ import (
"github.com/shopspring/decimal"
"github.com/stretchr/testify/assert"
new_sql "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql"
"github.com/zitadel/zitadel/internal/database"
db_mock "github.com/zitadel/zitadel/internal/database/mock"
"github.com/zitadel/zitadel/internal/database/postgres"
@@ -646,7 +647,7 @@ func Test_query_events_with_postgres(t *testing.T) {
pusher := new_es.NewEventstore(dbClient)
// setup initial data for query
if _, err := pusher.Push(context.Background(), dbClient.DB, tt.fields.existingEvents...); err != nil {
if _, err := pusher.Push(context.Background(), new_sql.SQLPool(dbClient.DB), tt.fields.existingEvents...); err != nil {
t.Errorf("error in setup = %v", err)
return
}
+12 -15
View File
@@ -13,6 +13,8 @@ import (
"github.com/jackc/pgx/v5/stdlib"
"github.com/zitadel/logging"
new_db "github.com/zitadel/zitadel/backend/v3/storage/database"
new_sql "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/database/dialect"
"github.com/zitadel/zitadel/internal/eventstore"
@@ -32,7 +34,7 @@ var (
)
type Eventstore struct {
client *database.DB
client new_db.Pool
queue eventstore.ExecutionQueue
}
@@ -153,14 +155,9 @@ func RegisterEventstoreTypes(ctx context.Context, conn *pgx.Conn) error {
return nil
}
// Client implements the [eventstore.Pusher]
func (es *Eventstore) Client() *database.DB {
return es.client
}
func NewEventstore(client *database.DB, opts ...EventstoreOption) *Eventstore {
es := &Eventstore{
client: client,
client: new_sql.SQLPool(client.DB),
}
for _, opt := range opts {
opt(es)
@@ -169,7 +166,7 @@ func NewEventstore(client *database.DB, opts ...EventstoreOption) *Eventstore {
}
func (es *Eventstore) Health(ctx context.Context) error {
return es.client.PingContext(ctx)
return es.client.Ping(ctx)
}
var errTypesNotFound = errors.New("types not found")
@@ -188,24 +185,24 @@ func CheckExecutionPlan(ctx context.Context, conn *sql.Conn) error {
})
}
func (es *Eventstore) pushTx(ctx context.Context, client database.ContextQueryExecuter) (tx database.Tx, deferrable func(err error) error, err error) {
tx, ok := client.(database.Tx)
func (es *Eventstore) pushTx(ctx context.Context, client new_db.QueryExecutor) (tx new_db.Transaction, deferrable func(err error) error, err error) {
tx, ok := client.(new_db.Transaction)
if ok {
return tx, nil, nil
}
beginner, ok := client.(database.Beginner)
beginner, ok := client.(new_db.Beginner)
if !ok {
beginner = es.client
}
tx, err = beginner.BeginTx(ctx, &sql.TxOptions{
Isolation: sql.LevelReadCommitted,
ReadOnly: false,
tx, err = beginner.Begin(ctx, &new_db.TransactionOptions{
IsolationLevel: new_db.IsolationLevelReadCommitted,
AccessMode: new_db.AccessModeReadWrite,
})
if err != nil {
return nil, nil, err
}
return tx, func(err error) error { return database.CloseTransaction(tx, err) }, nil
return tx, func(err error) error { return tx.End(ctx, err) }, nil
}
type EventstoreOption func(*Eventstore)
+39 -45
View File
@@ -2,7 +2,6 @@ package eventstore
import (
"context"
"database/sql"
_ "embed"
"encoding/json"
"reflect"
@@ -10,8 +9,8 @@ import (
"strconv"
"strings"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
"github.com/zitadel/zitadel/internal/zerrors"
@@ -29,16 +28,12 @@ func (es *Eventstore) FillFields(ctx context.Context, events ...eventstore.FillF
ctx, span := tracing.NewSpan(ctx)
defer span.End()
tx, err := es.client.BeginTx(ctx, &sql.TxOptions{Isolation: sql.LevelReadCommitted})
tx, err := es.client.Begin(ctx, &database.TransactionOptions{IsolationLevel: database.IsolationLevelReadCommitted})
if err != nil {
return err
}
defer func() {
if err != nil {
_ = tx.Rollback()
return
}
err = tx.Commit()
err = tx.End(ctx, err)
}()
return handleFieldFillEvents(ctx, tx, events)
@@ -52,40 +47,39 @@ func (es *Eventstore) Search(ctx context.Context, conditions ...map[eventstore.F
var builder strings.Builder
args := buildSearchStatement(ctx, &builder, conditions...)
err = es.client.QueryContext(
rows, err := es.client.Query(
ctx,
func(rows *sql.Rows) error {
for rows.Next() {
var (
res eventstore.SearchResult
value fieldValue
)
err = rows.Scan(
&res.Aggregate.InstanceID,
&res.Aggregate.ResourceOwner,
&res.Aggregate.Type,
&res.Aggregate.ID,
&res.Object.Type,
&res.Object.ID,
&res.Object.Revision,
&res.FieldName,
&value.value,
)
if err != nil {
return err
}
res.Value = &value
result = append(result, &res)
}
return nil
},
builder.String(),
args...,
)
if err != nil {
return nil, err
}
defer rows.Close()
for rows.Next() {
var (
res eventstore.SearchResult
value fieldValue
)
err = rows.Scan(
&res.Aggregate.InstanceID,
&res.Aggregate.ResourceOwner,
&res.Aggregate.Type,
&res.Aggregate.ID,
&res.Object.Type,
&res.Object.ID,
&res.Object.Revision,
&res.FieldName,
&value.value,
)
if err != nil {
return nil, err
}
res.Value = &value
result = append(result, &res)
}
return result, nil
}
@@ -143,7 +137,7 @@ func buildSearchCondition(builder *strings.Builder, index int, conditions map[ev
return args
}
func (es *Eventstore) handleFieldCommands(ctx context.Context, tx database.Tx, commands []eventstore.Command) error {
func (es *Eventstore) handleFieldCommands(ctx context.Context, tx database.Transaction, commands []eventstore.Command) error {
for _, command := range commands {
if len(command.Fields()) > 0 {
if err := handleFieldOperations(ctx, tx, command.Fields()); err != nil {
@@ -154,7 +148,7 @@ func (es *Eventstore) handleFieldCommands(ctx context.Context, tx database.Tx, c
return nil
}
func handleFieldFillEvents(ctx context.Context, tx database.Tx, events []eventstore.FillFieldsEvent) error {
func handleFieldFillEvents(ctx context.Context, tx database.Transaction, events []eventstore.FillFieldsEvent) error {
for _, event := range events {
if len(event.Fields()) == 0 {
continue
@@ -166,7 +160,7 @@ func handleFieldFillEvents(ctx context.Context, tx database.Tx, events []eventst
return nil
}
func handleFieldOperations(ctx context.Context, tx database.Tx, operations []*eventstore.FieldOperation) error {
func handleFieldOperations(ctx context.Context, tx database.Transaction, operations []*eventstore.FieldOperation) error {
for _, operation := range operations {
if operation.Set != nil {
if err := handleFieldSet(ctx, tx, operation.Set); err != nil {
@@ -184,7 +178,7 @@ func handleFieldOperations(ctx context.Context, tx database.Tx, operations []*ev
return nil
}
func handleFieldSet(ctx context.Context, tx database.Tx, field *eventstore.Field) error {
func handleFieldSet(ctx context.Context, tx database.Transaction, field *eventstore.Field) error {
if len(field.UpsertConflictFields) == 0 {
return handleSearchInsert(ctx, tx, field)
}
@@ -195,12 +189,12 @@ const (
insertField = `INSERT INTO eventstore.fields (instance_id, resource_owner, aggregate_type, aggregate_id, object_type, object_id, object_revision, field_name, value, value_must_be_unique, should_index) VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11)`
)
func handleSearchInsert(ctx context.Context, tx database.Tx, field *eventstore.Field) error {
func handleSearchInsert(ctx context.Context, tx database.Transaction, field *eventstore.Field) error {
value, err := json.Marshal(field.Value.Value)
if err != nil {
return zerrors.ThrowInvalidArgument(err, "V3-fcrW1", "unable to marshal field value")
}
_, err = tx.ExecContext(
_, err = tx.Exec(
ctx,
insertField,
@@ -224,13 +218,13 @@ const (
fieldsUpsertSuffix = ` RETURNING * ) INSERT INTO eventstore.fields (instance_id, resource_owner, aggregate_type, aggregate_id, object_type, object_id, object_revision, field_name, value, value_must_be_unique, should_index) SELECT $1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11 WHERE NOT EXISTS (SELECT 1 FROM upsert)`
)
func handleSearchUpsert(ctx context.Context, tx database.Tx, field *eventstore.Field) error {
func handleSearchUpsert(ctx context.Context, tx database.Transaction, field *eventstore.Field) error {
value, err := json.Marshal(field.Value.Value)
if err != nil {
return zerrors.ThrowInvalidArgument(err, "V3-fcrW1", "unable to marshal field value")
}
_, err = tx.ExecContext(
_, err = tx.Exec(
ctx,
writeUpsertField(field.UpsertConflictFields),
@@ -270,12 +264,12 @@ func writeUpsertField(fields []eventstore.FieldType) string {
const removeSearch = `DELETE FROM eventstore.fields WHERE `
func handleSearchDelete(ctx context.Context, tx database.Tx, clauses map[eventstore.FieldType]any) error {
func handleSearchDelete(ctx context.Context, tx database.Transaction, clauses map[eventstore.FieldType]any) error {
if len(clauses) == 0 {
return zerrors.ThrowInvalidArgument(nil, "V3-oqlBZ", "no conditions")
}
stmt, args := writeDeleteField(clauses)
_, err := tx.ExecContext(ctx, stmt, args...)
_, err := tx.Exec(ctx, stmt, args...)
return err
}
+12 -23
View File
@@ -2,22 +2,22 @@ package eventstore
import (
"context"
"database/sql"
_ "embed"
"fmt"
"github.com/riverqueue/river"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/queue"
exec_repo "github.com/zitadel/zitadel/internal/repository/execution"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
)
func (es *Eventstore) Push(ctx context.Context, client database.ContextQueryExecuter, commands ...eventstore.Command) (events []eventstore.Event, err error) {
func (es *Eventstore) Push(ctx context.Context, client database.QueryExecutor, commands ...eventstore.Command) (events []eventstore.Event, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
@@ -29,20 +29,9 @@ func (es *Eventstore) Push(ctx context.Context, client database.ContextQueryExec
return events, err
}
func (es *Eventstore) writeCommands(ctx context.Context, client database.ContextQueryExecuter, commands []eventstore.Command) (_ []eventstore.Event, err error) {
var conn *sql.Conn
switch c := client.(type) {
case database.Client:
conn, err = c.Conn(ctx)
case nil:
conn, err = es.client.Conn(ctx)
client = conn
}
if err != nil {
return nil, err
}
if conn != nil {
defer conn.Close()
func (es *Eventstore) writeCommands(ctx context.Context, client database.QueryExecutor, commands []eventstore.Command) (_ []eventstore.Event, err error) {
if client == nil {
client = es.client
}
tx, close, err := es.pushTx(ctx, client)
@@ -56,7 +45,7 @@ func (es *Eventstore) writeCommands(ctx context.Context, client database.Context
}
// lock the instance for reading events if await events is set for the duration of the transaction.
_, err = tx.ExecContext(ctx, "SELECT pg_advisory_xact_lock_shared('eventstore.events2'::REGCLASS::OID::INTEGER, hashtext($1))", authz.GetInstance(ctx).InstanceID())
_, err = tx.Exec(ctx, "SELECT pg_advisory_xact_lock_shared('eventstore.events2'::REGCLASS::OID::INTEGER, hashtext($1))", authz.GetInstance(ctx).InstanceID())
if err != nil {
return nil, err
}
@@ -82,7 +71,7 @@ func (es *Eventstore) writeCommands(ctx context.Context, client database.Context
return events, nil
}
func writeEvents(ctx context.Context, tx database.Tx, commands []eventstore.Command) (_ []eventstore.Event, err error) {
func writeEvents(ctx context.Context, tx database.Transaction, commands []eventstore.Command) (_ []eventstore.Event, err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
@@ -91,7 +80,7 @@ func writeEvents(ctx context.Context, tx database.Tx, commands []eventstore.Comm
return nil, err
}
rows, err := tx.QueryContext(ctx, `select owner, created_at, "sequence", position from eventstore.push($1::eventstore.command[])`, cmds)
rows, err := tx.Query(ctx, `select owner, created_at, "sequence", position from eventstore.push($1::eventstore.command[])`, cmds)
if err != nil {
return nil, err
}
@@ -110,12 +99,12 @@ func writeEvents(ctx context.Context, tx database.Tx, commands []eventstore.Comm
return events, nil
}
func (es *Eventstore) queueExecutions(ctx context.Context, tx database.Tx, events []eventstore.Event) error {
func (es *Eventstore) queueExecutions(ctx context.Context, tx database.Transaction, events []eventstore.Event) error {
if es.queue == nil {
return nil
}
sqlTx, ok := tx.(*sql.Tx)
sqlTx, ok := tx.(*sql.Transaction)
if !ok {
types := make([]string, len(events))
for i, event := range events {
@@ -132,7 +121,7 @@ func (es *Eventstore) queueExecutions(ctx context.Context, tx database.Tx, event
return nil
}
return es.queue.InsertManyFastTx(
ctx, sqlTx, jobArgs,
ctx, sqlTx.Tx, jobArgs,
queue.WithQueueName(exec_repo.QueueName),
)
}
+23 -8
View File
@@ -2,7 +2,6 @@ package eventstore
import (
"context"
"database/sql"
_ "embed"
"testing"
@@ -11,6 +10,9 @@ import (
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
new_db "github.com/zitadel/zitadel/backend/v3/storage/database"
new_pg "github.com/zitadel/zitadel/backend/v3/storage/database/dialect/postgres"
"github.com/zitadel/zitadel/backend/v3/storage/database/dialect/sql"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/database/postgres"
@@ -268,7 +270,7 @@ func TestEventstore_queueExecutions(t *testing.T) {
}
type args struct {
ctx context.Context
tx database.Tx
tx new_db.Transaction
events []eventstore.Event
}
tests := []struct {
@@ -278,7 +280,7 @@ func TestEventstore_queueExecutions(t *testing.T) {
wantErr bool
}{
{
name: "incorrect Tx type, noop",
name: "no Tx type, noop",
queue: func(t *testing.T) eventstore.ExecutionQueue {
mQueue := mock.NewMockExecutionQueue(gomock.NewController(t))
return mQueue
@@ -290,6 +292,19 @@ func TestEventstore_queueExecutions(t *testing.T) {
},
wantErr: false,
},
{
name: "incorrect Tx type, noop",
queue: func(t *testing.T) eventstore.ExecutionQueue {
mQueue := mock.NewMockExecutionQueue(gomock.NewController(t))
return mQueue
},
args: args{
ctx: context.Background(),
tx: new_pg.PGxTx(nil),
events: events,
},
wantErr: false,
},
{
name: "no events",
queue: func(t *testing.T) eventstore.ExecutionQueue {
@@ -298,7 +313,7 @@ func TestEventstore_queueExecutions(t *testing.T) {
},
args: args{
ctx: context.Background(),
tx: &sql.Tx{},
tx: sql.SQLTx(nil),
events: []eventstore.Event{},
},
wantErr: false,
@@ -311,7 +326,7 @@ func TestEventstore_queueExecutions(t *testing.T) {
},
args: args{
ctx: context.Background(),
tx: &sql.Tx{},
tx: sql.SQLTx(nil),
events: events,
},
wantErr: false,
@@ -331,7 +346,7 @@ func TestEventstore_queueExecutions(t *testing.T) {
},
}),
),
tx: &sql.Tx{},
tx: sql.SQLTx(nil),
events: events,
},
wantErr: false,
@@ -360,7 +375,7 @@ func TestEventstore_queueExecutions(t *testing.T) {
{ExecutionID: "event"},
}),
),
tx: &sql.Tx{},
tx: sql.SQLTx(nil),
events: events,
},
wantErr: false,
@@ -389,7 +404,7 @@ func TestEventstore_queueExecutions(t *testing.T) {
{ExecutionID: "event/ex.removed"},
}),
),
tx: &sql.Tx{},
tx: sql.SQLTx(nil),
events: events,
},
wantErr: false,
+4 -4
View File
@@ -10,7 +10,7 @@ import (
"github.com/jackc/pgx/v5/pgconn"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/zerrors"
)
@@ -35,7 +35,7 @@ var (
// pushWithoutFunc implements pushing events before setup step 39 was introduced.
// TODO: remove with v3
func (es *Eventstore) pushWithoutFunc(ctx context.Context, client database.ContextQueryExecuter, commands ...eventstore.Command) (events []eventstore.Event, err error) {
func (es *Eventstore) pushWithoutFunc(ctx context.Context, client database.QueryExecutor, commands ...eventstore.Command) (events []eventstore.Event, err error) {
tx, closeTx, err := es.pushTx(ctx, client)
if err != nil {
return nil, err
@@ -69,13 +69,13 @@ func (es *Eventstore) pushWithoutFunc(ctx context.Context, client database.Conte
return events, nil
}
func (es *Eventstore) writeEventsOld(ctx context.Context, tx database.Tx, sequences []*latestSequence, commands []eventstore.Command) ([]eventstore.Event, error) {
func (es *Eventstore) writeEventsOld(ctx context.Context, tx database.Transaction, sequences []*latestSequence, commands []eventstore.Command) ([]eventstore.Event, error) {
events, placeholders, args, err := mapCommands(commands, sequences)
if err != nil {
return nil, err
}
rows, err := tx.QueryContext(ctx, fmt.Sprintf(pushStmt, strings.Join(placeholders, ", ")), args...)
rows, err := tx.Query(ctx, fmt.Sprintf(pushStmt, strings.Join(placeholders, ", ")), args...)
if err != nil {
return nil, err
}
+4 -5
View File
@@ -2,15 +2,14 @@ package eventstore
import (
"context"
"database/sql"
_ "embed"
"fmt"
"strings"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/internal/api/authz"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/zerrors"
)
@@ -23,11 +22,11 @@ type latestSequence struct {
//go:embed sequences_query.sql
var latestSequencesStmt string
func latestSequences(ctx context.Context, tx database.Tx, commands []eventstore.Command) ([]*latestSequence, error) {
func latestSequences(ctx context.Context, tx database.Transaction, commands []eventstore.Command) ([]*latestSequence, error) {
sequences := commandsToSequences(ctx, commands)
conditions, args := sequencesToSql(sequences)
rows, err := tx.QueryContext(ctx, fmt.Sprintf(latestSequencesStmt, strings.Join(conditions, " UNION ALL ")), args...)
rows, err := tx.Query(ctx, fmt.Sprintf(latestSequencesStmt, strings.Join(conditions, " UNION ALL ")), args...)
if err != nil {
return nil, zerrors.ThrowInternal(err, "V3-5jU5z", "Errors.Internal")
}
@@ -104,7 +103,7 @@ func sequencesToSql(sequences []*latestSequence) (conditions []string, args []an
return conditions, args
}
func scanToSequence(rows *sql.Rows, sequences []*latestSequence) error {
func scanToSequence(rows database.Rows, sequences []*latestSequence) error {
var aggregateType eventstore.AggregateType
var aggregateID, instanceID string
var currentSequence uint64
+4 -4
View File
@@ -10,7 +10,7 @@ import (
"github.com/jackc/pgx/v5/pgconn"
"github.com/zitadel/logging"
"github.com/zitadel/zitadel/internal/database"
"github.com/zitadel/zitadel/backend/v3/storage/database"
"github.com/zitadel/zitadel/internal/eventstore"
"github.com/zitadel/zitadel/internal/telemetry/tracing"
"github.com/zitadel/zitadel/internal/zerrors"
@@ -25,7 +25,7 @@ var (
addConstraintStmt string
)
func handleUniqueConstraints(ctx context.Context, tx database.Tx, commands []eventstore.Command) (err error) {
func handleUniqueConstraints(ctx context.Context, tx database.Transaction, commands []eventstore.Command) (err error) {
ctx, span := tracing.NewSpan(ctx)
defer func() { span.EndWithError(err) }()
@@ -62,7 +62,7 @@ func handleUniqueConstraints(ctx context.Context, tx database.Tx, commands []eve
}
if len(deletePlaceholders) > 0 {
_, err := tx.ExecContext(ctx, fmt.Sprintf(deleteConstraintStmt, strings.Join(deletePlaceholders, " OR ")), deleteArgs...)
_, err := tx.Exec(ctx, fmt.Sprintf(deleteConstraintStmt, strings.Join(deletePlaceholders, " OR ")), deleteArgs...)
if err != nil {
logging.WithError(err).Warn("delete unique constraint failed")
errMessage := "Errors.Internal"
@@ -73,7 +73,7 @@ func handleUniqueConstraints(ctx context.Context, tx database.Tx, commands []eve
}
}
if len(addPlaceholders) > 0 {
_, err := tx.ExecContext(ctx, fmt.Sprintf(addConstraintStmt, strings.Join(addPlaceholders, ", ")), addArgs...)
_, err := tx.Exec(ctx, fmt.Sprintf(addConstraintStmt, strings.Join(addPlaceholders, ", ")), addArgs...)
if err != nil {
logging.WithError(err).Warn("add unique constraint failed")
errMessage := "Errors.Internal"