Skip to content

Commit

Permalink
feat: [#280] Add primary method (#712)
Browse files Browse the repository at this point in the history
* feat: [#280] Optimize

* feat: [#280] Remove Change function

* chore: update mocks

* Remove

* chore: update mocks

* fix lint

* feat: [#280] Add Primary method

* cancel test

---------

Co-authored-by: hwbrzzl <[email protected]>
  • Loading branch information
hwbrzzl and hwbrzzl authored Nov 4, 2024
1 parent 1cc1f60 commit 534a84b
Show file tree
Hide file tree
Showing 10 changed files with 186 additions and 1 deletion.
7 changes: 7 additions & 0 deletions contracts/database/schema/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Blueprint interface {
GetTableName() string
// HasCommand Determine if the blueprint has a specific command.
HasCommand(command string) bool
// Primary Specify the primary key(s) for the table.
Primary(column ...string)
// ID Create a new auto-incrementing big integer (8-byte) column on the table.
ID(column ...string) ColumnDefinition
// Integer Create a new integer (4-byte) column on the table.
Expand All @@ -30,3 +32,8 @@ type Blueprint interface {
// ToSql Get the raw SQL statements for the blueprint.
ToSql(query orm.Query, grammar Grammar) []string
}

type IndexConfig struct {
Algorithm string
Name string
}
2 changes: 2 additions & 0 deletions contracts/database/schema/grammar.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ type Grammar interface {
CompileDropAllViews(views []string) string
// CompileDropIfExists Compile a drop table (if exists) command.
CompileDropIfExists(blueprint Blueprint) string
// CompilePrimary Compile a primary key command.
CompilePrimary(blueprint Blueprint, command *Command) string
// CompileTables Compile the query to determine the tables.
CompileTables() string
// CompileTypes Compile the query to determine the types.
Expand Down
1 change: 1 addition & 0 deletions contracts/database/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type Connection interface {
type Command struct {
Algorithm string
Column ColumnDefinition
Columns []string
From string
Index string
On string
Expand Down
34 changes: 34 additions & 0 deletions database/schema/blueprint.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package schema

import (
"strings"

ormcontract "github.com/goravel/framework/contracts/database/orm"
"github.com/goravel/framework/contracts/database/schema"
"github.com/goravel/framework/database/schema/constants"
Expand Down Expand Up @@ -105,6 +107,10 @@ func (r *Blueprint) Integer(column string) schema.ColumnDefinition {
return columnImpl
}

func (r *Blueprint) Primary(columns ...string) {
r.indexCommand(constants.CommandPrimary, columns)
}

func (r *Blueprint) SetTable(name string) {
r.table = name
}
Expand Down Expand Up @@ -137,6 +143,8 @@ func (r *Blueprint) ToSql(query ormcontract.Query, grammar schema.Grammar) []str
statements = append(statements, grammar.CompileCreate(r, query))
case constants.CommandDropIfExists:
statements = append(statements, grammar.CompileDropIfExists(r))
case constants.CommandPrimary:
statements = append(statements, grammar.CompilePrimary(r, command))
}
}

Expand Down Expand Up @@ -180,6 +188,32 @@ func (r *Blueprint) addImpliedCommands(grammar schema.Grammar) {
r.addAttributeCommands(grammar)
}

func (r *Blueprint) createIndexName(ttype string, columns []string) string {
table := r.GetTableName()
index := strings.ToLower(table + "_" + strings.Join(columns, "_") + "_" + ttype)
index = strings.ReplaceAll(index, "-", "_")

return strings.ReplaceAll(index, ".", "_")
}

func (r *Blueprint) indexCommand(ttype string, columns []string, config ...schema.IndexConfig) *schema.Command {
command := &schema.Command{
Columns: columns,
Name: ttype,
}

if len(config) > 0 {
command.Algorithm = config[0].Algorithm
command.Index = config[0].Name
} else {
command.Index = r.createIndexName(ttype, columns)
}

r.addCommand(command)

return command
}

func (r *Blueprint) isCreate() bool {
for _, command := range r.commands {
if command.Name == constants.CommandCreate {
Expand Down
25 changes: 25 additions & 0 deletions database/schema/blueprint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,11 @@ func (s *BlueprintTestSuite) TestBuild() {
}
}

func (s *BlueprintTestSuite) TestCreateIndexName() {
name := s.blueprint.createIndexName("index", []string{"id", "name-1", "name.2"})
s.Equal("goravel_users_id_name_1_name_2_index", name)
}

func (s *BlueprintTestSuite) TestGetAddedColumns() {
name := "name"
addedColumn := &ColumnDefinition{
Expand All @@ -159,6 +164,26 @@ func (s *BlueprintTestSuite) TestHasCommand() {
s.True(s.blueprint.HasCommand(constants.CommandCreate))
}

func (s *BlueprintTestSuite) TestIndexCommand() {
s.blueprint.indexCommand("index", []string{"id", "name"})
s.Contains(s.blueprint.commands, &schema.Command{
Columns: []string{"id", "name"},
Name: "index",
Index: "goravel_users_id_name_index",
})

s.blueprint.indexCommand("index", []string{"id", "name"}, schema.IndexConfig{
Algorithm: "custom_algorithm",
Name: "custom_name",
})
s.Contains(s.blueprint.commands, &schema.Command{
Algorithm: "custom_algorithm",
Columns: []string{"id", "name"},
Name: "index",
Index: "custom_name",
})
}

func (s *BlueprintTestSuite) TestIsCreate() {
s.False(s.blueprint.isCreate())
s.blueprint.commands = []*schema.Command{
Expand Down
2 changes: 1 addition & 1 deletion database/schema/constants/constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ package constants

const (
CommandAdd = "add"
CommandChange = "change"
CommandComment = "comment"
CommandCreate = "create"
CommandDropIfExists = "dropIfExists"
CommandPrimary = "primary"
DefaultStringLength = 255
)
4 changes: 4 additions & 0 deletions database/schema/grammars/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,10 @@ func (r *Postgres) CompileDropIfExists(blueprint schema.Blueprint) string {
return fmt.Sprintf("drop table if exists %s", blueprint.GetTableName())
}

func (r *Postgres) CompilePrimary(blueprint schema.Blueprint, command *schema.Command) string {
return fmt.Sprintf("alter table %s add primary key (%s)", blueprint.GetTableName(), strings.Join(command.Columns, ","))
}

func (r *Postgres) CompileTables() string {
return "select c.relname as name, n.nspname as schema, pg_total_relation_size(c.oid) as size, " +
"obj_description(c.oid, 'pg_class') as comment from pg_class c, pg_namespace n " +
Expand Down
19 changes: 19 additions & 0 deletions database/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,25 @@ func (s *SchemaSuite) TestDropAllViews() {

}

func (s *SchemaSuite) TestPrimary() {
for driver, testQuery := range s.driverToTestQuery {
s.Run(driver.String(), func() {
schema := GetTestSchema(testQuery, s.driverToTestQuery)
table := "primaries"

s.NoError(schema.Create(table, func(table contractsschema.Blueprint) {
table.String("name")
table.String("age")
table.Primary("name", "age")
}))

s.Require().True(schema.HasTable(table))
// TODO Open below when implementing index methods
//s.Require().True(schema.HasIndex(table, "primaries_pkey"))
})
}
}

func (s *SchemaSuite) TestTable_GetTables() {
for driver, testQuery := range s.driverToTestQuery {
s.Run(driver.String(), func() {
Expand Down
46 changes: 46 additions & 0 deletions mocks/database/schema/Blueprint.go

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

47 changes: 47 additions & 0 deletions mocks/database/schema/Grammar.go

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

0 comments on commit 534a84b

Please sign in to comment.