2025-04-29 06:03:47 +02:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
2025-05-26 08:20:14 +02:00
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database/dialect/postgres/migration"
|
2025-04-29 06:03:47 +02:00
|
|
|
)
|
|
|
|
|
2025-05-26 09:31:45 +02:00
|
|
|
type pgxConn struct {
|
|
|
|
*pgxpool.Conn
|
|
|
|
}
|
2025-04-29 06:03:47 +02:00
|
|
|
|
2025-06-17 09:46:01 +02:00
|
|
|
var _ database.Client = (*pgxConn)(nil)
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
// Release implements [database.Client].
|
|
|
|
func (c *pgxConn) Release(_ context.Context) error {
|
|
|
|
c.Conn.Release()
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Begin implements [database.Client].
|
|
|
|
func (c *pgxConn) Begin(ctx context.Context, opts *database.TransactionOptions) (database.Transaction, error) {
|
|
|
|
tx, err := c.Conn.BeginTx(ctx, transactionOptionsToPgx(opts))
|
|
|
|
if err != nil {
|
2025-07-17 00:54:21 +02:00
|
|
|
return nil, wrapError(err)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
return &pgxTx{tx}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Query implements sql.Client.
|
|
|
|
// 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...)
|
2025-07-17 00:54:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, wrapError(err)
|
|
|
|
}
|
|
|
|
return &Rows{rows}, nil
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// QueryRow implements sql.Client.
|
|
|
|
// 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 c.Conn.QueryRow(ctx, sql, args...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exec implements [database.Pool].
|
|
|
|
// Subtle: this method shadows the method (Pool).Exec of pgxPool.Pool.
|
2025-06-17 09:46:01 +02:00
|
|
|
func (c *pgxConn) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
|
|
|
|
res, err := c.Conn.Exec(ctx, sql, args...)
|
2025-07-17 00:54:21 +02:00
|
|
|
if err != nil {
|
|
|
|
return 0, wrapError(err)
|
|
|
|
}
|
|
|
|
return res.RowsAffected(), nil
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
2025-05-26 08:20:14 +02:00
|
|
|
|
|
|
|
// Migrate implements [database.Migrator].
|
|
|
|
func (c *pgxConn) Migrate(ctx context.Context) error {
|
2025-05-26 09:31:45 +02:00
|
|
|
if isMigrated {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
err := migration.Migrate(ctx, c.Conn.Conn())
|
|
|
|
isMigrated = err == nil
|
2025-07-17 00:54:21 +02:00
|
|
|
return wrapError(err)
|
2025-05-26 08:20:14 +02:00
|
|
|
}
|