postgres

package
v2.5.4 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2024 License: Apache-2.0 Imports: 3 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetSchemaNameFromDb

func GetSchemaNameFromDb(tx *gorm.DB) (string, error)

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()

Jump to

Keyboard shortcuts

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