From e497f4d2f0e4593a04ce56d0418f15a18e3d63a5 Mon Sep 17 00:00:00 2001 From: Maximilian Date: Sat, 20 Jan 2024 16:32:07 -0600 Subject: [PATCH] Ignore fields that are zero value --- database/migrate.go | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/database/migrate.go b/database/migrate.go index d45f7ce..69f9ac6 100644 --- a/database/migrate.go +++ b/database/migrate.go @@ -9,7 +9,8 @@ import ( "reflect" ) -// Migrate given a dummy object of any type, it will create a table with the same name as the type and create columns with the same name as the fields of the object +// Migrate given a dummy object of any type, it will create a table with the same name +// as the type and create columns with the same name as the fields of the object func Migrate(app *app.App, anyStruct interface{}) error { valueOfStruct := reflect.ValueOf(anyStruct) typeOfStruct := valueOfStruct.Type() @@ -23,10 +24,15 @@ func Migrate(app *app.App, anyStruct interface{}) error { for i := 0; i < valueOfStruct.NumField(); i++ { fieldType := typeOfStruct.Field(i) fieldName := fieldType.Name - if fieldName != "Id" && fieldName != "id" { - err := createColumn(app, tableName, fieldName, fieldType.Type.Name()) - if err != nil { - return err + + // Create column if dummy for migration is NOT zero value + fieldValue := valueOfStruct.Field(i).Interface() + if !reflect.ValueOf(fieldValue).IsZero() { + if fieldName != "Id" && fieldName != "id" { + err := createColumn(app, tableName, fieldName, fieldType.Type.Name()) + if err != nil { + return err + } } } }