Skip to content

Commit

Permalink
Merge branch 'master' into feature/add-take-last-methods-to-query
Browse files Browse the repository at this point in the history
  • Loading branch information
saqijaan authored Feb 10, 2025
2 parents 3b7390c + da0832e commit e483ae3
Show file tree
Hide file tree
Showing 42 changed files with 550 additions and 267 deletions.
18 changes: 7 additions & 11 deletions auth/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,18 @@ import (

"github.com/goravel/framework/auth/access"
"github.com/goravel/framework/auth/console"
"github.com/goravel/framework/contracts"
contractconsole "github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/contracts/http"
"github.com/goravel/framework/errors"
)

const (
BindingAuth = "goravel.auth"
BindingGate = "goravel.gate"
)

type ServiceProvider struct {
}

func (database *ServiceProvider) Register(app foundation.Application) {
app.BindWith(BindingAuth, func(app foundation.Application, parameters map[string]any) (any, error) {
func (r *ServiceProvider) Register(app foundation.Application) {
app.BindWith(contracts.BindingAuth, func(app foundation.Application, parameters map[string]any) (any, error) {
config := app.MakeConfig()
if config == nil {
return nil, errors.ConfigFacadeNotSet.SetModule(errors.ModuleAuth)
Expand All @@ -44,16 +40,16 @@ func (database *ServiceProvider) Register(app foundation.Application) {
return NewAuth(config.GetString("auth.defaults.guard"),
cacheFacade, config, ctx, ormFacade), nil
})
app.Singleton(BindingGate, func(app foundation.Application) (any, error) {
app.Singleton(contracts.BindingGate, func(app foundation.Application) (any, error) {
return access.NewGate(context.Background()), nil
})
}

func (database *ServiceProvider) Boot(app foundation.Application) {
database.registerCommands(app)
func (r *ServiceProvider) Boot(app foundation.Application) {
r.registerCommands(app)
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
func (r *ServiceProvider) registerCommands(app foundation.Application) {
app.Commands([]contractconsole.Command{
console.NewJwtSecretCommand(app.MakeConfig()),
console.NewPolicyMakeCommand(),
Expand Down
13 changes: 6 additions & 7 deletions cache/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@ package cache

import (
"github.com/goravel/framework/cache/console"
"github.com/goravel/framework/contracts"
contractsconsole "github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/errors"
)

const Binding = "goravel.cache"

type ServiceProvider struct {
}

func (database *ServiceProvider) Register(app foundation.Application) {
app.Singleton(Binding, func(app foundation.Application) (any, error) {
func (r *ServiceProvider) Register(app foundation.Application) {
app.Singleton(contracts.BindingCache, func(app foundation.Application) (any, error) {
config := app.MakeConfig()
if config == nil {
return nil, errors.ConfigFacadeNotSet.SetModule(errors.ModuleCache)
Expand All @@ -30,11 +29,11 @@ func (database *ServiceProvider) Register(app foundation.Application) {
})
}

func (database *ServiceProvider) Boot(app foundation.Application) {
database.registerCommands(app)
func (r *ServiceProvider) Boot(app foundation.Application) {
r.registerCommands(app)
}

func (database *ServiceProvider) registerCommands(app foundation.Application) {
func (r *ServiceProvider) registerCommands(app foundation.Application) {
app.Commands([]contractsconsole.Command{
console.NewClearCommand(app.MakeCache()),
})
Expand Down
9 changes: 4 additions & 5 deletions config/service_provider.go
Original file line number Diff line number Diff line change
@@ -1,21 +1,20 @@
package config

import (
"github.com/goravel/framework/contracts"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/support"
)

const Binding = "goravel.config"

type ServiceProvider struct {
}

func (config *ServiceProvider) Register(app foundation.Application) {
app.Singleton(Binding, func(app foundation.Application) (any, error) {
func (r *ServiceProvider) Register(app foundation.Application) {
app.Singleton(contracts.BindingConfig, func(app foundation.Application) (any, error) {
return NewApplication(support.EnvPath), nil
})
}

func (config *ServiceProvider) Boot(app foundation.Application) {
func (r *ServiceProvider) Boot(app foundation.Application) {

}
13 changes: 6 additions & 7 deletions console/service_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,29 @@ package console

import (
"github.com/goravel/framework/console/console"
"github.com/goravel/framework/contracts"
consolecontract "github.com/goravel/framework/contracts/console"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/support/color"
)

const Binding = "goravel.console"

type ServiceProvider struct {
}

func (receiver *ServiceProvider) Register(app foundation.Application) {
app.Singleton(Binding, func(app foundation.Application) (any, error) {
func (r *ServiceProvider) Register(app foundation.Application) {
app.Singleton(contracts.BindingConsole, func(app foundation.Application) (any, error) {
name := "artisan"
usage := "Goravel Framework"
usageText := "artisan [global options] command [options] [arguments...]"
return NewApplication(name, usage, usageText, app.Version(), true), nil
})
}

func (receiver *ServiceProvider) Boot(app foundation.Application) {
receiver.registerCommands(app)
func (r *ServiceProvider) Boot(app foundation.Application) {
r.registerCommands(app)
}

func (receiver *ServiceProvider) registerCommands(app foundation.Application) {
func (r *ServiceProvider) registerCommands(app foundation.Application) {
artisanFacade := app.MakeArtisan()
if artisanFacade == nil {
color.Warningln("Artisan Facade is not initialized. Skipping command registration.")
Expand Down
28 changes: 28 additions & 0 deletions contracts/binding.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package contracts

const (
BindingAuth = "goravel.auth"
BindingCache = "goravel.cache"
BindingConfig = "goravel.config"
BindingConsole = "goravel.console"
BindingCrypt = "goravel.crypt"
BindingEvent = "goravel.event"
BindingFilesystem = "goravel.filesystem"
BindingGate = "goravel.gate"
BindingGrpc = "goravel.grpc"
BindingHash = "goravel.hash"
BindingLog = "goravel.log"
BindingMail = "goravel.mail"
BindingOrm = "goravel.orm"
BindingQueue = "goravel.queue"
BindingRateLimiter = "goravel.rate_limiter"
BindingRoute = "goravel.route"
BindingSchedule = "goravel.schedule"
BindingSchema = "goravel.schema"
BindingSeeder = "goravel.seeder"
BindingSession = "goravel.session"
BindingTesting = "goravel.testing"
BindingTranslation = "goravel.translation"
BindingValidation = "goravel.validation"
BindingView = "goravel.view"
)
6 changes: 5 additions & 1 deletion contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ type Blueprint interface {
Char(column string, length ...int) ColumnDefinition
// Column Create a new custom type column on the table.
Column(column string, ttype string) ColumnDefinition
// Comment Add a comment to the table.
Comment(value string)
// Create Indicate that the table needs to be created.
Create()
// Date Create a new date column on the table.
Expand Down Expand Up @@ -103,6 +105,8 @@ type Blueprint interface {
Primary(column ...string)
// Rename the table to a given name.
Rename(to string)
// RenameColumn Indicate that the given columns should be renamed.
RenameColumn(from, to string)
// RenameIndex Indicate that the given indexes should be renamed.
RenameIndex(from, to string)
// SetTable Set the table that the blueprint operates on.
Expand Down Expand Up @@ -138,7 +142,7 @@ type Blueprint interface {
// TinyText Create a new tiny text column on the table.
TinyText(column string) ColumnDefinition
// ToSql Get the raw SQL statements for the blueprint.
ToSql(grammar Grammar) []string
ToSql(grammar Grammar) ([]string, error)
// Unique Specify a unique index for the table.
Unique(column ...string) IndexDefinition
// UnsignedBigInteger Create a new unsigned big integer (8-byte) column on the table.
Expand Down
1 change: 1 addition & 0 deletions contracts/database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ type Column struct {
Collation string
Comment string
Default string
Extra string
Name string
Nullable bool
Type string
Expand Down
4 changes: 4 additions & 0 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,14 @@ type Grammar interface {
CompilePrimary(blueprint Blueprint, command *Command) string
// CompileRename Compile a rename table command.
CompileRename(blueprint Blueprint, command *Command) string
// CompileRenameColumn Compile a rename column command.
CompileRenameColumn(schema Schema, blueprint Blueprint, command *Command) (string, error)
// CompileRenameIndex Compile a rename index command.
CompileRenameIndex(schema Schema, blueprint Blueprint, command *Command) []string
// CompileTables Compile the query to determine the tables.
CompileTables(database string) string
// CompileTableComment Compile a table comment command.
CompileTableComment(blueprint Blueprint, command *Command) string
// CompileTypes Compile the query to determine the types.
CompileTypes() string
// CompileUnique Compile a unique key command.
Expand Down
9 changes: 4 additions & 5 deletions crypt/service_provider.go
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package crypt

import (
"github.com/goravel/framework/contracts"
"github.com/goravel/framework/contracts/foundation"
"github.com/goravel/framework/errors"
)

const Binding = "goravel.crypt"

type ServiceProvider struct {
}

func (crypt *ServiceProvider) Register(app foundation.Application) {
app.Singleton(Binding, func(app foundation.Application) (any, error) {
func (r *ServiceProvider) Register(app foundation.Application) {
app.Singleton(contracts.BindingCrypt, func(app foundation.Application) (any, error) {
config := app.MakeConfig()
if config == nil {
return nil, errors.ConfigFacadeNotSet.SetModule(errors.ModuleCrypt)
Expand All @@ -26,6 +25,6 @@ func (crypt *ServiceProvider) Register(app foundation.Application) {
})
}

func (crypt *ServiceProvider) Boot(app foundation.Application) {
func (r *ServiceProvider) Boot(app foundation.Application) {

}
5 changes: 2 additions & 3 deletions database/orm/orm.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"sync"

"github.com/goravel/framework/contracts"
"github.com/goravel/framework/contracts/config"
"github.com/goravel/framework/contracts/database"
contractsorm "github.com/goravel/framework/contracts/database/orm"
Expand All @@ -14,8 +15,6 @@ import (
"github.com/goravel/framework/database/gorm"
)

const BindingOrm = "goravel.orm"

type Orm struct {
ctx context.Context
config config.Config
Expand Down Expand Up @@ -135,7 +134,7 @@ func (r *Orm) SetQuery(query contractsorm.Query) {
}

func (r *Orm) Refresh() {
r.refresh(BindingOrm)
r.refresh(contracts.BindingOrm)
}

func (r *Orm) Transaction(txFunc func(tx contractsorm.Query) error) error {
Expand Down
42 changes: 38 additions & 4 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ const (
CommandIndex = "index"
CommandPrimary = "primary"
CommandRename = "rename"
CommandRenameColumn = "renameColumn"
CommandRenameIndex = "renameIndex"
CommandTableComment = "tableComment"
CommandUnique = "unique"
DefaultStringLength = 255
)
Expand Down Expand Up @@ -61,8 +63,13 @@ func (r *Blueprint) Boolean(column string) schema.ColumnDefinition {
}

func (r *Blueprint) Build(query orm.Query, grammar schema.Grammar) error {
for _, sql := range r.ToSql(grammar) {
if _, err := query.Exec(sql); err != nil {
statements, err := r.ToSql(grammar)
if err != nil {
return err
}

for _, sql := range statements {
if _, err = query.Exec(sql); err != nil {
return err
}
}
Expand All @@ -86,6 +93,13 @@ func (r *Blueprint) Column(column, ttype string) schema.ColumnDefinition {
return r.createAndAddColumn(ttype, column)
}

func (r *Blueprint) Comment(comment string) {
r.addCommand(&schema.Command{
Name: CommandTableComment,
Value: comment,
})
}

func (r *Blueprint) Create() {
r.addCommand(&schema.Command{
Name: CommandCreate,
Expand Down Expand Up @@ -335,6 +349,16 @@ func (r *Blueprint) Rename(to string) {
r.addCommand(command)
}

func (r *Blueprint) RenameColumn(from, to string) {
command := &schema.Command{
Name: CommandRenameColumn,
From: from,
To: to,
}

r.addCommand(command)
}

func (r *Blueprint) RenameIndex(from, to string) {
command := &schema.Command{
Name: CommandRenameIndex,
Expand Down Expand Up @@ -449,7 +473,7 @@ func (r *Blueprint) TinyText(column string) schema.ColumnDefinition {
return r.createAndAddColumn("tinyText", column)
}

func (r *Blueprint) ToSql(grammar schema.Grammar) []string {
func (r *Blueprint) ToSql(grammar schema.Grammar) ([]string, error) {
r.addImpliedCommands(grammar)

var statements []string
Expand Down Expand Up @@ -503,14 +527,24 @@ func (r *Blueprint) ToSql(grammar schema.Grammar) []string {
statements = append(statements, grammar.CompilePrimary(r, command))
case CommandRename:
statements = append(statements, grammar.CompileRename(r, command))
case CommandRenameColumn:
statement, err := grammar.CompileRenameColumn(r.schema, r, command)
if err != nil {
return statements, err
}
statements = append(statements, statement)
case CommandRenameIndex:
statements = append(statements, grammar.CompileRenameIndex(r.schema, r, command)...)
case CommandTableComment:
if statement := grammar.CompileTableComment(r, command); statement != "" {
statements = append(statements, statement)
}
case CommandUnique:
statements = append(statements, grammar.CompileUnique(r, command))
}
}

return statements
return statements, nil
}

func (r *Blueprint) Unique(column ...string) schema.IndexDefinition {
Expand Down
3 changes: 2 additions & 1 deletion database/schema/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -661,7 +661,8 @@ func (s *BlueprintTestSuite) TestToSql() {
test.setup()

// Execute ToSql
statements := s.blueprint.ToSql(mockGrammar)
statements, err := s.blueprint.ToSql(mockGrammar)
s.NoError(err)

// Verify results
s.Equal(test.expectedSQL, statements)
Expand Down
7 changes: 7 additions & 0 deletions database/schema/column.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ type ColumnDefinition struct {
useCurrentOnUpdate *bool
}

func NewColumnDefinition(name string, ttype string) schema.ColumnDefinition {
return &ColumnDefinition{
name: &name,
ttype: convert.Pointer(ttype),
}
}

func (r *ColumnDefinition) AutoIncrement() schema.ColumnDefinition {
r.autoIncrement = convert.Pointer(true)

Expand Down
Loading

0 comments on commit e483ae3

Please sign in to comment.