Documentation
¶
Index ¶
- Variables
- func AutoMigrateWithDialect(dialect string, models ...interface{}) ([]string, error)
- func GenerateAlterTableSQL(modelInfo *ModelInfo, tableInfo *TableInfo, dialect string) ([]string, error)
- func GenerateCreateTableSQL(info *ModelInfo, dialect string) (string, error)
- func GenerateMigrationsTableSQL(dialect string) (string, error)
- func ToSnakeCase(str string) string
- type ColumnInfo
- type ComparableFieldInfo
- type IndexInfo
- type ModelInfo
- type TableInfo
Constants ¶
This section is empty.
Variables ¶
var ModelCache sync.Map // map[reflect.Type]*ModelInfo
modelCache stores ModelInfo structs, keyed by reflect.Type. Renamed: modelCache -> ModelCache
var TypeMapping = map[string]map[string]string{
"mysql": {
"int": "INT",
"int8": "TINYINT",
"int16": "SMALLINT",
"int32": "INT",
"int64": "BIGINT",
"uint": "INT UNSIGNED",
"uint8": "TINYINT UNSIGNED",
"uint16": "SMALLINT UNSIGNED",
"uint32": "INT UNSIGNED",
"uint64": "BIGINT UNSIGNED",
"float32": "FLOAT",
"float64": "DOUBLE",
"string": "VARCHAR(255)",
"bool": "BOOLEAN",
"time.Time": "DATETIME",
"[]byte": "BLOB",
},
"postgres": {
"int": "INTEGER",
"int8": "SMALLINT",
"int16": "SMALLINT",
"int32": "INTEGER",
"int64": "BIGINT",
"uint": "BIGINT",
"uint8": "SMALLINT",
"uint16": "INTEGER",
"uint32": "BIGINT",
"uint64": "NUMERIC",
"float32": "REAL",
"float64": "DOUBLE PRECISION",
"string": "VARCHAR(255)",
"bool": "BOOLEAN",
"time.Time": "TIMESTAMP",
"[]byte": "BYTEA",
},
"sqlite": {
"int": "INTEGER",
"int8": "INTEGER",
"int16": "INTEGER",
"int32": "INTEGER",
"int64": "INTEGER",
"uint": "INTEGER",
"uint8": "INTEGER",
"uint16": "INTEGER",
"uint32": "INTEGER",
"uint64": "INTEGER",
"float32": "REAL",
"float64": "REAL",
"string": "TEXT",
"bool": "BOOLEAN",
"time.Time": "DATETIME",
"[]byte": "BLOB",
},
}
TypeMapping 定义 Go 类型到 SQL 类型的映射表
Functions ¶
func AutoMigrateWithDialect ¶
AutoMigrateWithDialect 支持指定方言
func GenerateAlterTableSQL ¶
func GenerateAlterTableSQL(modelInfo *ModelInfo, tableInfo *TableInfo, dialect string) ([]string, error)
GenerateAlterTableSQL 生成 ALTER TABLE 语句以将实际表结构迁移为目标 struct 定义 modelInfo: 目标模型的 *ModelInfo(Go struct/tag 解析) tableInfo: 实际表结构(数据库 introspection 得到) dialect: "mysql" | "postgres" | "sqlite"
func GenerateCreateTableSQL ¶
GenerateCreateTableSQL 生成单表 CREATE TABLE 语句及后续的 CREATE INDEX 语句
func GenerateMigrationsTableSQL ¶
GenerateMigrationsTableSQL 返回 schema_migrations 版本表的建表 SQL,兼容多数据库
func ToSnakeCase ¶
ToSnakeCase converts a string from CamelCase to snake_case.
Types ¶
type ColumnInfo ¶
type ColumnInfo struct {
Name string // Column name
DataType string // Database type (e.g., INT, VARCHAR(255))
IsNullable bool // Whether the column is nullable
IsPrimary bool // Whether this column is the primary key
IsUnique bool // Whether this column has a unique constraint
Default *string // Default value (if any)
}
type ComparableFieldInfo ¶
type ComparableFieldInfo struct {
GoName string // Go field name
DBColumn string // Database column name
Index []int // Index for fast field access via FieldByIndex
IsZero func(v reflect.Value) bool // Function to check if value is zero/empty
Kind reflect.Kind // Field kind (string, int, struct, etc.)
Type reflect.Type // Field type (for more detailed type checking)
IsEmbedded bool // Whether this is from an embedded struct
IgnoreInDiff bool // Whether to ignore this field during diffing (e.g., tags like db:"-")
DefaultValue *string // Parsed default value from db tag
}
ComparableFieldInfo holds pre-computed metadata for a single field used during comparisons.
type IndexInfo ¶
type IndexInfo struct {
Name string // Index name
Columns []string // Column names
Unique bool // Is unique index
}
IndexInfo holds metadata for a single index (normal or unique).
type ModelInfo ¶
type ModelInfo struct {
TableName string // Renamed: tableName -> TableName
PkName string // Database name of the primary key field (Exported)
Columns []string // Renamed: columns -> Columns
Fields []string // Corresponding Go struct field names (Exported)
FieldToColumnMap map[string]string // Map Go field name to its corresponding DB column name (Exported)
ColumnToFieldMap map[string]string // Map DB column name to its corresponding Go field name (Exported)
CompareFields []ComparableFieldInfo // Fields to compare during diff operations (new)
Indexes []IndexInfo // 普通索引
UniqueIndexes []IndexInfo // 唯一索引
}
ModelInfo holds pre-computed metadata about a model type T.
type TableInfo ¶
type TableInfo struct {
Name string // Table name
Columns []ColumnInfo // All columns
Indexes []IndexInfo // All indexes (including unique)
PrimaryKey string // Primary key column name (if any)
}
TableInfo holds the actual schema info introspected from the database (internal use only).
func ConvertTableInfo ¶
func ConvertTableInfo(src *driversSchema.TableInfo) *TableInfo
ConvertTableInfo converts a drivers/schema.TableInfo to internal/schema.TableInfo (deep copy).