From aa2262cdd2cfcdeddac1e378d861d5771946041b Mon Sep 17 00:00:00 2001 From: piyongcai Date: Thu, 26 May 2022 10:56:32 +0800 Subject: [PATCH 1/3] FIX DataType Defind 1. because select schema from db, return int2,int4,int8. but code name is smallint,integer,bigint. it will cause alter column. a. smallint -> int2 b. integer -> int4 c. bigint -> int8 2. in postgresql, set timestamptz Precision == 0, This means being accurate to the seconds. --- postgres.go | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/postgres.go b/postgres.go index c0fdea0..2a9ab86 100644 --- a/postgres.go +++ b/postgres.go @@ -6,7 +6,6 @@ import ( "regexp" "strconv" - "github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4/stdlib" "gorm.io/gorm" "gorm.io/gorm/callbacks" @@ -165,11 +164,11 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string { } else { switch { case size <= 16: - return "smallint" + return "int2" case size <= 32: - return "integer" + return "int4" default: - return "bigint" + return "int8" } } case schema.Float: @@ -186,10 +185,8 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string { } return "text" case schema.Time: - if field.Precision > 0 { - return fmt.Sprintf("timestamptz(%d)", field.Precision) - } - return "timestamptz" + // in postgresql, Precision == 0, This means being accurate to the seconds. + return fmt.Sprintf("timestamptz(%d)", field.Precision) case schema.Bytes: return "bytea" } @@ -210,11 +207,11 @@ func (dialectopr Dialector) RollbackTo(tx *gorm.DB, name string) error { func getSerialDatabaseType(s string) (dbType string, ok bool) { switch s { case "smallserial": - return "smallint", true + return "int2", true case "serial": - return "integer", true + return "int4", true case "bigserial": - return "bigint", true + return "int8", true default: return "", false } From 22be74d0d099c1197035876aed99d0cc40af5478 Mon Sep 17 00:00:00 2001 From: piyongcai Date: Thu, 26 May 2022 10:58:57 +0800 Subject: [PATCH 2/3] recover import --- postgres.go | 1 + 1 file changed, 1 insertion(+) diff --git a/postgres.go b/postgres.go index 2a9ab86..72ecad8 100644 --- a/postgres.go +++ b/postgres.go @@ -6,6 +6,7 @@ import ( "regexp" "strconv" + "github.com/jackc/pgx/v4" "github.com/jackc/pgx/v4/stdlib" "gorm.io/gorm" "gorm.io/gorm/callbacks" From c39b6867f39bf56739ec8cd9b6d3a1f862266633 Mon Sep 17 00:00:00 2001 From: piyongcai Date: Tue, 31 May 2022 14:39:18 +0800 Subject: [PATCH 3/3] fix timestamptz precision range in [0, 6] --- postgres.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/postgres.go b/postgres.go index 72ecad8..7c59701 100644 --- a/postgres.go +++ b/postgres.go @@ -187,7 +187,10 @@ func (dialector Dialector) DataTypeOf(field *schema.Field) string { return "text" case schema.Time: // in postgresql, Precision == 0, This means being accurate to the seconds. - return fmt.Sprintf("timestamptz(%d)", field.Precision) + if field.Precision >= 0 && field.Precision <= 6 { + return fmt.Sprintf("timestamptz(%d)", field.Precision) + } + return "timestamptz" case schema.Bytes: return "bytea" }