internal

package
v0.0.0-...-730df92 Latest Latest
Warning

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

Go to latest
Published: Oct 29, 2025 License: MIT Imports: 21 Imported by: 0

Documentation

Index

Constants

View Source
const AppURL = "https://github.com/hidu/mysql-schema-sync/"

AppURL site

View Source
const Version = "20251021.4"

Version 版本号,格式:更新日期(8位).更新次数(累加)

Variables

This section is empty.

Functions

func Execute

func Execute(cfg *Config)

func RemoveTableSchemaConfig

func RemoveTableSchemaConfig(schema string) string

RemoveTableSchemaConfig 删除表创建引擎信息,编码信息,分区信息,已修复同步表结构遇到分区表异常退出问题, 对于分区表,只会同步字段,索引,主键,外键的变更

Types

type AlterIgnoreTable

type AlterIgnoreTable struct {
	Column []string `json:"column"`
	Index  []string `json:"index"`

	// 外键
	ForeignKey []string `json:"foreign"`
}

AlterIgnoreTable table's ignore info

type Config

type Config struct {
	// AlterIgnore 忽略配置, eg:   "tb1*":{"column":["aaa","a*"],"index":["aa"],"foreign":[]}
	AlterIgnore map[string]*AlterIgnoreTable `json:"alter_ignore"`

	// Email 完成同步后发送同步信息的邮件账号信息
	Email *EmailStruct `json:"email"`

	// SourceDSN 同步的源头
	SourceDSN string `json:"source"`

	// DestDSN 将被同步
	DestDSN string `json:"dest"`

	ConfigPath string

	// Tables 同步表的白名单,若为空,则同步全库
	Tables []string `json:"tables"`

	// TablesIgnore 不同步的表
	TablesIgnore []string `json:"tables_ignore"`

	// Sync 是否真正的执行同步操作
	Sync bool

	// Drop 若目标数据库表比源头多了字段、索引,是否删除
	Drop bool

	// FieldOrder 是否同步字段顺序(需要重建表,可能影响性能)
	FieldOrder bool

	// HTTPAddress 生成站点报告的地址,如 :8080
	HTTPAddress string

	// SingleSchemaChange 生成sql ddl语言每条命令只会进行单个修改操作
	SingleSchemaChange bool `json:"single_schema_change"`
}

Config config struct

func LoadConfig

func LoadConfig(confPath string) *Config

LoadConfig load config file

func (*Config) Check

func (cfg *Config) Check()

Check check config

func (*Config) CheckMatchIgnoreTables

func (cfg *Config) CheckMatchIgnoreTables(name string) bool

CheckMatchIgnoreTables check table_Ignore is match

func (*Config) CheckMatchTables

func (cfg *Config) CheckMatchTables(name string) bool

CheckMatchTables check table is match

func (*Config) IsIgnoreField

func (cfg *Config) IsIgnoreField(table string, name string) bool

IsIgnoreField isIgnore

func (*Config) IsIgnoreForeignKey

func (cfg *Config) IsIgnoreForeignKey(table string, name string) bool

IsIgnoreForeignKey 检查外键是否忽略掉

func (*Config) IsIgnoreIndex

func (cfg *Config) IsIgnoreIndex(table string, name string) bool

IsIgnoreIndex is index ignore

func (*Config) SendMailFail

func (cfg *Config) SendMailFail(errStr string)

SendMailFail send fail mail

func (*Config) SetTables

func (cfg *Config) SetTables(tables []string)

func (*Config) SetTablesIgnore

func (cfg *Config) SetTablesIgnore(tables []string)

SetTablesIgnore 设置忽略

func (*Config) String

func (cfg *Config) String() string

type DbIndex

type DbIndex struct {
	IndexType indexType
	Name      string
	SQL       string

	// 相关联的表
	RelationTables []string
}

DbIndex db index

func (*DbIndex) String

func (idx *DbIndex) String() string

type EmailStruct

type EmailStruct struct {
	SMTPHost     string `json:"smtp_host"`
	From         string `json:"from"`
	Password     string `json:"password"`
	To           string `json:"to"`
	SendMailAble bool   `json:"send_mail"`
}

EmailStruct email conf info

func (*EmailStruct) SendMail

func (m *EmailStruct) SendMail(title string, body string)

SendMail send mail

type FieldInfo

type FieldInfo struct {
	ColumnName             string  `json:"column_name"`
	OrdinalPosition        int     `json:"ordinal_position"`
	ColumnDefault          *string `json:"column_default"`
	IsNullAble             string  `json:"is_nullable"`
	DataType               string  `json:"data_type"`
	CharacterMaximumLength *int    `json:"character_maximum_length"`
	NumericPrecision       *int    `json:"numeric_precision"`
	NumericScale           *int    `json:"numeric_scale"`
	CharsetName            *string `json:"character_set_name"`
	CollationName          *string `json:"collation_name"`
	ColumnType             string  `json:"column_type"`
	ColumnComment          string  `json:"column_comment"`
	Extra                  string  `json:"extra"`
}

FieldInfo represents detailed field information from INFORMATION_SCHEMA.COLUMNS

func (*FieldInfo) Equals

func (f *FieldInfo) Equals(other *FieldInfo) bool

Equals compares two FieldInfo instances for semantic equality

func (*FieldInfo) String

func (f *FieldInfo) String() string

String returns the full column definition as used in CREATE TABLE

type MyDb

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

MyDb db struct

func NewMyDb

func NewMyDb(dsn string, dbType dbType) *MyDb

NewMyDb parse dsn

func (*MyDb) GetTableNames

func (db *MyDb) GetTableNames() []string

GetTableNames table names

func (*MyDb) GetTableSchema

func (db *MyDb) GetTableSchema(name string) (schema string)

GetTableSchema table schema

func (*MyDb) Query

func (db *MyDb) Query(query string, args ...any) (rows *sql.Rows, err error)

Query execute sql query

func (*MyDb) TableFieldsFromInformationSchema

func (db *MyDb) TableFieldsFromInformationSchema(tableName string) (map[string]*FieldInfo, error)

TableFieldsFromInformationSchema retrieves detailed field information from INFORMATION_SCHEMA.COLUMNS

type MySchema

type MySchema struct {
	Fields     xmap.Ordered[string, string] // Legacy: field name -> field definition string
	FieldInfos map[string]*FieldInfo        // New: structured field information
	IndexAll   map[string]*DbIndex
	ForeignAll map[string]*DbIndex
	SchemaRaw  string
}

MySchema table schema

func NewSchemaWithFieldInfos

func NewSchemaWithFieldInfos(schema string, fieldInfos map[string]*FieldInfo) *MySchema

NewSchemaWithFieldInfos creates a MySchema with structured field information

func ParseSchema

func ParseSchema(schema string) *MySchema

ParseSchema parse table's schema

func (*MySchema) GetFieldNames

func (mys *MySchema) GetFieldNames() []string

GetFieldNames table names

func (*MySchema) RelationTables

func (mys *MySchema) RelationTables() []string

func (*MySchema) String

func (mys *MySchema) String() string

type SchemaDiff

type SchemaDiff struct {
	Source *MySchema
	Dest   *MySchema
	Table  string
}

func NewSchemaDiffWithFieldInfos

func NewSchemaDiffWithFieldInfos(table, sourceSchema, destSchema string, sourceFields, destFields map[string]*FieldInfo) *SchemaDiff

NewSchemaDiffWithFieldInfos creates a SchemaDiff with structured field information

func (*SchemaDiff) RelationTables

func (sdiff *SchemaDiff) RelationTables() []string

type SchemaSync

type SchemaSync struct {
	Config   *Config
	SourceDb *MyDb
	DestDb   *MyDb
}

SchemaSync 配置文件

func NewSchemaSync

func NewSchemaSync(config *Config) *SchemaSync

NewSchemaSync 对一个配置进行同步

func (*SchemaSync) AllDBTables

func (sc *SchemaSync) AllDBTables() []string

AllDBTables 合并源数据库和目标数据库的表名

func (*SchemaSync) GetNewTableNames

func (sc *SchemaSync) GetNewTableNames() []string

GetNewTableNames 获取所有新增加的表名

func (*SchemaSync) SyncSQL4Dest

func (sc *SchemaSync) SyncSQL4Dest(sqlStr string, sqls []string) error

SyncSQL4Dest sync schema change

type TableAlterData

type TableAlterData struct {
	SchemaDiff *SchemaDiff
	Table      string
	Comment    string
	SQL        []string
	Type       alterType
}

TableAlterData 表的变更情况

func (*TableAlterData) Split

func (ta *TableAlterData) Split() []*TableAlterData

func (*TableAlterData) String

func (ta *TableAlterData) String() string

Jump to

Keyboard shortcuts

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