mysql

package module
v8.0.0-...-4bfd36c Latest Latest
Warning

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

Go to latest
Published: Mar 4, 2025 License: Apache-2.0 Imports: 18 Imported by: 0

README

mysql

Go Reference Go Report Card License

MySQL provides a driver for the gorm-multitenancy package, allowing for easy multitenancy setup in MySQL databases.

Installation

go get -u github.com/greatrestaura/gorm-multitenancy/mysql/v8

Getting Started

Check out the pkg.go.dev documentation for comprehensive guides and API references.

For MySQL-specific documentation, refer to pkg.go.dev.

Running the Example Application

For a practical demonstration, you can run the example application. It showcases various configurations and usage scenarios.

Contributing

All contributions are welcome! See the Contributing Guide for more details.

License

This project is licensed under the Apache License 2.0 - see the LICENSE file for details.

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

View Source
const DriverName = "mysql"

DriverName is the name of the MySQL driver.

Variables

This section is empty.

Functions

func DropDatabaseForTenant

func DropDatabaseForTenant(db *gorm.DB, tenantID string) error

DropDatabaseForTenant drops the database for a specific tenant in the MySQL database.

func MigrateSharedModels

func MigrateSharedModels(db *gorm.DB) error

MigrateSharedModels migrates the public schema in the database.

func MigrateTenantModels

func MigrateTenantModels(db *gorm.DB, tenantID string) error

MigrateTenantModels creates a new schema for a specific tenant in the MySQL database.

func New

func New(config Config, opts ...Option) gorm.Dialector

New creates a new MySQL dialector with multitenancy support.

func Open

func Open(dsn string) gorm.Dialector

Open creates a new MySQL dialector with multitenancy support.

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 Config

type Config struct {
	mysql.Config
}

Config provides configuration with multitenancy support.

type Dialector

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

Dialector provides a dialector with multitenancy support.

func (Dialector) Migrator

func (dialector Dialector) Migrator(db *gorm.DB) gorm.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

type Migrator struct {
	mysql.Migrator
	Dialector
}

Migrator provides a migrator with multitenancy support.

func (Migrator) AutoMigrate

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

func (Migrator) ColumnTypes

func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error)

func (Migrator) CurrentDatabase

func (m Migrator) CurrentDatabase() (name string)

func (Migrator) CurrentSchema

func (m Migrator) CurrentSchema(stmt *gorm.Statement, table string) (interface{}, interface{})

func (Migrator) DropDatabaseForTenant

func (m Migrator) DropDatabaseForTenant(tenantID string) (err error)

DropDatabaseForTenant drops the database for a specific tenant.

func (Migrator) HasColumn

func (m Migrator) HasColumn(value interface{}, field string) bool

func (Migrator) HasConstraint

func (m Migrator) HasConstraint(value interface{}, name string) bool

func (Migrator) HasIndex

func (m Migrator) HasIndex(value interface{}, name string) bool

func (Migrator) HasTable

func (m Migrator) HasTable(value interface{}) bool

func (Migrator) MigrateSharedModels

func (m Migrator) MigrateSharedModels() error

MigrateSharedModels migrates the shared tables in the database.

func (Migrator) MigrateTenantModels

func (m Migrator) MigrateTenantModels(tenantID string) (err error)

MigrateTenantModels creates a database for a specific tenant and migrates the tenant tables.

type Option

type Option func(*Options)

Option is a function that modifies an Options instance.

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.

Jump to

Keyboard shortcuts

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