Skip to content

Commit

Permalink
force key to be array
Browse files Browse the repository at this point in the history
  • Loading branch information
tigrato committed Sep 11, 2024
1 parent 030072c commit a4e45a9
Showing 1 changed file with 19 additions and 5 deletions.
24 changes: 19 additions & 5 deletions lib/backend/firestore/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,32 @@ func MigrateIncorrectKeyTypes(ctx context.Context, b backend.Backend) error {
if !ok {
return trace.BadParameter("expected firestore backend")
}
if err := migrateKeyType[backend.Key](ctx, firestore); err != nil {

// backend.Key is converted to array of ints when sending to the db.
toArray := func(key []byte) []any {
arrKey := make([]any, len(key))
for i, b := range key {
arrKey[i] = int(b)
}
return arrKey
}

if err := migrateKeyType[[]any](ctx, firestore, toArray); err != nil {
return trace.Wrap(err, "failed to migrate backend key")
}
if err := migrateKeyType[string](ctx, firestore); err != nil {

stringKey := func(key []byte) string {
return string(key)
}
if err := migrateKeyType[string](ctx, firestore, stringKey); err != nil {
return trace.Wrap(err, "failed to migrate legacy key")
}
return nil
}

func migrateKeyType[T ~string | ~[]byte](ctx context.Context, b *Backend) error {
func migrateKeyType[T any](ctx context.Context, b *Backend, newKey func([]byte) T) error {
limit := 500
startKey := T("/")
startKey := newKey([]byte("/"))

for {
docs, err := b.svc.Collection(b.CollectionName).
Expand Down Expand Up @@ -76,7 +90,7 @@ func migrateKeyType[T ~string | ~[]byte](ctx context.Context, b *Backend) error
return trace.Wrap(err, "failed to upsert document")
}

startKey = T(newDoc.Key) // update start key
startKey = newKey(newDoc.Key) // update start key
}

if len(docs) < limit {
Expand Down

0 comments on commit a4e45a9

Please sign in to comment.