- lagoon.OrderBy takes variadic lagoon.OrderOption values; lagoon.Collate(name) emits a validated, double-quoted COLLATE clause (e.g. "pl-x-icu") - remove the exported CheckLocale and the ICU pl-PL check from Open and Use - framework test containers and per-test databases are plain PostgreSQL - lagoon README, root README and docs pages drop the locale requirement; queries-and-pagination gains a "Sorting with a collation" section backed by ExampleCollate
118 lines
3.5 KiB
Go
118 lines
3.5 KiB
Go
package lagoon
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"git.golem15.com/golem15/summercms/modules/backpack"
|
|
"git.golem15.com/golem15/summercms/modules/compass"
|
|
_ "github.com/jackc/pgx/v5/stdlib"
|
|
"gorm.io/driver/postgres"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
// Open pings dsn through pgx stdlib and returns that exact *sql.DB plus a
|
|
// GORM handle opened on it.
|
|
//
|
|
// The job worker in the conga module opens its own single-connection
|
|
// pgxpool.Pool for River LISTEN/NOTIFY. Do not create that listener pool
|
|
// here; application queries, including River's, share this *sql.DB.
|
|
func Open(ctx context.Context, dsn string) (*sql.DB, *gorm.DB, error) {
|
|
dsn = strings.TrimSpace(dsn)
|
|
if dsn == "" {
|
|
return nil, nil, fmt.Errorf("lagoon: database.dsn is empty (set SUMMER_DATABASE__DSN)")
|
|
}
|
|
sqlDB, err := sql.Open("pgx", dsn)
|
|
if err != nil {
|
|
return nil, nil, fmt.Errorf("lagoon: open postgres: %w", err)
|
|
}
|
|
if err := sqlDB.PingContext(ctx); err != nil {
|
|
_ = sqlDB.Close()
|
|
return nil, nil, fmt.Errorf("lagoon: ping postgres: %w", err)
|
|
}
|
|
gdb, err := gormFromSQL(sqlDB)
|
|
if err != nil {
|
|
_ = sqlDB.Close()
|
|
return nil, nil, err
|
|
}
|
|
return sqlDB, gdb, nil
|
|
}
|
|
|
|
// Use pings an existing pool and returns a GORM handle opened on that exact
|
|
// *sql.DB. Callers that already hold a pool (tests, the app boot seam) must
|
|
// not open a second connection.
|
|
func Use(ctx context.Context, sqlDB *sql.DB) (*gorm.DB, error) {
|
|
if sqlDB == nil {
|
|
return nil, fmt.Errorf("lagoon: sql db is nil")
|
|
}
|
|
if err := sqlDB.PingContext(ctx); err != nil {
|
|
return nil, fmt.Errorf("lagoon: ping postgres: %w", err)
|
|
}
|
|
return gormFromSQL(sqlDB)
|
|
}
|
|
|
|
func gormFromSQL(sqlDB *sql.DB) (*gorm.DB, error) {
|
|
gdb, err := gorm.Open(postgres.New(postgres.Config{Conn: sqlDB}), &gorm.Config{})
|
|
if err != nil {
|
|
return nil, fmt.Errorf("lagoon: gorm open: %w", err)
|
|
}
|
|
got, err := gdb.DB()
|
|
if err != nil {
|
|
return nil, fmt.Errorf("lagoon: gorm sql handle: %w", err)
|
|
}
|
|
if got != sqlDB {
|
|
return nil, fmt.Errorf("lagoon: GORM is not using the shared *sql.DB")
|
|
}
|
|
if err := registerAfterCommit(gdb); err != nil {
|
|
return nil, fmt.Errorf("lagoon: register %s: %w", AfterCommitCallback, err)
|
|
}
|
|
return gdb, nil
|
|
}
|
|
|
|
// OpenFromApp reads database.dsn from app config and opens the shared pool.
|
|
// It also loads app.key via LoadAppKey and PublishEncryptionKeys so Encrypted
|
|
// columns do not re-read config on every row.
|
|
func OpenFromApp(ctx context.Context, app *backpack.App) (*sql.DB, *gorm.DB, error) {
|
|
if app == nil || app.Config == nil {
|
|
return nil, nil, fmt.Errorf("lagoon: app config is missing")
|
|
}
|
|
sqlDB, gdb, err := Open(ctx, DSN(app.Config))
|
|
if err != nil {
|
|
return nil, nil, err
|
|
}
|
|
if err := loadEncryptionKeysFromApp(app); err != nil {
|
|
_ = sqlDB.Close()
|
|
return nil, nil, err
|
|
}
|
|
return sqlDB, gdb, nil
|
|
}
|
|
|
|
// DSN returns database.dsn from layered config (env SUMMER_DATABASE__DSN).
|
|
func DSN(cfg *compass.Config) string {
|
|
if cfg == nil {
|
|
return ""
|
|
}
|
|
return strings.TrimSpace(cfg.String("database.dsn"))
|
|
}
|
|
|
|
// Publish stores the shared SQL pool and GORM handle on the app. Both
|
|
// handles refer to the same *sql.DB. It then runs the callbacks queued by
|
|
// OnDatabase and returns the first error.
|
|
func Publish(app *backpack.App, sqlDB *sql.DB, gdb *gorm.DB) error {
|
|
if app == nil {
|
|
return fmt.Errorf("lagoon: app is nil")
|
|
}
|
|
if sqlDB == nil || gdb == nil {
|
|
return fmt.Errorf("lagoon: database handles are nil")
|
|
}
|
|
if err := app.Publish(sqlDB); err != nil {
|
|
return err
|
|
}
|
|
if err := app.Publish(gdb); err != nil {
|
|
return err
|
|
}
|
|
return runDatabaseHooks(app, sqlDB, gdb)
|
|
}
|