From 81cf496811bbb0884ef8a4a3e2767a3e077db689 Mon Sep 17 00:00:00 2001 From: Nurahmadie Date: Mon, 10 Mar 2014 07:08:58 +0700 Subject: [PATCH] Refactor migration. Add `ChangeColumn`, `AddIndex`, `DropIndex` method to Operation interface. --- pkg/database/database.go | 2 +- pkg/database/migrate/api.go | 36 +++++++++++++++++++++++++++++++++ pkg/database/migrate/migrate.go | 25 +---------------------- 3 files changed, 38 insertions(+), 25 deletions(-) create mode 100644 pkg/database/migrate/api.go diff --git a/pkg/database/database.go b/pkg/database/database.go index 654694e3..4b5a0c1a 100644 --- a/pkg/database/database.go +++ b/pkg/database/database.go @@ -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, diff --git a/pkg/database/migrate/api.go b/pkg/database/migrate/api.go new file mode 100644 index 00000000..6b98ab84 --- /dev/null +++ b/pkg/database/migrate/api.go @@ -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 + diff --git a/pkg/database/migrate/migrate.go b/pkg/database/migrate/migrate.go index 1b7cbf28..f67e7129 100644 --- a/pkg/database/migrate/migrate.go +++ b/pkg/database/migrate/migrate.go @@ -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}