Documentation
¶
Overview ¶
Package mysql provides a gorm.Dialector implementation for MySQL databases to support multitenancy in GORM applications, enabling tenant-specific operations and shared resources management. It includes utilities for registering models, migrating shared and tenant-specific models, and configuring the database for tenant-specific operations.
This package follows the "separate databases" approach for multitenancy, which allows for complete data isolation by utilizing separate databases for each tenant. This approach ensures maximum security and performance isolation between tenants, making it suitable for applications with stringent data security requirements.
URL Format ¶
The URL format for MySQL databases is as follows:
mysql://user:password@tcp(localhost:3306)/dbname?charset=utf8mb4&parseTime=True&loc=Local
See the MySQL connection strings documentation for more information.
Model Registration ¶
To register models for multitenancy support, use RegisterModels. This should be done before running any migrations or tenant-specific operations.
Migration Strategy ¶
To ensure data integrity and schema isolation across tenants,gorm.DB.AutoMigrate has been disabled. Instead, use the provided shared and tenant-specific migration methods. driver.ErrInvalidMigration is returned if the `AutoMigrate` method is called directly.
Concurrent Migrations ¶
To ensure tenant isolation and facilitate concurrent migrations, this package uses MySQL advisory locks. These locks prevent concurrent migrations from interfering with each other, ensuring that only one migration process can run at a time for a given tenant.
Retry Configuration ¶
Exponential backoff retry logic is enabled by default for migrations. To disable retry or customize the retry behavior, either provide options to New or specify options in the DSN connection string of Open. The following options are available:
- `gmt_disable_retry`: Whether to disable retry. Default is false.
- `gmt_max_retries`: The maximum number of retry attempts. Default is 6.
- `gmt_retry_interval`: The initial interval between retry attempts. Default is 2 seconds.
- `gmt_retry_max_interval`: The maximum interval between retry attempts. Default is 30 seconds.
Shared Model Migrations ¶
To migrate shared models, use MigrateSharedModels.
Tenant Model Migrations ¶
To migrate tenant-specific models, use MigrateTenantModels.
Tenant Offboarding ¶
To clean up the database for a removed tenant, use DropDatabaseForTenant.
Tenant Context Configuration ¶
To configure the database for operations specific to a tenant, use schema.UseDatabase.
Index ¶
- Constants
- func DropDatabaseForTenant(db *gorm.DB, tenantID string) error
- func MigrateSharedModels(db *gorm.DB) error
- func MigrateTenantModels(db *gorm.DB, tenantID string) error
- func New(config Config, opts ...Option) gorm.Dialector
- func Open(dsn string) gorm.Dialector
- func RegisterModels(db *gorm.DB, models ...driver.TenantTabler) error
- type Config
- type Dialector
- type Migrator
- func (m Migrator) AutoMigrate(values ...interface{}) error
- func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error)
- func (m Migrator) CurrentDatabase() (name string)
- func (m Migrator) CurrentSchema(stmt *gorm.Statement, table string) (interface{}, interface{})
- func (m Migrator) DropDatabaseForTenant(tenantID string) (err error)
- func (m Migrator) HasColumn(value interface{}, field string) bool
- func (m Migrator) HasConstraint(value interface{}, name string) bool
- func (m Migrator) HasIndex(value interface{}, name string) bool
- func (m Migrator) HasTable(value interface{}) bool
- func (m Migrator) MigrateSharedModels() error
- func (m Migrator) MigrateTenantModels(tenantID string) (err error)
- type Option
- type Options
Constants ¶
const DriverName = "mysql"
DriverName is the name of the MySQL driver.
Variables ¶
This section is empty.
Functions ¶
func DropDatabaseForTenant ¶
DropDatabaseForTenant drops the database for a specific tenant in the MySQL database.
func MigrateSharedModels ¶
MigrateSharedModels migrates the public schema in the database.
func MigrateTenantModels ¶
MigrateTenantModels creates a new schema for a specific tenant in the MySQL database.
func RegisterModels ¶
func RegisterModels(db *gorm.DB, models ...driver.TenantTabler) error
RegisterModels registers the given models with the provided gorm.DB instance for multitenancy support. Not safe for concurrent use by multiple goroutines.
Types ¶
type Dialector ¶
Dialector provides a dialector with multitenancy support.
func (Dialector) Migrator ¶
Migrator returns a gorm.Migrator implementation for the Dialector.
func (*Dialector) RegisterModels ¶
func (dialector *Dialector) RegisterModels(models ...driver.TenantTabler) error
RegisterModels registers the given models with the dialector for multitenancy support. Not safe for concurrent use by multiple goroutines.
type Migrator ¶
Migrator provides a migrator with multitenancy support.
func (Migrator) AutoMigrate ¶
func (Migrator) ColumnTypes ¶
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error)
func (Migrator) CurrentDatabase ¶
func (Migrator) CurrentSchema ¶
func (Migrator) DropDatabaseForTenant ¶
DropDatabaseForTenant drops the database for a specific tenant.
func (Migrator) HasConstraint ¶
func (Migrator) MigrateSharedModels ¶
MigrateSharedModels migrates the shared tables in the database.
func (Migrator) MigrateTenantModels ¶
MigrateTenantModels creates a database for a specific tenant and migrates the tenant tables.
type Options ¶
type Options struct {
DisableRetry bool `json:"gmt_disable_retry" mapstructure:"gmt_disable_retry"` // Whether to disable retry.
Retry backoff.Options `json:",inline" mapstructure:",squash"` // Retry options.
}
Options provides configuration options with multitenancy support. By default, retry is enabled. To disable retry, set DisableRetry to true. Note that the retry logic is only applied to migrations.
Directories
¶
| Path | Synopsis |
|---|---|
|
internal
|
|
|
dsn
Package dsn provides utilities for parsing and formatting DSNs (Data Source Names).
|
Package dsn provides utilities for parsing and formatting DSNs (Data Source Names). |
|
locking
Package locking provides functions for acquiring and releasing MySQL advisory locks.
|
Package locking provides functions for acquiring and releasing MySQL advisory locks. |
|
testutil
Package testutil provides utility functions for testing.
|
Package testutil provides utility functions for testing. |
|
Package schema provides utilities for managing MySQL databases in a multi-tenant application.
|
Package schema provides utilities for managing MySQL databases in a multi-tenant application. |