Skip to content

Commit

Permalink
improve: add a migration for destination name with dots
Browse files Browse the repository at this point in the history
  • Loading branch information
dnephin committed Oct 4, 2022
1 parent 9a1d22e commit 345848a
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 0 deletions.
40 changes: 40 additions & 0 deletions internal/server/data/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
_ "embed"
"errors"
"fmt"
"strings"
"time"

"gorm.io/gorm"
Expand Down Expand Up @@ -70,6 +71,7 @@ func migrations() []*migrator.Migration {
cleanCrossOrgGroupMemberships(),
fixProviderUserIndex(),
addProviderUserSCIMFields(),
removeDotFromDestinationName(),
// next one here
}
}
Expand Down Expand Up @@ -714,3 +716,41 @@ func addProviderUserSCIMFields() *migrator.Migration {
},
}
}

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
},
}
}
25 changes: 25 additions & 0 deletions internal/server/data/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -734,6 +734,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))
Expand Down

0 comments on commit 345848a

Please sign in to comment.