migrator

package
v0.0.0-...-900be4e Latest Latest
Warning

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

Go to latest
Published: Jan 4, 2024 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

View Source
const (
	Postgres = "postgres"
	SQLite   = "sqlite3"
	MySQL    = "mysql"
	MSSQL    = "mssql"
)
View Source
const (
	IndexType = iota + 1
	UniqueIndex
)

Variables

View Source
var (
	DB_Bit       = "BIT"
	DB_TinyInt   = "TINYINT"
	DB_SmallInt  = "SMALLINT"
	DB_MediumInt = "MEDIUMINT"
	DB_Int       = "INT"
	DB_Integer   = "INTEGER"
	DB_BigInt    = "BIGINT"

	DB_Set = "SET"

	DB_Char       = "CHAR"
	DB_Varchar    = "VARCHAR"
	DB_NVarchar   = "NVARCHAR"
	DB_TinyText   = "TINYTEXT"
	DB_Text       = "TEXT"
	DB_MediumText = "MEDIUMTEXT"
	DB_LongText   = "LONGTEXT"
	DB_Uuid       = "UUID"

	DB_Date       = "DATE"
	DB_DateTime   = "DATETIME"
	DB_Time       = "TIME"
	DB_TimeStamp  = "TIMESTAMP"
	DB_TimeStampz = "TIMESTAMPZ"

	DB_Decimal = "DECIMAL"
	DB_Numeric = "NUMERIC"

	DB_Real   = "REAL"
	DB_Float  = "FLOAT"
	DB_Double = "DOUBLE"

	DB_Binary     = "BINARY"
	DB_VarBinary  = "VARBINARY"
	DB_TinyBlob   = "TINYBLOB"
	DB_Blob       = "BLOB"
	DB_MediumBlob = "MEDIUMBLOB"
	DB_LongBlob   = "LONGBLOB"
	DB_Bytea      = "BYTEA"

	DB_Bool = "BOOL"

	DB_Serial    = "SERIAL"
	DB_BigSerial = "BIGSERIAL"
)

Functions

This section is empty.

Types

type AddColumnMigration

type AddColumnMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewAddColumnMigration

func NewAddColumnMigration(table Table, col *Column) *AddColumnMigration

func (*AddColumnMigration) Column

func (m *AddColumnMigration) Column(col *Column) *AddColumnMigration

func (*AddColumnMigration) SQL

func (m *AddColumnMigration) SQL(dialect Dialect) string

func (*AddColumnMigration) Table

func (m *AddColumnMigration) Table(tableName string) *AddColumnMigration

type AddIndexMigration

type AddIndexMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewAddIndexMigration

func NewAddIndexMigration(table Table, index *Index) *AddIndexMigration

func (*AddIndexMigration) SQL

func (m *AddIndexMigration) SQL(dialect Dialect) string

func (*AddIndexMigration) Table

func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration

type AddTableMigration

type AddTableMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewAddTableMigration

func NewAddTableMigration(table Table) *AddTableMigration

func (*AddTableMigration) SQL

func (m *AddTableMigration) SQL(d Dialect) string

type BaseDialect

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

func (*BaseDialect) AddColumnSQL

func (b *BaseDialect) AddColumnSQL(tableName string, col *Column) string

func (*BaseDialect) AndStr

func (b *BaseDialect) AndStr() string

func (*BaseDialect) CleanDB

func (b *BaseDialect) CleanDB() error

func (*BaseDialect) ColString

func (b *BaseDialect) ColString(col *Column) string

func (*BaseDialect) ColStringNoPk

func (b *BaseDialect) ColStringNoPk(col *Column) string

func (*BaseDialect) ColumnCheckSQL

func (b *BaseDialect) ColumnCheckSQL(tableName, columnName string) (string, []interface{})

func (*BaseDialect) CopyTableData

func (b *BaseDialect) CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string

func (*BaseDialect) CreateIndexSQL

func (b *BaseDialect) CreateIndexSQL(tableName string, index *Index) string

func (*BaseDialect) CreateTableSQL

func (b *BaseDialect) CreateTableSQL(table *Table) string

func (*BaseDialect) DateTimeFunc

func (b *BaseDialect) DateTimeFunc(value string) string

func (*BaseDialect) Default

func (b *BaseDialect) Default(col *Column) string

func (*BaseDialect) DriverName

func (b *BaseDialect) DriverName() string

func (*BaseDialect) DropIndexSQL

func (b *BaseDialect) DropIndexSQL(tableName string, index *Index) string

func (*BaseDialect) DropTable

func (b *BaseDialect) DropTable(tableName string) string

func (*BaseDialect) EqStr

func (b *BaseDialect) EqStr() string

func (*BaseDialect) LikeStr

func (b *BaseDialect) LikeStr() string

func (*BaseDialect) Limit

func (b *BaseDialect) Limit(limit int64) string

func (*BaseDialect) LimitOffset

func (b *BaseDialect) LimitOffset(limit int64, offset int64) string

func (*BaseDialect) NoOpSQL

func (b *BaseDialect) NoOpSQL() string

func (*BaseDialect) OrStr

func (b *BaseDialect) OrStr() string

func (*BaseDialect) PostInsertId

func (b *BaseDialect) PostInsertId(table string, sess *xorm.Session) error

func (*BaseDialect) PreInsertId

func (b *BaseDialect) PreInsertId(table string, sess *xorm.Session) error

func (*BaseDialect) QuoteColList

func (b *BaseDialect) QuoteColList(cols []string) string

func (*BaseDialect) RenameTable

func (b *BaseDialect) RenameTable(oldName string, newName string) string

func (*BaseDialect) ShowCreateNull

func (b *BaseDialect) ShowCreateNull() bool

func (*BaseDialect) TruncateDBTables

func (b *BaseDialect) TruncateDBTables() error

func (*BaseDialect) UpdateTableSQL

func (b *BaseDialect) UpdateTableSQL(tableName string, columns []*Column) string

func (*BaseDialect) UpsertSQL

func (b *BaseDialect) UpsertSQL(tableName string, keyCols, updateCols []string) string

UpsertSQL returns empty string

type CodeMigration

type CodeMigration interface {
	Migration
	Exec(sess *xorm.Session, migrator *Migrator) error
}

type Column

type Column struct {
	Name            string
	Type            string
	Length          int
	Length2         int
	Nullable        bool
	IsPrimaryKey    bool
	IsAutoIncrement bool
	Default         string
}

func (*Column) String

func (col *Column) String(d Dialect) string

func (*Column) StringNoPk

func (col *Column) StringNoPk(d Dialect) string

type ColumnType

type ColumnType string
const (
	DB_TYPE_STRING ColumnType = "String"
)

type CopyTableDataMigration

type CopyTableDataMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewCopyTableDataMigration

func NewCopyTableDataMigration(targetTable string, sourceTable string, colMap map[string]string) *CopyTableDataMigration

func (*CopyTableDataMigration) SQL

type Dialect

type Dialect interface {
	DriverName() string
	Quote(string) string
	AndStr() string
	AutoIncrStr() string
	OrStr() string
	EqStr() string
	ShowCreateNull() bool
	SQLType(col *Column) string
	SupportEngine() bool
	LikeStr() string
	Default(col *Column) string
	BooleanStr(bool) string
	DateTimeFunc(string) string

	CreateIndexSQL(tableName string, index *Index) string
	CreateTableSQL(table *Table) string
	AddColumnSQL(tableName string, col *Column) string
	CopyTableData(sourceTable string, targetTable string, sourceCols []string, targetCols []string) string
	DropTable(tableName string) string
	DropIndexSQL(tableName string, index *Index) string

	RenameTable(oldName string, newName string) string
	UpdateTableSQL(tableName string, columns []*Column) string

	IndexCheckSQL(tableName, indexName string) (string, []interface{})
	ColumnCheckSQL(tableName, columnName string) (string, []interface{})
	// UpsertSQL returns the upsert sql statement for a dialect
	UpsertSQL(tableName string, keyCols, updateCols []string) string

	ColString(*Column) string
	ColStringNoPk(*Column) string

	Limit(limit int64) string
	LimitOffset(limit int64, offset int64) string

	PreInsertId(table string, sess *xorm.Session) error
	PostInsertId(table string, sess *xorm.Session) error

	CleanDB() error
	TruncateDBTables() error
	NoOpSQL() string

	IsUniqueConstraintViolation(err error) bool
	ErrorMessage(err error) string
	IsDeadlock(err error) bool
}

func NewDialect

func NewDialect(engine *xorm.Engine) Dialect

func NewMysqlDialect

func NewMysqlDialect(engine *xorm.Engine) Dialect

func NewPostgresDialect

func NewPostgresDialect(engine *xorm.Engine) Dialect

func NewSQLite3Dialect

func NewSQLite3Dialect(engine *xorm.Engine) Dialect

type DropIndexMigration

type DropIndexMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewDropIndexMigration

func NewDropIndexMigration(table Table, index *Index) *DropIndexMigration

func (*DropIndexMigration) SQL

func (m *DropIndexMigration) SQL(dialect Dialect) string

type DropTableMigration

type DropTableMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewDropTableMigration

func NewDropTableMigration(tableName string) *DropTableMigration

func (*DropTableMigration) SQL

func (m *DropTableMigration) SQL(d Dialect) string

type ExistsMigrationCondition

type ExistsMigrationCondition struct{}

func (*ExistsMigrationCondition) IsFulfilled

func (c *ExistsMigrationCondition) IsFulfilled(results []map[string][]byte) bool

type IfColumnNotExistsCondition

type IfColumnNotExistsCondition struct {
	NotExistsMigrationCondition
	TableName  string
	ColumnName string
}

func (*IfColumnNotExistsCondition) SQL

func (c *IfColumnNotExistsCondition) SQL(dialect Dialect) (string, []interface{})

type IfIndexExistsCondition

type IfIndexExistsCondition struct {
	ExistsMigrationCondition
	TableName string
	IndexName string
}

func (*IfIndexExistsCondition) SQL

func (c *IfIndexExistsCondition) SQL(dialect Dialect) (string, []interface{})

type IfIndexNotExistsCondition

type IfIndexNotExistsCondition struct {
	NotExistsMigrationCondition
	TableName string
	IndexName string
}

func (*IfIndexNotExistsCondition) SQL

func (c *IfIndexNotExistsCondition) SQL(dialect Dialect) (string, []interface{})

type Index

type Index struct {
	Name string
	Type int
	Cols []string
}

func (*Index) XName

func (index *Index) XName(tableName string) string

type Migration

type Migration interface {
	SQL(dialect Dialect) string
	Id() string
	SetId(string)
	GetCondition() MigrationCondition
}

type MigrationBase

type MigrationBase struct {
	Condition MigrationCondition
	// contains filtered or unexported fields
}

func (*MigrationBase) GetCondition

func (m *MigrationBase) GetCondition() MigrationCondition

func (*MigrationBase) Id

func (m *MigrationBase) Id() string

func (*MigrationBase) SetId

func (m *MigrationBase) SetId(id string)

type MigrationCondition

type MigrationCondition interface {
	SQL(dialect Dialect) (string, []interface{})
	IsFulfilled(results []map[string][]byte) bool
}

type MigrationLog

type MigrationLog struct {
	Id          int64
	MigrationID string `xorm:"migration_id"`
	SQL         string `xorm:"sql"`
	Success     bool
	Error       string
	Timestamp   time.Time
}

type Migrator

type Migrator struct {
	Dialect Dialect

	Logger log.Logger
	// contains filtered or unexported fields
}

func NewMigrator

func NewMigrator(engine *xorm.Engine) *Migrator

func (*Migrator) AddMigration

func (mg *Migrator) AddMigration(id string, m Migration)

func (*Migrator) GetMigrationLog

func (mg *Migrator) GetMigrationLog() (map[string]MigrationLog, error)

func (*Migrator) MigrationsCount

func (mg *Migrator) MigrationsCount() int

func (*Migrator) Start

func (mg *Migrator) Start() error

type MySQLDialect

type MySQLDialect struct {
	BaseDialect
}

func (*MySQLDialect) AutoIncrStr

func (db *MySQLDialect) AutoIncrStr() string

func (*MySQLDialect) BooleanStr

func (db *MySQLDialect) BooleanStr(value bool) string

func (*MySQLDialect) CleanDB

func (db *MySQLDialect) CleanDB() error

func (*MySQLDialect) ColumnCheckSQL

func (db *MySQLDialect) ColumnCheckSQL(tableName, columnName string) (string, []interface{})

func (*MySQLDialect) ErrorMessage

func (db *MySQLDialect) ErrorMessage(err error) string

func (*MySQLDialect) IndexCheckSQL

func (db *MySQLDialect) IndexCheckSQL(tableName, indexName string) (string, []interface{})

func (*MySQLDialect) IsDeadlock

func (db *MySQLDialect) IsDeadlock(err error) bool

func (*MySQLDialect) IsUniqueConstraintViolation

func (db *MySQLDialect) IsUniqueConstraintViolation(err error) bool

func (*MySQLDialect) Quote

func (db *MySQLDialect) Quote(name string) string

func (*MySQLDialect) SQLType

func (db *MySQLDialect) SQLType(c *Column) string

func (*MySQLDialect) SupportEngine

func (db *MySQLDialect) SupportEngine() bool

func (*MySQLDialect) TruncateDBTables

func (db *MySQLDialect) TruncateDBTables() error

TruncateDBTables truncates all the tables. A special case is the dashboard_acl table where we keep the default permissions.

func (*MySQLDialect) UpdateTableSQL

func (db *MySQLDialect) UpdateTableSQL(tableName string, columns []*Column) string

func (*MySQLDialect) UpsertSQL

func (db *MySQLDialect) UpsertSQL(tableName string, keyCols, updateCols []string) string

UpsertSQL returns the upsert sql statement for PostgreSQL dialect

type NotExistsMigrationCondition

type NotExistsMigrationCondition struct{}

func (*NotExistsMigrationCondition) IsFulfilled

func (c *NotExistsMigrationCondition) IsFulfilled(results []map[string][]byte) bool

type PostgresDialect

type PostgresDialect struct {
	BaseDialect
}

func (*PostgresDialect) AutoIncrStr

func (db *PostgresDialect) AutoIncrStr() string

func (*PostgresDialect) BooleanStr

func (db *PostgresDialect) BooleanStr(value bool) string

func (*PostgresDialect) CleanDB

func (db *PostgresDialect) CleanDB() error

func (*PostgresDialect) Default

func (db *PostgresDialect) Default(col *Column) string

func (*PostgresDialect) DropIndexSQL

func (db *PostgresDialect) DropIndexSQL(tableName string, index *Index) string

func (*PostgresDialect) ErrorMessage

func (db *PostgresDialect) ErrorMessage(err error) string

func (*PostgresDialect) IndexCheckSQL

func (db *PostgresDialect) IndexCheckSQL(tableName, indexName string) (string, []interface{})

func (*PostgresDialect) IsDeadlock

func (db *PostgresDialect) IsDeadlock(err error) bool

func (*PostgresDialect) IsUniqueConstraintViolation

func (db *PostgresDialect) IsUniqueConstraintViolation(err error) bool

func (*PostgresDialect) LikeStr

func (db *PostgresDialect) LikeStr() string

func (*PostgresDialect) PostInsertId

func (db *PostgresDialect) PostInsertId(table string, sess *xorm.Session) error

func (*PostgresDialect) Quote

func (db *PostgresDialect) Quote(name string) string

func (*PostgresDialect) SQLType

func (db *PostgresDialect) SQLType(c *Column) string

func (*PostgresDialect) SupportEngine

func (db *PostgresDialect) SupportEngine() bool

func (*PostgresDialect) TruncateDBTables

func (db *PostgresDialect) TruncateDBTables() error

TruncateDBTables truncates all the tables. A special case is the dashboard_acl table where we keep the default permissions.

func (*PostgresDialect) UpdateTableSQL

func (db *PostgresDialect) UpdateTableSQL(tableName string, columns []*Column) string

func (*PostgresDialect) UpsertSQL

func (db *PostgresDialect) UpsertSQL(tableName string, keyCols, updateCols []string) string

UpsertSQL returns the upsert sql statement for PostgreSQL dialect

type RawSQLMigration

type RawSQLMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewRawSQLMigration

func NewRawSQLMigration(sql string) *RawSQLMigration

func (*RawSQLMigration) Default

func (m *RawSQLMigration) Default(sql string) *RawSQLMigration

func (*RawSQLMigration) Mssql

func (m *RawSQLMigration) Mssql(sql string) *RawSQLMigration

func (*RawSQLMigration) Mysql

func (m *RawSQLMigration) Mysql(sql string) *RawSQLMigration

func (*RawSQLMigration) Postgres

func (m *RawSQLMigration) Postgres(sql string) *RawSQLMigration

func (*RawSQLMigration) SQL

func (m *RawSQLMigration) SQL(dialect Dialect) string

func (*RawSQLMigration) SQLite

func (m *RawSQLMigration) SQLite(sql string) *RawSQLMigration

func (*RawSQLMigration) Set

func (m *RawSQLMigration) Set(dialect string, sql string) *RawSQLMigration

type RenameTableMigration

type RenameTableMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewRenameTableMigration

func NewRenameTableMigration(oldName string, newName string) *RenameTableMigration

func (*RenameTableMigration) Rename

func (m *RenameTableMigration) Rename(oldName string, newName string) *RenameTableMigration

func (*RenameTableMigration) SQL

type SQLType

type SQLType string

type SQLite3

type SQLite3 struct {
	BaseDialect
}

func (*SQLite3) AutoIncrStr

func (db *SQLite3) AutoIncrStr() string

func (*SQLite3) BooleanStr

func (db *SQLite3) BooleanStr(value bool) string

func (*SQLite3) CleanDB

func (db *SQLite3) CleanDB() error

func (*SQLite3) DateTimeFunc

func (db *SQLite3) DateTimeFunc(value string) string

func (*SQLite3) DropIndexSQL

func (db *SQLite3) DropIndexSQL(tableName string, index *Index) string

func (*SQLite3) ErrorMessage

func (db *SQLite3) ErrorMessage(err error) string

func (*SQLite3) IndexCheckSQL

func (db *SQLite3) IndexCheckSQL(tableName, indexName string) (string, []interface{})

func (*SQLite3) IsDeadlock

func (db *SQLite3) IsDeadlock(err error) bool

func (*SQLite3) IsUniqueConstraintViolation

func (db *SQLite3) IsUniqueConstraintViolation(err error) bool

func (*SQLite3) Quote

func (db *SQLite3) Quote(name string) string

func (*SQLite3) SQLType

func (db *SQLite3) SQLType(c *Column) string

func (*SQLite3) SupportEngine

func (db *SQLite3) SupportEngine() bool

func (*SQLite3) TruncateDBTables

func (db *SQLite3) TruncateDBTables() error

TruncateDBTables deletes all data from all the tables and resets the sequences. A special case is the dashboard_acl table where we keep the default permissions.

func (*SQLite3) UpsertSQL

func (db *SQLite3) UpsertSQL(tableName string, keyCols, updateCols []string) string

UpsertSQL returns the upsert sql statement for SQLite dialect

type Table

type Table struct {
	Name        string
	Columns     []*Column
	PrimaryKeys []string
	Indices     []*Index
}

type TableCharsetMigration

type TableCharsetMigration struct {
	MigrationBase
	// contains filtered or unexported fields
}

func NewTableCharsetMigration

func NewTableCharsetMigration(tableName string, columns []*Column) *TableCharsetMigration

func (*TableCharsetMigration) SQL

Jump to

Keyboard shortcuts

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