migration

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Jul 9, 2026 License: Apache-2.0 Imports: 4 Imported by: 0

Documentation

Overview

Package migration contains tenant schema migration planning APIs.

Index

Constants

View Source
const (
	DefaultTenantField     = "tenant_id"
	DefaultTenantFieldType = "VARCHAR(64)"
	DefaultSoftDeleteField = "deleted_at"
	DefaultDeleteMarker    = "deleted_flag"
)

Variables

View Source
var (
	// ErrInvalidIdentifier reports an unsafe SQL identifier.
	ErrInvalidIdentifier = errors.New("gotenancy/migration: invalid identifier")

	// ErrInvalidMigration reports incomplete migration input.
	ErrInvalidMigration = errors.New("gotenancy/migration: invalid migration")

	// ErrUnsupportedDialect reports a dialect not supported by the planner.
	ErrUnsupportedDialect = errors.New("gotenancy/migration: unsupported dialect")
)

Functions

func DetectDeletePolicy

func DetectDeletePolicy(columns []string, softDeleteField string) string

DetectDeletePolicy returns "soft" when softDeleteField exists, otherwise "hard".

Types

type Dialect

type Dialect string

Dialect identifies the SQL dialect used for generated DDL.

const (
	DialectPostgres Dialect = "postgres"
	DialectMySQL    Dialect = "mysql"
	DialectSQLite   Dialect = "sqlite"
)

type Migrator

type Migrator interface {
	AddTenantColumn(table string, tenantField string, fieldType string) (string, error)
	CreateSoftDeleteUniqueIndex(table string, indexName string, tenantField string, businessFields []string, softDeleteField string) (string, error)
	CreateHardDeleteUniqueIndex(table string, indexName string, tenantField string, businessFields []string) (string, error)
	SeedTenants(table string, tenants []types.Tenant) ([]Statement, error)
}

Migrator defines tenant-schema migration planning operations.

type Planner

type Planner struct {
	Dialect Dialect
}

Planner generates migration SQL without executing it.

func NewPlanner

func NewPlanner(dialect Dialect) Planner

NewPlanner creates a migration planner.

func (Planner) AddTenantColumn

func (planner Planner) AddTenantColumn(table string, tenantField string, fieldType string) (string, error)

AddTenantColumn returns SQL to add a tenant_id column.

func (Planner) CreateHardDeleteUniqueIndex

func (planner Planner) CreateHardDeleteUniqueIndex(table string, indexName string, tenantField string, businessFields []string) (string, error)

CreateHardDeleteUniqueIndex returns SQL for a hard-delete table unique index.

func (Planner) CreateMySQLSoftDeleteUniqueIndex

func (planner Planner) CreateMySQLSoftDeleteUniqueIndex(table string, indexName string, tenantField string, businessFields []string, markerField string) (string, error)

CreateMySQLSoftDeleteUniqueIndex returns SQL using a non-nullable delete marker.

func (Planner) CreateSoftDeleteUniqueIndex

func (planner Planner) CreateSoftDeleteUniqueIndex(table string, indexName string, tenantField string, businessFields []string, softDeleteField string) (string, error)

CreateSoftDeleteUniqueIndex returns SQL for a soft-delete-aware unique index.

func (Planner) SeedTenants

func (planner Planner) SeedTenants(table string, tenants []types.Tenant) ([]Statement, error)

SeedTenants returns parameterized inserts for tenant seed data.

type Statement

type Statement struct {
	SQL  string
	Args []any
}

Statement is a parameterized SQL statement.

Jump to

Keyboard shortcuts

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