-
Notifications
You must be signed in to change notification settings - Fork 405
/
postgres.go
63 lines (51 loc) · 1.11 KB
/
postgres.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package postgres
import (
"github.com/fnproject/fn/api/datastore/sql/dbhelper"
_ "github.com/go-sql-driver/mysql"
"github.com/jmoiron/sqlx"
"github.com/lib/pq"
"net/url"
)
type postgresHelper int
func (postgresHelper) Supports(scheme string) bool {
switch scheme {
case "postgres", "pgx":
return true
}
return false
}
func (postgresHelper) PreConnect(url *url.URL) (string, error) {
return url.String(), nil
}
func (postgresHelper) PostCreate(db *sqlx.DB) (*sqlx.DB, error) {
return db, nil
}
func (postgresHelper) CheckTableExists(tx *sqlx.Tx, table string) (bool, error) {
query := tx.Rebind(`SELECT count(*)
FROM information_schema.TABLES
WHERE TABLE_NAME = 'apps'
`)
row := tx.QueryRow(query)
var count int
err := row.Scan(&count)
if err != nil {
return false, err
}
exists := count > 0
return exists, nil
}
func (postgresHelper) String() string {
return "postgres"
}
func (postgresHelper) IsDuplicateKeyError(err error) bool {
switch dbErr := err.(type) {
case *pq.Error:
if dbErr.Code == "23505" {
return true
}
}
return false
}
func init() {
dbhelper.Register(postgresHelper(0))
}