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

sanitize migrations table name #130

Merged
merged 1 commit into from
Aug 18, 2019
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
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