Documentation
¶
Overview ¶
Package schema provides utilities for managing PostgreSQL schemas in a multi-tenant application.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ValidateTenantName ¶
ValidateTenantName checks the validity of a provided tenant name. A tenant name is considered valid if it:
- Matches the pattern `^[_a-zA-Z][_a-zA-Z0-9]{2,}$`. This means it must start with an underscore or a letter, followed by at least two characters that can be underscores, letters, or numbers.
- Does not start with "pg_". The prefix "pg_" is reserved for system schemas in PostgreSQL.
If the tenant name is invalid, the function returns an error with a detailed explanation.
Types ¶
type ResetSearchPath ¶
type ResetSearchPath func() error
ResetSearchPath is a function that resets the search path to the default value.
func SetSearchPath ¶
SetSearchPath sets the search path for the given database connection to the specified schema name. It returns the modified database connection and a function that can be used to reset the search path to the default 'public' schema. If the schema name is invalid or starts with 'pg_', an error is added to the database connection's error list.
Example:
db, reset := postgres.SetSearchPath(db, "domain1")
if db.Error != nil {
// handle the error
}
defer reset() // reset the search path to 'public'
// ... do something with the database connection (with the search path set to 'domain1')