Skip to content

Commit

Permalink
fix: mysql primary error (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
hwbrzzl authored Dec 25, 2024
1 parent b261a62 commit a1f7c38
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 1 deletion.
14 changes: 13 additions & 1 deletion database/schema/processors/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,5 +58,17 @@ func (r Mysql) ProcessForeignKeys(dbForeignKeys []schema.DBForeignKey) []schema.
}

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

return indexes
}
35 changes: 35 additions & 0 deletions database/schema/schema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package schema

import (
"fmt"
"strings"
"testing"
"time"

Expand Down Expand Up @@ -2033,6 +2034,40 @@ func (s *SchemaSuite) TestIndexMethods() {
}
s.False(index.Unique)
}
if index.Name == "name_index" {
s.ElementsMatch(index.Columns, []string{"name"})
s.False(index.Primary)
if driver == database.DriverSqlite {
s.Empty(index.Type)
} else if driver == database.DriverSqlserver {
s.Equal("nonclustered", index.Type)
} else {
s.Equal("btree", index.Type)
}
s.False(index.Unique)
}
if strings.HasPrefix(index.Name, "pk_") {
s.ElementsMatch(index.Columns, []string{"id"})
s.True(index.Primary)
s.Equal("clustered", index.Type)
s.True(index.Unique)
}
if index.Name == "primary" {
s.ElementsMatch(index.Columns, []string{"id"})
s.True(index.Primary)
if driver == database.DriverSqlite {
s.Empty(index.Type)
} else {
s.Equal("btree", index.Type)
}
s.True(index.Unique)
}
if index.Name == "goravel_indexes_pkey" {
s.ElementsMatch(index.Columns, []string{"id"})
s.True(index.Primary)
s.Equal("btree", index.Type)
s.True(index.Unique)
}
}

s.NoError(schema.Table(table, func(table contractsschema.Blueprint) {
Expand Down

0 comments on commit a1f7c38

Please sign in to comment.