Skip to content

Commit

Permalink
[-] fix syncing changed sources from configuration, fixes #594 (#597)
Browse files Browse the repository at this point in the history
  • Loading branch information
pashagolub authored Dec 16, 2024
1 parent 6120170 commit acb79bb
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 1 deletion.
12 changes: 11 additions & 1 deletion internal/sources/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package sources
import (
"context"
"maps"
"reflect"
"slices"

"github.com/cybertec-postgresql/pgwatch/v3/internal/db"
Expand Down Expand Up @@ -166,9 +167,18 @@ func (mds MonitoredDatabases) SyncFromReader(r Reader) (newmds MonitoredDatabase
}
newmds, err = srcs.ResolveDatabases()
for _, newMD := range newmds {
if md := mds.GetMonitoredDatabase(newMD.Name); md != nil {
md := mds.GetMonitoredDatabase(newMD.Name)
if md == nil {
continue
}
if reflect.DeepEqual(md.Source, newMD.Source) {
// keep the existing connection if the source is the same
newMD.Conn = md.Conn
newMD.ConnConfig = md.ConnConfig
continue
}
if md.Conn != nil {
md.Conn.Close()
}
}
return newmds, err
Expand Down
55 changes: 55 additions & 0 deletions internal/sources/types_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"testing"
"time"

"github.com/pashagolub/pgxmock/v4"
"github.com/stretchr/testify/assert"

"github.com/cybertec-postgresql/pgwatch/v3/internal/sources"
Expand Down Expand Up @@ -114,3 +115,57 @@ func TestMonitoredDatabase_IsPostgresSource(t *testing.T) {
md.Kind = sources.SourcePatroni
assert.True(t, md.IsPostgresSource(), "IsPostgresSource() = false, want true")
}

type testSourceReader struct {
sources.Sources
error
}

func (r testSourceReader) GetSources() (sources.Sources, error) {
return r.Sources, r.error
}

func TestMonitoredDatabases_SyncFromReader_error(t *testing.T) {
reader := testSourceReader{error: assert.AnError}
mds := sources.MonitoredDatabases{}
_, err := mds.SyncFromReader(reader)
assert.Error(t, err)
}

func TestMonitoredDatabases_SyncFromReader(t *testing.T) {
db, _ := pgxmock.NewPool()
src := sources.Source{
Name: "test",
Kind: sources.SourcePostgres,
IsEnabled: true,
ConnStr: "postgres://user:password@localhost:5432/mydatabase",
}
reader := testSourceReader{Sources: sources.Sources{src}}
// first read the sources
mds, _ := reader.GetSources()
assert.NotNil(t, mds, "GetSources() = nil, want not nil")
// then resolve the databases
mdbs, _ := mds.ResolveDatabases()
assert.NotNil(t, mdbs, "ResolveDatabases() = nil, want not nil")
// pretend that we have a connection
mdbs[0].Conn = db
db.ExpectClose()
// sync the databases and make sure they are the same
newmdbs, _ := mdbs.SyncFromReader(reader)
assert.NotNil(t, newmdbs)
assert.Equal(t, mdbs[0].ConnStr, newmdbs[0].ConnStr)
assert.Equal(t, db, newmdbs[0].Conn)
// change the connection string and check if databases are updated
reader.Sources[0].ConnStr = "postgres://user:password@localhost:5432/anotherdatabase"
newmdbs, _ = mdbs.SyncFromReader(reader)
assert.NotNil(t, newmdbs)
assert.NotEqual(t, mdbs[0].ConnStr, newmdbs[0].ConnStr)
assert.Nil(t, newmdbs[0].Conn)
assert.NoError(t, db.ExpectationsWereMet())
// change the unique name of the source and check if it's updated
reader.Sources[0].Name = "another"
newmdbs, _ = mdbs.SyncFromReader(reader)
assert.NotNil(t, newmdbs)
assert.NotEqual(t, mdbs[0].Name, newmdbs[0].Name)
assert.Nil(t, newmdbs[0].Conn)
}

0 comments on commit acb79bb

Please sign in to comment.