Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: sqlserver re-migrate column with default value #732

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions custom_type_tinyint.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
package main

import (
"database/sql/driver"
"encoding/json"
"fmt"
"strconv"
"strings"

"github.com/spf13/cast"
"gorm.io/gorm"
"gorm.io/gorm/schema"
)

type TinyInt uint8

// GormDataType returns gorm common data type. This type is used for the field's column type.
func (TinyInt) GormDataType() string {
return string(schema.Uint)
}

// GormDBDataType returns gorm DB data type based on the current using database.
func (TinyInt) GormDBDataType(db *gorm.DB, field *schema.Field) (dataType string) {
switch db.Dialector.Name() {
case "sqlite":
dataType = "integer"
case "mysql":
dataType = "tinyint unsigned"
case "postgres", "dm":
dataType = "smallint"
case "sqlserver":
dataType = "tinyint"
case "oracle":
dataType = "NUMBER(3, 0)"
default:
dataType = getGormTypeFromTag(field)
}
return
}

func getGormTypeFromTag(field *schema.Field) (dataType string) {
if field != nil {
if val, ok := field.TagSettings["TYPE"]; ok {
dataType = strings.ToLower(val)
}
}
return
}

// Scan implements sql.Scanner interface and scans value into TinyInt.
func (tinyint *TinyInt) Scan(src interface{}) error {
if src == nil {
*tinyint = 0
return nil
}

v, err := cast.ToUint8E(src)
if err != nil {
return fmt.Errorf("failed to parse TinyInt value: %v", src)
}
*tinyint = TinyInt(v)
return nil
}

// Value implements driver.Valuer interface and returns uint8 format of TinyInt.
func (tinyint TinyInt) Value() (driver.Value, error) {
if tinyint == 0 {
return nil, nil
}

return int64(tinyint), nil
}

// String implements fmt.Stringer interface.
func (tinyint TinyInt) String() string {
return strconv.FormatUint(uint64(tinyint), 10)
}

//goland:noinspection GoMixedReceiverTypes
func (tinyint TinyInt) Int() int {
return int(tinyint)
}

// MarshalJSON implements json.Marshaler to convert TinyInt to json serialization.
func (tinyint TinyInt) MarshalJSON() ([]byte, error) {
return json.Marshal(tinyint.Int())
}

// UnmarshalJSON implements json.Unmarshaler to deserialize json data.
func (tinyint *TinyInt) UnmarshalJSON(data []byte) error {
if data == nil {
return nil
}
s := string(data)
if value, err := strconv.Unquote(s); err == nil {
return tinyint.Scan(value)
}
return tinyint.Scan(s)
}
21 changes: 0 additions & 21 deletions gen.go

This file was deleted.

22 changes: 7 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ module gorm.io/playground
go 1.20

require (
gorm.io/driver/mysql v1.5.2
gorm.io/driver/postgres v1.5.2
gorm.io/driver/sqlite v1.5.3
gorm.io/driver/sqlserver v1.5.1
gorm.io/gen v0.3.25
gorm.io/gorm v1.25.4
github.com/spf13/cast v1.6.0
gorm.io/driver/mysql v1.5.6
gorm.io/driver/postgres v1.5.7
gorm.io/driver/sqlite v1.5.5
gorm.io/driver/sqlserver v1.5.3
gorm.io/gorm v1.25.10
)

require (
Expand All @@ -21,15 +21,7 @@ require (
github.com/jinzhu/inflection v1.0.0 // indirect
github.com/jinzhu/now v1.1.5 // indirect
github.com/mattn/go-sqlite3 v1.14.17 // indirect
github.com/microsoft/go-mssqldb v1.5.0 // indirect
github.com/microsoft/go-mssqldb v1.6.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/mod v0.14.0 // indirect
golang.org/x/sys v0.14.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.15.0 // indirect
gorm.io/datatypes v1.1.1-0.20230130040222-c43177d3cf8c // indirect
gorm.io/hints v1.1.0 // indirect
gorm.io/plugin/dbresolver v1.5.0 // indirect
)

replace gorm.io/gorm => ./gorm
41 changes: 40 additions & 1 deletion main_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package main

import (
"errors"
"testing"

"gorm.io/gorm"
)

// GORM_REPO: https://github.com/go-gorm/gorm.git
Expand All @@ -15,6 +18,42 @@ func TestGORM(t *testing.T) {

var result User
if err := DB.First(&result, user.ID).Error; err != nil {
t.Errorf("Failed, got error: %v", err)
if !errors.Is(err, gorm.ErrRecordNotFound) {
t.Errorf("Failed, got error: %v", err)
}
}
}

type TableColumnWithoutDefault struct {
gorm.Model

Type TinyInt `gorm:"column:type"`
}

func (TableColumnWithoutDefault) TableName() string {
return "table_column_default"
}

type TableColumnWithDefault struct {
gorm.Model

Type TinyInt `gorm:"column:type;default:1"`
}

func (TableColumnWithDefault) TableName() string {
return "table_column_default"
}

func TestReMigrateColumnWithDefault(t *testing.T) {
m1 := new(TableColumnWithoutDefault)
if !DB.Migrator().HasTable(m1) {
if err := DB.AutoMigrate(m1); err != nil {
t.Fatalf("Failed to auto migrate, but got error %v\n", err)
}
}

m2 := new(TableColumnWithDefault)
if err := DB.AutoMigrate(m2); err != nil {
t.Fatalf("Failed to auto migrate, but got error %v\n", err)
}
}
Loading