Skip to content

Commit 0e03825

Browse files
authored
Merge pull request #39 from go-co-op/35-bug-mysql-doesnt-treat-null-as-a-valid-default-for-not-null-fields
Fixing bug with mysql and default null value for not null columns
2 parents 2454605 + 6b73430 commit 0e03825

File tree

4 files changed

+82
-3
lines changed

4 files changed

+82
-3
lines changed

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@ require (
66
github.com/go-co-op/gocron v1.37.0
77
github.com/stretchr/testify v1.9.0
88
github.com/testcontainers/testcontainers-go v0.34.0
9+
github.com/testcontainers/testcontainers-go/modules/mysql v0.34.0
910
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0
11+
gorm.io/driver/mysql v1.5.7
1012
gorm.io/driver/postgres v1.5.9
1113
gorm.io/gorm v1.25.10
1214
)
@@ -29,6 +31,7 @@ require (
2931
github.com/go-logr/logr v1.4.1 // indirect
3032
github.com/go-logr/stdr v1.2.2 // indirect
3133
github.com/go-ole/go-ole v1.2.6 // indirect
34+
github.com/go-sql-driver/mysql v1.7.1 // indirect
3235
github.com/gogo/protobuf v1.3.2 // indirect
3336
github.com/google/uuid v1.6.0 // indirect
3437
github.com/jackc/pgpassfile v1.0.0 // indirect

go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@ github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag=
3939
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
4040
github.com/go-ole/go-ole v1.2.6 h1:/Fpf6oFPoeFik9ty7siob0G6Ke8QvQEuVcuChpwXzpY=
4141
github.com/go-ole/go-ole v1.2.6/go.mod h1:pprOEPIfldk/42T2oK7lQ4v4JSDwmV0As9GaiUsvbm0=
42+
github.com/go-sql-driver/mysql v1.7.0/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
43+
github.com/go-sql-driver/mysql v1.7.1 h1:lUIinVbN1DY0xBg0eMOzmmtGoHwWBbvnWubQUrtU8EI=
44+
github.com/go-sql-driver/mysql v1.7.1/go.mod h1:OXbVy3sEdcQ2Doequ6Z5BW6fXNQTmx+9S1MCJN5yJMI=
4245
github.com/gogo/protobuf v1.3.2 h1:Ov1cvc58UF3b5XjBnZv7+opcTcQFZebYjWzi34vdm4Q=
4346
github.com/gogo/protobuf v1.3.2/go.mod h1:P1XiOD3dCwIKUDQYPy72D8LYyHL2YPYrpS2s69NZV8Q=
4447
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
@@ -128,6 +131,8 @@ github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsT
128131
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
129132
github.com/testcontainers/testcontainers-go v0.34.0 h1:5fbgF0vIN5u+nD3IWabQwRybuB4GY8G2HHgCkbMzMHo=
130133
github.com/testcontainers/testcontainers-go v0.34.0/go.mod h1:6P/kMkQe8yqPHfPWNulFGdFHTD8HB2vLq/231xY2iPQ=
134+
github.com/testcontainers/testcontainers-go/modules/mysql v0.34.0 h1:Tqz17mGXjPORHFS/oBUGdeJyIsZXLsVVHRhaBqhewGI=
135+
github.com/testcontainers/testcontainers-go/modules/mysql v0.34.0/go.mod h1:hDpm3DLfjo7rd6232wWflEBDGr6Ow9ys43mJTiJwWx8=
131136
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0 h1:c51aBXT3v2HEBVarmaBnsKzvgZjC5amn0qsj8Naqi50=
132137
github.com/testcontainers/testcontainers-go/modules/postgres v0.34.0/go.mod h1:EWP75ogLQU4M4L8U+20mFipjV4WIR9WtlMXSB6/wiuc=
133138
github.com/tklauser/go-sysconf v0.3.12 h1:0QaGUFOdQaIVdPgfITYzaTegZvdCjmYO52cSFAEVmqU=
@@ -208,8 +213,11 @@ gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
208213
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
209214
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
210215
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
216+
gorm.io/driver/mysql v1.5.7 h1:MndhOPYOfEp2rHKgkZIhJ16eVUIRf2HmzgoPmh7FCWo=
217+
gorm.io/driver/mysql v1.5.7/go.mod h1:sEtPWMiqiN1N1cMXoXmBbd8C6/l+TESwriotuRRpkDM=
211218
gorm.io/driver/postgres v1.5.9 h1:DkegyItji119OlcaLjqN11kHoUgZ/j13E0jkJZgD6A8=
212219
gorm.io/driver/postgres v1.5.9/go.mod h1:DX3GReXH+3FPWGrrgffdvCk3DQ1dwDPdmbenSkweRGI=
220+
gorm.io/gorm v1.25.7/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
213221
gorm.io/gorm v1.25.10 h1:dQpO+33KalOA+aFYGlK+EfxcI5MbO7EP2yYygwh9h+s=
214222
gorm.io/gorm v1.25.10/go.mod h1:hbnx/Oo0ChWMn1BIhpy1oYozzpM15i4YPuHDmfYtwg8=
215223
gotest.tools/v3 v3.5.1 h1:EENdUnS3pdur5nybKYIh2Vfgc8IUNBjxDPSjtiJcOzU=

job_lock_struct.go renamed to model.go

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
package gormlock
22

3-
import "time"
3+
import (
4+
"errors"
5+
"gorm.io/gorm"
6+
"time"
7+
)
48

59
type JobLock[T any] interface {
610
GetID() T
@@ -15,8 +19,8 @@ type CronJobLock struct {
1519
UpdatedAt time.Time
1620
JobName string `gorm:"index:idx_name,unique"`
1721
JobIdentifier string `gorm:"index:idx_name,unique"`
18-
Worker string `gorm:"not null;default:null"`
19-
Status string `gorm:"not nullldefault:null"`
22+
Worker string `gorm:"not null"`
23+
Status string `gorm:"not null"`
2024
}
2125

2226
func (cjb *CronJobLock) SetJobIdentifier(ji string) {
@@ -26,3 +30,13 @@ func (cjb *CronJobLock) SetJobIdentifier(ji string) {
2630
func (cjb *CronJobLock) GetID() int {
2731
return cjb.ID
2832
}
33+
34+
func (cjb *CronJobLock) BeforeCreate(_ *gorm.DB) error {
35+
if cjb.Worker == "" {
36+
return errors.New("worker is required")
37+
}
38+
if cjb.Status == "" {
39+
return errors.New("status is required")
40+
}
41+
return nil
42+
}

model_test.go

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package gormlock
2+
3+
import (
4+
"context"
5+
"github.com/stretchr/testify/assert"
6+
"github.com/stretchr/testify/require"
7+
"github.com/testcontainers/testcontainers-go"
8+
"github.com/testcontainers/testcontainers-go/modules/mysql"
9+
gormMysql "gorm.io/driver/mysql"
10+
"gorm.io/gorm"
11+
"testing"
12+
)
13+
14+
func TestAutoMigration_MySQL(t *testing.T) {
15+
t.Parallel()
16+
tests := map[string]struct {
17+
img string
18+
args []string
19+
}{
20+
"8.0.36": {
21+
img: "mysql:8.0.36",
22+
args: []string{"charset=utf8mb4", "parseTime=True", "loc=Local"},
23+
},
24+
}
25+
26+
for name, tc := range tests {
27+
name, tc := name, tc
28+
t.Run(name, func(t *testing.T) {
29+
t.Parallel()
30+
ctx := context.Background()
31+
mysqlContainer, err := mysql.Run(ctx,
32+
tc.img,
33+
mysql.WithDatabase("test"),
34+
mysql.WithUsername("root"),
35+
mysql.WithPassword("password"),
36+
)
37+
require.NoError(t, err, "failed to start container")
38+
defer func() {
39+
if err := testcontainers.TerminateContainer(mysqlContainer); err != nil {
40+
t.Fatalf("failed to terminate container: %s", err)
41+
}
42+
}()
43+
44+
connStr, errConn := mysqlContainer.ConnectionString(ctx, tc.args...)
45+
require.NoError(t, errConn, "failed to get mysql connection string")
46+
47+
db, errOpeningConn := gorm.Open(gormMysql.Open(connStr), &gorm.Config{})
48+
require.NoError(t, errOpeningConn)
49+
50+
err = db.AutoMigrate(&CronJobLock{})
51+
assert.NoError(t, err)
52+
})
53+
}
54+
}

0 commit comments

Comments
 (0)