Skip to content

Commit

Permalink
feat: [#572] add rename column compiler
Browse files Browse the repository at this point in the history
  • Loading branch information
almas1992 committed Feb 8, 2025
1 parent 6d6f472 commit 90462aa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
8 changes: 8 additions & 0 deletions grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,14 @@ func (r *Grammar) CompileRename(blueprint contractsschema.Blueprint, command *co
return fmt.Sprintf("alter table %s rename to %s", r.wrap.Table(blueprint.GetTableName()), r.wrap.Table(command.To))
}

func (r *Grammar) CompileRenameColumn(_ contractsschema.Schema, blueprint contractsschema.Blueprint, command *contractsschema.Command) (string, error) {
return fmt.Sprintf("alter table %s rename column %s to %s",
r.wrap.Table(blueprint.GetTableName()),
r.wrap.Column(command.From),
r.wrap.Column(command.To),
), nil
}

func (r *Grammar) CompileRenameIndex(s contractsschema.Schema, blueprint contractsschema.Blueprint, command *contractsschema.Command) []string {
indexes, err := s.GetIndexes(blueprint.GetTableName())
if err != nil {
Expand Down
15 changes: 15 additions & 0 deletions grammar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,21 @@ func (s *GrammarSuite) TestCompileIndex() {
s.Equal(`create index "users" on "goravel_users" ("role_id", "permission_id")`, s.grammar.CompileIndex(mockBlueprint, command))
}

func (s *GrammarSuite) TestCompileRenameColumn() {
mockBlueprint := mocksschema.NewBlueprint(s.T())
mockColumn := mocksschema.NewColumnDefinition(s.T())

mockBlueprint.EXPECT().GetTableName().Return("users").Once()

sql := s.grammar.CompileRenameColumn(nil, mockBlueprint, &contractsschema.Command{

Check failure on line 165 in grammar_test.go

View workflow job for this annotation

GitHub Actions / lint / lint

assignment mismatch: 1 variable but s.grammar.CompileRenameColumn returns 2 values (typecheck)

Check failure on line 165 in grammar_test.go

View workflow job for this annotation

GitHub Actions / codecov / codecov

assignment mismatch: 1 variable but s.grammar.CompileRenameColumn returns 2 values

Check failure on line 165 in grammar_test.go

View workflow job for this annotation

GitHub Actions / test / ubuntu (1.23)

assignment mismatch: 1 variable but s.grammar.CompileRenameColumn returns 2 values
Column: mockColumn,
From: "before",
To: "after",
})

s.Equal(`alter table "goravel_users" rename column "before" to "after"`, sql)
}

func (s *GrammarSuite) TestCompileRenameIndex() {
var (
mockSchema *mocksschema.Schema
Expand Down

0 comments on commit 90462aa

Please sign in to comment.