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
This commit is contained in:
parent
412a8be600
commit
e182949a8d
9 changed files with 24 additions and 12 deletions
|
@ -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
|
||||
|
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -129,6 +129,7 @@ func insertUser(
|
|||
)
|
||||
}
|
||||
user := models.User{
|
||||
ID: shared.NewId(),
|
||||
Username: shared.ServerActorName,
|
||||
Server: *server,
|
||||
ServerId: server.ID,
|
||||
|
|
|
@ -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,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue