From e182949a8d528d90fada6596cee08f2896ac5735 Mon Sep 17 00:00:00 2001 From: mStar Date: Tue, 6 May 2025 14:34:32 +0200 Subject: [PATCH] Remove id autogeneration from db models Remove automatic uuid v4 ID generation from the models and replace it with `shared.NewId()`, which generates an Id depending on the config setting --- config/config.go | 11 +++++++---- shared/ids.go | 12 +++++++++--- storage-new/models/MediaMetadata.go | 2 +- storage-new/models/Note.go | 2 +- storage-new/models/TokenAccess.go | 2 +- storage-new/models/TokenLoginProcess.go | 2 +- storage-new/models/User.go | 2 +- storage-new/self.go | 1 + web/debug/users.go | 2 ++ 9 files changed, 24 insertions(+), 12 deletions(-) diff --git a/config/config.go b/config/config.go index 785b61c..d060e10 100644 --- a/config/config.go +++ b/config/config.go @@ -3,6 +3,7 @@ package config import ( "fmt" "os" + "strings" "git.mstar.dev/mstar/goutils/other" "github.com/BurntSushi/toml" @@ -131,12 +132,12 @@ type ConfigExperimental struct { // if the other server also requires authorized fetch for the server actor // Changing this setting is not expected to cause permanent problems AuthFetchForServerActor bool `toml:"auth_fetch_for_server_actor"` - // Use cuid2 for generation of new IDs that are expected to be used via Activitypub - // They are shorter than the main method used (uuid v4) but should still provide enough - // uniqueness such that collisions are not to be expected. + // Set the provider for ID generation. + // Default is "xid". + // Options are "uuid", "cuid" and "xid" // Changing this option will only affect new ID generations, not update existing ones // As of now, even that doesn't work due to implementation details - UseCuid2Ids bool `toml:"use_cuid2_ids"` + IdGenerator string `toml:"id_generator"` } type Config struct { @@ -220,6 +221,7 @@ var defaultConfig Config = Config{ Experimental: ConfigExperimental{ UseEd25519Keys: false, AuthFetchForServerActor: false, + IdGenerator: "xid", }, } @@ -322,6 +324,7 @@ func ReadAndWriteToGlobal(fileName string) error { log.Error().Err(err).Bytes("config-data-raw", data).Msg("Failed to unmarshal config file") return err } + config.Experimental.IdGenerator = strings.ToLower(config.Experimental.IdGenerator) GlobalConfig = config log.Info().Str("config-file", fileName).Msg("Read and applied config file") return nil diff --git a/shared/ids.go b/shared/ids.go index f1b2610..ca494a9 100644 --- a/shared/ids.go +++ b/shared/ids.go @@ -3,15 +3,21 @@ package shared import ( "github.com/google/uuid" "github.com/nrednav/cuid2" + "github.com/rs/xid" "git.mstar.dev/mstar/linstrom/config" ) // Generate a new string ID func NewId() string { - if config.GlobalConfig.Experimental.UseCuid2Ids { - return cuid2.Generate() - } else { + switch config.GlobalConfig.Experimental.IdGenerator { + case "uuid": return uuid.NewString() + case "cuid": + return cuid2.Generate() + case "xid": + return xid.New().String() + default: + return xid.New().String() } } diff --git a/storage-new/models/MediaMetadata.go b/storage-new/models/MediaMetadata.go index ef93ca1..25f6082 100644 --- a/storage-new/models/MediaMetadata.go +++ b/storage-new/models/MediaMetadata.go @@ -12,7 +12,7 @@ import ( // Instead, they are either stored on the remote server they originated from // or an s3 bucket if uploaded to Linstrom. type MediaMetadata struct { - ID string `gorm:"primarykey;default:gen_random_uuid()"` // The unique ID of this media file + ID string `gorm:"primarykey"` // The unique ID of this media file CreatedAt time.Time // When this entry was created UpdatedAt time.Time // When this entry was last updated // When this entry was deleted (for soft deletions) diff --git a/storage-new/models/Note.go b/storage-new/models/Note.go index 9bacccc..2b6aabb 100644 --- a/storage-new/models/Note.go +++ b/storage-new/models/Note.go @@ -9,7 +9,7 @@ import ( // A note describes some user generated text content. type Note struct { - ID string `gorm:"primarykey;default:gen_random_uuid()"` // Make ID a string (uuid) for other implementations + ID string `gorm:"primarykey"` // Make ID a string for other implementations CreatedAt time.Time // When this entry was created UpdatedAt time.Time // When this entry was last updated // When this entry was deleted (for soft deletions) diff --git a/storage-new/models/TokenAccess.go b/storage-new/models/TokenAccess.go index 1022ef6..c675e62 100644 --- a/storage-new/models/TokenAccess.go +++ b/storage-new/models/TokenAccess.go @@ -14,7 +14,7 @@ type AccessToken struct { User User // The account the token belongs to UserId string // The token itself is a uuid value - Token string `gorm:"primarykey;type:uuid;default:gen_random_uuid()"` + Token string `gorm:"primarykey"` Name string // Token name will be empty if autogenerated with sucessful login // Every token expires, even if set to "not expire". If set to "not expire", it just expires // at a point in the future this server should never reach diff --git a/storage-new/models/TokenLoginProcess.go b/storage-new/models/TokenLoginProcess.go index 405fadc..d000ded 100644 --- a/storage-new/models/TokenLoginProcess.go +++ b/storage-new/models/TokenLoginProcess.go @@ -10,7 +10,7 @@ type LoginProcessToken struct { ID uint64 `gorm:"primarykey"` User User UserId string `gorm:"unique"` - Token string `gorm:"type:uuid;default:gen_random_uuid()"` + Token string Name string ExpiresAt time.Time } diff --git a/storage-new/models/User.go b/storage-new/models/User.go index 69f2224..28feb53 100644 --- a/storage-new/models/User.go +++ b/storage-new/models/User.go @@ -27,7 +27,7 @@ type User struct { // Same also applies for other types that use a UUID as primary key // TODO: Change this to generate via cuid or uuid, depending on config // Or remove autogeneration alltogether - ID string `gorm:"primarykey;default:gen_random_uuid()"` + ID string `gorm:"primarykey"` // Username of the user (eg "max" if the full username is @max@example.com) // Assume unchangable (once set by a user) to be kind to other implementations // Would be an easy avenue to fuck with them though diff --git a/storage-new/self.go b/storage-new/self.go index bd86989..b417855 100644 --- a/storage-new/self.go +++ b/storage-new/self.go @@ -129,6 +129,7 @@ func insertUser( ) } user := models.User{ + ID: shared.NewId(), Username: shared.ServerActorName, Server: *server, ServerId: server.ID, diff --git a/web/debug/users.go b/web/debug/users.go index f14a982..bc3a592 100644 --- a/web/debug/users.go +++ b/web/debug/users.go @@ -129,6 +129,7 @@ func createLocalUser(w http.ResponseWriter, r *http.Request) { u := dbgen.User query := u.Select( + u.ID, u.Username, u.DisplayName, u.Description, @@ -149,6 +150,7 @@ func createLocalUser(w http.ResponseWriter, r *http.Request) { query = query.Select(u.Location) } user := models.User{ + ID: shared.NewId(), Username: data.Username, DisplayName: data.Displayname, Description: data.Description,