Keep indices when dropping/renaming columns.
This commit is contained in:
parent
2867818679
commit
7a75c2d004
1 changed files with 59 additions and 2 deletions
|
@ -79,6 +79,22 @@ func (s *SQLiteDriver) DropColumns(tableName string, columnsToDrop []string) (sq
|
||||||
return nil, fmt.Errorf("No columns match, drops nothing.")
|
return nil, fmt.Errorf("No columns match, drops nothing.")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fetch indices for this table
|
||||||
|
var indices []string
|
||||||
|
oldSQLIndices := s.getDdlFromIndex(tableName)
|
||||||
|
for _, idx := range oldSQLIndices {
|
||||||
|
listed := false
|
||||||
|
for _, cols := range columnsToDrop {
|
||||||
|
if strings.Contains(idx, cols) {
|
||||||
|
listed = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !listed {
|
||||||
|
indices = append(indices, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Rename old table, here's our proxy
|
// Rename old table, here's our proxy
|
||||||
proxyName := fmt.Sprintf("%s_%s", tableName, uniuri.NewLen(16))
|
proxyName := fmt.Sprintf("%s_%s", tableName, uniuri.NewLen(16))
|
||||||
if result, err := s.RenameTable(tableName, proxyName); err != nil {
|
if result, err := s.RenameTable(tableName, proxyName); err != nil {
|
||||||
|
@ -97,7 +113,19 @@ func (s *SQLiteDriver) DropColumns(tableName string, columnsToDrop []string) (sq
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up proxy table
|
// Clean up proxy table
|
||||||
return s.DropTable(proxyName)
|
if result, err := s.DropTable(proxyName); err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Recreate Indices
|
||||||
|
var err error
|
||||||
|
var result sql.Result
|
||||||
|
for _, idx := range indices {
|
||||||
|
if result, err = s.Tx.Exec(idx); err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteDriver) RenameColumns(tableName string, columnChanges map[string]string) (sql.Result, error) {
|
func (s *SQLiteDriver) RenameColumns(tableName string, columnChanges map[string]string) (sql.Result, error) {
|
||||||
|
@ -132,6 +160,24 @@ func (s *SQLiteDriver) RenameColumns(tableName string, columnChanges map[string]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// fetch indices for this table
|
||||||
|
oldSQLIndices := s.getDDLFromIndex(tableName)
|
||||||
|
var indices []string
|
||||||
|
for _, idx := range oldSQLIndices {
|
||||||
|
added := false
|
||||||
|
for Old, New := range columnChanges {
|
||||||
|
if strings.Contains(idx, Old) {
|
||||||
|
indx := strings.Replace(idx, Old, New, 2)
|
||||||
|
indices = append(indices, indx)
|
||||||
|
added = true
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if !added {
|
||||||
|
indices = append(indices, idx)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Rename current table
|
// Rename current table
|
||||||
proxyName := fmt.Sprintf("%s_%s", tableName, uniuri.NewLen(16))
|
proxyName := fmt.Sprintf("%s_%s", tableName, uniuri.NewLen(16))
|
||||||
if result, err := s.RenameTable(tableName, proxyName); err != nil {
|
if result, err := s.RenameTable(tableName, proxyName); err != nil {
|
||||||
|
@ -150,7 +196,18 @@ func (s *SQLiteDriver) RenameColumns(tableName string, columnChanges map[string]
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up proxy table
|
// Clean up proxy table
|
||||||
return s.DropTable(proxyName)
|
if result, err := s.DropTable(proxyName); err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
|
||||||
|
var err error
|
||||||
|
var result sql.Result
|
||||||
|
for _, idx := range indices {
|
||||||
|
if result, err = s.Tx.Exec(idx); err != nil {
|
||||||
|
return result, err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return result, err
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SQLiteDriver) getDDLFromTable(tableName string) (string, error) {
|
func (s *SQLiteDriver) getDDLFromTable(tableName string) (string, error) {
|
||||||
|
|
Loading…
Reference in a new issue