migrator

package
v1.1.0-rc Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2021 License: AGPL-3.0 Imports: 28 Imported by: 1

Documentation

Index

Constants

View Source
const SchemaMigrationHistory = `schema_migration_history`

Variables

This section is empty.

Functions

func Begin

func Begin() *gorm.DB

func ClearSandbox

func ClearSandbox()

ClearSandbox if you want to do a new migration in a clean sandbox, you should ClearSandbox to clear all changes on last migration

func DB

func DB() *gorm.DB

func Exec

func Exec(sql string, values ...interface{}) (tx *gorm.DB)

func Find

func Find(dest interface{}, cond ...interface{}) (tx *gorm.DB)

func First

func First(dest interface{}, cond ...interface{}) (tx *gorm.DB)

func Raw

func Raw(sql string, values ...interface{}) (tx *gorm.DB)

func SandBox

func SandBox() *gorm.DB

func SchemaMigrationHistoryExists

func SchemaMigrationHistoryExists() bool

SchemaMigrationHistoryExists returns whether schema_migration_history exists

func ShowCreateTables

func ShowCreateTables(tableNames ...string) (creates []string, err error)

ShowCreateTables select table's DDL if tableNames is nil returns all tables DDL

Types

type Equal

type Equal struct {
	// contains filtered or unexported fields
}

func FieldTypeEqual

func FieldTypeEqual(l, r *types.FieldType) *Equal

func (*Equal) Equal

func (e *Equal) Equal() bool

func (*Equal) Reason

func (e *Equal) Reason() string

type HistoryModel

type HistoryModel struct {
	ID        uint64    `gorm:"primarykey"`
	CreatedAt time.Time `json:"createdAt" gorm:"created_at;type:datetime;not null;default CURRENT_TIMESTAMP"`
	UpdatedAt time.Time `json:"updatedAt" gorm:"updated_at;type:datetime;not null;default CURRENT_TIMESTAMP; on update CURRENT_TIMESTAMP"`

	ServiceName  string `json:"serviceName"  gorm:"service_name;type:varchar(32);not null;comment:微服务名"`
	Filename     string `json:"filename"     gorm:"filename;type:varchar(191);not null;comment:脚本类型"`
	Checksum     string `json:"checksum"     gorm:"checksum;type:char(128);not null;comment:rawtext 的 md5 值"`
	InstalledBy  string `json:"installedBy"  gorm:"installed_by;type:varchar(32);not null;comment:执行人"`
	InstalledOn  string `json:"installedOn"  gorm:"install_on;type:varchar(32);not null;comment:执行平台"`
	LanguageType string `json:"languageType" gorm:"language_type;type:varchar(16);not null;comment:SQL, Go, Java"`
	Reversed     string `json:"reversed"     gorm:"reversed;type:longtext;not null;comment:反转的 DDL"`
}

func (HistoryModel) TableName

func (m HistoryModel) TableName() string

type Migrator

type Migrator struct {
	Parameters

	LocalScripts *Scripts
	// contains filtered or unexported fields
}

func New

func New(parameters Parameters) (*Migrator, error)

func (*Migrator) Run

func (mig *Migrator) Run() error

type Module

type Module []*Script

Module is the list of Script

func (Module) BaselineEqualCloud

func (s Module) BaselineEqualCloud(tx *gorm.DB) *Equal

BaselineEqualCloud check baseline script schema is equal with cloud schema or not

func (Module) BaselineSchema

func (s Module) BaselineSchema() *Schema

func (Module) BaselineTableNames

func (s Module) BaselineTableNames() []string

func (Module) Filenames

func (s Module) Filenames() []string

func (Module) Schema

func (s Module) Schema() *Schema

func (Module) Sort

func (s Module) Sort()

func (Module) TableNames

func (s Module) TableNames() []string

type Parameters

type Parameters interface {
	// DSN gets MySQL DSN
	DSN() string

	// SandboxDSN gets sandbox DSN
	SandboxDSN() string

	// database name
	Database() string

	// MigrationDir gets migration scripts direction from repo, like .dice/migrations or 4.1/sqls
	MigrationDir() string

	// Modules is the modules for installing.
	// if is nil, to install all modules in the MigrationDir()
	Modules() []string

	// Workdir gets pipeline node workdir
	Workdir() string

	// DebugSQL gets weather to debug SQL executing
	DebugSQL() bool

	NeedErdaMySQLLint() bool

	Rules() []rules.Ruler
}

type Schema

type Schema struct {
	TableDefinitions map[string]*TableDefinition
}

Schema is the set of TableDefinitions. Is presents the status of the set of some tables.

func NewSchema

func NewSchema() *Schema

func (*Schema) Enter

func (s *Schema) Enter(in ast.Node) (ast.Node, bool)

func (*Schema) Equal

func (s *Schema) Equal(o *Schema) *Equal

func (*Schema) Leave

func (s *Schema) Leave(in ast.Node) (ast.Node, bool)

type Script

type Script struct {
	// path from repo root, filepath.Base(script.Name) for base filename
	Name      string
	Rawtext   []byte
	Reversing []string
	Nodes     []ast.StmtNode
	Pending   bool
	Record    *HistoryModel
	Workdir   string
	// contains filtered or unexported fields
}

Script is the object from a SQL script file, it contains many statements.

func NewScript

func NewScript(workdir, pathFromRepoRoot string) (*Script, error)

NewScript read the local file, parse data as SQL AST nodes, and mark script IsBase or not

func (*Script) Checksum

func (s *Script) Checksum() string

func (*Script) DDLNodes

func (s *Script) DDLNodes() []ast.DDLNode

func (*Script) DMLNodes

func (s *Script) DMLNodes() []ast.StmtNode

func (*Script) Install

func (s *Script) Install(begin func() *gorm.DB, after func(tx *gorm.DB, err error)) (err error)

Install installs the script in database

func (*Script) IsBaseline

func (s *Script) IsBaseline() bool

type Scripts

type Scripts struct {
	Workdir       string
	Dirname       string
	ServicesNames []string
	Services      map[string]Module
	// contains filtered or unexported fields
}

Scripts is the set of Module

func NewScripts

func NewScripts(parameters Parameters) (*Scripts, error)

NewScripts range the directory

func (*Scripts) AlterPermissionLint

func (s *Scripts) AlterPermissionLint() error

func (*Scripts) Get

func (s *Scripts) Get(serviceName string) ([]*Script, bool)

func (*Scripts) HasDestructiveOperationInPending

func (s *Scripts) HasDestructiveOperationInPending() bool

func (*Scripts) InstalledChangesLint

func (s *Scripts) InstalledChangesLint() error

func (*Scripts) Lint

func (s *Scripts) Lint() error

func (*Scripts) MarkPending

func (s *Scripts) MarkPending(tx *gorm.DB)

func (*Scripts) SameNameLint

func (s *Scripts) SameNameLint() error

SameNameLint lint whether there is same script name in different directories

type TableDefinition

type TableDefinition struct {
	CreateStmt *ast.CreateTableStmt
}

TableDefinition is the table definition (CreateTableStmt) object. It can be accepted by SQL AST Node and then to update it's status.

func NewTableDefinition

func NewTableDefinition(stmt *ast.CreateTableStmt) *TableDefinition

func (*TableDefinition) Enter

func (d *TableDefinition) Enter(in ast.Node) (ast.Node, bool)

func (*TableDefinition) Equal

func (d *TableDefinition) Equal(o *TableDefinition) *Equal

func (*TableDefinition) Leave

func (d *TableDefinition) Leave(in ast.Node) (ast.Node, bool)

Jump to

Keyboard shortcuts

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