Skip to content

Commit

Permalink
sanitize migrations table name (#130)
Browse files Browse the repository at this point in the history
  • Loading branch information
adiweiss authored and Guy Baron committed Aug 18, 2019
1 parent 4337e55 commit 4c1c812
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
12 changes: 10 additions & 2 deletions gbus/tx/mysql/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@ package mysql

import (
"database/sql"
"regexp"
"strings"

"fmt"
"github.com/lopezator/migrator"
"github.com/wework/grabbit/gbus/tx"
)
Expand Down Expand Up @@ -86,7 +87,7 @@ func TimoutTableMigration(svcName string) *migrator.Migration {

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

migrate, err := migrator.New(migrator.TableName(migrationsTable), migrator.Migrations(
OutboxMigrations(svcName),
Expand All @@ -101,3 +102,10 @@ func EnsureSchema(db *sql.DB, svcName string) {
panic(err)
}
}

func sanitizedMigrationsTable(svcName string) string {
var re = regexp.MustCompile(`-|;|\\|`)
sanitized := re.ReplaceAllString(svcName, "")

return strings.ToLower("grabbitMigrations_" + sanitized)
}
11 changes: 11 additions & 0 deletions tests/bus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,17 @@ func TestHealthCheck(t *testing.T) {
}
}

func TestSanitizingSvcName(t *testing.T) {
svc4 := createNamedBusForTest(testSvc4)
err := svc4.Start()
if err != nil {
t.Error(err.Error())
}
defer svc4.Shutdown()

fmt.Println("succeeded sanitizing service name")
}

func noopTraceContext() context.Context {
return context.Background()
// tracer := opentracing.NoopTracer{}
Expand Down
2 changes: 2 additions & 0 deletions tests/consts.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,14 @@ var connStr string
var testSvc1 string
var testSvc2 string
var testSvc3 string
var testSvc4 string

func init() {
connStr = "amqp://rabbitmq:rabbitmq@localhost"
testSvc1 = "testSvc1"
testSvc2 = "testSvc2"
testSvc3 = "testSvc3"
testSvc4 = "test-svc4"
}

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

0 comments on commit 4c1c812

Please sign in to comment.