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

Fix index name generation #3511

Merged
merged 5 commits into from
Oct 9, 2023
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
6 changes: 4 additions & 2 deletions internal/backends/postgresql/metadata/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@
indexNamePart := specialCharacters.ReplaceAllString(strings.ToLower(index.Name), "_")

h := fnv.New32a()
must.NotFail(h.Write([]byte(collectionName)))
must.NotFail(h.Write([]byte(index.Name)))

Check warning on line 780 in internal/backends/postgresql/metadata/registry.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/metadata/registry.go#L780

Added line #L780 was not covered by tests
s := h.Sum32()

var pgIndexName string
Expand All @@ -788,7 +788,7 @@
indexNamePart = indexNamePart[:l]
}

pgIndexName = fmt.Sprintf("%s_%s", tableNamePart, indexNamePart)
pgIndexName = fmt.Sprintf("%s_%s%s", tableNamePart, indexNamePart, suffixHash)

Check warning on line 791 in internal/backends/postgresql/metadata/registry.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/metadata/registry.go#L791

Added line #L791 was not covered by tests

// indexes must be unique across the whole database, so we check for duplicates for all collections
_, duplicate := allPgIndexes[pgIndexName]
Expand Down Expand Up @@ -842,6 +842,8 @@

created = append(created, index.Name)
c.Indexes = append(c.Indexes, index)
allIndexes[index.Name] = collectionName
allPgIndexes[index.PgIndex] = collectionName

Check warning on line 846 in internal/backends/postgresql/metadata/registry.go

View check run for this annotation

Codecov / codecov/patch

internal/backends/postgresql/metadata/registry.go#L845-L846

Added lines #L845 - L846 were not covered by tests
}

b, err := sjson.Marshal(c.marshal())
Expand Down
104 changes: 104 additions & 0 deletions internal/backends/postgresql/metadata/registry_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package metadata
import (
"context"
"fmt"
"strings"
"sync/atomic"
"testing"

Expand Down Expand Up @@ -639,3 +640,106 @@ func TestIndexesCreateDrop(t *testing.T) {
assert.Equal(t, expected, sql)
})
}

func TestLongIndexNames(t *testing.T) {
if testing.Short() {
t.Skip("skipping in -short mode")
}

t.Parallel()

connInfo := conninfo.New()
ctx := conninfo.Ctx(testutil.Ctx(t), connInfo)

r, db, dbName := createDatabase(t, ctx)

batch1 := []IndexInfo{{
Name: strings.Repeat("aB", 75),
Key: []IndexKeyPair{{
Field: "foo",
Descending: false,
}, {
Field: "bar",
Descending: true,
}},
}, {
Name: strings.Repeat("aB", 75) + "_unique",
Key: []IndexKeyPair{{
Field: "foo",
Descending: false,
}},
Unique: true,
}}

batch2 := []IndexInfo{{
Name: strings.Repeat("aB", 75) + "_bar",
Key: []IndexKeyPair{{
Field: "bar",
Descending: false,
}},
}}

for name, tc := range map[string]struct {
collectionName string
tablePartInIndex string
}{
"ShortCollectionName": {
collectionName: testutil.CollectionName(t),
tablePartInIndex: "testlongindexnames_47546aa3",
},
"LongCollectionName": {
collectionName: "Collection" + strings.Repeat("cD", 75),
tablePartInIndex: "collection" + strings.Repeat("cd", 10),
},
} {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()

err := r.IndexesCreate(ctx, dbName, tc.collectionName, batch1)
require.NoError(t, err)

collection, err := r.CollectionGet(ctx, dbName, tc.collectionName)
require.NoError(t, err)
require.Equal(t, 3, len(collection.Indexes))

for _, index := range collection.Indexes {
switch index.Name {
case "_id_":
assert.Equal(t, tc.tablePartInIndex+"__id__67399184_idx", index.PgIndex)
case batch1[0].Name:
assert.Equal(t, tc.tablePartInIndex+"_ababababababababab_12fa1dfe_idx", index.PgIndex)
case batch1[1].Name:
assert.Equal(t, tc.tablePartInIndex+"_ababababababababab_ca7ee610_idx", index.PgIndex)
default:
t.Errorf("unexpected index: %s", index.Name)
}
}

err = r.IndexesCreate(ctx, dbName, tc.collectionName, batch2)
require.NoError(t, err)

err = r.initCollections(ctx, dbName, db)
require.NoError(t, err)

collection, err = r.CollectionGet(ctx, dbName, tc.collectionName)
require.NoError(t, err)
require.Equal(t, 4, len(collection.Indexes))

for _, index := range collection.Indexes {
switch index.Name {
case "_id_":
assert.Equal(t, tc.tablePartInIndex+"__id__67399184_idx", index.PgIndex)
case batch1[0].Name:
assert.Equal(t, tc.tablePartInIndex+"_ababababababababab_12fa1dfe_idx", index.PgIndex)
case batch1[1].Name:
assert.Equal(t, tc.tablePartInIndex+"_ababababababababab_ca7ee610_idx", index.PgIndex)
case batch2[0].Name:
assert.Equal(t, tc.tablePartInIndex+"_ababababababababab_aaf0d99c_idx", index.PgIndex)
default:
t.Errorf("unexpected index: %s", index.Name)
}
}
})
}
}
Loading