Skip to content

Commit

Permalink
Check if column exist before rename if exist, just return with no err…
Browse files Browse the repository at this point in the history
…or (go-gitea#17870) (go-gitea#17882)

* Check if column exist before rename if exist, just return with no error

* Also check if errors column exist

* Add comment for migration

* Fix sqlite test
  • Loading branch information
lunny authored Dec 2, 2021
1 parent 9a8532d commit 56a3b50
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions models/migrations/v184.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
package migrations

import (
"context"
"fmt"

"code.gitea.io/gitea/modules/setting"
Expand All @@ -19,6 +20,22 @@ func renameTaskErrorsToMessage(x *xorm.Engine) error {
Status int `xorm:"index"`
}

// This migration maybe rerun so that we should check if it has been run
messageExist, err := x.Dialect().IsColumnExist(x.DB(), context.Background(), "task", "message")
if err != nil {
return err
}

if messageExist {
errorsExist, err := x.Dialect().IsColumnExist(x.DB(), context.Background(), "task", "errors")
if err != nil {
return err
}
if !errorsExist {
return nil
}
}

sess := x.NewSession()
defer sess.Close()
if err := sess.Begin(); err != nil {
Expand All @@ -29,6 +46,13 @@ func renameTaskErrorsToMessage(x *xorm.Engine) error {
return fmt.Errorf("error on Sync2: %v", err)
}

if messageExist {
// if both errors and message exist, drop message at first
if err := dropTableColumns(sess, "task", "message"); err != nil {
return err
}
}

switch {
case setting.Database.UseMySQL:
if _, err := sess.Exec("ALTER TABLE `task` CHANGE errors message text"); err != nil {
Expand Down

0 comments on commit 56a3b50

Please sign in to comment.