40 Star 146 Fork 3

Gitee 极速下载/grafana

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
此仓库是为了提升国内下载速度的镜像仓库,每日同步一次。 原始仓库: https://github.com/grafana/grafana
克隆/下载
migrations.go 4.76 KB
一键复制 编辑 原始数据 按行查看 历史
Dan Cech 提交于 2017-03-28 08:34 . utf8mb4 encoding (#7959)
package migrator
import (
"fmt"
"strings"
)
type MigrationBase struct {
id string
Condition MigrationCondition
}
func (m *MigrationBase) Id() string {
return m.id
}
func (m *MigrationBase) SetId(id string) {
m.id = id
}
func (m *MigrationBase) GetCondition() MigrationCondition {
return m.Condition
}
type RawSqlMigration struct {
MigrationBase
sqlite string
mysql string
postgres string
}
func (m *RawSqlMigration) Sql(dialect Dialect) string {
switch dialect.DriverName() {
case MYSQL:
return m.mysql
case SQLITE:
return m.sqlite
case POSTGRES:
return m.postgres
}
panic("db type not supported")
}
func (m *RawSqlMigration) Sqlite(sql string) *RawSqlMigration {
m.sqlite = sql
return m
}
func (m *RawSqlMigration) Mysql(sql string) *RawSqlMigration {
m.mysql = sql
return m
}
func (m *RawSqlMigration) Postgres(sql string) *RawSqlMigration {
m.postgres = sql
return m
}
type AddColumnMigration struct {
MigrationBase
tableName string
column *Column
}
func NewAddColumnMigration(table Table, col *Column) *AddColumnMigration {
return &AddColumnMigration{tableName: table.Name, column: col}
}
func (m *AddColumnMigration) Table(tableName string) *AddColumnMigration {
m.tableName = tableName
return m
}
func (m *AddColumnMigration) Column(col *Column) *AddColumnMigration {
m.column = col
return m
}
func (m *AddColumnMigration) Sql(dialect Dialect) string {
return dialect.AddColumnSql(m.tableName, m.column)
}
type AddIndexMigration struct {
MigrationBase
tableName string
index *Index
}
func NewAddIndexMigration(table Table, index *Index) *AddIndexMigration {
return &AddIndexMigration{tableName: table.Name, index: index}
}
func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
m.tableName = tableName
return m
}
func (m *AddIndexMigration) Sql(dialect Dialect) string {
return dialect.CreateIndexSql(m.tableName, m.index)
}
type DropIndexMigration struct {
MigrationBase
tableName string
index *Index
}
func NewDropIndexMigration(table Table, index *Index) *DropIndexMigration {
return &DropIndexMigration{tableName: table.Name, index: index}
}
func (m *DropIndexMigration) Sql(dialect Dialect) string {
if m.index.Name == "" {
m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
}
return dialect.DropIndexSql(m.tableName, m.index)
}
type AddTableMigration struct {
MigrationBase
table Table
}
func NewAddTableMigration(table Table) *AddTableMigration {
for _, col := range table.Columns {
if col.IsPrimaryKey {
table.PrimaryKeys = append(table.PrimaryKeys, col.Name)
}
}
return &AddTableMigration{table: table}
}
func (m *AddTableMigration) Sql(d Dialect) string {
return d.CreateTableSql(&m.table)
}
type DropTableMigration struct {
MigrationBase
tableName string
}
func NewDropTableMigration(tableName string) *DropTableMigration {
return &DropTableMigration{tableName: tableName}
}
func (m *DropTableMigration) Sql(d Dialect) string {
return d.DropTable(m.tableName)
}
type RenameTableMigration struct {
MigrationBase
oldName string
newName string
}
func NewRenameTableMigration(oldName string, newName string) *RenameTableMigration {
return &RenameTableMigration{oldName: oldName, newName: newName}
}
func (m *RenameTableMigration) IfTableExists(tableName string) *RenameTableMigration {
m.Condition = &IfTableExistsCondition{TableName: tableName}
return m
}
func (m *RenameTableMigration) Rename(oldName string, newName string) *RenameTableMigration {
m.oldName = oldName
m.newName = newName
return m
}
func (m *RenameTableMigration) Sql(d Dialect) string {
return d.RenameTable(m.oldName, m.newName)
}
type CopyTableDataMigration struct {
MigrationBase
sourceTable string
targetTable string
sourceCols []string
targetCols []string
colMap map[string]string
}
func NewCopyTableDataMigration(targetTable string, sourceTable string, colMap map[string]string) *CopyTableDataMigration {
m := &CopyTableDataMigration{sourceTable: sourceTable, targetTable: targetTable}
for key, value := range colMap {
m.targetCols = append(m.targetCols, key)
m.sourceCols = append(m.sourceCols, value)
}
return m
}
func (m *CopyTableDataMigration) IfTableExists(tableName string) *CopyTableDataMigration {
m.Condition = &IfTableExistsCondition{TableName: tableName}
return m
}
func (m *CopyTableDataMigration) Sql(d Dialect) string {
return d.CopyTableData(m.sourceTable, m.targetTable, m.sourceCols, m.targetCols)
}
type TableCharsetMigration struct {
MigrationBase
tableName string
columns []*Column
}
func NewTableCharsetMigration(tableName string, columns []*Column) *TableCharsetMigration {
return &TableCharsetMigration{tableName: tableName, columns: columns}
}
func (m *TableCharsetMigration) Sql(d Dialect) string {
return d.UpdateTableSql(m.tableName, m.columns)
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
JavaScript
1
https://gitee.com/mirrors/grafana.git
git@gitee.com:mirrors/grafana.git
mirrors
grafana
grafana
v4.6.4

搜索帮助

0d507c66 1850385 C8b1a773 1850385