Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print DB log #480

Merged
merged 2 commits into from
Feb 12, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cmd/replication/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ func main() {
if options.Replication.Enable || options.Sync.Enable || options.Indexer.Enable {
dbInstance, err = db.NewNamespacedDB(
ctx,
logger,
options.DB.WriterConnectionString,
utils.BuildNamespace(options),
options.DB.WaitForDB,
Expand Down
4 changes: 4 additions & 0 deletions pkg/db/pgx.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"go.uber.org/zap"
"regexp"
"time"

Expand Down Expand Up @@ -124,6 +125,7 @@ func createNamespace(
// Creates a new database with the given namespace if it doesn't exist and returns the full DSN for the new database.
func NewNamespacedDB(
ctx context.Context,
logger *zap.Logger,
dsn string,
namespace string,
waitForDB time.Duration, statementTimeout time.Duration,
Expand All @@ -138,6 +140,8 @@ func NewNamespacedDB(
return nil, err
}

logger.Info("Successfully connected to DB", zap.String("namespace", namespace))

config.ConnConfig.Database = namespace

db, err := newPGXDB(ctx, config, waitForDB)
Expand Down
22 changes: 20 additions & 2 deletions pkg/db/pgx_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package db
import (
"context"
"fmt"
"go.uber.org/zap"
"log"
"net"
"testing"
Expand All @@ -15,9 +16,12 @@ import (
func TestNamespacedDB(t *testing.T) {
startingDsn := testutils.LocalTestDBDSNPrefix + "/foo" + testutils.LocalTestDBDSNSuffix
newDBName := "xmtp_" + testutils.RandomString(24)
logger, err := zap.NewDevelopment()
require.NoError(t, err)
// Create namespaced DB
namespacedDB, err := NewNamespacedDB(
context.Background(),
logger,
startingDsn,
newDBName,
time.Second,
Expand All @@ -40,9 +44,12 @@ func TestNamespacedDB(t *testing.T) {
func TestNamespaceRepeat(t *testing.T) {
startingDsn := testutils.LocalTestDBDSNPrefix + "/foo" + testutils.LocalTestDBDSNSuffix
newDBName := "xmtp_" + testutils.RandomString(24)
logger, err := zap.NewDevelopment()
require.NoError(t, err)
// Create namespaced DB
db1, err := NewNamespacedDB(
context.Background(),
logger,
startingDsn,
newDBName,
time.Second,
Expand All @@ -55,6 +62,7 @@ func TestNamespaceRepeat(t *testing.T) {
// Create again with the same name
db2, err := NewNamespacedDB(
context.Background(),
logger,
startingDsn,
newDBName,
time.Second,
Expand All @@ -66,8 +74,11 @@ func TestNamespaceRepeat(t *testing.T) {
}

func TestNamespacedDBInvalidName(t *testing.T) {
_, err := NewNamespacedDB(
logger, err := zap.NewDevelopment()
require.NoError(t, err)
_, err = NewNamespacedDB(
context.Background(),
logger,
testutils.LocalTestDBDSNPrefix+"/foo"+testutils.LocalTestDBDSNSuffix,
"invalid/name",
time.Second,
Expand All @@ -77,8 +88,11 @@ func TestNamespacedDBInvalidName(t *testing.T) {
}

func TestNamespacedDBInvalidDSN(t *testing.T) {
_, err := NewNamespacedDB(
logger, err := zap.NewDevelopment()
require.NoError(t, err)
_, err = NewNamespacedDB(
context.Background(),
logger,
"invalid-dsn",
"dbname",
time.Second,
Expand Down Expand Up @@ -126,6 +140,9 @@ func TestBlackholeDNS(t *testing.T) {
port := listener.Addr().(*net.TCPAddr).Port
listener.Close()

logger, err := zap.NewDevelopment()
require.NoError(t, err)

dsn := fmt.Sprintf("postgres://user:password@localhost:%d/dbname?sslmode=disable", port)
const testTimeout = 5 * time.Second
const dbTimeout = 200 * time.Millisecond
Expand All @@ -146,6 +163,7 @@ func TestBlackholeDNS(t *testing.T) {

_, err = NewNamespacedDB(
testCtx,
logger,
dsn,
"dbname",
dbTimeout,
Expand Down
Loading