Documentation ¶
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetSchemaNameFromDb ¶
GetSchemaNameFromDb retrieves the schema name from the given gorm.DB transaction. It first checks if the table expression is not nil, then extracts the schema name from the table expression SQL. If the schema name is empty, it returns an error.
It is intended to be used in a gorm hook, such as BeforeCreate, BeforeUpdate, etc.
Example:
type User struct { gorm.Model Username string } func (User) TableName() string { return "domain1.mock_private" } func (User) BeforeCreate(tx *gorm.DB) (err error) { schemaName, err := postgres.GetSchemaNameFromDb(tx) // schemaName = "domain1" if err != nil { return err } // ... do something with schemaName return nil }
Types ¶
type ResetSearchPath ¶ added in v2.4.0
type ResetSearchPath func() error
ResetSearchPath is a function that resets the search path to the default value.
func SetSearchPath ¶ added in v2.4.0
func SetSearchPath(db *gorm.DB, schemaName string) (ResetSearchPath, error)
SetSearchPath sets the search path to the given schema name. It returns a function that resets the search path to the default value.
Example:
resetSearchPath, err := postgres.SetSearchPath(db, "domain1") if err != nil { return err } defer resetSearchPath()
Click to show internal directories.
Click to hide internal directories.