Skip to content

Commit 4c1c812

Browse files
adiweissGuy Baron
adiweiss
authored and
Guy Baron
committed
sanitize migrations table name (#130)
1 parent 4337e55 commit 4c1c812

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

gbus/tx/mysql/migrations.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package mysql
22

33
import (
44
"database/sql"
5+
"regexp"
6+
"strings"
57

6-
"fmt"
78
"github.com/lopezator/migrator"
89
"github.com/wework/grabbit/gbus/tx"
910
)
@@ -86,7 +87,7 @@ func TimoutTableMigration(svcName string) *migrator.Migration {
8687

8788
//EnsureSchema implements Grabbit's migrations strategy
8889
func EnsureSchema(db *sql.DB, svcName string) {
89-
migrationsTable := fmt.Sprintf("grabbitMigrations_%s", svcName)
90+
migrationsTable := sanitizedMigrationsTable(svcName)
9091

9192
migrate, err := migrator.New(migrator.TableName(migrationsTable), migrator.Migrations(
9293
OutboxMigrations(svcName),
@@ -101,3 +102,10 @@ func EnsureSchema(db *sql.DB, svcName string) {
101102
panic(err)
102103
}
103104
}
105+
106+
func sanitizedMigrationsTable(svcName string) string {
107+
var re = regexp.MustCompile(`-|;|\\|`)
108+
sanitized := re.ReplaceAllString(svcName, "")
109+
110+
return strings.ToLower("grabbitMigrations_" + sanitized)
111+
}

tests/bus_test.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,17 @@ func TestHealthCheck(t *testing.T) {
412412
}
413413
}
414414

415+
func TestSanitizingSvcName(t *testing.T) {
416+
svc4 := createNamedBusForTest(testSvc4)
417+
err := svc4.Start()
418+
if err != nil {
419+
t.Error(err.Error())
420+
}
421+
defer svc4.Shutdown()
422+
423+
fmt.Println("succeeded sanitizing service name")
424+
}
425+
415426
func noopTraceContext() context.Context {
416427
return context.Background()
417428
// tracer := opentracing.NoopTracer{}

tests/consts.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@ var connStr string
1212
var testSvc1 string
1313
var testSvc2 string
1414
var testSvc3 string
15+
var testSvc4 string
1516

1617
func init() {
1718
connStr = "amqp://rabbitmq:rabbitmq@localhost"
1819
testSvc1 = "testSvc1"
1920
testSvc2 = "testSvc2"
2021
testSvc3 = "testSvc3"
22+
testSvc4 = "test-svc4"
2123
}
2224

2325
func createBusWithConfig(svcName string, deadletter string, txnl, pos bool, conf gbus.BusConfiguration) gbus.Bus {

0 commit comments

Comments
 (0)