postgres

package
v1.0.9 Latest Latest
Warning

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

Go to latest
Published: Dec 28, 2023 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CreateSchemaForTenant

func CreateSchemaForTenant(db *gorm.DB, schemaName string) error

CreateSchemaForTenant creates the schema for the tenant, and migrates the private tables

func DropSchemaForTenant

func DropSchemaForTenant(db *gorm.DB, schemaName string) error

DropSchemaForTenant drops the schema for the tenant (CASCADING tables)

func MigratePublicSchema

func MigratePublicSchema(db *gorm.DB) error

MigratePublicSchema migrates the public tables

func New

func New(config Config, models ...interface{}) gorm.Dialector

New creates a new postgres dialector with multitenancy support

func Open

func Open(dsn string, models ...interface{}) gorm.Dialector

Open creates a new postgres dialector with multitenancy support

func RegisterModels

func RegisterModels(db *gorm.DB, models ...interface{})

RegisterModels registers the models for multitenancy

Types

type Config

type Config = postgres.Config

Config is the configuration for the postgres driver

type Dialector

type Dialector struct {
	postgres.Dialector
	// contains filtered or unexported fields
}

Dialector is the postgres dialector with multitenancy support

func (Dialector) Migrator

func (dialector Dialector) Migrator(db *gorm.DB) gorm.Migrator

Migrator returns the migrator with multitenancy support

type Migrator

type Migrator struct {
	postgres.Migrator
	// contains filtered or unexported fields
}

Migrator is the struct that implements the Migratorer interface

func (Migrator) AutoMigrate

func (m Migrator) AutoMigrate(values ...interface{}) error

AutoMigrate migrates the tables based on the migration options.

func (*Migrator) CreateSchemaForTenant

func (m *Migrator) CreateSchemaForTenant(tenant string) error

CreateSchemaForTenant creates the schema for the tenant and migrates the private tables

func (*Migrator) DropSchemaForTenant

func (m *Migrator) DropSchemaForTenant(tenant string) error

DropSchemaForTenant drops the schema for the tenant (CASCADING tables)

func (*Migrator) MigratePublicSchema

func (m *Migrator) MigratePublicSchema() error

MigratePublicSchema migrates the public tables

type MultitenancyMigrator

type MultitenancyMigrator interface {
	multitenancy.Migrator

	// CreateSchemaForTenant creates the schema for the tenant, and migrates the private tables
	//
	// Parameters:
	// 	- tenant: the tenant's schema name
	//
	// Returns:
	// 	- error: the error if any
	CreateSchemaForTenant(tenant string) error
	// DropSchemaForTenant drops the schema for the tenant (CASCADING tables)
	//
	// Parameters:
	// 	- tenant: the tenant's schema name
	//
	// Returns:
	// 	- error: the error if any
	DropSchemaForTenant(tenant string) error
	// MigratePublicSchema migrates the public tables
	MigratePublicSchema() error
}

MultitenancyMigrator is the interface for the postgres migrator with multitenancy support

type TenantModel

type TenantModel struct {
	// DomainURL is the domain URL of the tenant
	DomainURL string `gorm:"column:domain_url;uniqueIndex;size:128" json:"domainURL"`

	// SchemaName is the schema name of the tenant.
	//
	// Field-level permissions are restricted to read and create.
	//
	// The following constraints are applied:
	// 	- unique index
	// 	- size: 63
	// 	- check: schema_name ~ '^[_a-zA-Z][_a-zA-Z0-9]{2,}$' AND schema_name !~ '^pg_' (to prevent invalid schema names)
	SchemaName string `` /* 152-byte string literal not displayed */
}

TenantModel a basic GoLang struct which includes the following fields: DomainURL, SchemaName. It's intended to be embedded into any public postgresql model that needs to be scoped to a tenant. It may be embedded into your model or you may build your own model without it.

For example:

type Tenant struct {
  postgres.TenantModel
}

Directories

Path Synopsis
module

Jump to

Keyboard shortcuts

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