go-gorm / postgres

GORM PostgreSQL driver
MIT License
228 stars 120 forks source link

fix : fail to alter column from smallint to boolean #165

Closed jeffry-luqman closed 1 year ago

jeffry-luqman commented 1 year ago

What did this pull request do?

Fix USING ?::? sql for boolean field on AlterColumn func

Without this, it will report error as I described in the issue https://github.com/go-gorm/postgres/issues/164

User Case Description

If we want to change column from smallint to boolean, it will report error like below:

2023/02/28 14:03:35 /home/user/go/pkg/mod/gorm.io/driver/postgres@v1.4.8/migrator.go:318 ERROR: cannot cast type smallint to boolean (SQLSTATE 42846)
[0.374ms] [rows:0] ALTER TABLE "column_structs" ALTER COLUMN "is_active" TYPE boolean USING "is_active"::boolean

We only need to fix sql become USING xxx::int::boolean for boolean field in func AlterColumn in postgres@v1.4.8/migrator.go

jeffry-luqman commented 1 year ago

In fact, it should throw an error if the default rules fail, instead of us defining the conversion rules.

for example

type ColumnStruct struct {
name string
IsActive string // set IsActive to "true" and alter column using `::INT::boolean`
}

OK, I just sent the fix with this sql, please review again

ALTER TABLE "column_structs" ALTER COLUMN "is_active" TYPE boolean USING CASE WHEN "is_active" IN ('1','TRUE','true','T','t') THEN true ELSE false END
jeffry-luqman commented 1 year ago

ok, I've fixed it

jeffry-luqman commented 1 year ago

if there's nothing else to be fixed, can it be merged @a631807682 @jinzhu , please?

jeffry-luqman commented 1 year ago

close #164 at v1.5.0