Skip to content

Commit

Permalink
optimize
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl committed Nov 17, 2024
1 parent e32eb5e commit 93ab764
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 87 deletions.
15 changes: 1 addition & 14 deletions database/schema/processors/mysql.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package processors

import (
"strings"

"github.com/goravel/framework/contracts/database/schema"
)

Expand All @@ -14,16 +12,5 @@ func NewMysql() Mysql {
}

func (r Mysql) ProcessIndexes(dbIndexes []DBIndex) []schema.Index {
var indexes []schema.Index
for _, dbIndex := range dbIndexes {
indexes = append(indexes, schema.Index{
Columns: strings.Split(dbIndex.Columns, ","),
Name: strings.ToLower(dbIndex.Name),
Type: strings.ToLower(dbIndex.Type),
Primary: dbIndex.Primary,
Unique: dbIndex.Unique,
})
}

return indexes
return processIndexes(dbIndexes)
}
15 changes: 1 addition & 14 deletions database/schema/processors/postgres.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package processors

import (
"strings"

"github.com/goravel/framework/contracts/database/schema"
)

Expand All @@ -14,18 +12,7 @@ func NewPostgres() Postgres {
}

func (r Postgres) ProcessIndexes(dbIndexes []DBIndex) []schema.Index {
var indexes []schema.Index
for _, dbIndex := range dbIndexes {
indexes = append(indexes, schema.Index{
Columns: strings.Split(dbIndex.Columns, ","),
Name: strings.ToLower(dbIndex.Name),
Type: strings.ToLower(dbIndex.Type),
Primary: dbIndex.Primary,
Unique: dbIndex.Unique,
})
}

return indexes
return processIndexes(dbIndexes)
}

func (r Postgres) ProcessTypes(types []schema.Type) []schema.Type {
Expand Down
24 changes: 0 additions & 24 deletions database/schema/processors/postgres_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,6 @@ import (
"github.com/goravel/framework/contracts/database/schema"
)

func TestPostgresProcessIndexes(t *testing.T) {
// Test with valid indexes
input := []DBIndex{
{Name: "INDEX_A", Type: "BTREE", Columns: "a,b"},
{Name: "INDEX_B", Type: "HASH", Columns: "c,d"},
}
expected := []schema.Index{
{Name: "index_a", Type: "btree", Columns: []string{"a", "b"}},
{Name: "index_b", Type: "hash", Columns: []string{"c", "d"}},
}

postgres := NewPostgres()
result := postgres.ProcessIndexes(input)

assert.Equal(t, expected, result)

// Test with empty input
input = []DBIndex{}

result = postgres.ProcessIndexes(input)

assert.Nil(t, result)
}

func TestPostgresProcessTypes(t *testing.T) {
// ValidTypes_ReturnsProcessedTypes
input := []schema.Type{
Expand Down
15 changes: 1 addition & 14 deletions database/schema/processors/sqlserver.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package processors

import (
"strings"

"github.com/goravel/framework/contracts/database/schema"
)

Expand All @@ -14,16 +12,5 @@ func NewSqlserver() Sqlserver {
}

func (r Sqlserver) ProcessIndexes(dbIndexes []DBIndex) []schema.Index {
var indexes []schema.Index
for _, dbIndex := range dbIndexes {
indexes = append(indexes, schema.Index{
Columns: strings.Split(dbIndex.Columns, ","),
Name: strings.ToLower(dbIndex.Name),
Type: strings.ToLower(dbIndex.Type),
Primary: dbIndex.Primary,
Unique: dbIndex.Unique,
})
}

return indexes
return processIndexes(dbIndexes)
}
22 changes: 22 additions & 0 deletions database/schema/processors/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package processors

import (
"strings"

"github.com/goravel/framework/contracts/database/schema"
)

func processIndexes(dbIndexes []DBIndex) []schema.Index {
var indexes []schema.Index
for _, dbIndex := range dbIndexes {
indexes = append(indexes, schema.Index{
Columns: strings.Split(dbIndex.Columns, ","),
Name: strings.ToLower(dbIndex.Name),
Type: strings.ToLower(dbIndex.Type),
Primary: dbIndex.Primary,
Unique: dbIndex.Unique,
})
}

return indexes
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/goravel/framework/contracts/database/schema"
)

func TestMysqlProcessIndexes(t *testing.T) {
func TestProcessIndexes(t *testing.T) {
// Test with valid indexes
input := []DBIndex{
{Name: "INDEX_A", Type: "BTREE", Columns: "a,b"},
Expand All @@ -19,15 +19,14 @@ func TestMysqlProcessIndexes(t *testing.T) {
{Name: "index_b", Type: "hash", Columns: []string{"c", "d"}},
}

mysql := NewMysql()
result := mysql.ProcessIndexes(input)
result := processIndexes(input)

assert.Equal(t, expected, result)

// Test with empty input
input = []DBIndex{}

result = mysql.ProcessIndexes(input)
result = processIndexes(input)

assert.Nil(t, result)
}
3 changes: 1 addition & 2 deletions database/schema/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/goravel/framework/contracts/log"
"github.com/goravel/framework/database/schema/grammars"
"github.com/goravel/framework/errors"
"github.com/goravel/framework/support/color"
)

const BindingSchema = "goravel.schema"
Expand Down Expand Up @@ -116,7 +115,7 @@ func (r *Schema) GetIndexListing(table string) []string {
for _, index := range indexes {
names = append(names, index.Name)
}
color.Red().Println(names)

return names
}

Expand Down
30 changes: 15 additions & 15 deletions database/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,28 @@ func TestSchemaSuite(t *testing.T) {
}

func (s *SchemaSuite) SetupTest() {
//postgresDocker := docker.Postgres()
//s.Require().NoError(postgresDocker.Ready())
//
//postgresQuery := gorm.NewTestQuery(postgresDocker, true)
//
//sqliteDocker := docker.Sqlite()
//sqliteQuery := gorm.NewTestQuery(sqliteDocker, true)
//
//mysqlDocker := docker.Mysql()
//s.Require().NoError(mysqlDocker.Ready())
//
//mysqlQuery := gorm.NewTestQuery(mysqlDocker, true)
postgresDocker := docker.Postgres()
s.Require().NoError(postgresDocker.Ready())

postgresQuery := gorm.NewTestQuery(postgresDocker, true)

sqliteDocker := docker.Sqlite()
sqliteQuery := gorm.NewTestQuery(sqliteDocker, true)

mysqlDocker := docker.Mysql()
s.Require().NoError(mysqlDocker.Ready())

mysqlQuery := gorm.NewTestQuery(mysqlDocker, true)

sqlserverDocker := docker.Sqlserver()
s.Require().NoError(sqlserverDocker.Ready())

sqlserverQuery := gorm.NewTestQuery(sqlserverDocker, true)

s.driverToTestQuery = map[database.Driver]*gorm.TestQuery{
//database.DriverPostgres: postgresQuery,
//database.DriverSqlite: sqliteQuery,
//database.DriverMysql: mysqlQuery,
database.DriverPostgres: postgresQuery,
database.DriverSqlite: sqliteQuery,
database.DriverMysql: mysqlQuery,
database.DriverSqlserver: sqlserverQuery,
}
}
Expand Down

0 comments on commit 93ab764

Please sign in to comment.