Skip to content

Commit acb79bb

Browse files
authored
[-] fix syncing changed sources from configuration, fixes #594 (#597)
1 parent 6120170 commit acb79bb

File tree

2 files changed

+66
-1
lines changed

2 files changed

+66
-1
lines changed

internal/sources/types.go

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package sources
33
import (
44
"context"
55
"maps"
6+
"reflect"
67
"slices"
78

89
"github.com/cybertec-postgresql/pgwatch/v3/internal/db"
@@ -166,9 +167,18 @@ func (mds MonitoredDatabases) SyncFromReader(r Reader) (newmds MonitoredDatabase
166167
}
167168
newmds, err = srcs.ResolveDatabases()
168169
for _, newMD := range newmds {
169-
if md := mds.GetMonitoredDatabase(newMD.Name); md != nil {
170+
md := mds.GetMonitoredDatabase(newMD.Name)
171+
if md == nil {
172+
continue
173+
}
174+
if reflect.DeepEqual(md.Source, newMD.Source) {
175+
// keep the existing connection if the source is the same
170176
newMD.Conn = md.Conn
171177
newMD.ConnConfig = md.ConnConfig
178+
continue
179+
}
180+
if md.Conn != nil {
181+
md.Conn.Close()
172182
}
173183
}
174184
return newmds, err

internal/sources/types_test.go

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"testing"
66
"time"
77

8+
"github.com/pashagolub/pgxmock/v4"
89
"github.com/stretchr/testify/assert"
910

1011
"github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
@@ -114,3 +115,57 @@ func TestMonitoredDatabase_IsPostgresSource(t *testing.T) {
114115
md.Kind = sources.SourcePatroni
115116
assert.True(t, md.IsPostgresSource(), "IsPostgresSource() = false, want true")
116117
}
118+
119+
type testSourceReader struct {
120+
sources.Sources
121+
error
122+
}
123+
124+
func (r testSourceReader) GetSources() (sources.Sources, error) {
125+
return r.Sources, r.error
126+
}
127+
128+
func TestMonitoredDatabases_SyncFromReader_error(t *testing.T) {
129+
reader := testSourceReader{error: assert.AnError}
130+
mds := sources.MonitoredDatabases{}
131+
_, err := mds.SyncFromReader(reader)
132+
assert.Error(t, err)
133+
}
134+
135+
func TestMonitoredDatabases_SyncFromReader(t *testing.T) {
136+
db, _ := pgxmock.NewPool()
137+
src := sources.Source{
138+
Name: "test",
139+
Kind: sources.SourcePostgres,
140+
IsEnabled: true,
141+
ConnStr: "postgres://user:password@localhost:5432/mydatabase",
142+
}
143+
reader := testSourceReader{Sources: sources.Sources{src}}
144+
// first read the sources
145+
mds, _ := reader.GetSources()
146+
assert.NotNil(t, mds, "GetSources() = nil, want not nil")
147+
// then resolve the databases
148+
mdbs, _ := mds.ResolveDatabases()
149+
assert.NotNil(t, mdbs, "ResolveDatabases() = nil, want not nil")
150+
// pretend that we have a connection
151+
mdbs[0].Conn = db
152+
db.ExpectClose()
153+
// sync the databases and make sure they are the same
154+
newmdbs, _ := mdbs.SyncFromReader(reader)
155+
assert.NotNil(t, newmdbs)
156+
assert.Equal(t, mdbs[0].ConnStr, newmdbs[0].ConnStr)
157+
assert.Equal(t, db, newmdbs[0].Conn)
158+
// change the connection string and check if databases are updated
159+
reader.Sources[0].ConnStr = "postgres://user:password@localhost:5432/anotherdatabase"
160+
newmdbs, _ = mdbs.SyncFromReader(reader)
161+
assert.NotNil(t, newmdbs)
162+
assert.NotEqual(t, mdbs[0].ConnStr, newmdbs[0].ConnStr)
163+
assert.Nil(t, newmdbs[0].Conn)
164+
assert.NoError(t, db.ExpectationsWereMet())
165+
// change the unique name of the source and check if it's updated
166+
reader.Sources[0].Name = "another"
167+
newmdbs, _ = mdbs.SyncFromReader(reader)
168+
assert.NotNil(t, newmdbs)
169+
assert.NotEqual(t, mdbs[0].Name, newmdbs[0].Name)
170+
assert.Nil(t, newmdbs[0].Conn)
171+
}

0 commit comments

Comments
 (0)