2015-01-17 14:40:22 -06:00
|
|
|
package migrations
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
POSTGRES = "postgres"
|
|
|
|
SQLITE = "sqlite3"
|
|
|
|
MYSQL = "mysql"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Migration interface {
|
|
|
|
Sql(dialect Dialect) string
|
2015-01-17 14:40:22 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
type ColumnType string
|
2015-01-18 06:08:59 -06:00
|
|
|
|
|
|
|
const (
|
2015-01-18 07:51:51 -06:00
|
|
|
DB_TYPE_STRING ColumnType = "String"
|
2015-01-18 06:08:59 -06:00
|
|
|
)
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
type MigrationBase struct {
|
|
|
|
desc string
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *RawSqlMigration) Desc(desc string) *RawSqlMigration {
|
|
|
|
m.desc = desc
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
type AddColumnMigration struct {
|
|
|
|
MigrationBase
|
|
|
|
tableName string
|
|
|
|
columnName string
|
|
|
|
columnType ColumnType
|
|
|
|
length int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddColumnMigration) Table(tableName string) *AddColumnMigration {
|
|
|
|
m.tableName = tableName
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddColumnMigration) Length(length int) *AddColumnMigration {
|
|
|
|
m.length = length
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddColumnMigration) Column(columnName string) *AddColumnMigration {
|
|
|
|
m.columnName = columnName
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddColumnMigration) Type(columnType ColumnType) *AddColumnMigration {
|
|
|
|
m.columnType = columnType
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddColumnMigration) Sql(dialect Dialect) string {
|
|
|
|
return fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s %s", m.tableName, m.columnName, dialect.ToDBTypeSql(m.columnType, m.length))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *AddColumnMigration) Desc(desc string) *AddColumnMigration {
|
|
|
|
m.desc = desc
|
|
|
|
return m
|
2015-01-17 14:40:22 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
type AddIndexMigration struct {
|
|
|
|
MigrationBase
|
|
|
|
tableName string
|
|
|
|
columns string
|
|
|
|
indexName string
|
2015-01-17 14:40:22 -06:00
|
|
|
}
|
|
|
|
|
2015-01-18 07:51:51 -06:00
|
|
|
func (m *AddIndexMigration) Name(name string) *AddIndexMigration {
|
|
|
|
m.indexName = name
|
|
|
|
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 07:51:51 -06:00
|
|
|
func (m *AddIndexMigration) Columns(columns ...string) *AddIndexMigration {
|
|
|
|
m.columns = strings.Join(columns, ",")
|
|
|
|
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 {
|
|
|
|
return fmt.Sprintf("CREATE UNIQUE INDEX %s ON %s(%s)", m.indexName, m.tableName, m.columns)
|
2015-01-17 14:40:22 -06:00
|
|
|
}
|