• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

cybertec-postgresql / pgwatch3 / 8847643207

26 Apr 2024 11:35AM UTC coverage: 9.534% (+1.5%) from 8.047%
8847643207

push

github

web-flow
[*] improve `--help` output (#436)

[*] improve `--help` output
[*] remove `--version` command option
[*] print help message for any error

3 of 5 new or added lines in 2 files covered. (60.0%)

478 existing lines in 14 files now uncovered.

434 of 4552 relevant lines covered (9.53%)

0.11 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/src/sources/postgres.go
1
package sources
2

3
import (
4
        "context"
5

6
        "github.com/cybertec-postgresql/pgwatch3/db"
7
        pgx "github.com/jackc/pgx/v5"
8
)
9

UNCOV
10
func NewPostgresSourcesReaderWriter(ctx context.Context, conn db.PgxPoolIface) (ReaderWriter, error) {
×
UNCOV
11
        return &dbSourcesReaderWriter{
×
UNCOV
12
                ctx:      ctx,
×
UNCOV
13
                configDb: conn,
×
UNCOV
14
        }, conn.Ping(ctx)
×
UNCOV
15

×
16
}
×
17

18
type dbSourcesReaderWriter struct {
19
        ctx      context.Context
20
        configDb db.PgxIface
21
}
22

23
func (r *dbSourcesReaderWriter) WriteMonitoredDatabases(dbs MonitoredDatabases) error {
×
24
        tx, err := r.configDb.Begin(context.Background())
×
25
        if err != nil {
×
UNCOV
26
                return err
×
UNCOV
27
        }
×
UNCOV
28
        if _, err = tx.Exec(context.Background(), `truncate pgwatch3.source`); err != nil {
×
UNCOV
29
                return err
×
UNCOV
30
        }
×
UNCOV
31
        defer func() { _ = tx.Rollback(context.Background()) }()
×
UNCOV
32
        for _, md := range dbs {
×
UNCOV
33
                if err = updateDatabase(tx, md); err != nil {
×
34
                        return err
×
35
                }
×
36
        }
37
        return tx.Commit(context.Background())
×
38
}
39

40
func updateDatabase(conn db.PgxIface, md *MonitoredDatabase) (err error) {
×
41
        sql := `insert into pgwatch3.source(
×
42
name, "group", dbtype, connstr, config, config_standby, preset_config, 
×
43
preset_config_standby, is_superuser, include_pattern, exclude_pattern, custom_tags, host_config, only_if_master) 
×
44
values ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12, $13, $14) on conflict (name) do update set
×
45
"group" = $2, dbtype = $3, connstr = $4, config = $5, config_standby = $6, preset_config = $7,
×
46
preset_config_standby = $8, is_superuser = $9, include_pattern = $10, exclude_pattern = $11, custom_tags = $12,
×
47
host_config = $13, only_if_master = $14`
×
48
        _, err = conn.Exec(context.Background(), sql,
×
49
                md.DBUniqueName, md.Group, md.Kind,
×
50
                md.ConnStr, md.Metrics, md.MetricsStandby, md.PresetMetrics, md.PresetMetricsStandby,
×
51
                md.IsSuperuser, md.IncludePattern, md.ExcludePattern, md.CustomTags,
×
52
                md.HostConfig, md.OnlyIfMaster)
×
53
        return err
×
54
}
×
55

56
func (r *dbSourcesReaderWriter) UpdateDatabase(md *MonitoredDatabase) error {
×
57
        return updateDatabase(r.configDb, md)
×
58
}
×
59

60
func (r *dbSourcesReaderWriter) DeleteDatabase(name string) error {
×
61
        _, err := r.configDb.Exec(context.Background(), `delete from pgwatch3.source where name = $1`, name)
×
62
        return err
×
63
}
×
64

65
func (r *dbSourcesReaderWriter) GetMonitoredDatabases() (dbs MonitoredDatabases, err error) {
×
UNCOV
66
        sqlLatest := `select /* pgwatch3_generated */
×
67
        name, 
×
UNCOV
68
        "group", 
×
UNCOV
69
        dbtype, 
×
UNCOV
70
        connstr,
×
71
        coalesce(config, '{}'::jsonb) as config, 
×
72
        coalesce(config_standby, '{}'::jsonb) as config_standby,
×
73
        coalesce(preset_config, '') as preset_config,
×
74
        coalesce(preset_config_standby, '') as preset_config_standby,
×
75
        is_superuser,
×
76
        coalesce(include_pattern, '') as include_pattern, 
×
77
        coalesce(exclude_pattern, '') as exclude_pattern,
×
78
        coalesce(custom_tags, '{}'::jsonb) as custom_tags, 
×
79
        coalesce(host_config, '{}') as host_config, 
×
80
        only_if_master,
×
81
        is_enabled
×
82
from
×
83
        pgwatch3.source
×
84
`
×
85
        rows, err := r.configDb.Query(context.Background(), sqlLatest)
×
86
        if err != nil {
×
87
                return nil, err
×
88
        }
×
89
        dbs, err = pgx.CollectRows[*MonitoredDatabase](rows, pgx.RowToAddrOfStructByNameLax)
×
90
        return
×
91
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc