diff --git a/internal/server/data/migrations.go b/internal/server/data/migrations.go index 9602dd46cf..874d30cf3c 100644 --- a/internal/server/data/migrations.go +++ b/internal/server/data/migrations.go @@ -5,6 +5,7 @@ import ( _ "embed" "errors" "fmt" + "strings" "time" "gorm.io/gorm" @@ -69,6 +70,7 @@ func migrations() []*migrator.Migration { addIdentityVerifiedFields(), cleanCrossOrgGroupMemberships(), fixProviderUserIndex(), + removeDotFromDestinationName(), // next one here } } @@ -695,3 +697,41 @@ ALTER TABLE provider_users ADD CONSTRAINT }, } } + +func removeDotFromDestinationName() *migrator.Migration { + return &migrator.Migration{ + ID: "2022-10-04T11:44", + Migrate: func(tx migrator.DB) error { + type idName struct { + id uid.ID + name string + } + + rows, err := tx.Query(`SELECT id, name FROM destinations WHERE name LIKE '%.%'`) + if err != nil { + return err + } + defer rows.Close() + var toRename []idName + for rows.Next() { + pair := idName{} + if err := rows.Scan(&pair.id, &pair.name); err != nil { + return err + } + toRename = append(toRename, pair) + } + if err := rows.Err(); err != nil { + return err + } + for _, item := range toRename { + item.name = strings.ReplaceAll(item.name, ".", "_") + _, err := tx.Exec(`UPDATE destinations SET name = ? WHERE id = ?`, + item.name, item.id) + if err != nil { + return err + } + } + return nil + }, + } +} diff --git a/internal/server/data/migrations_test.go b/internal/server/data/migrations_test.go index d53adfa8a1..81e435e4ac 100644 --- a/internal/server/data/migrations_test.go +++ b/internal/server/data/migrations_test.go @@ -728,6 +728,31 @@ DELETE FROM settings WHERE id=24567; // schema changes are tested with schema comparison }, }, + { + label: testCaseLine("2022-10-04T11:44"), + setup: func(t *testing.T, db WriteTxn) { + _, err := db.Exec(` + INSERT INTO destinations(id, name) VALUES + (10009, 'with.dot.no.more'), + (10010, 'no-dots')`) + assert.NilError(t, err) + + }, + cleanup: func(t *testing.T, db WriteTxn) { + _, err := db.Exec("DELETE FROM destinations") + assert.NilError(t, err) + }, + expected: func(t *testing.T, db WriteTxn) { + row := db.QueryRow("SELECT name from destinations where id=?", 10009) + var name string + assert.NilError(t, row.Scan(&name)) + assert.Equal(t, name, "with_dot_no_more") + + row = db.QueryRow("SELECT name from destinations where id=?", 10010) + assert.NilError(t, row.Scan(&name)) + assert.Equal(t, name, "no-dots") + }, + }, } ids := make(map[string]struct{}, len(testCases))