This repository has been archived by the owner on Nov 5, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
postgres.go
133 lines (117 loc) · 2.82 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
// abstractions for working with postgres databases
package main
import (
"database/sql"
"fmt"
"github.com/gchaincl/dotsql"
"time"
_ "github.com/lib/pq"
)
// this interface unifies both *sql.Row & *sql.Rows
type sqlScannable interface {
Scan(...interface{}) error
}
// sqlQuerable unifies both *sql.DB & *sql.Tx for querying purposes
type sqlQueryable interface {
Query(query string, args ...interface{}) (*sql.Rows, error)
QueryRow(query string, args ...interface{}) *sql.Row
}
type sqlExecable interface {
Exec(query string, args ...interface{}) (sql.Result, error)
}
type sqlQueryExecable interface {
sqlQueryable
sqlExecable
}
func connectToAppDb() {
var err error
fmt.Println("connecting to db")
for i := 0; i < 1000; i++ {
appDB, err = SetupConnection(cfg.PostgresDbUrl)
if err != nil {
fmt.Println(err)
time.Sleep(time.Second)
continue
}
fmt.Println("connected to db")
if err := initializeDatabase(appDB); err != nil {
fmt.Println(err.Error())
}
break
}
}
// Sets up a connection with a given postgres db connection string
func SetupConnection(connString string) (db *sql.DB, err error) {
db, err = sql.Open("postgres", connString)
if err != nil {
return
}
if err = db.Ping(); err != nil {
return
}
return
}
// WARNING - THIS ZAPS WHATEVER DB IT'S GIVEN. DO NOT CALL THIS SHIT.
// used for testing only, returns a teardown func
func initializeDatabase(db *sql.DB) error {
var err error
// test query to check for database schema existence
var exists bool
if err = db.QueryRow("select exists(select * from primers limit 1)").Scan(&exists); err == nil {
return nil
}
fmt.Println("initializing database with base test data")
schema, err := dotsql.LoadFromFile(packagePath("/sql/schema.sql"))
if err != nil {
return err
}
for _, cmd := range []string{
"drop-all",
"create-primers",
"create-sources",
"create-urls",
"create-links",
"create-metadata",
"create-snapshots",
"create-collections",
"create-archive_requests",
"create-uncrawlables",
"create-collection_items",
} {
if _, err := schema.Exec(db, cmd); err != nil {
fmt.Println(cmd, "error:", err)
return err
}
}
if err := insertTestData(
appDB,
"primers",
"sources",
"urls",
"links",
"metadata",
"snapshots",
"collections",
"archive_requests",
"uncrawlables",
"collection_items",
); err != nil {
return err
}
return nil
}
// drops test data tables & re-inserts base data from sql/test_data.sql, based on
// passed in table names
func insertTestData(db *sql.DB, tables ...string) error {
schema, err := dotsql.LoadFromFile(packagePath("sql/test_data.sql"))
if err != nil {
return err
}
for _, t := range tables {
if _, err := schema.Exec(db, fmt.Sprintf("insert-%s", t)); err != nil {
err = fmt.Errorf("error insert-%s: %s", t, err.Error())
return err
}
}
return nil
}