go-gorm / postgres

GORM PostgreSQL driver
MIT License
225 stars 119 forks source link

Fix same type comparing #261

Closed 0marq closed 5 months ago

0marq commented 5 months ago

fieldColumnType.DatabaseTypeName() returns non capitalized string, while fileType.SQL returns capitalized string. This commit aims to fix this and avoid detecting a type change when the type did not change.

What did this pull request do?

Fix same type comparing when running auto-migrate

User Case Description

When running auto migrate, we detected we ended up attempting to execute the statement.

ALTER TABLE "myTable" ALTER COLUMN "myColumn" TYPE JSONB USING "myColumn"::JSONB

This resulted in an error since our database (CockroachDB) does not allow type alter statements on JSONB columns.

After reviewing the code and debugging, I noticed that this behaviour was due to this comparison.
fieldColumnType.DatabaseTypeName() returns non capitalised string, while fileType.SQL returns capitalised string.
After using strings.ToUpper on fieldColumnType.DatabaseTypeName() before comparing it to fileType.SQL, this type change is no longer detected and auto-migrate succeeds.

This PR aims to fix this and avoid detecting a type change when the type did not change.

0marq commented 5 months ago

Maybe I could call strings.ToUpper on both expressions, instead of only on fieldColumnType.DatabaseTypeName. I'll wait for your opinion before. :)