From f7730f9966c86c0d6dbf848e22c93a012252de94 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Wed, 4 Oct 2023 17:55:41 +0400 Subject: [PATCH 1/2] Extract `ReservedPrefix` constant --- internal/backends/postgresql/metadata/registry.go | 9 ++------- internal/backends/sqlite/metadata/metadata.go | 3 ++- internal/backends/sqlite/metadata/registry.go | 2 +- internal/backends/validation.go | 7 +++++-- 4 files changed, 10 insertions(+), 11 deletions(-) diff --git a/internal/backends/postgresql/metadata/registry.go b/internal/backends/postgresql/metadata/registry.go index a6220eea0825..6544041ac489 100644 --- a/internal/backends/postgresql/metadata/registry.go +++ b/internal/backends/postgresql/metadata/registry.go @@ -30,6 +30,7 @@ import ( "go.uber.org/zap" "golang.org/x/exp/maps" + "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/backends/postgresql/metadata/pool" "github.com/FerretDB/FerretDB/internal/clientconn/conninfo" "github.com/FerretDB/FerretDB/internal/handlers/sjson" @@ -41,11 +42,8 @@ import ( ) const ( - // Reserved prefix for database and collection names. - reservedPrefix = "_ferretdb_" - // PostgreSQL table name where FerretDB metadata is stored. - metadataTableName = reservedPrefix + "database_metadata" + metadataTableName = backends.ReservedPrefix + "database_metadata" // PostgreSQL max table name length. maxTableNameLength = 63 @@ -473,9 +471,6 @@ func (r *Registry) collectionCreate(ctx context.Context, p *pgxpool.Pool, dbName for { tableName = specialCharacters.ReplaceAllString(strings.ToLower(collectionName), "_") - if strings.HasPrefix(tableName, reservedPrefix) { - tableName = "_" + tableName - } suffixHash := fmt.Sprintf("_%08x", s) if l := maxTableNameLength - len(suffixHash); len(tableName) > l { diff --git a/internal/backends/sqlite/metadata/metadata.go b/internal/backends/sqlite/metadata/metadata.go index 89ffe3e4bd34..c6a334947272 100644 --- a/internal/backends/sqlite/metadata/metadata.go +++ b/internal/backends/sqlite/metadata/metadata.go @@ -22,6 +22,7 @@ import ( "golang.org/x/exp/slices" + "github.com/FerretDB/FerretDB/internal/backends" "github.com/FerretDB/FerretDB/internal/util/lazyerrors" ) @@ -32,7 +33,7 @@ import ( const ( // DefaultColumn is a column name for all fields. - DefaultColumn = "_ferretdb_sjson" + DefaultColumn = backends.ReservedPrefix + "_sjson" // IDColumn is a SQLite path expression for _id field. IDColumn = DefaultColumn + "->'$._id'" diff --git a/internal/backends/sqlite/metadata/registry.go b/internal/backends/sqlite/metadata/registry.go index 3bc5dfc7c3c5..6b84bc5b0d57 100644 --- a/internal/backends/sqlite/metadata/registry.go +++ b/internal/backends/sqlite/metadata/registry.go @@ -42,7 +42,7 @@ const ( reservedTablePrefix = "sqlite_" // SQLite table name where FerretDB metadata is stored. - metadataTableName = "_ferretdb_collections" + metadataTableName = backends.ReservedPrefix + "_collections" ) // Parts of Prometheus metric names. diff --git a/internal/backends/validation.go b/internal/backends/validation.go index 37e4391f5a35..160eb353b190 100644 --- a/internal/backends/validation.go +++ b/internal/backends/validation.go @@ -26,6 +26,9 @@ var databaseNameRe = regexp.MustCompile("^[a-zA-Z0-9_-]{1,63}$") // collectionNameRe validates collection names. var collectionNameRe = regexp.MustCompile("^[^\\.$\x00][^$\x00]{0,234}$") +// ReservedPrefix for names: databases, collections, schemas, tables, indexes, columns, etc. +const ReservedPrefix = "_ferretdb_" + // validateDatabaseName checks that database name is valid for FerretDB. // // It follows MongoDB restrictions plus @@ -41,7 +44,7 @@ func validateDatabaseName(name string) error { return NewError(ErrorCodeDatabaseNameIsInvalid, nil) } - if strings.HasPrefix(name, "_ferretdb_") { + if strings.HasPrefix(name, ReservedPrefix) { return NewError(ErrorCodeDatabaseNameIsInvalid, nil) } @@ -64,7 +67,7 @@ func validateCollectionName(name string) error { return NewError(ErrorCodeCollectionNameIsInvalid, nil) } - if strings.HasPrefix(name, "_ferretdb_") || strings.HasPrefix(name, "system.") { + if strings.HasPrefix(name, ReservedPrefix) || strings.HasPrefix(name, "system.") { return NewError(ErrorCodeCollectionNameIsInvalid, nil) } From 5f98f3f0ac56d907ea27cc2d33b2bd0c5ad6cd37 Mon Sep 17 00:00:00 2001 From: Alexey Palazhchenko Date: Thu, 5 Oct 2023 11:42:04 +0400 Subject: [PATCH 2/2] Fix names --- internal/backends/sqlite/metadata/metadata.go | 2 +- internal/backends/sqlite/metadata/registry.go | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/backends/sqlite/metadata/metadata.go b/internal/backends/sqlite/metadata/metadata.go index c6a334947272..72c54b881611 100644 --- a/internal/backends/sqlite/metadata/metadata.go +++ b/internal/backends/sqlite/metadata/metadata.go @@ -33,7 +33,7 @@ import ( const ( // DefaultColumn is a column name for all fields. - DefaultColumn = backends.ReservedPrefix + "_sjson" + DefaultColumn = backends.ReservedPrefix + "sjson" // IDColumn is a SQLite path expression for _id field. IDColumn = DefaultColumn + "->'$._id'" diff --git a/internal/backends/sqlite/metadata/registry.go b/internal/backends/sqlite/metadata/registry.go index 6b84bc5b0d57..c379b95bd178 100644 --- a/internal/backends/sqlite/metadata/registry.go +++ b/internal/backends/sqlite/metadata/registry.go @@ -42,7 +42,7 @@ const ( reservedTablePrefix = "sqlite_" // SQLite table name where FerretDB metadata is stored. - metadataTableName = backends.ReservedPrefix + "_collections" + metadataTableName = backends.ReservedPrefix + "collections" ) // Parts of Prometheus metric names.