refactor: database interaction and error handling (#10762)

This pull request introduces a significant refactoring of the database
interaction layer, focusing on improving explicitness, transactional
control, and error handling. The core change is the removal of the
stateful `QueryExecutor` from repository instances. Instead, it is now
passed as an argument to each method that interacts with the database.

This change makes transaction management more explicit and flexible, as
the same repository instance can be used with a database pool or a
specific transaction without needing to be re-instantiated.

### Key Changes

- **Explicit `QueryExecutor` Passing:**
- All repository methods (`Get`, `List`, `Create`, `Update`, `Delete`,
etc.) in `InstanceRepository`, `OrganizationRepository`,
`UserRepository`, and their sub-repositories now require a
`database.QueryExecutor` (e.g., a `*pgxpool.Pool` or `pgx.Tx`) as the
first argument.
- Repository constructors no longer accept a `QueryExecutor`. For
example, `repository.InstanceRepository(pool)` is now
`repository.InstanceRepository()`.

- **Enhanced Error Handling:**
- A new `database.MissingConditionError` is introduced to enforce
required query conditions, such as ensuring an `instance_id` is always
present in `UPDATE` and `DELETE` operations.
- The database error wrapper in the `postgres` package now correctly
identifies and wraps `pgx.ErrTooManyRows` and similar errors from the
`scany` library into a `database.MultipleRowsFoundError`.

- **Improved Database Conditions:**
- The `database.Condition` interface now includes a
`ContainsColumn(Column) bool` method. This allows for runtime checks to
ensure that critical filters (like `instance_id`) are included in a
query, preventing accidental cross-tenant data modification.
- A new `database.Exists()` condition has been added to support `EXISTS`
subqueries, enabling more complex filtering logic, such as finding an
organization that has a specific domain.

- **Repository and Interface Refactoring:**
- The method for loading related entities (e.g., domains for an
organization) has been changed from a boolean flag (`Domains(true)`) to
a more explicit, chainable method (`LoadDomains()`). This returns a new
repository instance configured to load the sub-resource, promoting
immutability.
- The custom `OrgIdentifierCondition` has been removed in favor of using
the standard `database.Condition` interface, simplifying the API.

- **Code Cleanup and Test Updates:**
  - Unnecessary struct embeddings and metadata have been removed.
- All integration and repository tests have been updated to reflect the
new method signatures, passing the database pool or transaction object
explicitly.
- New tests have been added to cover the new `ExistsDomain`
functionality and other enhancements.

These changes make the data access layer more robust, predictable, and
easier to work with, especially in the context of database transactions.
This commit is contained in:
Silvan
2025-09-24 10:12:31 +00:00
committed by GitHub
parent 09d09ab337
commit cccfc816f6
53 changed files with 3893 additions and 1423 deletions
@@ -13,15 +13,15 @@ type pgxConn struct {
*pgxpool.Conn
}
var _ database.Client = (*pgxConn)(nil)
var _ database.Connection = (*pgxConn)(nil)
// Release implements [database.Client].
// Release implements [database.Connection].
func (c *pgxConn) Release(_ context.Context) error {
c.Conn.Release()
return nil
}
// Begin implements [database.Client].
// Begin implements [database.Connection].
func (c *pgxConn) Begin(ctx context.Context, opts *database.TransactionOptions) (database.Transaction, error) {
tx, err := c.BeginTx(ctx, transactionOptionsToPgx(opts))
if err != nil {
@@ -30,8 +30,8 @@ func (c *pgxConn) Begin(ctx context.Context, opts *database.TransactionOptions)
return &Transaction{tx}, nil
}
// Query implements sql.Client.
// Subtle: this method shadows the method (*Conn).Query of pgxConn.Conn.
// Query implements [database.Connection].
// Subtle: this method shadows the method (*Conn).Query of [pgxConn.Conn].
func (c *pgxConn) Query(ctx context.Context, sql string, args ...any) (database.Rows, error) {
rows, err := c.Conn.Query(ctx, sql, args...)
if err != nil {
@@ -40,14 +40,14 @@ func (c *pgxConn) Query(ctx context.Context, sql string, args ...any) (database.
return &Rows{rows}, nil
}
// QueryRow implements sql.Client.
// Subtle: this method shadows the method (*Conn).QueryRow of pgxConn.Conn.
// QueryRow implements [database.Connection].
// Subtle: this method shadows the method (*Conn).QueryRow of [pgxConn.Conn].
func (c *pgxConn) QueryRow(ctx context.Context, sql string, args ...any) database.Row {
return &Row{c.Conn.QueryRow(ctx, sql, args...)}
}
// Exec implements [database.Pool].
// Subtle: this method shadows the method (Pool).Exec of pgxPool.Pool.
// QueryRow implements [database.Connection].
// Subtle: this method shadows the method (*Conn).QueryRow of [pgxConn.Conn].
func (c *pgxConn) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
res, err := c.Conn.Exec(ctx, sql, args...)
if err != nil {
@@ -2,6 +2,7 @@ package postgres
import (
"errors"
"strings"
"github.com/jackc/pgx/v5"
"github.com/jackc/pgx/v5/pgconn"
@@ -16,6 +17,18 @@ func wrapError(err error) error {
if errors.Is(err, pgx.ErrNoRows) {
return database.NewNoRowFoundError(err)
}
if errors.Is(err, pgx.ErrTooManyRows) {
return database.NewMultipleRowsFoundError(err)
}
// scany only exports its errors as strings
if strings.HasPrefix(err.Error(), "scany: expected 1 row, got: ") {
return database.NewMultipleRowsFoundError(err)
}
if strings.HasPrefix(err.Error(), "scany:") || strings.HasPrefix(err.Error(), "scanning:") {
return database.NewScanError(err)
}
var pgxErr *pgconn.PgError
if !errors.As(err, &pgxErr) {
return database.NewUnknownError(err)
@@ -0,0 +1,16 @@
package migration
import (
_ "embed"
)
var (
//go:embed 004_correct_set_updated_at/up.sql
up004CorrectSetUpdatedAt string
//go:embed 004_correct_set_updated_at/down.sql
down004CorrectSetUpdatedAt string
)
func init() {
registerSQLMigration(4, up004CorrectSetUpdatedAt, down004CorrectSetUpdatedAt)
}
@@ -0,0 +1,23 @@
CREATE OR REPLACE TRIGGER trigger_set_updated_at
BEFORE UPDATE ON zitadel.instances
FOR EACH ROW
WHEN (OLD.updated_at IS NOT DISTINCT FROM NEW.updated_at)
EXECUTE FUNCTION zitadel.set_updated_at();
CREATE OR REPLACE TRIGGER trigger_set_updated_at
BEFORE UPDATE ON zitadel.organizations
FOR EACH ROW
WHEN (OLD.updated_at IS NOT DISTINCT FROM NEW.updated_at)
EXECUTE FUNCTION zitadel.set_updated_at();
CREATE OR REPLACE TRIGGER trg_set_updated_at_instance_domains
BEFORE UPDATE ON zitadel.instance_domains
FOR EACH ROW
WHEN (OLD.updated_at IS NOT DISTINCT FROM NEW.updated_at)
EXECUTE FUNCTION zitadel.set_updated_at();
CREATE OR REPLACE TRIGGER trg_set_updated_at_org_domains
BEFORE UPDATE ON zitadel.org_domains
FOR EACH ROW
WHEN (OLD.updated_at IS NOT DISTINCT FROM NEW.updated_at)
EXECUTE FUNCTION zitadel.set_updated_at();
@@ -0,0 +1,23 @@
CREATE OR REPLACE TRIGGER trigger_set_updated_at
BEFORE UPDATE ON zitadel.instances
FOR EACH ROW
WHEN (NEW.updated_at IS NULL)
EXECUTE FUNCTION zitadel.set_updated_at();
CREATE OR REPLACE TRIGGER trigger_set_updated_at
BEFORE UPDATE ON zitadel.organizations
FOR EACH ROW
WHEN (NEW.updated_at IS NULL)
EXECUTE FUNCTION zitadel.set_updated_at();
CREATE OR REPLACE TRIGGER trg_set_updated_at_instance_domains
BEFORE UPDATE ON zitadel.instance_domains
FOR EACH ROW
WHEN (NEW.updated_at IS NULL)
EXECUTE FUNCTION zitadel.set_updated_at();
CREATE OR REPLACE TRIGGER trg_set_updated_at_org_domains
BEFORE UPDATE ON zitadel.org_domains
FOR EACH ROW
WHEN (NEW.updated_at IS NULL)
EXECUTE FUNCTION zitadel.set_updated_at();
@@ -22,8 +22,8 @@ func PGxPool(pool *pgxpool.Pool) *pgxPool {
}
// Acquire implements [database.Pool].
func (c *pgxPool) Acquire(ctx context.Context) (database.Client, error) {
conn, err := c.Pool.Acquire(ctx)
func (p *pgxPool) Acquire(ctx context.Context) (database.Connection, error) {
conn, err := p.Pool.Acquire(ctx)
if err != nil {
return nil, wrapError(err)
}
@@ -32,8 +32,8 @@ func (c *pgxPool) Acquire(ctx context.Context) (database.Client, error) {
// Query implements [database.Pool].
// Subtle: this method shadows the method (Pool).Query of pgxPool.Pool.
func (c *pgxPool) Query(ctx context.Context, sql string, args ...any) (database.Rows, error) {
rows, err := c.Pool.Query(ctx, sql, args...)
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)
}
@@ -42,14 +42,14 @@ func (c *pgxPool) Query(ctx context.Context, sql string, args ...any) (database.
// QueryRow implements [database.Pool].
// Subtle: this method shadows the method (Pool).QueryRow of pgxPool.Pool.
func (c *pgxPool) QueryRow(ctx context.Context, sql string, args ...any) database.Row {
return &Row{c.Pool.QueryRow(ctx, sql, args...)}
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 (c *pgxPool) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
res, err := c.Pool.Exec(ctx, sql, args...)
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)
}
@@ -57,8 +57,8 @@ func (c *pgxPool) Exec(ctx context.Context, sql string, args ...any) (int64, err
}
// Begin implements [database.Pool].
func (c *pgxPool) Begin(ctx context.Context, opts *database.TransactionOptions) (database.Transaction, error) {
tx, err := c.BeginTx(ctx, transactionOptionsToPgx(opts))
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)
}
@@ -66,23 +66,23 @@ func (c *pgxPool) Begin(ctx context.Context, opts *database.TransactionOptions)
}
// Close implements [database.Pool].
func (c *pgxPool) Close(_ context.Context) error {
c.Pool.Close()
func (p *pgxPool) Close(_ context.Context) error {
p.Pool.Close()
return nil
}
// Ping implements [database.Pool].
func (c *pgxPool) Ping(ctx context.Context) error {
return wrapError(c.Pool.Ping(ctx))
func (p *pgxPool) Ping(ctx context.Context) error {
return wrapError(p.Pool.Ping(ctx))
}
// Migrate implements [database.Migrator].
func (c *pgxPool) Migrate(ctx context.Context) error {
func (p *pgxPool) Migrate(ctx context.Context) error {
if isMigrated {
return nil
}
client, err := c.Pool.Acquire(ctx)
client, err := p.Pool.Acquire(ctx)
if err != nil {
return err
}
@@ -93,8 +93,8 @@ func (c *pgxPool) Migrate(ctx context.Context) error {
}
// Migrate implements [database.PoolTest].
func (c *pgxPool) MigrateTest(ctx context.Context) error {
client, err := c.Pool.Acquire(ctx)
func (p *pgxPool) MigrateTest(ctx context.Context) error {
client, err := p.Pool.Acquire(ctx)
if err != nil {
return err
}