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

Allow dashes (-) in collection names #1312

Merged
merged 14 commits into from Oct 25, 2022
26 changes: 18 additions & 8 deletions integration/basic_test.go
Expand Up @@ -297,7 +297,7 @@ func TestCollectionName(t *testing.T) {

t.Parallel()

t.Run("Err", func(t *testing.T) {
t.Run("All", func(t *testing.T) {
ctx, collection := setup.Setup(t)

collectionName300 := strings.Repeat("aB", 150)
Expand All @@ -312,11 +312,11 @@ func TestCollectionName(t *testing.T) {
Name: "InvalidNamespace",
Code: 73,
Message: fmt.Sprintf(
"Fully qualified namespace is too long. Namespace: testcollectionname_err.%s Max: 255",
"Fully qualified namespace is too long. Namespace: testcollectionname_all.%s Max: 255",
collectionName300,
),
},
alt: fmt.Sprintf("Invalid collection name: 'testcollectionname_err.%s'", collectionName300),
alt: fmt.Sprintf("Invalid collection name: 'testcollectionname_all.%s'", collectionName300),
},
"WithADollarSign": {
collection: "collection_name_with_a-$",
Expand All @@ -325,29 +325,39 @@ func TestCollectionName(t *testing.T) {
Code: 73,
Message: `Invalid collection name: collection_name_with_a-$`,
},
alt: `Invalid collection name: 'testcollectionname_err.collection_name_with_a-$'`,
alt: `Invalid collection name: 'testcollectionname_all.collection_name_with_a-$'`,
},
"WithADash": {
collection: "collection_name_with_a-",
},
"WithADashAtBeginning": {
collection: "-collection_name",
},
"Empty": {
collection: "",
err: &mongo.CommandError{
Name: "InvalidNamespace",
Code: 73,
Message: "Invalid namespace specified 'testcollectionname_err.'",
Message: "Invalid namespace specified 'testcollectionname_all.'",
},
alt: "Invalid collection name: 'testcollectionname_err.'",
alt: "Invalid collection name: 'testcollectionname_all.'",
},
}

for name, tc := range cases {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
err := collection.Database().CreateCollection(ctx, tc.collection)
AssertEqualAltError(t, *tc.err, tc.alt, err)
if tc.err != nil {
AssertEqualAltError(t, *tc.err, tc.alt, err)
return
}
assert.NoError(t, err)
})
}
})

t.Run("Ok", func(t *testing.T) {
t.Run("LongName", func(t *testing.T) {
ctx, collection := setup.Setup(t)

longCollectionName := strings.Repeat("a", 100)
Expand Down
2 changes: 1 addition & 1 deletion internal/handlers/pg/pgdb/collections.go
Expand Up @@ -31,7 +31,7 @@ import (
)

// validateCollectionNameRe validates collection names.
var validateCollectionNameRe = regexp.MustCompile("^[a-zA-Z_][a-zA-Z0-9_]{0,119}$")
var validateCollectionNameRe = regexp.MustCompile("^[a-zA-Z_-][a-zA-Z0-9_-]{0,119}$")

// Collections returns a sorted list of FerretDB collection names.
//
Expand Down
2 changes: 2 additions & 0 deletions internal/handlers/pg/pgdb/databases.go
Expand Up @@ -28,6 +28,8 @@ import (
)

// validateDatabaseNameRe validates FerretDB database / PostgreSQL schema names.
//
// TODO: https://github.com/FerretDB/FerretDB/issues/1321
var validateDatabaseNameRe = regexp.MustCompile("^[a-z_][a-z0-9_]{0,62}$")

// Databases returns a sorted list of FerretDB database / PostgreSQL schema.
Expand Down
3 changes: 2 additions & 1 deletion website/docs/diff.md
Expand Up @@ -11,7 +11,8 @@ sidebar_position: 6
3. Document keys must not contain `$` sign.
4. Database and collection names restrictions:
* name cannot start with the reserved prefix `_ferretdb_`;
* name must not include non-latin letters, spaces, dots, dollars or dashes;
* database name must not include non-latin letters, spaces, dots, dollars or dashes;
* collection name must not include non-latin letters, spaces, dots or dollars;
* name must not start with a number;
* database name cannot contain capital letters;
* database name length cannot be more than 63 characters;
Expand Down