Refactor migration.

Add `ChangeColumn`, `AddIndex`, `DropIndex` method to
Operation interface.
This commit is contained in:
Nurahmadie 2014-03-10 07:08:58 +07:00
parent 28fef9e3f4
commit 81cf496811
3 changed files with 38 additions and 25 deletions

View file

@ -27,7 +27,7 @@ var db *sql.DB
func Init(name, datasource string) error {
driver := map[string]struct {
Md *meddler.Database
Mg migrate.DriverFunction
Mg migrate.DriverBuilder
}{
"sqlite3": {
meddler.SQLite,

View file

@ -0,0 +1,36 @@
package migrate
import (
"database/sql"
)
// Operation interface covers basic migration operations.
// Implementation details is specific for each database,
// see migrate/sqlite.go for implementation reference.
type Operation interface {
CreateTable(tableName string, args []string) (sql.Result, error)
RenameTable(tableName, newName string) (sql.Result, error)
DropTable(tableName string) (sql.Result, error)
AddColumn(tableName, columnSpec string) (sql.Result, error)
ChangeColumn(tableName, columnName, newType string) (sql.Result, error)
DropColumns(tableName string, columnsToDrop []string) (sql.Result, error)
RenameColumns(tableName string, columnChanges map[string]string) (sql.Result, error)
AddIndex(tableName string, columns []string, flag string) (sql.Result, error)
DropIndex(tableName string, columns[]string) (sql.Result, error)
}
type MigrationDriver struct {
Tx *sql.Tx
Operation
}
type DriverBuilder func(tx *sql.Tx) *MigrationDriver

View file

@ -48,35 +48,12 @@ INSERT INTO migration (revision) VALUES (?)
const deleteRevisionStmt = `
DELETE FROM migration where revision = ?
`
// Operation interface covers basic migration operations.
// Implementation details is specific for each database,
// see migrate/sqlite.go for implementation reference.
type Operation interface {
CreateTable(tableName string, args []string) (sql.Result, error)
RenameTable(tableName, newName string) (sql.Result, error)
DropTable(tableName string) (sql.Result, error)
AddColumn(tableName, columnSpec string) (sql.Result, error)
DropColumns(tableName string, columnsToDrop []string) (sql.Result, error)
RenameColumns(tableName string, columnChanges map[string]string) (sql.Result, error)
}
type Revision interface {
Up(mg *MigrationDriver) error
Down(mg *MigrationDriver) error
Revision() int64
}
type MigrationDriver struct {
Tx *sql.Tx
Operation
}
type Migration struct {
db *sql.DB
revs []Revision
@ -84,7 +61,7 @@ type Migration struct {
type DriverFunction func(tx *sql.Tx) *MigrationDriver
var Driver DriverFunction
var Driver DriverBuilder
func New(db *sql.DB) *Migration {
return &Migration{db: db}