Skip to content

Commit

Permalink
Merge branch 'master' into bowen/sqlserver-schema
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Dec 25, 2024
2 parents 7202c41 + c409ce8 commit 8b4943d
Show file tree
Hide file tree
Showing 8 changed files with 34 additions and 8 deletions.
3 changes: 2 additions & 1 deletion auth/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func (database *ServiceProvider) Register(app foundation.Application) {

ormFacade := app.MakeOrm()
if ormFacade == nil {
return nil, errors.OrmFacadeNotSet.SetModule(errors.ModuleAuth)
// The Orm module will print the error message, so it's safe to return nil.
return nil, nil
}

ctx, ok := parameters["ctx"].(http.Context)
Expand Down
2 changes: 1 addition & 1 deletion database/gorm/dialector.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ func getDialectors(configs []database.FullConfig) ([]gorm.Dialector, error) {
var dialector gorm.Dialector
dsn := db.Dsn(config)
if dsn == "" {
return nil, errors.OrmFailedToGenerateDNS.Args(config.Connection)
return nil, errors.OrmFailedToGenerateDNS
}

switch config.Driver {
Expand Down
2 changes: 1 addition & 1 deletion database/gorm/dialector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func TestGetDialectors(t *testing.T) {
Connection: "postgres",
},
},
expectError: errors.OrmFailedToGenerateDNS.Args("postgres"),
expectError: errors.OrmFailedToGenerateDNS,
},
{
name: "Happy path - mysql",
Expand Down
2 changes: 1 addition & 1 deletion database/migration/sql_migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ func getMigrator(configBuilder *databasedb.ConfigBuilder, table string) (*migrat
writeConfig := writeConfigs[0]
dsn := databasedb.Dsn(writeConfigs[0])
if dsn == "" {
return nil, errors.OrmFailedToGenerateDNS.Args(writeConfig.Connection)
return nil, errors.OrmFailedToGenerateDNS
}

var (
Expand Down
12 changes: 10 additions & 2 deletions database/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
databaseschema "github.com/goravel/framework/database/schema"
databaseseeder "github.com/goravel/framework/database/seeder"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/color"
)

type ServiceProvider struct {
Expand All @@ -32,9 +33,15 @@ func (r *ServiceProvider) Register(app foundation.Application) {
}

connection := config.GetString("database.default")
if connection == "" {
return nil, nil
}

orm, err := databaseorm.BuildOrm(ctx, config, connection, log, app.Refresh)
if err != nil {
return nil, errors.OrmInitConnection.Args(connection, err).SetModule(errors.ModuleOrm)
color.Warningln(errors.OrmInitConnection.Args(connection, err).SetModule(errors.ModuleOrm))

return nil, nil
}

return orm, nil
Expand All @@ -52,7 +59,8 @@ func (r *ServiceProvider) Register(app foundation.Application) {

orm := app.MakeOrm()
if orm == nil {
return nil, errors.OrmFacadeNotSet.SetModule(errors.ModuleSchema)
// The Orm module will print the error message, so it's safe to return an empty schema.
return &databaseschema.Schema{}, nil
}

return databaseschema.NewSchema(config, log, orm, nil), nil
Expand Down
2 changes: 1 addition & 1 deletion errors/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ var (

OrmDatabaseConfigNotFound = New("not found database configuration")
OrmDriverNotSupported = New("invalid driver: %s, only support mysql, postgres, sqlite and sqlserver")
OrmFailedToGenerateDNS = New("failed to generate DSN for connection: %s")
OrmFailedToGenerateDNS = New("failed to generate DSN, please check the database configuration")
OrmFactoryMissingAttributes = New("failed to get raw attributes")
OrmFactoryMissingMethod = New("%s does not find factory method")
OrmInitConnection = New("init %s connection error: %v")
Expand Down
9 changes: 9 additions & 0 deletions foundation/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ func (c *Container) MakeAuth(ctx contractshttp.Context) contractsauth.Auth {
color.Errorln(err)
return nil
}
if instance == nil {
return nil
}

return instance.(contractsauth.Auth)
}
Expand Down Expand Up @@ -214,6 +217,9 @@ func (c *Container) MakeOrm() contractsorm.Orm {
color.Errorln(err)
return nil
}
if instance == nil {
return nil
}

return instance.(contractsorm.Orm)
}
Expand Down Expand Up @@ -264,6 +270,9 @@ func (c *Container) MakeSchema() contractsmigration.Schema {
color.Errorln(err)
return nil
}
if instance == nil {
return nil
}

return instance.(contractsmigration.Schema)
}
Expand Down
10 changes: 9 additions & 1 deletion support/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package color
import (
"bytes"
"io"
"os"

"github.com/pterm/pterm"

Expand Down Expand Up @@ -38,7 +39,14 @@ const (
)

var (
info = pterm.Info
info = pterm.PrefixPrinter{
MessageStyle: &pterm.ThemeDefault.DefaultText,
Prefix: pterm.Prefix{
Style: &pterm.Style{pterm.FgBlack, pterm.BgLightWhite},
Text: " INFO ",
},
Writer: os.Stdout,
}
warning = pterm.Warning
err = pterm.Error
debug = pterm.Debug
Expand Down

0 comments on commit 8b4943d

Please sign in to comment.