mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-02-16 19:35:14 +00:00
Fix helper functions for MySQL syntax (#3874)
This commit is contained in:
parent
388f14185c
commit
40b496f13b
2 changed files with 40 additions and 21 deletions
|
@ -15,32 +15,13 @@
|
|||
package migration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"src.techknowlogick.com/xormigrate"
|
||||
"xorm.io/xorm"
|
||||
)
|
||||
|
||||
type registryV032 struct {
|
||||
ID int64 `json:"id" xorm:"pk autoincr 'id'"`
|
||||
RepoID int64 `json:"-" xorm:"UNIQUE(s) INDEX 'repo_id'"`
|
||||
Address string `json:"address" xorm:"UNIQUE(s) INDEX 'address'"`
|
||||
Username string `json:"username" xorm:"varchar(2000) 'username'"`
|
||||
Password string `json:"password" xorm:"TEXT 'password'"`
|
||||
}
|
||||
|
||||
func (r registryV032) TableName() string {
|
||||
return "registries"
|
||||
}
|
||||
|
||||
var alterTableRegistriesFixRequiredFields = xormigrate.Migration{
|
||||
ID: "alter-table-registries-fix-required-fields",
|
||||
MigrateSession: func(sess *xorm.Session) error {
|
||||
// make sure old registry exists
|
||||
if err := sess.Sync(new(registryV032)); err != nil {
|
||||
return fmt.Errorf("sync models failed: %w", err)
|
||||
}
|
||||
|
||||
if err := alterColumnDefault(sess, "registries", "repo_id", "0"); err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -185,7 +185,26 @@ func dropTableColumns(sess *xorm.Session, tableName string, columnNames ...strin
|
|||
func alterColumnDefault(sess *xorm.Session, table, column, defValue string) error {
|
||||
dialect := sess.Engine().Dialect().URI().DBType
|
||||
switch dialect {
|
||||
case schemas.MYSQL, schemas.POSTGRES:
|
||||
case schemas.MYSQL:
|
||||
sql := fmt.Sprintf("SHOW COLUMNS FROM `%s` WHERE lower(field) = '%s'", table, strings.ToLower(column))
|
||||
res, err := sess.Query(sql)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(res) == 0 || len(res[0]["Type"]) == 0 {
|
||||
return fmt.Errorf("column %s data type in table %s can not be detected", column, table)
|
||||
}
|
||||
|
||||
dataType := string(res[0]["Type"])
|
||||
var nullable string
|
||||
if string(res[0]["Null"]) == "NO" {
|
||||
nullable = "NOT NULL"
|
||||
}
|
||||
|
||||
_, err = sess.Exec(fmt.Sprintf("ALTER TABLE `%s` MODIFY `%s` %s %s DEFAULT %s;", table, column, dataType, nullable, defValue))
|
||||
return err
|
||||
case schemas.POSTGRES:
|
||||
_, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` SET DEFAULT %s;", table, column, defValue))
|
||||
return err
|
||||
case schemas.SQLITE:
|
||||
|
@ -204,7 +223,26 @@ func alterColumnNull(sess *xorm.Session, table, column string, null bool) error
|
|||
dialect := sess.Engine().Dialect().URI().DBType
|
||||
switch dialect {
|
||||
case schemas.MYSQL:
|
||||
_, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` COLUMN `%s` SET %s;", table, column, val))
|
||||
sql := fmt.Sprintf("SHOW COLUMNS FROM `%s` WHERE lower(field) = '%s'", table, strings.ToLower(column))
|
||||
res, err := sess.Query(sql)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if len(res) == 0 || len(res[0]["Type"]) == 0 {
|
||||
return fmt.Errorf("column %s data type in table %s can not be detected", column, table)
|
||||
}
|
||||
|
||||
dataType := string(res[0]["Type"])
|
||||
defValue := string(res[0]["Default"])
|
||||
|
||||
if defValue != "NULL" && defValue != "" {
|
||||
defValue = fmt.Sprintf("DEFAULT '%s'", defValue)
|
||||
} else {
|
||||
defValue = ""
|
||||
}
|
||||
|
||||
_, err = sess.Exec(fmt.Sprintf("ALTER TABLE `%s` MODIFY `%s` %s %s %s;", table, column, dataType, val, defValue))
|
||||
return err
|
||||
case schemas.POSTGRES:
|
||||
_, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` SET %s;", table, column, val))
|
||||
|
|
Loading…
Reference in a new issue