internal/postgres: use pgx for tests

Switch the default DB driver from postgres to pgx.

The pgx driver returns local times. That isn't against the spec (I
can't find anything about it in the documentation of database/sql),
and since changing it to UTC could break users it's unlikely to get
into pgx. Since it only affects display, we change times to UTC
wherever they are formatted.

Change-Id: I8e8d17e8b1f2456fba59182425c41db23e870f0c
Reviewed-on: https://go-review.googlesource.com/c/pkgsite/+/305530
Trust: Jonathan Amsterdam <jba@google.com>
Run-TryBot: Jonathan Amsterdam <jba@google.com>
Reviewed-by: Julie Qiu <julie@golang.org>
This commit is contained in:
Jonathan Amsterdam 2021-03-25 15:08:22 -04:00
Родитель 194473c58f
Коммит ace022fde6
3 изменённых файлов: 7 добавлений и 3 удалений

Просмотреть файл

@ -99,5 +99,9 @@ func effectiveName(pkgPath, pkgName string) string {
// absoluteTime takes a date and returns returns a human-readable, // absoluteTime takes a date and returns returns a human-readable,
// date with the format mmm d, yyyy: // date with the format mmm d, yyyy:
func absoluteTime(date time.Time) string { func absoluteTime(date time.Time) string {
return date.Format("Jan _2, 2006") // Convert to UTC because that is how the date is represented in the DB.
// (The pgx driver returns local times.) Example: if a date is stored
// as Jan 30 at midnight, then the local NYC time is on Jan 29, and this
// function would return "Jan 29" instead of the correct "Jan 30".
return date.In(time.UTC).Format("Jan _2, 2006")
} }

Просмотреть файл

@ -236,5 +236,5 @@ func elapsedTime(date time.Time) string {
return fmt.Sprintf("%d days ago", elapsedDays) return fmt.Sprintf("%d days ago", elapsedDays)
} }
return date.Format("Jan _2, 2006") return absoluteTime(date)
} }

Просмотреть файл

@ -92,7 +92,7 @@ func SetupTestDB(dbName string) (_ *DB, err error) {
} }
driver := os.Getenv("GO_DISCOVERY_DATABASE_DRIVER") driver := os.Getenv("GO_DISCOVERY_DATABASE_DRIVER")
if driver == "" { if driver == "" {
driver = "postgres" driver = "pgx"
} }
db, err := database.Open(driver, dbtest.DBConnURI(dbName), "test") db, err := database.Open(driver, dbtest.DBConnURI(dbName), "test")
if err != nil { if err != nil {