Skip to content

Commit

Permalink
chore: cleanup
Browse files Browse the repository at this point in the history
Signed-off-by: Jaime Silvela <[email protected]>
  • Loading branch information
jsilvela committed Dec 8, 2024
1 parent 124ce0d commit f0f9295
Show file tree
Hide file tree
Showing 8 changed files with 30 additions and 38 deletions.
33 changes: 18 additions & 15 deletions internal/management/controller/database_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ import (
. "github.com/onsi/gomega"
)

const databaseDetectionQuery = `SELECT count(*)
FROM pg_database
WHERE datname = $1`

var _ = Describe("Managed Database status", func() {
var (
dbMock sqlmock.Sqlmock
Expand Down Expand Up @@ -116,15 +120,15 @@ var _ = Describe("Managed Database status", func() {
func() {
// Mocking DetectDB
expectedValue := sqlmock.NewRows([]string{""}).AddRow("0")
dbMock.ExpectQuery(`SELECT count(*)
FROM pg_database
WHERE datname = $1`).WithArgs(database.Spec.Name).WillReturnRows(expectedValue)
dbMock.ExpectQuery(databaseDetectionQuery).WithArgs(database.Spec.Name).
WillReturnRows(expectedValue)

// Mocking CreateDB
expectedCreate := sqlmock.NewResult(0, 1)
expectedQuery := fmt.Sprintf(
"CREATE DATABASE %s OWNER %s",
pgx.Identifier{database.Spec.Name}.Sanitize(), pgx.Identifier{database.Spec.Owner}.Sanitize(),
pgx.Identifier{database.Spec.Name}.Sanitize(),
pgx.Identifier{database.Spec.Owner}.Sanitize(),
)
dbMock.ExpectExec(expectedQuery).WillReturnResult(expectedCreate)
},
Expand All @@ -142,9 +146,8 @@ var _ = Describe("Managed Database status", func() {
func() {
// Mocking DetectDB
expectedValue := sqlmock.NewRows([]string{""}).AddRow("1")
dbMock.ExpectQuery(`SELECT count(*)
FROM pg_database
WHERE datname = $1`).WithArgs(database.Spec.Name).WillReturnRows(expectedValue)
dbMock.ExpectQuery(databaseDetectionQuery).WithArgs(database.Spec.Name).
WillReturnRows(expectedValue)

// Mocking Alter Database
expectedQuery := fmt.Sprintf("ALTER DATABASE %s OWNER TO %s",
Expand All @@ -166,15 +169,15 @@ var _ = Describe("Managed Database status", func() {
func() {
// Mocking DetectDB
expectedValue := sqlmock.NewRows([]string{""}).AddRow("0")
dbMock.ExpectQuery(`SELECT count(*)
FROM pg_database
WHERE datname = $1`).WithArgs(database.Spec.Name).WillReturnRows(expectedValue)
dbMock.ExpectQuery(databaseDetectionQuery).WithArgs(database.Spec.Name).
WillReturnRows(expectedValue)

// Mocking CreateDB
expectedCreate := sqlmock.NewResult(0, 1)
expectedQuery := fmt.Sprintf(
"CREATE DATABASE %s OWNER %s",
pgx.Identifier{database.Spec.Name}.Sanitize(), pgx.Identifier{database.Spec.Owner}.Sanitize(),
pgx.Identifier{database.Spec.Name}.Sanitize(),
pgx.Identifier{database.Spec.Owner}.Sanitize(),
)
dbMock.ExpectExec(expectedQuery).WillReturnResult(expectedCreate)

Expand All @@ -197,15 +200,15 @@ var _ = Describe("Managed Database status", func() {
func() {
// Mocking DetectDB
expectedValue := sqlmock.NewRows([]string{""}).AddRow("0")
dbMock.ExpectQuery(`SELECT count(*)
FROM pg_database
WHERE datname = $1`).WithArgs(database.Spec.Name).WillReturnRows(expectedValue)
dbMock.ExpectQuery(databaseDetectionQuery).WithArgs(database.Spec.Name).
WillReturnRows(expectedValue)

// Mocking CreateDB
expectedCreate := sqlmock.NewResult(0, 1)
expectedQuery := fmt.Sprintf(
"CREATE DATABASE %s OWNER %s",
pgx.Identifier{database.Spec.Name}.Sanitize(), pgx.Identifier{database.Spec.Owner}.Sanitize(),
pgx.Identifier{database.Spec.Name}.Sanitize(),
pgx.Identifier{database.Spec.Owner}.Sanitize(),
)
dbMock.ExpectExec(expectedQuery).WillReturnResult(expectedCreate)
},
Expand Down
13 changes: 3 additions & 10 deletions internal/management/controller/generic_controller_asserts.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package controller

import (
"context"
"fmt"

g "github.com/onsi/gomega"
apierrors "k8s.io/apimachinery/pkg/api/errors"
Expand All @@ -19,11 +18,11 @@ type postgresObjectManager interface {
SetObservedGeneration(gen int64)
}

// assertObjectWasReconciled reconciles the object and retrieves its updtate
// assertObjectWasReconciled reconciles the object and retrieves its update
// from kubernetes
//
// NOTE: in the `newObj` argument, simply pass an empty struct of the type T
// you are testig (e.g. &apiv1.Database{}), as this will be populated in the
// you are testing (e.g. &apiv1.Database{}), as this will be populated in the
// kubernetes Get() calls
func assertObjectWasReconciled[T postgresObjectManager](
ctx context.Context,
Expand All @@ -49,12 +48,6 @@ func assertObjectWasReconciled[T postgresObjectManager](
Namespace: obj.GetNamespace(),
Name: obj.GetName(),
}, newObj)

errstr := fmt.Sprintf("err: %#v\n", err)
_ = errstr
kind := obj.GetObjectKind().GroupVersionKind()
g.Expect(kind).NotTo(g.BeNil())

g.Expect(err).ToNot(g.HaveOccurred())

updatedObjectExpectations(newObj)
Expand All @@ -67,7 +60,7 @@ func assertObjectWasReconciled[T postgresObjectManager](
// - a second reconciliation deletes the finalizers and *may* perform DROPs in Postgres
//
// NOTE: in the `newObj` argument, simply pass an empty struct of the type T
// you are testig (e.g. &apiv1.Database{}), as this will be populated in the
// you are testing (e.g. &apiv1.Database{}), as this will be populated in the
// kubernetes Get() calls
func assertObjectReconciledAfterDeletion[T postgresObjectManager](
ctx context.Context,
Expand Down
4 changes: 2 additions & 2 deletions internal/management/controller/instance_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ func (r *InstanceReconciler) Reconcile(

r.configureSlotReplicator(cluster)

postgresDB, err := r.instance.ConnectionPool().Connection("postgres")
postgresDB, err := r.instance.GetNamedDB("postgres")
if err != nil {
return reconcile.Result{}, fmt.Errorf("while getting the postgres connection: %w", err)
}
Expand Down Expand Up @@ -573,7 +573,7 @@ func (r *InstanceReconciler) reconcileDatabases(ctx context.Context, cluster *ap

databases, errors := r.getAllAccessibleDatabases(ctx, db)
for _, databaseName := range databases {
db, err := r.instance.ConnectionPool().Connection(databaseName)
db, err := r.instance.GetNamedDB(databaseName)
if err != nil {
errors = append(errors,
fmt.Errorf("could not connect to database %s: %w", databaseName, err))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,9 @@ import (
. "github.com/onsi/gomega"
)

const (
publicationDetectionQuery = `SELECT count(*)
const publicationDetectionQuery = `SELECT count(*)
FROM pg_publication
WHERE pubname = $1`
)

var _ = Describe("Managed publication controller tests", func() {
var (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,9 @@ import (
. "github.com/onsi/gomega"
)

const (
subscriptionDetectionQuery = `SELECT count(*)
const subscriptionDetectionQuery = `SELECT count(*)
FROM pg_subscription
WHERE subname = $1`
)

var _ = Describe("Managed subscription controller tests", func() {
var (
Expand Down
2 changes: 1 addition & 1 deletion pkg/management/postgres/initdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,7 +355,7 @@ func (info InitInfo) ConfigureNewInstance(instance *Instance) error {
if err != nil {
return fmt.Errorf("could not create ApplicationDatabase: %w", err)
}
appDB, err := instance.ConnectionPool().Connection(info.ApplicationDatabase)
appDB, err := instance.GetNamedDB(info.ApplicationDatabase)
if err != nil {
return fmt.Errorf("could not get connection to ApplicationDatabase: %w", err)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/management/postgres/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -721,12 +721,12 @@ func (instance *Instance) WithActiveInstance(inner func() error) error {

// GetSuperUserDB gets a connection to the "postgres" database on this instance
func (instance *Instance) GetSuperUserDB() (*sql.DB, error) {
return instance.ConnectionPool().Connection("postgres")
return instance.GetNamedDB("postgres")
}

// GetTemplateDB gets a connection to the "template1" database on this instance
func (instance *Instance) GetTemplateDB() (*sql.DB, error) {
return instance.ConnectionPool().Connection("template1")
return instance.GetNamedDB("template1")
}

// GetNamedDB gets a connection to the named database on this instance
Expand Down
4 changes: 2 additions & 2 deletions pkg/management/postgres/metrics/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (q *QueriesCollector) collectUserQueries(ch chan<- prometheus.Metric) error

allTargetDatabases := q.expandTargetDatabases(targetDatabases, allAccessibleDatabasesCache)
for targetDatabase := range allTargetDatabases {
conn, err := q.instance.ConnectionPool().Connection(targetDatabase)
conn, err := q.instance.GetNamedDB(targetDatabase)
if err != nil {
q.reportUserQueryErrorMetric(name + ": " + err.Error())
continue
Expand Down Expand Up @@ -207,7 +207,7 @@ func (q QueriesCollector) expandTargetDatabases(
}

func (q QueriesCollector) getAllAccessibleDatabases() ([]string, error) {
conn, err := q.instance.ConnectionPool().Connection(q.defaultDBName)
conn, err := q.instance.GetNamedDB(q.defaultDBName)
if err != nil {
return nil, fmt.Errorf("while connecting to expand target_database *: %w", err)
}
Expand Down

0 comments on commit f0f9295

Please sign in to comment.