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

Extract ReservedPrefix constant #3497

Merged
merged 3 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 2 additions & 7 deletions internal/backends/postgresql/metadata/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion internal/backends/sqlite/metadata/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

"golang.org/x/exp/slices"

"github.com/FerretDB/FerretDB/internal/backends"
"github.com/FerretDB/FerretDB/internal/util/lazyerrors"
)

Expand All @@ -32,7 +33,7 @@ import (

const (
// DefaultColumn is a column name for all fields.
DefaultColumn = "_ferretdb_sjson"
DefaultColumn = backends.ReservedPrefix + "_sjson"
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved

// IDColumn is a SQLite path expression for _id field.
IDColumn = DefaultColumn + "->'$._id'"
Expand Down
2 changes: 1 addition & 1 deletion internal/backends/sqlite/metadata/registry.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ const (
reservedTablePrefix = "sqlite_"

// SQLite table name where FerretDB metadata is stored.
metadataTableName = "_ferretdb_collections"
metadataTableName = backends.ReservedPrefix + "_collections"
rumyantseva marked this conversation as resolved.
Show resolved Hide resolved
)

// Parts of Prometheus metric names.
Expand Down
7 changes: 5 additions & 2 deletions internal/backends/validation.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}

Expand All @@ -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)
}

Expand Down