-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdatabase.go
More file actions
127 lines (98 loc) · 3.02 KB
/
database.go
File metadata and controls
127 lines (98 loc) · 3.02 KB
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
package database
import (
"context"
"crypto/tls"
"fmt"
"io"
"strings"
"github.com/aweris/postgres-data-dump/internal/log"
"github.com/go-pg/pg/extra/pgotel"
"github.com/go-pg/pg/v10"
"github.com/pkg/errors"
)
// DB wrapper interface for the postgres database.
type DB interface {
// GetTableColumns returns column names for the given table
GetTableColumns(table string) ([]string, error)
// GetTableDependencies returns dependent tables for the given table
GetTableDependencies(table string) ([]string, error)
// CopyTo copy data from a table to io.Writer
CopyTo(w io.Writer, table string) error
}
type db struct {
pgdb *pg.DB
logger log.Logger
}
// ConnectDB connects to a database using provided options.
func ConnectDB(logger log.Logger, cfg *Config) (DB, error) {
var tlsConfig tls.Config
if cfg.EnableSSL {
serverName := strings.Split(cfg.Addr, ":")[0]
tlsConfig = tls.Config{ServerName: serverName}
}
pgdb := pg.Connect(
&pg.Options{
Addr: cfg.Addr,
User: cfg.User,
Password: cfg.Password,
Database: cfg.Database,
MaxRetries: cfg.MaxRetries,
DialTimeout: cfg.DialTimeout,
ReadTimeout: cfg.ReadTimeout,
TLSConfig: &tlsConfig,
},
)
// Enable tracing
pgdb.AddQueryHook(pgotel.TracingHook{})
if err := pgdb.Ping(context.Background()); err != nil {
return nil, errors.Wrap(err, "failed to connect database")
}
logger.Debug("msg", "connected to the database", "database", cfg.Database, "user", cfg.User)
return &db{pgdb: pgdb, logger: logger}, nil
}
func (d *db) GetTableColumns(table string) ([]string, error) {
var model []struct{ Name string }
sql := `
SELECT attname as name
FROM pg_catalog.pg_attribute
WHERE attrelid = ?::regclass
AND attnum > 0
AND attisdropped = FALSE
ORDER BY attnum
`
if _, err := d.pgdb.Query(&model, sql, table); err != nil {
d.logger.Error("msg", "failed to get table columns", "table", table, "err", err)
return nil, errors.Wrap(err, "failed to get table columns")
}
cols := make([]string, 0)
for _, v := range model {
cols = append(cols, v.Name)
}
d.logger.Debug("msg", "get table columns", "table", table, "cols", strings.Join(cols, ","))
return cols, nil
}
func (d *db) GetTableDependencies(table string) ([]string, error) {
var model []struct{ Name string }
sql := `
SELECT confrelid::regclass AS name
FROM pg_catalog.pg_constraint
WHERE conrelid = ?::regclass
AND contype = 'f'
`
if _, err := d.pgdb.Query(&model, sql, table); err != nil {
d.logger.Error("msg", "failed to get table dependencies", "table", table, "err", err)
return nil, errors.Wrap(err, "failed to get table dependencies")
}
tables := make([]string, 0)
for _, v := range model {
tables = append(tables, v.Name)
}
d.logger.Debug("msg", "get table dependencies", "table", table, "dependencies", strings.Join(tables, ","))
return tables, nil
}
func (d *db) CopyTo(w io.Writer, table string) error {
if _, err := d.pgdb.CopyTo(w, fmt.Sprintf("COPY %s TO STDOUT", table)); err != nil {
return err
}
return nil
}