mirror of https://github.com/go-gorm/gorm.git
Compare commits
9 Commits
31afa4e73d
...
5774374f55
Author | SHA1 | Date |
---|---|---|
Rankgice | 5774374f55 | |
Ivan Ryabov | 49bbaa637f | |
홍성욱 | b0d70a26d1 | |
rankgice | 8e9c1358b5 | |
rankgice | fa23822f68 | |
rankgice | 202cbae492 | |
rankgice | 96c8a2edba | |
rankgice | 599aad63a1 | |
rankgice | c930baab81 |
|
@ -88,6 +88,7 @@ func (expr NamedExpr) Build(builder Builder) {
|
|||
inName bool
|
||||
afterParenthesis bool
|
||||
namedMap = make(map[string]interface{}, len(expr.Vars))
|
||||
quotationMarks byte
|
||||
)
|
||||
|
||||
for _, v := range expr.Vars {
|
||||
|
@ -124,6 +125,14 @@ func (expr NamedExpr) Build(builder Builder) {
|
|||
name := make([]byte, 0, 10)
|
||||
|
||||
for _, v := range []byte(expr.SQL) {
|
||||
if quotationMarks == v && (v == '"' || v == '\'') {
|
||||
quotationMarks = 0
|
||||
} else if quotationMarks == 0 && (v == '"' || v == '\'') {
|
||||
quotationMarks = v
|
||||
} else if quotationMarks == '"' || quotationMarks == '\'' {
|
||||
builder.WriteByte(v)
|
||||
continue
|
||||
}
|
||||
if v == '@' && !inName {
|
||||
inName = true
|
||||
name = name[:0]
|
||||
|
|
|
@ -524,8 +524,8 @@ func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnTy
|
|||
|
||||
// check nullable
|
||||
if nullable, ok := columnType.Nullable(); ok && nullable == field.NotNull {
|
||||
// not primary key & database is nullable
|
||||
if !field.PrimaryKey && nullable {
|
||||
// not primary key & current database is non-nullable(to be nullable)
|
||||
if !field.PrimaryKey && !nullable {
|
||||
alterColumn = true
|
||||
}
|
||||
}
|
||||
|
|
|
@ -76,11 +76,12 @@ func (schema *Schema) ParseIndexes() map[string]Index {
|
|||
func (schema *Schema) LookIndex(name string) *Index {
|
||||
if schema != nil {
|
||||
indexes := schema.ParseIndexes()
|
||||
for _, index := range indexes {
|
||||
if index.Name == name {
|
||||
return &index
|
||||
}
|
||||
|
||||
if index, found := indexes[name]; found {
|
||||
return &index
|
||||
}
|
||||
|
||||
for _, index := range indexes {
|
||||
for _, field := range index.Fields {
|
||||
if field.Name == name {
|
||||
return &index
|
||||
|
@ -111,10 +112,7 @@ func parseFieldIndexes(field *Field) (indexes []Index, err error) {
|
|||
idx = len(tag)
|
||||
}
|
||||
|
||||
if idx != -1 {
|
||||
name = tag[0:idx]
|
||||
}
|
||||
|
||||
name = tag[0:idx]
|
||||
if name == "" {
|
||||
subName := field.Name
|
||||
const key = "COMPOSITE"
|
||||
|
|
|
@ -21,6 +21,9 @@ type UserIndex struct {
|
|||
Name7 string `gorm:"index:type"`
|
||||
Name8 string `gorm:"index:,length:10;index:,collate:utf8"`
|
||||
|
||||
CompName1 string `gorm:"index:,unique,composite:idx_compname_1,option:NULLS NOT DISTINCT;not null"`
|
||||
CompName2 string `gorm:"index:,composite:idx_compname_1"`
|
||||
|
||||
// Composite Index: Flattened structure.
|
||||
Data0A string `gorm:"index:,composite:comp_id0"`
|
||||
Data0B string `gorm:"index:,composite:comp_id0"`
|
||||
|
@ -154,6 +157,15 @@ func TestParseIndex(t *testing.T) {
|
|||
Field: &schema.Field{Name: "Data2B"},
|
||||
}},
|
||||
},
|
||||
"idx_user_indices_idx_compname_1": {
|
||||
Class: "UNIQUE",
|
||||
Name: "idx_user_indices_idx_compname_1",
|
||||
Option: "NULLS NOT DISTINCT",
|
||||
Fields: []schema.IndexOption{
|
||||
{Field: &schema.Field{Name: "CompName1", NotNull: true}},
|
||||
{Field: &schema.Field{Name: "CompName2"}},
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
CheckIndices(t, results, user.ParseIndexes())
|
||||
|
@ -253,7 +265,7 @@ func CheckIndices(t *testing.T, expected, actual map[string]schema.Index) {
|
|||
}
|
||||
for i, ef := range ei.Fields {
|
||||
af := ai.Fields[i]
|
||||
tests.AssertObjEqual(t, af, ef, "Name", "Unique", "UniqueIndex", "Expression", "Sort", "Collate", "Length")
|
||||
tests.AssertObjEqual(t, af, ef, "Name", "Unique", "UniqueIndex", "Expression", "Sort", "Collate", "Length", "NotNull")
|
||||
}
|
||||
})
|
||||
delete(actual, k)
|
||||
|
|
|
@ -10,7 +10,7 @@ require (
|
|||
gorm.io/driver/mysql v1.5.7
|
||||
gorm.io/driver/postgres v1.5.9
|
||||
gorm.io/driver/sqlite v1.5.6
|
||||
gorm.io/driver/sqlserver v1.5.3
|
||||
gorm.io/driver/sqlserver v1.5.4
|
||||
gorm.io/gorm v1.25.12
|
||||
)
|
||||
|
||||
|
|
|
@ -141,6 +141,48 @@ func TestAutoMigrateSelfReferential(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestAutoMigrateNullable(t *testing.T) {
|
||||
type MigrateNullableColumn struct {
|
||||
ID uint
|
||||
Bonus float64 `gorm:"not null"`
|
||||
Stock float64
|
||||
}
|
||||
|
||||
DB.Migrator().DropTable(&MigrateNullableColumn{})
|
||||
|
||||
DB.AutoMigrate(&MigrateNullableColumn{})
|
||||
|
||||
type MigrateNullableColumn2 struct {
|
||||
ID uint
|
||||
Bonus float64
|
||||
Stock float64 `gorm:"not null"`
|
||||
}
|
||||
|
||||
if err := DB.Table("migrate_nullable_columns").AutoMigrate(&MigrateNullableColumn2{}); err != nil {
|
||||
t.Fatalf("failed to auto migrate, got error: %v", err)
|
||||
}
|
||||
|
||||
columnTypes, err := DB.Table("migrate_nullable_columns").Migrator().ColumnTypes(&MigrateNullableColumn{})
|
||||
if err != nil {
|
||||
t.Fatalf("failed to get column types, got error: %v", err)
|
||||
}
|
||||
|
||||
for _, columnType := range columnTypes {
|
||||
switch columnType.Name() {
|
||||
case "bonus":
|
||||
// allow to change non-nullable to nullable
|
||||
if nullable, _ := columnType.Nullable(); !nullable {
|
||||
t.Fatalf("bonus's nullable should be true, bug got %t", nullable)
|
||||
}
|
||||
case "stock":
|
||||
// do not allow to change nullable to non-nullable
|
||||
if nullable, _ := columnType.Nullable(); !nullable {
|
||||
t.Fatalf("stock's nullable should be true, bug got %t", nullable)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func TestSmartMigrateColumn(t *testing.T) {
|
||||
fullSupported := map[string]bool{"mysql": true, "postgres": true}[DB.Dialector.Name()]
|
||||
|
||||
|
|
|
@ -0,0 +1,103 @@
|
|||
package tests_test
|
||||
|
||||
import (
|
||||
. "gorm.io/gorm/utils/tests"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestRawSelect(t *testing.T) {
|
||||
users := []User{
|
||||
*GetUser("raw1", Config{}),
|
||||
*GetUser("raw2", Config{}),
|
||||
*GetUser("raw3", Config{}),
|
||||
*GetUser("@name", Config{}),
|
||||
*GetUser("@age", Config{}),
|
||||
}
|
||||
|
||||
if err := DB.Create(&users).Error; err != nil {
|
||||
t.Fatalf("errors happened when create users: %v", err)
|
||||
}
|
||||
tests := []struct {
|
||||
TestName string
|
||||
Sql string
|
||||
args map[string]interface{}
|
||||
Expect []User
|
||||
}{
|
||||
{
|
||||
"raw_test1",
|
||||
`select * from users where name like @name and age = 18`,
|
||||
map[string]interface{}{
|
||||
"name": "raw1",
|
||||
},
|
||||
[]User{
|
||||
users[0],
|
||||
},
|
||||
},
|
||||
{
|
||||
"raw_test2",
|
||||
`select * from users where name like @name and age = 18`,
|
||||
map[string]interface{}{
|
||||
"name": "@name",
|
||||
},
|
||||
[]User{
|
||||
users[3],
|
||||
},
|
||||
},
|
||||
{
|
||||
"raw_test3",
|
||||
`select * from users where name like @name and age = 18`,
|
||||
map[string]interface{}{
|
||||
"name": "@age",
|
||||
},
|
||||
[]User{
|
||||
users[4],
|
||||
},
|
||||
},
|
||||
{
|
||||
"raw_test4",
|
||||
`select * from users where name like 'raw3' and age = @age`,
|
||||
map[string]interface{}{
|
||||
"age": 18,
|
||||
},
|
||||
[]User{
|
||||
users[2],
|
||||
},
|
||||
},
|
||||
{
|
||||
"raw_test5",
|
||||
`select * from users where name like '@name' and age = @age`,
|
||||
map[string]interface{}{
|
||||
"age": 18,
|
||||
},
|
||||
[]User{
|
||||
users[3],
|
||||
},
|
||||
},
|
||||
{
|
||||
"raw_test6",
|
||||
`select * from users where name like '@age' and age = @age`,
|
||||
map[string]interface{}{
|
||||
"age": 18,
|
||||
},
|
||||
[]User{
|
||||
users[4],
|
||||
},
|
||||
},
|
||||
}
|
||||
for _, test := range tests {
|
||||
t.Run(test.TestName, func(t *testing.T) {
|
||||
var results []User
|
||||
if err := DB.Raw(test.Sql, test.args).Scan(&results).Error; err != nil {
|
||||
t.Errorf("errors %s: %v", test.TestName, err)
|
||||
} else {
|
||||
if len(results) != len(test.Expect) {
|
||||
t.Errorf("errors %s: %v", test.TestName, err)
|
||||
} else {
|
||||
for i := 0; i < len(results); i++ {
|
||||
CheckUser(t, results[i], test.Expect[i])
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue