2014-02-15 12:29:53 +00:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/dchest/uniuri"
|
2014-02-15 15:17:22 +00:00
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2014-02-15 12:29:53 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type SQLiteDriver MigrationDriver
|
|
|
|
|
|
|
|
func SQLite(tx *sql.Tx) Operation {
|
|
|
|
return &SQLiteDriver{Tx: tx}
|
|
|
|
}
|
|
|
|
|
2014-02-18 06:08:06 +00:00
|
|
|
func (s *SQLiteDriver) Exec(query string, args ...interface{}) (sql.Result, error) {
|
2014-02-18 18:11:41 +00:00
|
|
|
return s.Tx.Exec(query, args...)
|
2014-02-18 06:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLiteDriver) Query(query string, args ...interface{}) (*sql.Rows, error) {
|
2014-02-18 18:11:41 +00:00
|
|
|
return s.Tx.Query(query, args...)
|
2014-02-18 06:08:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLiteDriver) QueryRow(query string, args ...interface{}) *sql.Row {
|
2014-02-18 18:11:41 +00:00
|
|
|
return s.Tx.QueryRow(query, args...)
|
2014-02-18 06:08:06 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 12:29:53 +00:00
|
|
|
func (s *SQLiteDriver) CreateTable(tableName string, args []string) (sql.Result, error) {
|
|
|
|
return s.Tx.Exec(fmt.Sprintf("CREATE TABLE %s (%s);", tableName, strings.Join(args, ", ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLiteDriver) RenameTable(tableName, newName string) (sql.Result, error) {
|
|
|
|
return s.Tx.Exec(fmt.Sprintf("ALTER TABLE %s RENAME TO %s;", tableName, newName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLiteDriver) DropTable(tableName string) (sql.Result, error) {
|
|
|
|
return s.Tx.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s;", tableName))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLiteDriver) AddColumn(tableName, columnSpec string) (sql.Result, error) {
|
|
|
|
return s.Tx.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s;", tableName, columnSpec))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLiteDriver) DropColumns(tableName string, columnsToDrop []string) (sql.Result, error) {
|
2014-02-18 14:22:22 +00:00
|
|
|
var err error
|
|
|
|
var result sql.Result
|
2014-02-15 12:29:53 +00:00
|
|
|
|
|
|
|
if len(columnsToDrop) == 0 {
|
|
|
|
return nil, fmt.Errorf("No columns to drop.")
|
|
|
|
}
|
|
|
|
|
2014-02-18 14:22:22 +00:00
|
|
|
tableSQL, err := s.getDDLFromTable(tableName)
|
2014-02-15 12:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-18 14:22:22 +00:00
|
|
|
columns, err := fetchColumns(tableSQL)
|
2014-02-15 12:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
columnNames := selectName(columns)
|
2014-02-15 15:17:22 +00:00
|
|
|
|
|
|
|
var preparedColumns []string
|
2014-02-15 12:29:53 +00:00
|
|
|
for k, column := range columnNames {
|
|
|
|
listed := false
|
|
|
|
for _, dropped := range columnsToDrop {
|
|
|
|
if column == dropped {
|
|
|
|
listed = true
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !listed {
|
|
|
|
preparedColumns = append(preparedColumns, columns[k])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(preparedColumns) == 0 {
|
|
|
|
return nil, fmt.Errorf("No columns match, drops nothing.")
|
|
|
|
}
|
|
|
|
|
2014-02-18 11:32:04 +00:00
|
|
|
// fetch indices for this table
|
2014-02-18 14:22:22 +00:00
|
|
|
oldSQLIndices, err := s.getDDLFromIndex(tableName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-19 00:38:53 +00:00
|
|
|
var oldIdxColumns [][]string
|
2014-02-18 11:32:04 +00:00
|
|
|
for _, idx := range oldSQLIndices {
|
2014-02-19 00:38:53 +00:00
|
|
|
idxCols, err := fetchColumns(idx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
oldIdxColumns = append(oldIdxColumns, idxCols)
|
|
|
|
}
|
|
|
|
|
|
|
|
var indices []string
|
|
|
|
for k, idx := range oldSQLIndices {
|
2014-02-18 11:32:04 +00:00
|
|
|
listed := false
|
2014-02-19 00:38:53 +00:00
|
|
|
OIdxLoop:
|
|
|
|
for _, oidx := range oldIdxColumns[k] {
|
|
|
|
for _, cols := range columnsToDrop {
|
|
|
|
if oidx == cols {
|
|
|
|
listed = true
|
|
|
|
break OIdxLoop
|
|
|
|
}
|
2014-02-18 11:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !listed {
|
|
|
|
indices = append(indices, idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 12:29:53 +00:00
|
|
|
// Rename old table, here's our proxy
|
|
|
|
proxyName := fmt.Sprintf("%s_%s", tableName, uniuri.NewLen(16))
|
|
|
|
if result, err := s.RenameTable(tableName, proxyName); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate table with dropped columns omitted
|
2014-02-18 14:22:22 +00:00
|
|
|
if result, err = s.CreateTable(tableName, preparedColumns); err != nil {
|
2014-02-15 12:29:53 +00:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move data from old table
|
2014-02-18 14:22:22 +00:00
|
|
|
if result, err = s.Tx.Exec(fmt.Sprintf("INSERT INTO %s SELECT %s FROM %s;", tableName,
|
2014-02-15 12:29:53 +00:00
|
|
|
strings.Join(selectName(preparedColumns), ", "), proxyName)); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up proxy table
|
2014-02-18 14:22:22 +00:00
|
|
|
if result, err = s.DropTable(proxyName); err != nil {
|
2014-02-18 11:32:04 +00:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate Indices
|
|
|
|
for _, idx := range indices {
|
|
|
|
if result, err = s.Tx.Exec(idx); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, err
|
2014-02-15 12:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLiteDriver) RenameColumns(tableName string, columnChanges map[string]string) (sql.Result, error) {
|
2014-02-18 14:22:22 +00:00
|
|
|
var err error
|
|
|
|
var result sql.Result
|
|
|
|
|
|
|
|
tableSQL, err := s.getDDLFromTable(tableName)
|
2014-02-15 12:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-18 14:22:22 +00:00
|
|
|
columns, err := fetchColumns(tableSQL)
|
2014-02-15 12:29:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-15 17:56:03 +00:00
|
|
|
// We need a list of columns name to migrate data to the new table
|
|
|
|
var oldColumnsName = selectName(columns)
|
|
|
|
|
|
|
|
// newColumns will be used to create the new table
|
2014-02-15 15:17:22 +00:00
|
|
|
var newColumns []string
|
2014-02-15 17:56:03 +00:00
|
|
|
|
|
|
|
for k, column := range oldColumnsName {
|
|
|
|
added := false
|
2014-02-15 12:29:53 +00:00
|
|
|
for Old, New := range columnChanges {
|
|
|
|
if column == Old {
|
|
|
|
columnToAdd := strings.Replace(columns[k], Old, New, 1)
|
2014-02-15 17:56:03 +00:00
|
|
|
newColumns = append(newColumns, columnToAdd)
|
|
|
|
added = true
|
2014-02-15 12:29:53 +00:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-02-15 17:56:03 +00:00
|
|
|
if !added {
|
|
|
|
newColumns = append(newColumns, columns[k])
|
|
|
|
}
|
2014-02-15 12:29:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-18 11:32:04 +00:00
|
|
|
// fetch indices for this table
|
2014-02-18 14:22:22 +00:00
|
|
|
oldSQLIndices, err := s.getDDLFromIndex(tableName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-19 00:38:53 +00:00
|
|
|
var idxColumns [][]string
|
2014-02-18 11:32:04 +00:00
|
|
|
for _, idx := range oldSQLIndices {
|
2014-02-19 00:38:53 +00:00
|
|
|
idxCols, err := fetchColumns(idx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
idxColumns = append(idxColumns, idxCols)
|
|
|
|
}
|
|
|
|
|
|
|
|
var indices []string
|
|
|
|
for k, idx := range oldSQLIndices {
|
2014-02-18 11:32:04 +00:00
|
|
|
added := false
|
2014-02-19 00:38:53 +00:00
|
|
|
IdcLoop:
|
|
|
|
for _, oldIdx := range idxColumns[k] {
|
|
|
|
for Old, New := range columnChanges {
|
|
|
|
if oldIdx == Old {
|
|
|
|
indx := strings.Replace(idx, Old, New, 2)
|
|
|
|
indices = append(indices, indx)
|
|
|
|
added = true
|
|
|
|
break IdcLoop
|
|
|
|
}
|
2014-02-18 11:32:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !added {
|
|
|
|
indices = append(indices, idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 17:56:03 +00:00
|
|
|
// Rename current table
|
|
|
|
proxyName := fmt.Sprintf("%s_%s", tableName, uniuri.NewLen(16))
|
|
|
|
if result, err := s.RenameTable(tableName, proxyName); err != nil {
|
|
|
|
return result, err
|
2014-02-15 12:29:53 +00:00
|
|
|
}
|
|
|
|
|
2014-02-15 17:56:03 +00:00
|
|
|
// Create new table with the new columns
|
2014-02-18 14:22:22 +00:00
|
|
|
if result, err = s.CreateTable(tableName, newColumns); err != nil {
|
2014-02-15 17:56:03 +00:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate data
|
2014-02-18 14:22:22 +00:00
|
|
|
if result, err = s.Tx.Exec(fmt.Sprintf("INSERT INTO %s SELECT %s FROM %s", tableName,
|
2014-02-15 17:56:03 +00:00
|
|
|
strings.Join(oldColumnsName, ", "), proxyName)); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up proxy table
|
2014-02-18 14:22:22 +00:00
|
|
|
if result, err = s.DropTable(proxyName); err != nil {
|
2014-02-18 11:32:04 +00:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, idx := range indices {
|
|
|
|
if result, err = s.Tx.Exec(idx); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, err
|
2014-02-15 12:29:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *SQLiteDriver) getDDLFromTable(tableName string) (string, error) {
|
|
|
|
var sql string
|
2014-02-15 15:17:22 +00:00
|
|
|
query := `SELECT sql FROM sqlite_master WHERE type='table' and name=?;`
|
2014-02-15 12:29:53 +00:00
|
|
|
err := s.Tx.QueryRow(query, tableName).Scan(&sql)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return sql, nil
|
|
|
|
}
|
2014-02-18 14:22:22 +00:00
|
|
|
|
|
|
|
func (s *SQLiteDriver) getDDLFromIndex(tableName string) ([]string, error) {
|
|
|
|
var sqls []string
|
|
|
|
|
|
|
|
query := `SELECT sql FROM sqlite_master WHERE type='index' and tbl_name=?;`
|
|
|
|
rows, err := s.Tx.Query(query, tableName)
|
|
|
|
if err != nil {
|
|
|
|
return sqls, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
|
|
|
var sql string
|
|
|
|
if err := rows.Scan(&sql); err != nil {
|
2014-02-18 18:11:41 +00:00
|
|
|
// This error came from autoindex, since its sql value is null,
|
|
|
|
// we want to continue.
|
2014-02-18 14:22:22 +00:00
|
|
|
if strings.Contains(err.Error(), "Scan pair: <nil> -> *string") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
return sqls, err
|
|
|
|
}
|
2014-02-18 18:11:41 +00:00
|
|
|
sqls = append(sqls, sql)
|
2014-02-18 14:22:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return sqls, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sqls, nil
|
|
|
|
}
|