Skip to content

Commit 2b50b1d

Browse files
authored
Cleanup (#138)
1 parent e223b06 commit 2b50b1d

File tree

3 files changed

+12
-9
lines changed

3 files changed

+12
-9
lines changed

internal/api/handler.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func localConnection(ctx context.Context, database string) (*pgx.Conn, error) {
6767
return nil, err
6868
}
6969

70-
pg, err := node.NewLocalConnection(ctx, database)
70+
pg, err := node.NewLocalConnection(ctx, database, node.OperatorCredentials)
7171
if err != nil {
7272
return nil, err
7373
}

internal/flycheck/pg.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func CheckPostgreSQL(ctx context.Context, checks *check.CheckSuite) (*check.Chec
1919
return checks, fmt.Errorf("failed to initialize node: %s", err)
2020
}
2121

22-
localConn, err := node.NewLocalConnection(ctx, "postgres")
22+
localConn, err := node.NewLocalConnection(ctx, "postgres", node.SUCredentials)
2323
if err != nil {
2424
return checks, fmt.Errorf("failed to connect with local node: %s", err)
2525
}

internal/flypg/node.go

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -65,12 +65,13 @@ func NewNode() (*Node, error) {
6565
Password: os.Getenv("SU_PASSWORD"),
6666
}
6767

68-
// Superuser
68+
// Postgres user
6969
node.OperatorCredentials = admin.Credential{
7070
Username: "postgres",
7171
Password: os.Getenv("OPERATOR_PASSWORD"),
7272
}
7373

74+
// Repmgr user
7475
node.ReplCredentials = admin.Credential{
7576
Username: "repmgr",
7677
Password: os.Getenv("REPL_PASSWORD"),
@@ -212,7 +213,8 @@ func (n *Node) PostInit(ctx context.Context) error {
212213
return fmt.Errorf("unrecoverable zombie")
213214
}
214215

215-
conn, err := n.NewLocalConnection(ctx, "postgres")
216+
// Use the Postgres user on boot, since our internal user may not have been created yet.
217+
conn, err := n.NewLocalConnection(ctx, "postgres", n.OperatorCredentials)
216218
if err != nil {
217219
return fmt.Errorf("failed to establish connection to member: %s", err)
218220
}
@@ -355,11 +357,6 @@ func (n *Node) PostInit(ctx context.Context) error {
355357
return nil
356358
}
357359

358-
func (n *Node) NewLocalConnection(ctx context.Context, database string) (*pgx.Conn, error) {
359-
host := net.JoinHostPort(n.PrivateIP, strconv.Itoa(n.Port))
360-
return openConnection(ctx, host, database, n.OperatorCredentials)
361-
}
362-
363360
func (n *Node) setupCredentials(ctx context.Context, conn *pgx.Conn) error {
364361
requiredCredentials := []admin.Credential{
365362
n.OperatorCredentials,
@@ -370,6 +367,12 @@ func (n *Node) setupCredentials(ctx context.Context, conn *pgx.Conn) error {
370367
return admin.ManageDefaultUsers(ctx, conn, requiredCredentials)
371368
}
372369

370+
// NewLocalConnection opens up a new connection using the flypgadmin user.
371+
func (n *Node) NewLocalConnection(ctx context.Context, database string, creds admin.Credential) (*pgx.Conn, error) {
372+
host := net.JoinHostPort(n.PrivateIP, strconv.Itoa(n.Port))
373+
return openConnection(ctx, host, database, creds)
374+
}
375+
373376
func openConnection(parentCtx context.Context, host string, database string, creds admin.Credential) (*pgx.Conn, error) {
374377
ctx, cancel := context.WithTimeout(parentCtx, 10*time.Second)
375378
defer cancel()

0 commit comments

Comments
 (0)