postgres

package
v0.9.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 18, 2026 License: Apache-2.0 Imports: 15 Imported by: 0

Documentation

Overview

Package postgres provides a gorm.Dialector implementation for PostgreSQL databases to support multitenancy in GORM applications, enabling tenant-specific operations and shared resources management using the "shared database, separate schemas" approach.

To ensure data integrity and schema isolation across tenants, gorm.DB.AutoMigrate is disabled; use MigratePublicSchema and MigrateTenantModels instead. Tenant migrations are guarded by PostgreSQL transaction advisory locks so only one migration runs at a time, with exponential-backoff retry enabled by default.

Index

Constants

View Source
const DriverName = "postgres"

DriverName is the name of the PostgreSQL driver.

Variables

View Source
var (
	ErrNoTenantTables = errors.New("no tenant tables to migrate")
	ErrNoPublicTables = errors.New("no public tables to migrate")
)

Migration errors.

View Source
var ErrEmptySchemaName = errors.New("schema name is empty")

ErrEmptySchemaName is returned when SetSearchPath is called with an empty schema name.

Functions

func CurrentSearchPath

func CurrentSearchPath(tx *gorm.DB) string

CurrentSearchPath returns the current search path for the given database connection.

func DropSchemaForTenant

func DropSchemaForTenant(db *gorm.DB, schemaName string) error

DropSchemaForTenant drops the schema for a specific tenant in the PostgreSQL database (CASCADE).

func MigratePublicSchema

func MigratePublicSchema(db *gorm.DB) error

MigratePublicSchema migrates the public schema in the database.

func MigrateTenantModels

func MigrateTenantModels(db *gorm.DB, schemaName string) error

MigrateTenantModels creates a new schema for a specific tenant in the PostgreSQL database.

func New

func New(config Config, opts ...Option) gorm.Dialector

New creates a new PostgreSQL dialector with multitenancy support.

func RegisterModels

func RegisterModels(db *gorm.DB, models ...driver.TenantTabler) error

RegisterModels registers the given models with the provided gorm.DB instance for multitenancy support. Not safe for concurrent use by multiple goroutines.

func SetSearchPath

func SetSearchPath(tx *gorm.DB, schemaName string) (reset func() error, err error)

SetSearchPath sets the search path for the given database connection to the specified schema name. It returns a function that can be used to reset the search path to the default value.

This function does not perform any validation on the schemaName parameter. It is the responsibility of the caller to ensure that the schemaName has been sanitized to avoid SQL injection vulnerabilities (the value is quoted via the dialect's identifier quoter).

Technically safe for concurrent use by multiple goroutines, but should not be used concurrently w.r.t. ensuring data integrity and schema isolation. Use a separate database connection or transaction for each goroutine that requires a different search path.

Types

type Config

type Config struct {
	postgres.Config
}

Config provides configuration with multitenancy support.

type Dialector

type Dialector struct {
	*postgres.Dialector
	// contains filtered or unexported fields
}

Dialector provides a dialector with multitenancy support.

func (Dialector) Migrator

func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator

Migrator returns a gorm.Migrator implementation for the Dialector.

func (*Dialector) RegisterModels

func (dialector *Dialector) RegisterModels(models ...driver.TenantTabler) error

RegisterModels registers the given models with the dialector for multitenancy support.

type Migrator

type Migrator struct {
	postgres.Migrator
	Dialector
}

Migrator provides a migrator with multitenancy support.

func (Migrator) AutoMigrate

func (m Migrator) AutoMigrate(values ...any) error

func (Migrator) DropSchemaForTenant

func (m Migrator) DropSchemaForTenant(tenant string) error

DropSchemaForTenant drops the schema for a specific tenant.

func (Migrator) MigrateSharedModels

func (m Migrator) MigrateSharedModels() error

MigrateSharedModels migrates the public tables in the database.

func (Migrator) MigrateTenantModels

func (m Migrator) MigrateTenantModels(tenantID string) error

MigrateTenantModels creates a schema for a specific tenant and migrates the private tables.

type Option

type Option func(*Options)

Option is a function that modifies an Options instance.

type Options

type Options struct {
	DisableRetry bool          // Whether to disable retry.
	MaxRetries   uint          // Maximum retry attempts.
	RetryDelay   time.Duration // Initial delay between retries.
	MaxInterval  time.Duration // Maximum delay between retries.
}

Options provides configuration options with multitenancy support. By default, retry is enabled. To disable retry, set DisableRetry to true. Note that the retry logic is only applied to migrations.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL