go-gorm / postgres

GORM PostgreSQL driver
MIT License
228 stars 120 forks source link

[CockroachDB] Fixing how it handle fields DEFAULT values and column types #143

Closed rwrz closed 1 year ago

rwrz commented 1 year ago

What did this pull request do?

Currently, CockroachDB returns :::int8 together with their default values. We have a regex to remove it, that solves issues with PostgreSQL values, but it doesn't solve cockroachdb ones. So, I'm adding another regex to fix it. Also, column field types has aliases, and the migrator check them to decide if we need to ALTER a column or not. But the driver is not checking it, so, I'm adding this alias check as well.

User Case Description

You can test this this model:

type Example struct {
    Code   int    `gorm:"default:0"`
}

It would trigger the ALTER COLUMN every time since int returns int8 and wasn't considering the alias to bigint. Also, it would have issues with the default:0, since it would check against 0:::int8 and 0.

rwrz commented 1 year ago

I've merged the regex, but forgot to remove another fix that I'm using here. This fix is to consider the "field type aliases" when checking if the column type has changed. Mostly because we rely on those aliases for cockroachdb compatibility, but I believe it is also important for PostgreSQL.