Skip to content

Commit

Permalink
Merge pull request #195 from trheyi/main
Browse files Browse the repository at this point in the history
[fix] the index update bug in the migrate process
  • Loading branch information
trheyi authored May 24, 2024
2 parents fd16ec8 + ba0bc25 commit 053dae9
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 32 deletions.
8 changes: 4 additions & 4 deletions schema/types/blueprint.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package types

import (
"crypto/md5"
"encoding/json"
"fmt"
"io/ioutil"
"os"

jsoniter "github.com/json-iterator/go"
"golang.org/x/crypto/md4"
)

// New create a Blueprint
Expand All @@ -20,7 +20,7 @@ func New() Blueprint {

// NewFile create a Blueprint by File
func NewFile(file string) (Blueprint, error) {
data, err := ioutil.ReadFile(file)
data, err := os.ReadFile(file)
if err != nil {
return Blueprint{}, nil
}
Expand Down Expand Up @@ -120,6 +120,6 @@ func (index Index) Hash() string {
}

func hash(s string) string {
h := md4.New()
h := md5.New()
return string(h.Sum([]byte(s)))
}
3 changes: 3 additions & 0 deletions schema/types/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,16 +200,19 @@ func (diff *Diff) columnsAddAlt(columns []Column, blueprint map[string]Column) {
}

if origin.Index != column.Index {
column.RemoveIndex = true
diff.Columns.Alt = append(diff.Columns.Alt, column)
continue
}

if origin.Unique != column.Unique {
column.RemoveUnique = true
diff.Columns.Alt = append(diff.Columns.Alt, column)
continue
}

if origin.Primary != column.Primary {
column.RemovePrimary = true
diff.Columns.Alt = append(diff.Columns.Alt, column)
continue
}
Expand Down
41 changes: 22 additions & 19 deletions schema/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,28 @@ type BlueprintOption struct {

// Column the field description struct
type Column struct {
Name string `json:"name"`
Label string `json:"label,omitempty"`
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Comment string `json:"comment,omitempty"`
Length int `json:"length,omitempty"`
Precision int `json:"precision,omitempty"`
Scale int `json:"scale,omitempty"`
Nullable bool `json:"nullable,omitempty"`
Option []string `json:"option,omitempty"`
Default interface{} `json:"default,omitempty"`
DefaultRaw string `json:"default_raw,omitempty"`
Generate string `json:"generate,omitempty"` // Increment, UUID,...
Crypt string `json:"crypt,omitempty"` // AES, PASSWORD, AES-256, AES-128, PASSWORD-HASH, ...
Index bool `json:"index,omitempty"`
Unique bool `json:"unique,omitempty"`
Primary bool `json:"primary,omitempty"`
Origin string `json:"origin,omitempty"`
Name string `json:"name"`
Label string `json:"label,omitempty"`
Type string `json:"type,omitempty"`
Title string `json:"title,omitempty"`
Description string `json:"description,omitempty"`
Comment string `json:"comment,omitempty"`
Length int `json:"length,omitempty"`
Precision int `json:"precision,omitempty"`
Scale int `json:"scale,omitempty"`
Nullable bool `json:"nullable,omitempty"`
Option []string `json:"option,omitempty"`
Default interface{} `json:"default,omitempty"`
DefaultRaw string `json:"default_raw,omitempty"`
Generate string `json:"generate,omitempty"` // Increment, UUID,...
Crypt string `json:"crypt,omitempty"` // AES, PASSWORD, AES-256, AES-128, PASSWORD-HASH, ...
Index bool `json:"index,omitempty"`
Unique bool `json:"unique,omitempty"`
Primary bool `json:"primary,omitempty"`
Origin string `json:"origin,omitempty"`
RemoveIndex bool `json:"-"`
RemoveUnique bool `json:"-"`
RemovePrimary bool `json:"-"`
}

// Index the search index struct
Expand Down
14 changes: 8 additions & 6 deletions schema/xun/blueprint.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,15 +130,17 @@ func ColumnToBlueprint(col *schema.Column, prikeys []string) types.Column {
column.Scale = *col.Scale
}

if len(col.Indexes) == 1 {
for _, idx := range col.Indexes {
if len(idx.Columns) != 1 {
continue
}

switch col.Indexes[0].Type {
case "index":
if idx.Type == "index" {
column.Index = true
break
case "unique":
}

if idx.Type == "unique" {
column.Unique = true
break
}
}

Expand Down
14 changes: 11 additions & 3 deletions schema/xun/xun.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,13 +228,21 @@ func (x *Xun) ColumnAdd(name string, column types.Column) error {
func (x *Xun) ColumnAlt(name string, column types.Column) error {
sch := x.Manager.Schema()

// drop indexes
if column.Index {
// drop index
if column.RemoveIndex || column.Index {
x.IndexDel(name, fmt.Sprintf("%s_index", column.Name))
} else if column.Unique {
}

// drop unique
if column.RemoveUnique || column.Unique {
x.IndexDel(name, fmt.Sprintf("%s_unique", column.Name))
}

// drop primary
if column.RemovePrimary {
x.IndexDel(name, "PRIMARY")
}

return sch.AlterTable(name, func(table schema.Blueprint) {
_, err := setColumn(table, column)
if err != nil {
Expand Down

0 comments on commit 053dae9

Please sign in to comment.