2014-03-10 03:08:58 +03:00
|
|
|
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 {
|
2014-03-16 06:52:28 +03:00
|
|
|
|
|
|
|
// CreateTable may be used to create a table named `tableName`
|
|
|
|
// with its columns specification listed in `args` as an array of string
|
2014-03-10 03:08:58 +03:00
|
|
|
CreateTable(tableName string, args []string) (sql.Result, error)
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// RenameTable simply rename table from `tableName` to `newName`
|
2014-03-10 03:08:58 +03:00
|
|
|
RenameTable(tableName, newName string) (sql.Result, error)
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// DropTable drops table named `tableName`
|
2014-03-10 03:08:58 +03:00
|
|
|
DropTable(tableName string) (sql.Result, error)
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// AddColumn adds single new column to `tableName`, columnSpec is
|
|
|
|
// a standard column definition (column name included) which may looks like this:
|
|
|
|
//
|
|
|
|
// mg.AddColumn("example", "email VARCHAR(255) UNIQUE")
|
|
|
|
//
|
|
|
|
// it's equivalent to:
|
|
|
|
//
|
|
|
|
// mg.AddColumn("example", mg.T.String("email", UNIQUE))
|
|
|
|
//
|
2014-03-10 03:08:58 +03:00
|
|
|
AddColumn(tableName, columnSpec string) (sql.Result, error)
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// ChangeColumn may be used to change the type of a column
|
|
|
|
// `newType` should always specify the column's new type even
|
|
|
|
// if the type is not meant to be change. Eg.
|
|
|
|
//
|
|
|
|
// mg.ChangeColumn("example", "name", "VARCHAR(255) UNIQUE")
|
|
|
|
//
|
2014-03-10 03:08:58 +03:00
|
|
|
ChangeColumn(tableName, columnName, newType string) (sql.Result, error)
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// DropColumns drops a list of columns
|
|
|
|
DropColumns(tableName string, columnsToDrop ...string) (sql.Result, error)
|
2014-03-10 03:08:58 +03:00
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// RenameColumns will rename columns listed in `columnChanges`
|
2014-03-10 03:08:58 +03:00
|
|
|
RenameColumns(tableName string, columnChanges map[string]string) (sql.Result, error)
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// AddIndex adds index on `tableName` indexed by `columns`
|
2014-03-13 22:14:16 +03:00
|
|
|
AddIndex(tableName string, columns []string, flags ...string) (sql.Result, error)
|
2014-03-10 03:08:58 +03:00
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// DropIndex drops index indexed by `columns` from `tableName`
|
2014-03-10 07:30:39 +03:00
|
|
|
DropIndex(tableName string, columns []string) (sql.Result, error)
|
2014-03-10 03:08:58 +03:00
|
|
|
}
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// MigrationDriver drives migration script by injecting transaction object (*sql.Tx),
|
|
|
|
// `Operation` implementation and column type helper.
|
2014-03-10 03:08:58 +03:00
|
|
|
type MigrationDriver struct {
|
|
|
|
Operation
|
2014-03-13 14:53:52 +03:00
|
|
|
T *columnType
|
|
|
|
Tx *sql.Tx
|
2014-03-10 03:08:58 +03:00
|
|
|
}
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
// DriverBuilder is a constructor for MigrationDriver
|
2014-03-10 03:08:58 +03:00
|
|
|
type DriverBuilder func(tx *sql.Tx) *MigrationDriver
|