mirror of
https://github.com/woodpecker-ci/woodpecker.git
synced 2025-07-17 17:01:46 +00:00
Fix helper functions for MySQL syntax (#3874)
This commit is contained in:
parent
388f14185c
commit
40b496f13b
@ -15,32 +15,13 @@
|
|||||||
package migration
|
package migration
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
|
||||||
|
|
||||||
"src.techknowlogick.com/xormigrate"
|
"src.techknowlogick.com/xormigrate"
|
||||||
"xorm.io/xorm"
|
"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{
|
var alterTableRegistriesFixRequiredFields = xormigrate.Migration{
|
||||||
ID: "alter-table-registries-fix-required-fields",
|
ID: "alter-table-registries-fix-required-fields",
|
||||||
MigrateSession: func(sess *xorm.Session) error {
|
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 {
|
if err := alterColumnDefault(sess, "registries", "repo_id", "0"); err != nil {
|
||||||
return err
|
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 {
|
func alterColumnDefault(sess *xorm.Session, table, column, defValue string) error {
|
||||||
dialect := sess.Engine().Dialect().URI().DBType
|
dialect := sess.Engine().Dialect().URI().DBType
|
||||||
switch dialect {
|
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))
|
_, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` SET DEFAULT %s;", table, column, defValue))
|
||||||
return err
|
return err
|
||||||
case schemas.SQLITE:
|
case schemas.SQLITE:
|
||||||
@ -204,7 +223,26 @@ func alterColumnNull(sess *xorm.Session, table, column string, null bool) error
|
|||||||
dialect := sess.Engine().Dialect().URI().DBType
|
dialect := sess.Engine().Dialect().URI().DBType
|
||||||
switch dialect {
|
switch dialect {
|
||||||
case schemas.MYSQL:
|
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
|
return err
|
||||||
case schemas.POSTGRES:
|
case schemas.POSTGRES:
|
||||||
_, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` SET %s;", table, column, val))
|
_, err := sess.Exec(fmt.Sprintf("ALTER TABLE `%s` ALTER COLUMN `%s` SET %s;", table, column, val))
|
||||||
|
Loading…
Reference in New Issue
Block a user