2014-02-15 15:29:53 +03:00
|
|
|
package migrate
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-03-08 08:19:28 +03:00
|
|
|
type sqliteDriver struct {
|
|
|
|
Tx *sql.Tx
|
2014-02-18 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:19:28 +03:00
|
|
|
func SQLite(tx *sql.Tx) *MigrationDriver {
|
|
|
|
return &MigrationDriver{
|
|
|
|
Tx: tx,
|
|
|
|
Operation: &sqliteDriver{Tx: tx},
|
2014-03-13 14:53:52 +03:00
|
|
|
T: &columnType{},
|
2014-03-08 08:19:28 +03:00
|
|
|
}
|
2014-02-18 09:08:06 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:19:28 +03:00
|
|
|
func (s *sqliteDriver) CreateTable(tableName string, args []string) (sql.Result, error) {
|
2014-03-27 00:26:51 +03:00
|
|
|
return s.Tx.Exec(fmt.Sprintf("CREATE TABLE IF NOT EXISTS %s (%s)", tableName, strings.Join(args, ", ")))
|
2014-02-15 15:29:53 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:19:28 +03:00
|
|
|
func (s *sqliteDriver) RenameTable(tableName, newName string) (sql.Result, error) {
|
2014-03-13 22:14:16 +03:00
|
|
|
return s.Tx.Exec(fmt.Sprintf("ALTER TABLE %s RENAME TO %s", tableName, newName))
|
2014-02-15 15:29:53 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:19:28 +03:00
|
|
|
func (s *sqliteDriver) DropTable(tableName string) (sql.Result, error) {
|
2014-03-13 22:14:16 +03:00
|
|
|
return s.Tx.Exec(fmt.Sprintf("DROP TABLE IF EXISTS %s", tableName))
|
2014-02-15 15:29:53 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:19:28 +03:00
|
|
|
func (s *sqliteDriver) AddColumn(tableName, columnSpec string) (sql.Result, error) {
|
2014-03-13 22:14:16 +03:00
|
|
|
return s.Tx.Exec(fmt.Sprintf("ALTER TABLE %s ADD COLUMN %s", tableName, columnSpec))
|
2014-02-15 15:29:53 +03:00
|
|
|
}
|
|
|
|
|
2014-03-10 03:11:15 +03:00
|
|
|
func (s *sqliteDriver) ChangeColumn(tableName, columnName, newType string) (sql.Result, error) {
|
|
|
|
var result sql.Result
|
|
|
|
var err error
|
|
|
|
|
|
|
|
tableSQL, err := s.getTableDefinition(tableName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
columns, err := fetchColumns(tableSQL)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-03-12 03:04:34 +03:00
|
|
|
columnNames := selectName(columns)
|
|
|
|
|
|
|
|
for k, column := range columnNames {
|
|
|
|
if columnName == column {
|
|
|
|
columns[k] = fmt.Sprintf("%s %s", columnName, newType)
|
2014-03-10 03:11:15 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
indices, err := s.getIndexDefinition(tableName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
proxy := proxyName(tableName)
|
|
|
|
if result, err = s.RenameTable(tableName, proxy); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if result, err = s.CreateTable(tableName, columns); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate data
|
|
|
|
if result, err = s.Tx.Exec(fmt.Sprintf("INSERT INTO %s SELECT %s FROM %s", tableName,
|
2014-03-12 03:04:34 +03:00
|
|
|
strings.Join(columnNames, ", "), proxy)); err != nil {
|
2014-03-10 03:11:15 +03:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up proxy table
|
|
|
|
if result, err = s.DropTable(proxy); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, idx := range indices {
|
|
|
|
if result, err = s.Tx.Exec(idx); err != nil {
|
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return result, err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2014-03-16 06:52:28 +03:00
|
|
|
func (s *sqliteDriver) DropColumns(tableName string, columnsToDrop ...string) (sql.Result, error) {
|
2014-02-18 17:22:22 +03:00
|
|
|
var err error
|
|
|
|
var result sql.Result
|
2014-02-15 15:29:53 +03:00
|
|
|
|
|
|
|
if len(columnsToDrop) == 0 {
|
|
|
|
return nil, fmt.Errorf("No columns to drop.")
|
|
|
|
}
|
|
|
|
|
2014-03-10 03:11:15 +03:00
|
|
|
tableSQL, err := s.getTableDefinition(tableName)
|
2014-02-15 15:29:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-18 17:22:22 +03:00
|
|
|
columns, err := fetchColumns(tableSQL)
|
2014-02-15 15:29:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
columnNames := selectName(columns)
|
2014-02-15 18:17:22 +03:00
|
|
|
|
|
|
|
var preparedColumns []string
|
2014-02-15 15:29:53 +03: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 14:32:04 +03:00
|
|
|
// fetch indices for this table
|
2014-03-10 03:11:15 +03:00
|
|
|
oldSQLIndices, err := s.getIndexDefinition(tableName)
|
2014-02-18 17:22:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-19 03:38:53 +03:00
|
|
|
var oldIdxColumns [][]string
|
2014-02-18 14:32:04 +03:00
|
|
|
for _, idx := range oldSQLIndices {
|
2014-02-19 03:38:53 +03: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 14:32:04 +03:00
|
|
|
listed := false
|
2014-02-19 03:38:53 +03:00
|
|
|
OIdxLoop:
|
|
|
|
for _, oidx := range oldIdxColumns[k] {
|
|
|
|
for _, cols := range columnsToDrop {
|
|
|
|
if oidx == cols {
|
|
|
|
listed = true
|
|
|
|
break OIdxLoop
|
|
|
|
}
|
2014-02-18 14:32:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !listed {
|
|
|
|
indices = append(indices, idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 15:29:53 +03:00
|
|
|
// Rename old table, here's our proxy
|
2014-03-10 03:11:15 +03:00
|
|
|
proxy := proxyName(tableName)
|
|
|
|
if result, err := s.RenameTable(tableName, proxy); err != nil {
|
2014-02-15 15:29:53 +03:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Recreate table with dropped columns omitted
|
2014-02-18 17:22:22 +03:00
|
|
|
if result, err = s.CreateTable(tableName, preparedColumns); err != nil {
|
2014-02-15 15:29:53 +03:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Move data from old table
|
2014-03-13 22:14:16 +03:00
|
|
|
if result, err = s.Tx.Exec(fmt.Sprintf("INSERT INTO %s SELECT %s FROM %s", tableName,
|
2014-03-10 03:11:15 +03:00
|
|
|
strings.Join(selectName(preparedColumns), ", "), proxy)); err != nil {
|
2014-02-15 15:29:53 +03:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up proxy table
|
2014-03-10 03:11:15 +03:00
|
|
|
if result, err = s.DropTable(proxy); err != nil {
|
2014-02-18 14:32:04 +03: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 15:29:53 +03:00
|
|
|
}
|
|
|
|
|
2014-03-08 08:19:28 +03:00
|
|
|
func (s *sqliteDriver) RenameColumns(tableName string, columnChanges map[string]string) (sql.Result, error) {
|
2014-02-18 17:22:22 +03:00
|
|
|
var err error
|
|
|
|
var result sql.Result
|
|
|
|
|
2014-03-10 03:11:15 +03:00
|
|
|
tableSQL, err := s.getTableDefinition(tableName)
|
2014-02-15 15:29:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-18 17:22:22 +03:00
|
|
|
columns, err := fetchColumns(tableSQL)
|
2014-02-15 15:29:53 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-15 20:56:03 +03: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 18:17:22 +03:00
|
|
|
var newColumns []string
|
2014-02-15 20:56:03 +03:00
|
|
|
|
|
|
|
for k, column := range oldColumnsName {
|
|
|
|
added := false
|
2014-02-15 15:29:53 +03:00
|
|
|
for Old, New := range columnChanges {
|
|
|
|
if column == Old {
|
|
|
|
columnToAdd := strings.Replace(columns[k], Old, New, 1)
|
2014-02-15 20:56:03 +03:00
|
|
|
newColumns = append(newColumns, columnToAdd)
|
|
|
|
added = true
|
2014-02-15 15:29:53 +03:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
2014-02-15 20:56:03 +03:00
|
|
|
if !added {
|
|
|
|
newColumns = append(newColumns, columns[k])
|
|
|
|
}
|
2014-02-15 15:29:53 +03:00
|
|
|
}
|
|
|
|
|
2014-02-18 14:32:04 +03:00
|
|
|
// fetch indices for this table
|
2014-03-10 03:11:15 +03:00
|
|
|
oldSQLIndices, err := s.getIndexDefinition(tableName)
|
2014-02-18 17:22:22 +03:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2014-02-19 03:38:53 +03:00
|
|
|
var idxColumns [][]string
|
2014-02-18 14:32:04 +03:00
|
|
|
for _, idx := range oldSQLIndices {
|
2014-02-19 03:38:53 +03: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 14:32:04 +03:00
|
|
|
added := false
|
2014-02-19 03:38:53 +03: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 14:32:04 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if !added {
|
|
|
|
indices = append(indices, idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-15 20:56:03 +03:00
|
|
|
// Rename current table
|
2014-03-10 03:11:15 +03:00
|
|
|
proxy := proxyName(tableName)
|
|
|
|
if result, err := s.RenameTable(tableName, proxy); err != nil {
|
2014-02-15 20:56:03 +03:00
|
|
|
return result, err
|
2014-02-15 15:29:53 +03:00
|
|
|
}
|
|
|
|
|
2014-02-15 20:56:03 +03:00
|
|
|
// Create new table with the new columns
|
2014-02-18 17:22:22 +03:00
|
|
|
if result, err = s.CreateTable(tableName, newColumns); err != nil {
|
2014-02-15 20:56:03 +03:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Migrate data
|
2014-02-18 17:22:22 +03:00
|
|
|
if result, err = s.Tx.Exec(fmt.Sprintf("INSERT INTO %s SELECT %s FROM %s", tableName,
|
2014-03-10 03:11:15 +03:00
|
|
|
strings.Join(oldColumnsName, ", "), proxy)); err != nil {
|
2014-02-15 20:56:03 +03:00
|
|
|
return result, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clean up proxy table
|
2014-03-10 03:11:15 +03:00
|
|
|
if result, err = s.DropTable(proxy); err != nil {
|
2014-02-18 14:32:04 +03: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 15:29:53 +03:00
|
|
|
}
|
|
|
|
|
2014-03-13 22:14:16 +03:00
|
|
|
func (s *sqliteDriver) AddIndex(tableName string, columns []string, flags ...string) (sql.Result, error) {
|
|
|
|
flag := ""
|
|
|
|
if len(flags) > 0 {
|
|
|
|
if strings.ToUpper(flags[0]) == "UNIQUE" {
|
|
|
|
flag = flags[0]
|
|
|
|
}
|
2014-03-10 03:11:15 +03:00
|
|
|
}
|
|
|
|
return s.Tx.Exec(fmt.Sprintf("CREATE %s INDEX %s ON %s (%s)", flag, indexName(tableName, columns),
|
|
|
|
tableName, strings.Join(columns, ", ")))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sqliteDriver) DropIndex(tableName string, columns []string) (sql.Result, error) {
|
|
|
|
return s.Tx.Exec(fmt.Sprintf("DROP INDEX %s", indexName(tableName, columns)))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *sqliteDriver) getTableDefinition(tableName string) (string, error) {
|
2014-02-15 15:29:53 +03:00
|
|
|
var sql string
|
2014-03-13 22:14:16 +03:00
|
|
|
query := `SELECT sql FROM sqlite_master WHERE type='table' and name=?`
|
2014-02-15 15:29:53 +03:00
|
|
|
err := s.Tx.QueryRow(query, tableName).Scan(&sql)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
return sql, nil
|
|
|
|
}
|
2014-02-18 17:22:22 +03:00
|
|
|
|
2014-03-10 03:11:15 +03:00
|
|
|
func (s *sqliteDriver) getIndexDefinition(tableName string) ([]string, error) {
|
2014-02-18 17:22:22 +03:00
|
|
|
var sqls []string
|
|
|
|
|
2014-03-13 22:14:16 +03:00
|
|
|
query := `SELECT sql FROM sqlite_master WHERE type='index' and tbl_name=?`
|
2014-02-18 17:22:22 +03:00
|
|
|
rows, err := s.Tx.Query(query, tableName)
|
|
|
|
if err != nil {
|
|
|
|
return sqls, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for rows.Next() {
|
2014-03-16 07:28:55 +03:00
|
|
|
var sql sql.NullString
|
2014-02-18 17:22:22 +03:00
|
|
|
if err := rows.Scan(&sql); err != nil {
|
|
|
|
return sqls, err
|
|
|
|
}
|
2014-03-16 07:28:55 +03:00
|
|
|
if sql.Valid {
|
|
|
|
sqls = append(sqls, sql.String)
|
|
|
|
}
|
2014-02-18 17:22:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := rows.Err(); err != nil {
|
|
|
|
return sqls, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sqls, nil
|
|
|
|
}
|