Skip to content

Commit

Permalink
Merge pull request #92 from xataio/replace-period-replication-slot-name
Browse files Browse the repository at this point in the history
Replace period on replication slot name
  • Loading branch information
eminano authored Dec 2, 2024
2 parents 9186e3e + 96ace49 commit bd02bf7
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
4 changes: 4 additions & 0 deletions internal/postgres/pg_replication_conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"context"
"errors"
"fmt"
"strings"
"time"

"github.com/jackc/pglogrepl"
Expand Down Expand Up @@ -121,6 +122,9 @@ func (c *ReplicationConn) Close(ctx context.Context) error {
}

func DefaultReplicationSlotName(dbName string) string {
// sanitise the dbName before creating the replication slot name to ensure
// the name does not contain invalid characters.
dbName = strings.ReplaceAll(dbName, ".", "_")
return "pgstream_" + dbName + "_slot"
}

Expand Down
41 changes: 41 additions & 0 deletions internal/postgres/pg_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
// SPDX-License-Identifier: Apache-2.0

package postgres

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_DefaultReplicationSlotName(t *testing.T) {
t.Parallel()

tests := []struct {
name string
wantSlotName string
}{
{
name: "example",
wantSlotName: "pgstream_example_slot",
},
{
name: "example.com",
wantSlotName: "pgstream_example_com_slot",
},
{
name: "example.test.com",
wantSlotName: "pgstream_example_test_com_slot",
},
}

for _, tc := range tests {
tc := tc
t.Run(tc.name, func(t *testing.T) {
t.Parallel()

slotName := DefaultReplicationSlotName(tc.name)
require.Equal(t, tc.wantSlotName, slotName)
})
}
}

0 comments on commit bd02bf7

Please sign in to comment.