Skip to content

Commit 1ffb5a7

Browse files
authored
feat: support dynamic timestamp ScanLocation via DSN timezone (#311)
* feat: support dynamic timestamp ScanLocation via DSN timezone * chore: raise minimum Go version to support newer pgx
1 parent 8385c4a commit 1ffb5a7

File tree

4 files changed

+29
-13
lines changed

4 files changed

+29
-13
lines changed

.github/workflows/tests.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ jobs:
1111
run-tests:
1212
strategy:
1313
matrix:
14-
go: ['1.19']
14+
go: ['1.20']
1515
platform: [ubuntu-latest]
1616
runs-on: ubuntu-latest
1717

go.mod

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
11
module gorm.io/driver/postgres
22

3-
go 1.19
3+
go 1.20
44

55
require (
6-
github.com/jackc/pgx/v5 v5.5.5
6+
github.com/jackc/pgx/v5 v5.6.0
77
gorm.io/gorm v1.25.10
88
)
99

1010
require (
1111
github.com/jackc/pgpassfile v1.0.0 // indirect
12-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a // indirect
13-
github.com/jackc/puddle/v2 v2.2.1 // indirect
12+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 // indirect
13+
github.com/jackc/puddle/v2 v2.2.2 // indirect
1414
github.com/jinzhu/inflection v1.0.0 // indirect
1515
github.com/jinzhu/now v1.1.5 // indirect
1616
golang.org/x/crypto v0.31.0 // indirect

go.sum

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@ github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
22
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
33
github.com/jackc/pgpassfile v1.0.0 h1:/6Hmqy13Ss2zCq62VdNG8tM1wchn8zjSGOBJ6icpsIM=
44
github.com/jackc/pgpassfile v1.0.0/go.mod h1:CEx0iS5ambNFdcRtxPj5JhEz+xB6uRky5eyVu/W2HEg=
5-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a h1:bbPeKD0xmW/Y25WS6cokEszi5g+S0QxI/d45PkRi7Nk=
6-
github.com/jackc/pgservicefile v0.0.0-20221227161230-091c0ba34f0a/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
7-
github.com/jackc/pgx/v5 v5.5.5 h1:amBjrZVmksIdNjxGW/IiIMzxMKZFelXbUoPNb+8sjQw=
8-
github.com/jackc/pgx/v5 v5.5.5/go.mod h1:ez9gk+OAat140fv9ErkZDYFWmXLfV+++K0uAOiwgm1A=
9-
github.com/jackc/puddle/v2 v2.2.1 h1:RhxXJtFG022u4ibrCSMSiu5aOq1i77R3OHKNJj77OAk=
10-
github.com/jackc/puddle/v2 v2.2.1/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
5+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761 h1:iCEnooe7UlwOQYpKFhBabPMi4aNAfoODPEFNiAnClxo=
6+
github.com/jackc/pgservicefile v0.0.0-20240606120523-5a60cdf6a761/go.mod h1:5TJZWKEWniPve33vlWYSoGYefn3gLQRzjfDlhSJ9ZKM=
7+
github.com/jackc/pgx/v5 v5.6.0 h1:SWJzexBzPL5jb0GEsrPMLIsi/3jOo7RHlzTjcAeDrPY=
8+
github.com/jackc/pgx/v5 v5.6.0/go.mod h1:DNZ/vlrUnhWCoFGxHAG8U2ljioxukquj7utPDgtQdTw=
9+
github.com/jackc/puddle/v2 v2.2.2 h1:PR8nw+E/1w0GLuRFSmiioY6UooMp6KJv0/61nB7icHo=
10+
github.com/jackc/puddle/v2 v2.2.2/go.mod h1:vriiEXHvEE654aYKXXjOvZM39qJ0q+azkZFrfEOc3H4=
1111
github.com/jinzhu/inflection v1.0.0 h1:K317FqzuhWc8YvSVlFMCCUb36O/S9MCKRDI7QkRKD/E=
1212
github.com/jinzhu/inflection v1.0.0/go.mod h1:h+uFLlag+Qp1Va5pdKtLDYj+kHp5pxUVkryuEj+Srlc=
1313
github.com/jinzhu/now v1.1.5 h1:/o9tlHleP7gOFmsnYNz3RGnqzefHA47wQpKrrdTIwXQ=

postgres.go

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package postgres
22

33
import (
4+
"context"
45
"database/sql"
56
"fmt"
67
"regexp"
78
"strconv"
89
"strings"
10+
"time"
911

1012
"github.com/jackc/pgx/v5"
13+
"github.com/jackc/pgx/v5/pgtype"
1114
"github.com/jackc/pgx/v5/stdlib"
1215
"gorm.io/gorm"
1316
"gorm.io/gorm/callbacks"
@@ -31,7 +34,7 @@ type Config struct {
3134
}
3235

3336
var (
34-
timeZoneMatcher = regexp.MustCompile("(time_zone|TimeZone)=(.*?)($|&| )")
37+
timeZoneMatcher = regexp.MustCompile("(time_zone|TimeZone|timezone)=(.*?)($|&| )")
3538
defaultIdentifierLength = 63 //maximum identifier length for postgres
3639
)
3740

@@ -99,10 +102,23 @@ func (dialector Dialector) Initialize(db *gorm.DB) (err error) {
99102
config.DefaultQueryExecMode = pgx.QueryExecModeSimpleProtocol
100103
}
101104
result := timeZoneMatcher.FindStringSubmatch(dialector.Config.DSN)
105+
var options []stdlib.OptionOpenDB
102106
if len(result) > 2 {
103107
config.RuntimeParams["timezone"] = result[2]
108+
options = append(options, stdlib.OptionAfterConnect(func(ctx context.Context, conn *pgx.Conn) error {
109+
loc, tzErr := time.LoadLocation(result[2])
110+
if tzErr != nil {
111+
return tzErr
112+
}
113+
conn.TypeMap().RegisterType(&pgtype.Type{
114+
Name: "timestamp",
115+
OID: pgtype.TimestampOID,
116+
Codec: &pgtype.TimestampCodec{ScanLocation: loc},
117+
})
118+
return nil
119+
}))
104120
}
105-
db.ConnPool = stdlib.OpenDB(*config)
121+
db.ConnPool = stdlib.OpenDB(*config, options...)
106122
}
107123
return
108124
}

0 commit comments

Comments
 (0)