Skip to content

Commit

Permalink
feat: [#358] Remove driver.Schema (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Mar 9, 2025
1 parent 9a7bcbe commit 0d45fdd
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 270 deletions.
14 changes: 2 additions & 12 deletions contracts/database/driver/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package driver

import (
sq "github.com/Masterminds/squirrel"
"github.com/goravel/framework/contracts/database/orm"
"gorm.io/gorm/clause"
)

Expand Down Expand Up @@ -62,9 +61,9 @@ type SchemaGrammar interface {
// 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)
CompileRenameColumn(blueprint Blueprint, command *Command, columns []Column) (string, error)
// CompileRenameIndex Compile a rename index command.
CompileRenameIndex(schema Schema, blueprint Blueprint, command *Command) []string
CompileRenameIndex(blueprint Blueprint, command *Command, indexes []Index) []string
// CompileTables Compile the query to determine the tables.
CompileTables(database string) string
// CompileTableComment Compile a table comment command.
Expand Down Expand Up @@ -159,15 +158,6 @@ type CompileLimitGrammar interface {
CompileLimit(builder sq.SelectBuilder, conditions *Conditions) sq.SelectBuilder
}

type Schema interface {
// GetColumns Get the columns for a given table.
GetColumns(table string) ([]Column, error)
// GetIndexes Get the indexes for a given table.
GetIndexes(table string) ([]Index, error)
// Orm Get the orm instance.
Orm() orm.Orm
}

type Blueprint interface {
// GetAddedColumns Get the added columns.
GetAddedColumns() []ColumnDefinition
Expand Down
12 changes: 10 additions & 2 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,13 +529,21 @@ func (r *Blueprint) ToSql(grammar driver.Grammar) ([]string, error) {
case CommandRename:
statements = append(statements, grammar.CompileRename(r, command))
case CommandRenameColumn:
statement, err := grammar.CompileRenameColumn(r.schema, r, command)
columns, err := r.schema.GetColumns(r.GetTableName())
if err != nil {
return statements, err
}
statement, err := grammar.CompileRenameColumn(r, command, columns)
if err != nil {
return statements, err
}
statements = append(statements, statement)
case CommandRenameIndex:
statements = append(statements, grammar.CompileRenameIndex(r.schema, r, command)...)
indexes, err := r.schema.GetIndexes(r.GetTableName())
if err != nil {
return statements, err
}
statements = append(statements, grammar.CompileRenameIndex(r, command, indexes)...)
case CommandTableComment:
if statement := grammar.CompileTableComment(r, command); statement != "" {
statements = append(statements, statement)
Expand Down
20 changes: 16 additions & 4 deletions database/schema/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,24 @@ import (
"github.com/goravel/framework/contracts/database/schema"
mocksdriver "github.com/goravel/framework/mocks/database/driver"
mocksorm "github.com/goravel/framework/mocks/database/orm"
mocksschema "github.com/goravel/framework/mocks/database/schema"
"github.com/goravel/framework/support/convert"
)

type BlueprintTestSuite struct {
suite.Suite
blueprint *Blueprint
mockSchema *mocksschema.Schema
blueprint *Blueprint
}

func TestBlueprintTestSuite(t *testing.T) {
suite.Run(t, new(BlueprintTestSuite))
}

func (s *BlueprintTestSuite) SetupTest() {
s.blueprint = NewBlueprint(nil, "goravel_", "users")
s.mockSchema = mocksschema.NewSchema(s.T())

s.blueprint = NewBlueprint(s.mockSchema, "goravel_", "users")
}

func (s *BlueprintTestSuite) TestAddAttributeCommands() {
Expand Down Expand Up @@ -611,9 +615,17 @@ func (s *BlueprintTestSuite) TestToSql() {
name: "Rename index command",
setup: func() {
s.blueprint.RenameIndex("old_index", "new_index")
s.mockSchema.EXPECT().GetIndexes(s.blueprint.GetTableName()).Return([]driver.Index{
{
Name: "old_index",
},
}, nil).Once()
mockGrammar.EXPECT().GetAttributeCommands().Return([]string{}).Once()
mockGrammar.EXPECT().CompileRenameIndex(s.blueprint.schema, s.blueprint, s.blueprint.commands[0]).
Return([]string{"ALTER INDEX old_index RENAME TO new_index"}).Once()
mockGrammar.EXPECT().CompileRenameIndex(s.blueprint, s.blueprint.commands[0], []driver.Index{
{
Name: "old_index",
},
}).Return([]string{"ALTER INDEX old_index RENAME TO new_index"}).Once()
},
expectedSQL: []string{"ALTER INDEX old_index RENAME TO new_index"},
},
Expand Down
52 changes: 26 additions & 26 deletions mocks/database/driver/Grammar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 0d45fdd

Please sign in to comment.