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
- Variables
- func CurrentSearchPath(tx *gorm.DB) string
- func DropSchemaForTenant(db *gorm.DB, schemaName string) error
- func MigratePublicSchema(db *gorm.DB) error
- func MigrateTenantModels(db *gorm.DB, schemaName string) error
- func New(config Config, opts ...Option) gorm.Dialector
- func RegisterModels(db *gorm.DB, models ...driver.TenantTabler) error
- func SetSearchPath(tx *gorm.DB, schemaName string) (reset func() error, err error)
- type Config
- type Dialector
- type Migrator
- type Option
- type Options
Constants ¶
const DriverName = "postgres"
DriverName is the name of the PostgreSQL driver.
Variables ¶
var ( ErrNoTenantTables = errors.New("no tenant tables to migrate") ErrNoPublicTables = errors.New("no public tables to migrate") )
Migration errors.
var ErrEmptySchemaName = errors.New("schema name is empty")
ErrEmptySchemaName is returned when SetSearchPath is called with an empty schema name.
Functions ¶
func CurrentSearchPath ¶
CurrentSearchPath returns the current search path for the given database connection.
func DropSchemaForTenant ¶
DropSchemaForTenant drops the schema for a specific tenant in the PostgreSQL database (CASCADE).
func MigratePublicSchema ¶
MigratePublicSchema migrates the public schema in the database.
func MigrateTenantModels ¶
MigrateTenantModels creates a new schema for a specific tenant in the PostgreSQL database.
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 ¶
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 Dialector ¶
Dialector provides a dialector with multitenancy support.
func (Dialector) 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 ¶
Migrator provides a migrator with multitenancy support.
func (Migrator) AutoMigrate ¶
func (Migrator) DropSchemaForTenant ¶
DropSchemaForTenant drops the schema for a specific tenant.
func (Migrator) MigrateSharedModels ¶
MigrateSharedModels migrates the public tables in the database.
func (Migrator) MigrateTenantModels ¶
MigrateTenantModels creates a schema for a specific tenant and migrates the private tables.
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.