mirror of
https://github.com/andwati/zitadel.git
synced 2026-10-19 06:02:52 +00:00
# Which Problems Are Solved This PR provides an example for testing DB reduction to relational tables without exploiting integration tests. # How the Problems Are Solved - Set up a disposable embedded Postgres DB used to store the results of the calls to the reducers and verify such results - Add utility method to return transactions from the existing DB pool. These transactions can be used by the single test cases to insert and read data from DB - Add a test to verify that the handler contains the expected reducers (checking also that they are on the right aggregate type) - Add tests to verify the single reduce functions. A parent test creates the required objects to satisfy the foreign key constraints. # Additional Changes - Typo fixed in `postgres.DecodeConfig()` - Discard unneeded `projectionName` parameter for `Executor` function # Additional Context - Closes #11430
116 lines
2.8 KiB
Go
116 lines
2.8 KiB
Go
package postgres
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
"github.com/jackc/pgx/v5/stdlib"
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database/dialect/postgres/migration"
|
|
)
|
|
|
|
type pgxPool struct {
|
|
*pgxpool.Pool
|
|
}
|
|
|
|
// RawDB implements [database.PoolTest].
|
|
func (p *pgxPool) RawDB() *sql.DB {
|
|
return stdlib.OpenDBFromPool(p.Pool)
|
|
}
|
|
|
|
var _ database.Pool = (*pgxPool)(nil)
|
|
var _ database.PoolTest = (*pgxPool)(nil)
|
|
|
|
func PGxPool(pool *pgxpool.Pool) *pgxPool {
|
|
return &pgxPool{
|
|
Pool: pool,
|
|
}
|
|
}
|
|
|
|
// Acquire implements [database.Pool].
|
|
func (p *pgxPool) Acquire(ctx context.Context) (database.Connection, error) {
|
|
conn, err := p.Pool.Acquire(ctx)
|
|
if err != nil {
|
|
return nil, wrapError(err)
|
|
}
|
|
return &pgxConn{Conn: conn}, nil
|
|
}
|
|
|
|
// Query implements [database.Pool].
|
|
// Subtle: this method shadows the method (Pool).Query of pgxPool.Pool.
|
|
func (p *pgxPool) Query(ctx context.Context, sql string, args ...any) (database.Rows, error) {
|
|
rows, err := p.Pool.Query(ctx, sql, args...)
|
|
if err != nil {
|
|
return nil, wrapError(err)
|
|
}
|
|
return &Rows{rows}, nil
|
|
}
|
|
|
|
// QueryRow implements [database.Pool].
|
|
// Subtle: this method shadows the method (Pool).QueryRow of pgxPool.Pool.
|
|
func (p *pgxPool) QueryRow(ctx context.Context, sql string, args ...any) database.Row {
|
|
return &Row{p.Pool.QueryRow(ctx, sql, args...)}
|
|
}
|
|
|
|
// Exec implements [database.Pool].
|
|
// Subtle: this method shadows the method (Pool).Exec of pgxPool.Pool.
|
|
func (p *pgxPool) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
|
|
res, err := p.Pool.Exec(ctx, sql, args...)
|
|
if err != nil {
|
|
return 0, wrapError(err)
|
|
}
|
|
return res.RowsAffected(), nil
|
|
}
|
|
|
|
// Begin implements [database.Pool].
|
|
func (p *pgxPool) Begin(ctx context.Context, opts *database.TransactionOptions) (database.Transaction, error) {
|
|
tx, err := p.BeginTx(ctx, transactionOptionsToPgx(opts))
|
|
if err != nil {
|
|
return nil, wrapError(err)
|
|
}
|
|
return &Transaction{tx}, nil
|
|
}
|
|
|
|
// Close implements [database.Pool].
|
|
func (p *pgxPool) Close(_ context.Context) error {
|
|
p.Pool.Close()
|
|
return nil
|
|
}
|
|
|
|
// Ping implements [database.Pool].
|
|
func (p *pgxPool) Ping(ctx context.Context) error {
|
|
return wrapError(p.Pool.Ping(ctx))
|
|
}
|
|
|
|
// Migrate implements [database.Migrator].
|
|
func (p *pgxPool) Migrate(ctx context.Context) error {
|
|
if isMigrated {
|
|
return nil
|
|
}
|
|
|
|
client, err := p.Pool.Acquire(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer client.Release()
|
|
|
|
err = migration.Migrate(ctx, client.Conn())
|
|
isMigrated = err == nil
|
|
return wrapError(err)
|
|
}
|
|
|
|
// Migrate implements [database.PoolTest].
|
|
func (p *pgxPool) MigrateTest(ctx context.Context) error {
|
|
client, err := p.Pool.Acquire(ctx)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer client.Release()
|
|
|
|
err = migration.Migrate(ctx, client.Conn())
|
|
isMigrated = err == nil
|
|
return err
|
|
}
|