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 pgxPool struct {
|
|
|
|
*pgxpool.Pool
|
|
|
|
}
|
2025-04-29 06:03:47 +02:00
|
|
|
|
2025-06-17 09:46:01 +02:00
|
|
|
var _ database.Pool = (*pgxPool)(nil)
|
|
|
|
|
|
|
|
func PGxPool(pool *pgxpool.Pool) *pgxPool {
|
|
|
|
return &pgxPool{
|
|
|
|
Pool: pool,
|
|
|
|
}
|
|
|
|
}
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
// Acquire implements [database.Pool].
|
|
|
|
func (c *pgxPool) Acquire(ctx context.Context) (database.Client, error) {
|
|
|
|
conn, err := c.Pool.Acquire(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2025-05-26 09:31:45 +02:00
|
|
|
return &pgxConn{Conn: conn}, nil
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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...)
|
|
|
|
return &Rows{rows}, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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 c.Pool.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 *pgxPool) Exec(ctx context.Context, sql string, args ...any) (int64, error) {
|
|
|
|
res, err := c.Pool.Exec(ctx, sql, args...)
|
|
|
|
return res.RowsAffected(), err
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Begin implements [database.Pool].
|
|
|
|
func (c *pgxPool) Begin(ctx context.Context, opts *database.TransactionOptions) (database.Transaction, error) {
|
|
|
|
tx, err := c.Pool.BeginTx(ctx, transactionOptionsToPgx(opts))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &pgxTx{tx}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Close implements [database.Pool].
|
|
|
|
func (c *pgxPool) Close(_ context.Context) error {
|
|
|
|
c.Pool.Close()
|
|
|
|
return nil
|
|
|
|
}
|
2025-05-26 08:20:14 +02:00
|
|
|
|
|
|
|
// Migrate implements [database.Migrator].
|
|
|
|
func (c *pgxPool) Migrate(ctx context.Context) error {
|
2025-05-26 09:31:45 +02:00
|
|
|
if isMigrated {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2025-05-26 08:20:14 +02:00
|
|
|
client, err := c.Pool.Acquire(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2025-05-26 09:31:45 +02:00
|
|
|
|
|
|
|
err = migration.Migrate(ctx, client.Conn())
|
|
|
|
isMigrated = err == nil
|
|
|
|
return err
|
2025-05-26 08:20:14 +02:00
|
|
|
}
|
2025-06-17 09:46:01 +02:00
|
|
|
|
|
|
|
// Migrate implements [database.PoolTest].
|
|
|
|
func (c *pgxPool) MigrateTest(ctx context.Context) error {
|
|
|
|
client, err := c.Pool.Acquire(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = migration.Migrate(ctx, client.Conn())
|
|
|
|
isMigrated = err == nil
|
|
|
|
return err
|
|
|
|
}
|