From 40b496f13b1fe25f687d55be59009e76854d5f12 Mon Sep 17 00:00:00 2001 From: Lauris BH Date: Fri, 12 Jul 2024 19:18:55 +0300 Subject: [PATCH] Fix helper functions for MySQL syntax (#3874) --- .../migration/032_registries_add_user.go | 19 --------- server/store/datastore/migration/common.go | 42 ++++++++++++++++++- 2 files changed, 40 insertions(+), 21 deletions(-) diff --git a/server/store/datastore/migration/032_registries_add_user.go b/server/store/datastore/migration/032_registries_add_user.go index 4329d6861..df8d29eb6 100644 --- a/server/store/datastore/migration/032_registries_add_user.go +++ b/server/store/datastore/migration/032_registries_add_user.go @@ -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 } diff --git a/server/store/datastore/migration/common.go b/server/store/datastore/migration/common.go index f562caa7b..d705f99be 100644 --- a/server/store/datastore/migration/common.go +++ b/server/store/datastore/migration/common.go @@ -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))