Skip to content

Commit

Permalink
Merge pull request #39 from go-co-op/35-bug-mysql-doesnt-treat-null-a…
Browse files Browse the repository at this point in the history
…s-a-valid-default-for-not-null-fields

Fixing bug with mysql and default null value for not null columns
  • Loading branch information
manuelarte authored Nov 18, 2024
2 parents 2454605 + 6b73430 commit 0e03825
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 3 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ require (
github.com/go-co-op/gocron v1.37.0
github.com/stretchr/testify v1.9.0
github.com/testcontainers/testcontainers-go v0.34.0
github.com/testcontainers/testcontainers-go/modules/mysql v0.34.0
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0
gorm.io/driver/mysql v1.5.7
gorm.io/driver/postgres v1.5.9
gorm.io/gorm v1.25.10
)
Expand All @@ -29,6 +31,7 @@ require (
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/go-sql-driver/mysql v1.7.1 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
Expand Down
8 changes: 8 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
Expand Down Expand Up @@ -128,6 +131,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
github.com/testcontainers/testcontainers-go/modules/mysql v0.34.0 h1:Tqz17mGXjPORHFS/oBUGdeJyIsZXLsVVHRhaBqhewGI=
github.com/testcontainers/testcontainers-go/modules/mysql v0.34.0/go.mod h1:hDpm3DLfjo7rd6232wWflEBDGr6Ow9ys43mJTiJwWx8=
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 h1:c51aBXT3v2HEBVarmaBnsKzvgZjC5amn0qsj8Naqi50=
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0/go.mod h1:EWP75ogLQU4M4L8U+20mFipjV4WIR9WtlMXSB6/wiuc=
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
Expand Down Expand Up @@ -208,8 +213,11 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=
20 changes: 17 additions & 3 deletions job_lock_struct.go → model.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
package gormlock

import "time"
import (
"errors"
"gorm.io/gorm"
"time"
)

type JobLock[T any] interface {
GetID() T
Expand All @@ -15,8 +19,8 @@ type CronJobLock struct {
UpdatedAt time.Time
JobName string `gorm:"index:idx_name,unique"`
JobIdentifier string `gorm:"index:idx_name,unique"`
Worker string `gorm:"not null;default:null"`
Status string `gorm:"not nullldefault:null"`
Worker string `gorm:"not null"`
Status string `gorm:"not null"`
}

func (cjb *CronJobLock) SetJobIdentifier(ji string) {
Expand All @@ -26,3 +30,13 @@ func (cjb *CronJobLock) SetJobIdentifier(ji string) {
func (cjb *CronJobLock) GetID() int {
return cjb.ID
}

func (cjb *CronJobLock) BeforeCreate(_ *gorm.DB) error {
if cjb.Worker == "" {
return errors.New("worker is required")
}
if cjb.Status == "" {
return errors.New("status is required")
}
return nil
}
54 changes: 54 additions & 0 deletions model_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package gormlock

import (
"context"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/mysql"
gormMysql "gorm.io/driver/mysql"
"gorm.io/gorm"
"testing"
)

func TestAutoMigration_MySQL(t *testing.T) {
t.Parallel()
tests := map[string]struct {
img string
args []string
}{
"8.0.36": {
img: "mysql:8.0.36",
args: []string{"charset=utf8mb4", "parseTime=True", "loc=Local"},
},
}

for name, tc := range tests {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
ctx := context.Background()
mysqlContainer, err := mysql.Run(ctx,
tc.img,
mysql.WithDatabase("test"),
mysql.WithUsername("root"),
mysql.WithPassword("password"),
)
require.NoError(t, err, "failed to start container")
defer func() {
if err := testcontainers.TerminateContainer(mysqlContainer); err != nil {
t.Fatalf("failed to terminate container: %s", err)
}
}()

connStr, errConn := mysqlContainer.ConnectionString(ctx, tc.args...)
require.NoError(t, errConn, "failed to get mysql connection string")

db, errOpeningConn := gorm.Open(gormMysql.Open(connStr), &gorm.Config{})
require.NoError(t, errOpeningConn)

err = db.AutoMigrate(&CronJobLock{})
assert.NoError(t, err)
})
}
}

0 comments on commit 0e03825

Please sign in to comment.