More work on defining the new data structure
Some checks are pending
/ test (push) Waiting to run

This commit is contained in:
Melody Becker 2025-03-26 17:14:42 +01:00
parent daf401a2f7
commit 8d4ba2ecae
Signed by: mstar
SSH key fingerprint: SHA256:9VAo09aaVNTWKzPW7Hq2LW+ox9OdwmTSHRoD4mlz1yI
14 changed files with 56 additions and 7 deletions

View file

@ -4,12 +4,33 @@ import (
"fmt" "fmt"
"strings" "strings"
"git.mstar.dev/mstar/goutils/other"
"git.mstar.dev/mstar/goutils/sliceutils" "git.mstar.dev/mstar/goutils/sliceutils"
"gorm.io/gorm" "gorm.io/gorm"
"git.mstar.dev/mstar/linstrom/storage-new/models" "git.mstar.dev/mstar/linstrom/storage-new/models"
) )
func Migrate(db *gorm.DB) error {
if err := createAccountAuthMethodType(db); err != nil {
return other.Error("storage", "Failed to create Auth Method type", err)
}
if err := createBeingType(db); err != nil {
return other.Error("storage", "Failed to create Being type", err)
}
if err := createAccountRelationType(db); err != nil {
return other.Error("storage", "Failed to create Account Relation type", err)
}
if err := createRemoteServerSoftwareType(db); err != nil {
return other.Error("storage", "Failed to create Server Software type", err)
}
if err := migrateTypes(db); err != nil {
return other.Error("storage", "Failed to automigrate data structs", err)
}
return nil
}
// Returns the raw error created by gorm, with no wrapping
func migrateTypes(db *gorm.DB) error { func migrateTypes(db *gorm.DB) error {
if err := db.AutoMigrate( if err := db.AutoMigrate(
&models.Emote{}, &models.Emote{},
@ -31,7 +52,7 @@ func migrateTypes(db *gorm.DB) error {
&models.UserRole{}, &models.UserRole{},
&models.UserTag{}, &models.UserTag{},
); err != nil { ); err != nil {
return fmt.Errorf("storage: automigrate structs: %w", err) return err
} }
return nil return nil
} }
@ -81,7 +102,11 @@ func createRemoteServerSoftwareType(db *gorm.DB) error {
// Helper function for ensuring the existence of an enum with the given values // Helper function for ensuring the existence of an enum with the given values
func migrateEnum(db *gorm.DB, name string, values []string) error { func migrateEnum(db *gorm.DB, name string, values []string) error {
if err := db.Exec("DROP TYPE IF EXISTS " + name).Error; err != nil { if err := db.Exec("DROP TYPE IF EXISTS " + name).Error; err != nil {
return fmt.Errorf("storage: migrate %s: %w", name, err) return other.Error(
"storage",
fmt.Sprintf("Failed to remove old type %s (if it exists)", name),
err,
)
} }
queryBuilder := strings.Builder{} queryBuilder := strings.Builder{}
queryBuilder.WriteString("CREATE TYPE") queryBuilder.WriteString("CREATE TYPE")
@ -96,7 +121,7 @@ func migrateEnum(db *gorm.DB, name string, values []string) error {
} }
} }
if err := db.Exec(queryBuilder.String()).Error; err != nil { if err := db.Exec(queryBuilder.String()).Error; err != nil {
return fmt.Errorf("storage: migrate %s: %w", name, err) return err
} }
return nil return nil
} }

View file

@ -30,4 +30,9 @@ type Note struct {
Quotes *string // url of the message this note quotes Quotes *string // url of the message this note quotes
AccessLevel NoteAccessLevel // Where to send this message to (public, home, followers, dm) AccessLevel NoteAccessLevel // Where to send this message to (public, home, followers, dm)
OriginServer string // Url of the origin server. Also the primary key for those OriginServer string // Url of the origin server. Also the primary key for those
AttachmentRelations []NoteToAttachment `gorm:"foreignKey:NoteId"`
EmoteRelations []NoteToEmote `gorm:"foreignKey:NoteId"`
PingRelations []NoteToPing `gorm:"foreignKey:NoteId"`
Tags []NoteTag `gorm:"foreignKey:NoteId"`
} }

View file

@ -1,6 +1,8 @@
package models package models
type NoteToAttachment struct { type NoteToAttachment struct {
UserId string Note Note
NoteId string
Attachment MediaMetadata
AttachmentId string AttachmentId string
} }

View file

@ -1,6 +1,8 @@
package models package models
type NoteToEmote struct { type NoteToEmote struct {
UserId string Note Note
NoteId string
Emote Emote
EmoteId string EmoteId string
} }

View file

@ -1,6 +1,8 @@
package models package models
type NoteToPing struct { type NoteToPing struct {
UserId string Note Note
NoteId string
PingTarget User
PingTargetId string PingTargetId string
} }

View file

@ -1,6 +1,7 @@
package models package models
type NoteTag struct { type NoteTag struct {
UserId string Note Note
NoteId string
Tag string Tag string
} }

View file

@ -4,7 +4,10 @@ import "gorm.io/gorm"
type Reaction struct { type Reaction struct {
gorm.Model gorm.Model
Note Note
NoteId string NoteId string
Reactor User
ReactorId string ReactorId string
Emote Emote
EmoteId uint EmoteId uint
} }

View file

@ -1,6 +1,7 @@
package models package models
type UserAuthMethod struct { type UserAuthMethod struct {
User User
UserId string UserId string
AuthMethod AuthenticationMethodType `gorm:"type:auth_method_type"` AuthMethod AuthenticationMethodType `gorm:"type:auth_method_type"`
Token []byte Token []byte

View file

@ -1,6 +1,7 @@
package models package models
type UserBeings struct { type UserBeings struct {
User User
UserId string UserId string
Being BeingType `gorm:"type:being_type"` Being BeingType `gorm:"type:being_type"`
} }

View file

@ -15,5 +15,6 @@ type UserInfoField struct {
// of the provided url via the common method of // of the provided url via the common method of
// "Does the target url contain a rel='me' link to the owner's account" // "Does the target url contain a rel='me' link to the owner's account"
Confirmed bool Confirmed bool
User User
UserId string // Id of account this info field belongs to UserId string // Id of account this info field belongs to
} }

View file

@ -1,7 +1,9 @@
package models package models
type UserRelation struct { type UserRelation struct {
User User
UserId string UserId string
TargetUser User
TargetUserId string TargetUserId string
Relation RelationType `gorm:"type:relation_type"` Relation RelationType `gorm:"type:relation_type"`
} }

View file

@ -6,6 +6,7 @@ type UserRemoteLinks struct {
// ---- Section: gorm // ---- Section: gorm
// Sets this struct up as a value that an Account may have // Sets this struct up as a value that an Account may have
gorm.Model gorm.Model
User User
UserId string UserId string
// Just about every link here is optional to accomodate for servers with only minimal accounts // Just about every link here is optional to accomodate for servers with only minimal accounts

View file

@ -1,6 +1,8 @@
package models package models
type UserRole struct { type UserRole struct {
User User
UserId string UserId string
Role Role
RoleId uint RoleId uint
} }

View file

@ -1,6 +1,7 @@
package models package models
type UserTag struct { type UserTag struct {
User User
UserId string UserId string
Tag string Tag string
} }