2015-01-20 07:15:48 -06:00
|
|
|
package migrator
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
2015-01-17 14:40:22 -06:00
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
type MigrationBase struct {
|
2015-01-18 11:41:03 -06:00
|
|
|
id string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MigrationBase) Id() string {
|
|
|
|
return m.id
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *MigrationBase) SetId(id string) {
|
|
|
|
m.id = id
|
2015-01-18 07:51:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type RawSqlMigration struct {
|
|
|
|
MigrationBase
|
|
|
|
|
|
|
|
sqlite string
|
|
|
|
mysql string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RawSqlMigration) Sql(dialect Dialect) string {
|
|
|
|
switch dialect.DriverName() {
|
|
|
|
case MYSQL:
|
2015-01-18 06:08:59 -06:00
|
|
|
return m.mysql
|
2015-01-18 07:51:51 -06:00
|
|
|
case SQLITE:
|
2015-01-18 06:08:59 -06:00
|
|
|
return m.sqlite
|
|
|
|
}
|
|
|
|
|
|
|
|
panic("db type not supported")
|
|
|
|
}
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
func (m *RawSqlMigration) Sqlite(sql string) *RawSqlMigration {
|
|
|
|
m.sqlite = sql
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RawSqlMigration) Mysql(sql string) *RawSqlMigration {
|
|
|
|
m.mysql = sql
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddColumnMigration struct {
|
|
|
|
MigrationBase
|
2015-01-19 03:44:16 -06:00
|
|
|
tableName string
|
|
|
|
column *Column
|
2015-01-18 07:51:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddColumnMigration) Table(tableName string) *AddColumnMigration {
|
|
|
|
m.tableName = tableName
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-01-19 03:44:16 -06:00
|
|
|
func (m *AddColumnMigration) Column(col *Column) *AddColumnMigration {
|
|
|
|
m.column = col
|
2015-01-18 07:51:51 -06:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddColumnMigration) Sql(dialect Dialect) string {
|
2015-01-19 03:44:16 -06:00
|
|
|
return dialect.AddColumnSql(m.tableName, m.column)
|
2015-01-18 07:51:51 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
type AddIndexMigration struct {
|
|
|
|
MigrationBase
|
|
|
|
tableName string
|
2015-01-19 03:44:16 -06:00
|
|
|
index Index
|
2015-01-17 14:40:22 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
func (m *AddIndexMigration) Name(name string) *AddIndexMigration {
|
2015-01-19 03:44:16 -06:00
|
|
|
m.index.Name = name
|
2015-01-18 07:51:51 -06:00
|
|
|
return m
|
2015-01-18 06:08:59 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
func (m *AddIndexMigration) Table(tableName string) *AddIndexMigration {
|
|
|
|
m.tableName = tableName
|
|
|
|
return m
|
2015-01-17 14:40:22 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 11:41:03 -06:00
|
|
|
func (m *AddIndexMigration) Unique() *AddIndexMigration {
|
2015-01-19 03:44:16 -06:00
|
|
|
m.index.Type = UniqueIndex
|
2015-01-18 11:41:03 -06:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
func (m *AddIndexMigration) Columns(columns ...string) *AddIndexMigration {
|
2015-01-19 03:44:16 -06:00
|
|
|
m.index.Cols = columns
|
2015-01-18 07:51:51 -06:00
|
|
|
return m
|
2015-01-17 14:40:22 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
func (m *AddIndexMigration) Sql(dialect Dialect) string {
|
2015-01-20 07:15:48 -06:00
|
|
|
if m.index.Name == "" {
|
|
|
|
m.index.Name = fmt.Sprintf("%s", strings.Join(m.index.Cols, "_"))
|
|
|
|
}
|
2015-01-19 03:44:16 -06:00
|
|
|
return dialect.CreateIndexSql(m.tableName, &m.index)
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddTableMigration struct {
|
|
|
|
MigrationBase
|
|
|
|
table Table
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddTableMigration) Sql(d Dialect) string {
|
|
|
|
return d.CreateTableSql(&m.table)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddTableMigration) Name(name string) *AddTableMigration {
|
|
|
|
m.table.Name = name
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddTableMigration) WithColumns(columns ...*Column) *AddTableMigration {
|
|
|
|
for _, col := range columns {
|
|
|
|
m.table.Columns = append(m.table.Columns, col)
|
|
|
|
if col.IsPrimaryKey {
|
|
|
|
m.table.PrimaryKeys = append(m.table.PrimaryKeys, col.Name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddTableMigration) WithColumn(col *Column) *AddTableMigration {
|
|
|
|
m.table.Columns = append(m.table.Columns, col)
|
|
|
|
if col.IsPrimaryKey {
|
|
|
|
m.table.PrimaryKeys = append(m.table.PrimaryKeys, col.Name)
|
|
|
|
}
|
|
|
|
return m
|
2015-01-17 14:40:22 -06:00
|
|
|
}
|
2015-02-23 13:48:43 -06:00
|
|
|
|
|
|
|
type RenameColumnMigration struct {
|
|
|
|
MigrationBase
|
|
|
|
tableName string
|
|
|
|
oldName string
|
|
|
|
newName string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RenameColumnMigration) Table(tableName string) *RenameColumnMigration {
|
|
|
|
m.tableName = tableName
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RenameColumnMigration) Rename(oldName string, newName string) *RenameColumnMigration {
|
|
|
|
m.oldName = oldName
|
|
|
|
m.newName = newName
|
|
|
|
return m
|
|
|
|
}
|