migrator

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Dec 2, 2023 License: MIT, MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BuildIndexOptionsInterface

type BuildIndexOptionsInterface interface {
	BuildIndexOptions([]schema.IndexOption, *gorm.Statement) []interface{}
}

BuildIndexOptionsInterface build index options interface

type ColumnType

type ColumnType struct {
	SQLColumnType      *sql.ColumnType
	NameValue          sql.NullString
	DataTypeValue      sql.NullString
	ColumnTypeValue    sql.NullString
	PrimaryKeyValue    sql.NullBool
	UniqueValue        sql.NullBool
	AutoIncrementValue sql.NullBool
	LengthValue        sql.NullInt64
	DecimalSizeValue   sql.NullInt64
	ScaleValue         sql.NullInt64
	NullableValue      sql.NullBool
	ScanTypeValue      reflect.Type
	CommentValue       sql.NullString
	DefaultValueValue  sql.NullString
}

ColumnType column type implements ColumnType interface

func (ColumnType) AutoIncrement

func (ct ColumnType) AutoIncrement() (isAutoIncrement bool, ok bool)

AutoIncrement returns the column is auto increment or not.

func (ColumnType) ColumnType

func (ct ColumnType) ColumnType() (columnType string, ok bool)

ColumnType returns the database type of the column. like `varchar(16)`

func (ColumnType) Comment

func (ct ColumnType) Comment() (value string, ok bool)

Comment returns the comment of current column.

func (ColumnType) DatabaseTypeName

func (ct ColumnType) DatabaseTypeName() string

DatabaseTypeName returns the database system name of the column type. If an empty string is returned, then the driver type name is not supported. Consult your driver documentation for a list of driver data types. Length specifiers are not included. Common type names include "VARCHAR", "TEXT", "NVARCHAR", "DECIMAL", "BOOL", "INT", and "BIGINT".

func (ColumnType) DecimalSize

func (ct ColumnType) DecimalSize() (precision int64, scale int64, ok bool)

DecimalSize returns the scale and precision of a decimal type.

func (ColumnType) DefaultValue

func (ct ColumnType) DefaultValue() (value string, ok bool)

DefaultValue returns the default value of current column.

func (ColumnType) Length

func (ct ColumnType) Length() (length int64, ok bool)

Length returns the column type length for variable length column types

func (ColumnType) Name

func (ct ColumnType) Name() string

Name returns the name or alias of the column.

func (ColumnType) Nullable

func (ct ColumnType) Nullable() (nullable bool, ok bool)

Nullable reports whether the column may be null.

func (ColumnType) PrimaryKey

func (ct ColumnType) PrimaryKey() (isPrimaryKey bool, ok bool)

PrimaryKey returns the column is primary key or not.

func (ColumnType) ScanType

func (ct ColumnType) ScanType() reflect.Type

ScanType returns a Go type suitable for scanning into using Rows.Scan.

func (ColumnType) Unique

func (ct ColumnType) Unique() (unique bool, ok bool)

Unique reports whether the column may be unique.

type Config

type Config struct {
	CreateIndexAfterCreateTable bool
	DB                          *gorm.DB
	gorm.Dialector
}

Config schema config

type GormDataTypeInterface

type GormDataTypeInterface interface {
	GormDBDataType(*gorm.DB, *schema.Field) string
}

GormDataTypeInterface gorm data type interface

type Index

type Index struct {
	TableName       string
	NameValue       string
	ColumnList      []string
	PrimaryKeyValue sql.NullBool
	UniqueValue     sql.NullBool
	OptionValue     string
}

Index implements gorm.Index interface

func (Index) Columns

func (idx Index) Columns() []string

Columns return the columns of the index

func (Index) Name

func (idx Index) Name() string

Name return the name of the index.

func (Index) Option

func (idx Index) Option() string

Option return the optional attribute of the index

func (Index) PrimaryKey

func (idx Index) PrimaryKey() (isPrimaryKey bool, ok bool)

PrimaryKey returns the index is primary key or not.

func (Index) Table

func (idx Index) Table() string

Table return the table name of the index.

func (Index) Unique

func (idx Index) Unique() (unique bool, ok bool)

Unique returns whether the index is unique or not.

type Migrator

type Migrator struct {
	Config
}

Migrator m struct

func (Migrator) AddColumn

func (m Migrator) AddColumn(value interface{}, name string) error

AddColumn create `name` column for value

func (Migrator) AlterColumn

func (m Migrator) AlterColumn(value interface{}, field string) error

AlterColumn alter value's `field` column' type based on schema definition

func (Migrator) AutoMigrate

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

AutoMigrate auto migrate values

func (Migrator) BuildIndexOptions

func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{})

BuildIndexOptions build index options

func (Migrator) ColumnTypes

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

ColumnTypes return columnTypes []gorm.ColumnType and execErr error

func (Migrator) CreateConstraint

func (m Migrator) CreateConstraint(value interface{}, name string) error

CreateConstraint create constraint

func (Migrator) CreateIndex

func (m Migrator) CreateIndex(value interface{}, name string) error

CreateIndex create index `name`

func (Migrator) CreateTable

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

CreateTable create table in database for values

func (Migrator) CreateView

func (m Migrator) CreateView(name string, option gorm.ViewOption) error

CreateView create view from Query in gorm.ViewOption. Query in gorm.ViewOption is a subquery

// CREATE VIEW `user_view` AS SELECT * FROM `users` WHERE age > 20
q := DB.Model(&User{}).Where("age > ?", 20)
DB.Debug().Migrator().CreateView("user_view", gorm.ViewOption{Query: q})

// CREATE OR REPLACE VIEW `users_view` AS SELECT * FROM `users` WITH CHECK OPTION
q := DB.Model(&User{})
DB.Debug().Migrator().CreateView("user_view", gorm.ViewOption{Query: q, Replace: true, CheckOption: "WITH CHECK OPTION"})

func (Migrator) CurrentDatabase

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

CurrentDatabase returns current database name

func (Migrator) CurrentTable

func (m Migrator) CurrentTable(stmt *gorm.Statement) interface{}

CurrentTable returns current statement's table expression

func (Migrator) DataTypeOf

func (m Migrator) DataTypeOf(field *schema.Field) string

DataTypeOf return field's db data type

func (Migrator) DropColumn

func (m Migrator) DropColumn(value interface{}, name string) error

DropColumn drop value's `name` column

func (Migrator) DropConstraint

func (m Migrator) DropConstraint(value interface{}, name string) error

DropConstraint drop constraint

func (Migrator) DropIndex

func (m Migrator) DropIndex(value interface{}, name string) error

DropIndex drop index `name`

func (Migrator) DropTable

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

DropTable drop table for values

func (Migrator) DropView

func (m Migrator) DropView(name string) error

DropView drop view

func (Migrator) FullDataTypeOf

func (m Migrator) FullDataTypeOf(field *schema.Field) (expr clause.Expr)

FullDataTypeOf returns field's db full data type

func (Migrator) GetIndexes

func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error)

GetIndexes return Indexes []gorm.Index and execErr error

func (Migrator) GetTables

func (m Migrator) GetTables() (tableList []string, err error)

GetTables returns tables

func (Migrator) GetTypeAliases

func (m Migrator) GetTypeAliases(databaseTypeName string) []string

GetTypeAliases return database type aliases

func (Migrator) GuessConstraintAndTable

func (m Migrator) GuessConstraintAndTable(stmt *gorm.Statement, name string) (_ *schema.Constraint, _ *schema.Check, table string)

GuessConstraintAndTable guess statement's constraint and it's table based on name

func (Migrator) HasColumn

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

HasColumn check has column `field` for value or not

func (Migrator) HasConstraint

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

HasConstraint check has constraint or not

func (Migrator) HasIndex

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

HasIndex check has index `name` or not

func (Migrator) HasTable

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

HasTable returns table exists or not for value, value could be a struct or string

func (Migrator) MigrateColumn

func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnType gorm.ColumnType) error

MigrateColumn migrate column

func (Migrator) RenameColumn

func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error

RenameColumn rename value's field name from oldName to newName

func (Migrator) RenameIndex

func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error

RenameIndex rename index from oldName to newName

func (Migrator) RenameTable

func (m Migrator) RenameTable(oldName, newName interface{}) error

RenameTable rename table from oldName to newName

func (Migrator) ReorderModels

func (m Migrator) ReorderModels(values []interface{}, autoAdd bool) (results []interface{})

ReorderModels reorder models according to constraint dependencies

func (Migrator) RunWithValue

func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error) error

RunWithValue run migration with statement value

func (Migrator) TableType

func (m Migrator) TableType(dst interface{}) (gorm.TableType, error)

TableType return tableType gorm.TableType and execErr error

type TableType

type TableType struct {
	SchemaValue  string
	NameValue    string
	TypeValue    string
	CommentValue sql.NullString
}

TableType table type implements TableType interface

func (TableType) Comment

func (ct TableType) Comment() (comment string, ok bool)

Comment returns the comment of current table.

func (TableType) Name

func (ct TableType) Name() string

Name returns the name of the table.

func (TableType) Schema

func (ct TableType) Schema() string

Schema returns the schema of the table.

func (TableType) Type

func (ct TableType) Type() string

Type returns the type of the table.

Jump to

Keyboard shortcuts

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