schema

package
v7.0.1 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2024 License: Apache-2.0 Imports: 4 Imported by: 0

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

func ValidateTenantName(tenant string) error

ValidateTenantName checks the validity of a provided tenant name. A tenant name is considered valid if it:

  1. 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.
  2. 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

func SetSearchPath(db *gorm.DB, schemaName string) (*gorm.DB, ResetSearchPath)

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

Jump to

Keyboard shortcuts

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