Skip to content

Use the pg_type table to get valid type sizes for numeric types #61

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

Merged
merged 1 commit into from
Sep 28, 2021
Merged
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
20 changes: 14 additions & 6 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ type Column struct {
radix sql.NullInt64
scale sql.NullInt64
datetimeprecision sql.NullInt64
typlen sql.NullInt64
}

func (c Column) Name() string {
Expand All @@ -35,11 +36,16 @@ func (c Column) DatabaseTypeName() string {
}

func (c Column) Length() (length int64, ok bool) {
ok = c.maxlen.Valid
if ok {
length = c.maxlen.Int64
ok = c.typlen.Valid
if ok && c.typlen.Int64 > 0 {
length = c.typlen.Int64
} else {
length = 0
ok = c.maxlen.Valid
if ok {
length = c.maxlen.Int64
} else {
length = 0
}
}
return
}
Expand Down Expand Up @@ -298,8 +304,9 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
currentSchema, table := m.CurrentSchema(stmt, stmt.Table)
columns, err := m.DB.Raw(
"SELECT column_name, is_nullable, udt_name, character_maximum_length, "+
"numeric_precision, numeric_precision_radix, numeric_scale, datetime_precision "+
"FROM information_schema.columns WHERE table_catalog = ? AND table_schema = ? AND table_name = ?",
"numeric_precision, numeric_precision_radix, numeric_scale, datetime_precision, 8 * typlen "+
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need the 8 * typlen?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From looking at the code, the value produced by migrator.ColumnType.Length() is expected to be in bits, so we either need to multiply by 8 here or in the Length() accessor. I thought doing it in the SQL expression would be less confusing.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thank you for your pr.

"FROM information_schema.columns AS cols JOIN pg_type AS pgt ON cols.udt_name = pgt.typname "+
"WHERE table_catalog = ? AND table_schema = ? AND table_name = ?",
currentDatabase, currentSchema, table).Rows()
if err != nil {
return err
Expand All @@ -317,6 +324,7 @@ func (m Migrator) ColumnTypes(value interface{}) (columnTypes []gorm.ColumnType,
&column.radix,
&column.scale,
&column.datetimeprecision,
&column.typlen,
)
if err != nil {
return err
Expand Down