Semaphore/db/sql/Migration_2_8_28.go
2022-02-01 02:36:28 +05:00

54 lines
882 B
Go

package sql
import (
"github.com/go-gorp/gorp/v3"
"strings"
)
type Migration_2_8_26 struct {
DB *SqlDb
}
func (m Migration_2_8_26) Apply(tx *gorp.Transaction) error {
rows, err := tx.Query(m.DB.PrepareQuery("SELECT id, git_url FROM project__repository"))
if err != nil {
return err
}
repoUrls := make(map[string]string)
for rows.Next() {
var id, url string
err3 := rows.Scan(&id, &url)
if err3 != nil {
continue
}
repoUrls[id] = url
}
err = rows.Close()
if err != nil {
return err
}
for id, url := range repoUrls {
branch := "master"
parts := strings.Split(url, "#")
if len(parts) > 1 {
url, branch = parts[0], parts[1]
}
q := m.DB.PrepareQuery("UPDATE project__repository " +
"SET git_url = ?, git_branch = ? " +
"WHERE id = ?")
_, err = tx.Exec(q, url, branch, id)
if err != nil {
return err
}
}
return nil
}