2025-04-29 06:03:47 +02:00
|
|
|
package postgres
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"errors"
|
|
|
|
"slices"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/jackc/pgx/v5/pgxpool"
|
|
|
|
"github.com/mitchellh/mapstructure"
|
|
|
|
|
|
|
|
"github.com/zitadel/zitadel/backend/v3/storage/database"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2025-05-26 09:31:45 +02:00
|
|
|
_ database.Connector = (*Config)(nil)
|
|
|
|
Name = "postgres"
|
|
|
|
isMigrated bool
|
2025-04-29 06:03:47 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
type Config struct {
|
|
|
|
*pgxpool.Config
|
2025-05-26 08:20:14 +02:00
|
|
|
*pgxpool.Pool
|
2025-04-29 06:03:47 +02:00
|
|
|
|
|
|
|
// Host string
|
|
|
|
// Port int32
|
|
|
|
// Database string
|
|
|
|
// MaxOpenConns uint32
|
|
|
|
// MaxIdleConns uint32
|
|
|
|
// MaxConnLifetime time.Duration
|
|
|
|
// MaxConnIdleTime time.Duration
|
|
|
|
// User User
|
|
|
|
// // Additional options to be appended as options=<Options>
|
|
|
|
// // The value will be taken as is. Multiple options are space separated.
|
|
|
|
// Options string
|
|
|
|
|
2025-06-13 17:05:37 +02:00
|
|
|
// configuredFields []string
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Connect implements [database.Connector].
|
|
|
|
func (c *Config) Connect(ctx context.Context) (database.Pool, error) {
|
2025-05-26 08:20:14 +02:00
|
|
|
pool, err := c.getPool(ctx)
|
2025-04-29 06:03:47 +02:00
|
|
|
if err != nil {
|
2025-07-17 16:20:02 +02:00
|
|
|
return nil, wrapError(err)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
if err = pool.Ping(ctx); err != nil {
|
2025-07-17 16:20:02 +02:00
|
|
|
return nil, wrapError(err)
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
2025-05-26 09:31:45 +02:00
|
|
|
return &pgxPool{Pool: pool}, nil
|
2025-04-29 06:03:47 +02:00
|
|
|
}
|
|
|
|
|
2025-05-26 08:20:14 +02:00
|
|
|
func (c *Config) getPool(ctx context.Context) (*pgxpool.Pool, error) {
|
|
|
|
if c.Pool != nil {
|
|
|
|
return c.Pool, nil
|
|
|
|
}
|
|
|
|
return pgxpool.NewWithConfig(ctx, c.Config)
|
|
|
|
}
|
|
|
|
|
2025-04-29 06:03:47 +02:00
|
|
|
func NameMatcher(name string) bool {
|
|
|
|
return slices.Contains([]string{"postgres", "pg"}, strings.ToLower(name))
|
|
|
|
}
|
|
|
|
|
|
|
|
func DecodeConfig(input any) (database.Connector, error) {
|
|
|
|
switch c := input.(type) {
|
|
|
|
case string:
|
|
|
|
config, err := pgxpool.ParseConfig(c)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Config{Config: config}, nil
|
|
|
|
case map[string]any:
|
|
|
|
connector := new(Config)
|
|
|
|
decoder, err := mapstructure.NewDecoder(&mapstructure.DecoderConfig{
|
|
|
|
DecodeHook: mapstructure.StringToTimeDurationHookFunc(),
|
|
|
|
WeaklyTypedInput: true,
|
|
|
|
Result: connector,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err = decoder.Decode(c); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Config{
|
|
|
|
Config: &pgxpool.Config{},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, errors.New("invalid configuration")
|
|
|
|
}
|