fix migration (setup keys)

This commit is contained in:
İsmail
2024-11-12 17:23:29 +03:00
parent ea06c874d0
commit 2d9bc2719e
+20 -7
View File
@@ -17,12 +17,21 @@ import (
"gorm.io/gorm" "gorm.io/gorm"
) )
func GetColumnName(db *gorm.DB, column string) string {
if db.Name() == "mysql" {
return fmt.Sprintf("`%s`", column)
}
return column
}
// MigrateFieldFromGobToJSON migrates a column from Gob encoding to JSON encoding. // MigrateFieldFromGobToJSON migrates a column from Gob encoding to JSON encoding.
// T is the type of the model that contains the field to be migrated. // T is the type of the model that contains the field to be migrated.
// S is the type of the field to be migrated. // S is the type of the field to be migrated.
func MigrateFieldFromGobToJSON[T any, S any](ctx context.Context, db *gorm.DB, fieldName string) error { func MigrateFieldFromGobToJSON[T any, S any](ctx context.Context, db *gorm.DB, fieldName string) error {
orgColumnName := fieldName
oldColumnName := fieldName oldColumnName := GetColumnName(db, orgColumnName)
newColumnName := fieldName + "_tmp" newColumnName := fieldName + "_tmp"
var model T var model T
@@ -72,7 +81,7 @@ func MigrateFieldFromGobToJSON[T any, S any](ctx context.Context, db *gorm.DB, f
for _, row := range rows { for _, row := range rows {
var field S var field S
str, ok := row[oldColumnName].(string) str, ok := row[orgColumnName].(string)
if !ok { if !ok {
return fmt.Errorf("type assertion failed") return fmt.Errorf("type assertion failed")
} }
@@ -111,7 +120,8 @@ func MigrateFieldFromGobToJSON[T any, S any](ctx context.Context, db *gorm.DB, f
// MigrateNetIPFieldFromBlobToJSON migrates a Net IP column from Blob encoding to JSON encoding. // MigrateNetIPFieldFromBlobToJSON migrates a Net IP column from Blob encoding to JSON encoding.
// T is the type of the model that contains the field to be migrated. // T is the type of the model that contains the field to be migrated.
func MigrateNetIPFieldFromBlobToJSON[T any](ctx context.Context, db *gorm.DB, fieldName string, indexName string) error { func MigrateNetIPFieldFromBlobToJSON[T any](ctx context.Context, db *gorm.DB, fieldName string, indexName string) error {
oldColumnName := fieldName orgColumnName := fieldName
oldColumnName := GetColumnName(db, orgColumnName)
newColumnName := fieldName + "_tmp" newColumnName := fieldName + "_tmp"
var model T var model T
@@ -163,7 +173,7 @@ func MigrateNetIPFieldFromBlobToJSON[T any](ctx context.Context, db *gorm.DB, fi
for _, row := range rows { for _, row := range rows {
var blobValue string var blobValue string
if columnValue := row[oldColumnName]; columnValue != nil { if columnValue := row[orgColumnName]; columnValue != nil {
value, ok := columnValue.(string) value, ok := columnValue.(string)
if !ok { if !ok {
return fmt.Errorf("type assertion failed") return fmt.Errorf("type assertion failed")
@@ -210,7 +220,9 @@ func MigrateNetIPFieldFromBlobToJSON[T any](ctx context.Context, db *gorm.DB, fi
} }
func MigrateSetupKeyToHashedSetupKey[T any](ctx context.Context, db *gorm.DB) error { func MigrateSetupKeyToHashedSetupKey[T any](ctx context.Context, db *gorm.DB) error {
oldColumnName := "key"
orgColumnName := "key"
oldColumnName := GetColumnName(db, orgColumnName)
newColumnName := "key_secret" newColumnName := "key_secret"
var model T var model T
@@ -250,8 +262,9 @@ func MigrateSetupKeyToHashedSetupKey[T any](ctx context.Context, db *gorm.DB) er
} }
for _, row := range rows { for _, row := range rows {
var plainKey string var plainKey string
if columnValue := row[oldColumnName]; columnValue != nil { if columnValue := row[orgColumnName]; columnValue != nil {
value, ok := columnValue.(string) value, ok := columnValue.(string)
if !ok { if !ok {
return fmt.Errorf("type assertion failed") return fmt.Errorf("type assertion failed")