Skip to content

Commit

Permalink
fix: [#533] there is a bug when validating nested data (#751)
Browse files Browse the repository at this point in the history
* fix:[#533] there is a bug when validating nested data

* fix mod

* fix sqlserver docker image

* fix sqlservefr
  • Loading branch information
hwbrzzl authored Dec 12, 2024
1 parent d4de868 commit 20c6d6d
Show file tree
Hide file tree
Showing 10 changed files with 142 additions and 84 deletions.
2 changes: 1 addition & 1 deletion database/factory.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"fmt"
"reflect"

"github.com/mitchellh/mapstructure"
"github.com/go-viper/mapstructure/v2"

"github.com/goravel/framework/contracts/database/factory"
ormcontract "github.com/goravel/framework/contracts/database/orm"
Expand Down
2 changes: 1 addition & 1 deletion database/gorm/cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"strings"
"time"

"github.com/mitchellh/mapstructure"
"github.com/go-viper/mapstructure/v2"
"gorm.io/gorm"

"github.com/goravel/framework/support/carbon"
Expand Down
72 changes: 35 additions & 37 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ require (
github.com/glebarez/go-sqlite v1.22.0
github.com/glebarez/sqlite v1.11.0
github.com/go-sql-driver/mysql v1.8.1
github.com/go-viper/mapstructure/v2 v2.2.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/golang-migrate/migrate/v4 v4.17.1
github.com/golang-module/carbon/v2 v2.3.12
Expand Down Expand Up @@ -50,60 +51,40 @@ require (
atomicgo.dev/cursor v0.2.0 // indirect
atomicgo.dev/keyboard v0.2.9 // indirect
atomicgo.dev/schedule v0.1.0 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/catppuccin/go v0.2.0 // indirect
github.com/charmbracelet/bubbles v0.18.0 // indirect
github.com/charmbracelet/bubbletea v0.26.3 // indirect
github.com/charmbracelet/x/ansi v0.1.1 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240524151031-ff83003bf67a // indirect
github.com/charmbracelet/x/input v0.1.1 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.2 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/rabbitmq/amqp091-go v1.9.0 // indirect
github.com/rivo/uniseg v0.4.7 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
golang.org/x/term v0.21.0 // indirect
)

require (
cloud.google.com/go v0.112.1 // indirect
cloud.google.com/go/compute v1.25.1 // indirect
cloud.google.com/go/compute/metadata v0.2.3 // indirect
cloud.google.com/go/iam v1.1.6 // indirect
cloud.google.com/go/pubsub v1.36.1 // indirect
filippo.io/edwards25519 v1.1.0 // indirect
github.com/Azure/go-autorest v14.2.0+incompatible // indirect
github.com/Azure/go-autorest/autorest/adal v0.9.16 // indirect
github.com/Azure/go-autorest/autorest/date v0.3.0 // indirect
github.com/Azure/go-autorest/logger v0.2.1 // indirect
github.com/Azure/go-autorest/tracing v0.6.0 // indirect
github.com/RichardKnop/logging v0.0.0-20190827224416-1a693bdd4fae // indirect
github.com/atotto/clipboard v0.1.4 // indirect
github.com/aws/aws-sdk-go v1.49.6 // indirect
github.com/aymanbagabas/go-osc52/v2 v2.0.1 // indirect
github.com/brianvoe/gofakeit/v6 v6.28.0
github.com/catppuccin/go v0.2.0 // indirect
github.com/cespare/xxhash/v2 v2.2.0 // indirect
github.com/charmbracelet/bubbles v0.18.0 // indirect
github.com/charmbracelet/bubbletea v0.26.3 // indirect
github.com/charmbracelet/x/ansi v0.1.1 // indirect
github.com/charmbracelet/x/exp/strings v0.0.0-20240524151031-ff83003bf67a // indirect
github.com/charmbracelet/x/input v0.1.1 // indirect
github.com/charmbracelet/x/term v0.1.1 // indirect
github.com/charmbracelet/x/windows v0.1.2 // indirect
github.com/containerd/console v1.0.4-0.20230313162750-1ae8d489ac81 // indirect
github.com/cpuguy83/go-md2man/v2 v2.0.4 // indirect
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f // indirect
github.com/dustin/go-humanize v1.0.1 // indirect
github.com/erikgeiser/coninput v0.0.0-20211004153227-1c3628e74d0f // indirect
github.com/felixge/httpsnoop v1.0.4 // indirect
github.com/fsnotify/fsnotify v1.7.0 // indirect
github.com/go-logr/logr v1.4.1 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-redis/redis/v8 v8.11.5 // indirect
github.com/go-redsync/redsync/v4 v4.8.1 // indirect
github.com/go-stack/stack v1.8.0 // indirect
Expand All @@ -116,28 +97,39 @@ require (
github.com/google/s2a-go v0.1.7 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.3.2 // indirect
github.com/googleapis/gax-go/v2 v2.12.3 // indirect
github.com/gookit/color v1.5.4 // indirect
github.com/gookit/filter v1.2.1 // indirect
github.com/gookit/goutil v0.6.15 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/jackc/pgpassfile v1.0.0 // indirect
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
github.com/jackc/pgx/v5 v5.5.4 // indirect
github.com/jackc/puddle/v2 v2.2.1 // indirect
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/kelseyhightower/envconfig v1.4.0 // indirect
github.com/klauspost/compress v1.17.2 // indirect
github.com/klauspost/cpuid/v2 v2.2.5 // indirect
github.com/lib/pq v1.10.9 // indirect
github.com/lithammer/fuzzysearch v1.1.8 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/mattn/go-localereader v0.0.1 // indirect
github.com/mattn/go-runewidth v0.0.15 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
github.com/mitchellh/mapstructure v1.5.0
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/muesli/ansi v0.0.0-20230316100256-276c6243b2f6 // indirect
github.com/muesli/cancelreader v0.2.2 // indirect
github.com/muesli/termenv v0.15.2 // indirect
github.com/opentracing/opentracing-go v1.2.0 // indirect
github.com/pelletier/go-toml/v2 v2.2.2 // indirect
github.com/pmezard/go-difflib v1.0.1-0.20181226105442-5d4384ee4fb2 // indirect
github.com/rabbitmq/amqp091-go v1.9.0 // indirect
github.com/remyoudompheng/bigfft v0.0.0-20230129092748-24d4a6f8daec // indirect
github.com/rivo/uniseg v0.4.7 // indirect
github.com/russross/blackfriday/v2 v2.1.0 // indirect
github.com/sagikazarmark/locafero v0.4.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
Expand All @@ -154,11 +146,17 @@ require (
github.com/youmark/pkcs8 v0.0.0-20181117223130-1be2e3e5546d // indirect
go.mongodb.org/mongo-driver v1.7.5 // indirect
go.opencensus.io v0.24.0 // indirect
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.49.0 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.49.0 // indirect
go.opentelemetry.io/otel v1.24.0 // indirect
go.opentelemetry.io/otel/metric v1.24.0 // indirect
go.opentelemetry.io/otel/trace v1.24.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/oauth2 v0.18.0 // indirect
golang.org/x/sync v0.7.0 // indirect
golang.org/x/sys v0.21.0 // indirect
golang.org/x/term v0.21.0 // indirect
golang.org/x/text v0.16.0
golang.org/x/time v0.5.0 // indirect
google.golang.org/api v0.171.0 // indirect
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,8 @@ github.com/go-sql-driver/mysql v1.8.1/go.mod h1:wEBSXgmK//2ZFJyE+qWnIsVGmvmEKlqw
github.com/go-stack/stack v1.8.0 h1:5SgMzNM5HxrEjV0ww2lTmX6E2Izsfxas4+YHWRs3Lsk=
github.com/go-stack/stack v1.8.0/go.mod h1:v0f6uXyyMGvRgIKkXu+yp6POWl0qKG85gN/melR3HDY=
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0/go.mod h1:fyg7847qk6SyHyPtNmDHnmrv/HOrqktSC+C9fM+CJOE=
github.com/go-viper/mapstructure/v2 v2.2.1 h1:ZAaOCxANMuZx5RCeg0mBdEZk7DZasvvZIxtHqx8aGss=
github.com/go-viper/mapstructure/v2 v2.2.1/go.mod h1:oJDH3BJKyqBA2TXFhDsKDGDTlndYOZ6rGS0BRZIxGhM=
github.com/gobuffalo/attrs v0.0.0-20190224210810-a9411de4debd/go.mod h1:4duuawTqi2wkkpB4ePgWMaai6/Kc6WEz83bhFwpHzj0=
github.com/gobuffalo/depgen v0.0.0-20190329151759-d478694a28d3/go.mod h1:3STtPUQYuzV0gBVOY3vy6CfMm/ljR4pABfrTeHNLHUY=
github.com/gobuffalo/depgen v0.1.0/go.mod h1:+ifsuy7fhi15RWncXQQKjWS9JPkdah5sZvtHc2RXGlg=
Expand Down
2 changes: 1 addition & 1 deletion support/docker/database.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package docker

const (
password = "Goravel123"
password = "Framework!123"
username = "goravel"
database = "goravel"
)
Expand Down
92 changes: 65 additions & 27 deletions support/docker/sqlserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,19 @@ func NewSqlserver(database, username, password string) *Sqlserver {
username: username,
password: password,
image: &testing.Image{
Repository: "mcmoe/mssqldocker",
Repository: "mcr.microsoft.com/mssql/server",
Tag: "latest",
Env: []string{
"ACCEPT_EULA=Y",
"MSSQL_DB=" + database,
"MSSQL_USER=" + username,
"MSSQL_PASSWORD=" + password,
"SA_PASSWORD=" + password,
"MSSQL_SA_PASSWORD=" + password,
},
ExposedPorts: []string{"1433"},
},
}
}

func (receiver *Sqlserver) Build() error {
command, exposedPorts := imageToCommand(receiver.image)
func (r *Sqlserver) Build() error {
command, exposedPorts := imageToCommand(r.image)
containerID, err := run(command)
if err != nil {
return fmt.Errorf("init Sqlserver docker error: %v", err)
Expand All @@ -52,28 +49,28 @@ func (receiver *Sqlserver) Build() error {
return fmt.Errorf("no container id return when creating Sqlserver docker")
}

receiver.containerID = containerID
receiver.port = getExposedPort(exposedPorts, 1433)
r.containerID = containerID
r.port = getExposedPort(exposedPorts, 1433)

if _, err := receiver.connect(); err != nil {
if _, err := r.connect(); err != nil {
return fmt.Errorf("connect Sqlserver docker error: %v", err)
}

return nil
}

func (receiver *Sqlserver) Config() testing.DatabaseConfig {
func (r *Sqlserver) Config() testing.DatabaseConfig {
return testing.DatabaseConfig{
Host: receiver.host,
Port: receiver.port,
Database: receiver.database,
Username: receiver.username,
Password: receiver.password,
Host: r.host,
Port: r.port,
Database: r.database,
Username: r.username,
Password: r.password,
}
}

func (receiver *Sqlserver) Fresh() error {
instance, err := receiver.connect()
func (r *Sqlserver) Fresh() error {
instance, err := r.connect()
if err != nil {
return fmt.Errorf("connect Sqlserver error when clearing: %v", err)
}
Expand All @@ -99,36 +96,77 @@ func (receiver *Sqlserver) Fresh() error {
return nil
}

func (receiver *Sqlserver) Image(image testing.Image) {
receiver.image = &image
func (r *Sqlserver) Image(image testing.Image) {
r.image = &image
}

func (receiver *Sqlserver) Name() orm.Driver {
func (r *Sqlserver) Name() orm.Driver {
return orm.DriverSqlserver
}

func (receiver *Sqlserver) Stop() error {
if _, err := run(fmt.Sprintf("docker stop %s", receiver.containerID)); err != nil {
func (r *Sqlserver) Stop() error {
if _, err := run(fmt.Sprintf("docker stop %s", r.containerID)); err != nil {
return fmt.Errorf("stop Sqlserver error: %v", err)
}

return nil
}

func (receiver *Sqlserver) connect() (*gormio.DB, error) {
func (r *Sqlserver) connect() (*gormio.DB, error) {
var (
instance *gormio.DB
err error
)

// docker compose need time to start
for i := 0; i < 60; i++ {
for i := 0; i < 100; i++ {
instance, err = gormio.Open(sqlserver.New(sqlserver.Config{
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s",
receiver.username, receiver.password, receiver.host, receiver.port, receiver.database),
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=master",
"sa", r.password, r.host, r.port),
}))

if err == nil {
// Check if database exists
var exists bool
query := fmt.Sprintf("SELECT CASE WHEN EXISTS (SELECT * FROM sys.databases WHERE name = '%s') THEN CAST(1 AS BIT) ELSE CAST(0 AS BIT) END", r.database)
if err := instance.Raw(query).Scan(&exists).Error; err != nil {
return nil, err
}

if !exists {
// Create User database
if err := instance.Exec(fmt.Sprintf(`CREATE DATABASE "%s";`, r.database)).Error; err != nil {
return nil, err
}

query = fmt.Sprintf("SELECT 1 FROM sys.server_principals WHERE name = '%s' AND type = 'S'", r.username)
if err := instance.Raw(query).Scan(&exists).Error; err != nil {
return nil, err
}

if !exists {
// Create User account
if err := instance.Exec(fmt.Sprintf("CREATE LOGIN %s WITH PASSWORD = '%s'", r.username, r.password)).Error; err != nil {
return nil, err
}
}

// Create DB account for User
if err := instance.Exec(fmt.Sprintf("USE %s; CREATE USER %s FOR LOGIN %s", r.database, r.username, r.username)).Error; err != nil {
return nil, err
}

// Add permission
if err := instance.Exec(fmt.Sprintf("USE %s; ALTER ROLE db_owner ADD MEMBER %s", r.database, r.username)).Error; err != nil {
return nil, err
}
}

instance, err = gormio.Open(sqlserver.New(sqlserver.Config{
DSN: fmt.Sprintf("sqlserver://%s:%s@%s:%d?database=%s",
r.username, r.password, r.host, r.port, r.database),
}))

break
}

Expand Down
4 changes: 2 additions & 2 deletions support/docker/sqlserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func TestSqlserverTestSuite(t *testing.T) {

func (s *SqlserverTestSuite) SetupTest() {
s.mockConfig = &configmocks.Config{}
s.sqlserver = NewSqlserver("goravel", "goravel", "Goravel123")
s.sqlserver = NewSqlserver("goravel", "goravel", "Framework!123")
}

func (s *SqlserverTestSuite) TestBuild() {
Expand All @@ -39,7 +39,7 @@ func (s *SqlserverTestSuite) TestBuild() {
s.Equal("127.0.0.1", s.sqlserver.Config().Host)
s.Equal("goravel", s.sqlserver.Config().Database)
s.Equal("goravel", s.sqlserver.Config().Username)
s.Equal("Goravel123", s.sqlserver.Config().Password)
s.Equal("Framework!123", s.sqlserver.Config().Password)
s.True(s.sqlserver.Config().Port > 0)

res := instance.Exec(`
Expand Down
2 changes: 1 addition & 1 deletion testing/docker/database_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ func TestNewDatabase(t *testing.T) {
mockGormInitialize *gormmocks.Initialize
database = "goravel"
username = "goravel"
password = "goravel"
password = "Framework!123"
)

beforeEach := func() {
Expand Down
Loading

0 comments on commit 20c6d6d

Please sign in to comment.