Skip to content

Commit

Permalink
🐛 修复自动迁移已存在的表字段报 ORA-22859 错误的问题
Browse files Browse the repository at this point in the history
添加类型别名:

- `blob`: `longraw`
- `clob`: `longvarchar`

Fixed #26

Signed-off-by: liutianqi <zixizixi@vip.qq.com>
  • Loading branch information
iTanken committed Apr 26, 2024
1 parent ffb2cd0 commit 29d2cfe
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 2 deletions.
6 changes: 4 additions & 2 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,10 @@ func (m Migrator) CurrentDatabase() (name string) {
// GetTypeAliases return database type aliases
func (m Migrator) GetTypeAliases(databaseTypeName string) (types []string) {
switch databaseTypeName {
case "clob", "ocicloblocator":
types = append(types, "clob", "ocicloblocator")
case "blob", "longraw":
types = append(types, "blob", "longraw")
case "clob", "longvarchar", "ocicloblocator":
types = append(types, "clob", "longvarchar", "ocicloblocator")
case "nchar", "varchar", "varchar2":
types = append(types, "nchar", "varchar", "varchar2")
case "number", "integer", "smallint":
Expand Down
56 changes: 56 additions & 0 deletions migrator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"reflect"
"testing"
"time"

"gorm.io/gorm"
)

func TestMigrator_AutoMigrate(t *testing.T) {
Expand Down Expand Up @@ -165,3 +167,57 @@ type TestTableUserMigrateColumn struct {
func (TestTableUserMigrateColumn) TableName() string {
return "test_user"
}

type testTableColumnTypeModel struct {
ID int64 `gorm:"column:id;size:64;not null;autoIncrement:true;autoIncrementIncrement:1;primaryKey"`
Name string `gorm:"column:name;size:50"`
Age uint8 `gorm:"column:age;size:8"`

Avatar []byte `gorm:"column:avatar;"` // type:varbinary(8000)

Balance float64 `gorm:"column:balance;type:decimal(18, 2)"`
Remark string `gorm:"column:remark;size:-1"`
Enabled bool `gorm:"column:enabled;"`

CreatedAt time.Time
UpdatedAt time.Time
DeletedAt gorm.DeletedAt
}

func (t testTableColumnTypeModel) TableName() string {
return "test_table_column_type"
}

func TestMigrator_TableColumnType(t *testing.T) {
db, err := dbNamingCase, dbErrors[0]
if err != nil {
t.Fatal(err)
}
if db == nil {
t.Log("db is nil!")
return
}
testModel := new(testTableColumnTypeModel)

type args struct {
model interface{}
drop bool
}
tests := []struct {
name string
args args
}{
{name: "create", args: args{model: testModel}},
{name: "alter", args: args{model: testModel, drop: true}},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if err = db.AutoMigrate(tt.args.model); err != nil {
t.Errorf("AutoMigrate failed:%v", err)
}
if tt.args.drop {
_ = db.Migrator().DropTable(tt.args.model)
}
})
}
}

0 comments on commit 29d2cfe

Please sign in to comment.