Skip to content

Commit

Permalink
Extract ReservedPrefix constant (#3497)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekSi authored Oct 5, 2023
1 parent 0a56f5b commit a2f93bc
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 11 deletions.
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"

// 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"
)

// 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

0 comments on commit a2f93bc

Please sign in to comment.