diff options
| author | Julie Qiu <julie@golang.org> | 2019-02-11 11:12:07 -0500 |
|---|---|---|
| committer | Julie Qiu <julie@golang.org> | 2020-03-27 16:46:33 -0400 |
| commit | dc3e6e940c80df9bf36c7ef973668cc1c4dea565 (patch) | |
| tree | 73fe3295153826799213b606aa7016aafbc9edea /internal/postgres/postgres.go | |
| parent | f0bb052bb476fd60346f0e6ca564024922582251 (diff) | |
| download | go-x-pkgsite-dc3e6e940c80df9bf36c7ef973668cc1c4dea565.tar.xz | |
discovery/internal/postgres: read/write to the version_logs table
The discovery/internal/postgres package is created to interact
with the discovery database. The following methods are implemented:
- LastestProxyIndexUpdate() *time.Time
- InsertNewVersionsToLog(newVersions []*internal.VersionLog) error
These will be used by the proxy index cron when fetching new versions
from the module index.
The version_log table is renamed to version_logs. This was initially
a typo.
A dependency to github.com/lib/pq v1.0.0 is introduced.
Change-Id: I720a836dc85a37a5df863e879ce6f60082c795f1
Reviewed-on: https://team-review.git.corp.google.com/c/413847
Reviewed-by: Andrew Bonventre <andybons@google.com>
Diffstat (limited to 'internal/postgres/postgres.go')
| -rw-r--r-- | internal/postgres/postgres.go | 45 |
1 files changed, 45 insertions, 0 deletions
diff --git a/internal/postgres/postgres.go b/internal/postgres/postgres.go new file mode 100644 index 00000000..cd45e6bd --- /dev/null +++ b/internal/postgres/postgres.go @@ -0,0 +1,45 @@ +// Copyright 2019 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package postgres + +import ( + "database/sql" +) + +type DB struct { + *sql.DB +} + +func Open(dbinfo string) (*DB, error) { + db, err := sql.Open("postgres", dbinfo) + if err != nil { + return nil, err + } + + if err = db.Ping(); err != nil { + return nil, err + } + return &DB{db}, nil +} + +func (db *DB) Transact(txFunc func(*sql.Tx) error) (err error) { + tx, err := db.Begin() + if err != nil { + return + } + + defer func() { + if p := recover(); p != nil { + tx.Rollback() + panic(p) + } else if err != nil { + tx.Rollback() + } else { + err = tx.Commit() + } + }() + + return txFunc(tx) +} |
