From 8d4ba2ecaefaeeafc0cd02ee3baf3dfdaec77a18 Mon Sep 17 00:00:00 2001 From: mstar Date: Wed, 26 Mar 2025 17:14:42 +0100 Subject: [PATCH] More work on defining the new data structure --- storage-new/migrations.go | 31 +++++++++++++++++-- storage-new/models/Note.go | 5 +++ ...oteAttachments.go => NoteToAttachments.go} | 4 ++- .../models/{NoteEmotes.go => NoteToEmotes.go} | 4 ++- .../models/{NotePings.go => NoteToPing.go} | 4 ++- .../models/{NoteTags.go => NoteToTag.go} | 3 +- storage-new/models/Reaction.go | 3 ++ storage-new/models/UserAuthentication.go | 1 + storage-new/models/UserBeings.go | 1 + storage-new/models/UserInfoField.go | 1 + storage-new/models/UserRelation.go | 2 ++ storage-new/models/UserRemote.go | 1 + storage-new/models/UserRoles.go | 2 ++ storage-new/models/UserTags.go | 1 + 14 files changed, 56 insertions(+), 7 deletions(-) rename storage-new/models/{NoteAttachments.go => NoteToAttachments.go} (50%) rename storage-new/models/{NoteEmotes.go => NoteToEmotes.go} (57%) rename storage-new/models/{NotePings.go => NoteToPing.go} (52%) rename storage-new/models/{NoteTags.go => NoteToTag.go} (66%) diff --git a/storage-new/migrations.go b/storage-new/migrations.go index fb6d7a7..059757c 100644 --- a/storage-new/migrations.go +++ b/storage-new/migrations.go @@ -4,12 +4,33 @@ import ( "fmt" "strings" + "git.mstar.dev/mstar/goutils/other" "git.mstar.dev/mstar/goutils/sliceutils" "gorm.io/gorm" "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 { if err := db.AutoMigrate( &models.Emote{}, @@ -31,7 +52,7 @@ func migrateTypes(db *gorm.DB) error { &models.UserRole{}, &models.UserTag{}, ); err != nil { - return fmt.Errorf("storage: automigrate structs: %w", err) + return err } 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 func migrateEnum(db *gorm.DB, name string, values []string) error { 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.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 { - return fmt.Errorf("storage: migrate %s: %w", name, err) + return err } return nil } diff --git a/storage-new/models/Note.go b/storage-new/models/Note.go index a236244..631a0cb 100644 --- a/storage-new/models/Note.go +++ b/storage-new/models/Note.go @@ -30,4 +30,9 @@ type Note struct { Quotes *string // url of the message this note quotes 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 + + AttachmentRelations []NoteToAttachment `gorm:"foreignKey:NoteId"` + EmoteRelations []NoteToEmote `gorm:"foreignKey:NoteId"` + PingRelations []NoteToPing `gorm:"foreignKey:NoteId"` + Tags []NoteTag `gorm:"foreignKey:NoteId"` } diff --git a/storage-new/models/NoteAttachments.go b/storage-new/models/NoteToAttachments.go similarity index 50% rename from storage-new/models/NoteAttachments.go rename to storage-new/models/NoteToAttachments.go index e29585f..13ef7fb 100644 --- a/storage-new/models/NoteAttachments.go +++ b/storage-new/models/NoteToAttachments.go @@ -1,6 +1,8 @@ package models type NoteToAttachment struct { - UserId string + Note Note + NoteId string + Attachment MediaMetadata AttachmentId string } diff --git a/storage-new/models/NoteEmotes.go b/storage-new/models/NoteToEmotes.go similarity index 57% rename from storage-new/models/NoteEmotes.go rename to storage-new/models/NoteToEmotes.go index 124f1dc..70ce207 100644 --- a/storage-new/models/NoteEmotes.go +++ b/storage-new/models/NoteToEmotes.go @@ -1,6 +1,8 @@ package models type NoteToEmote struct { - UserId string + Note Note + NoteId string + Emote Emote EmoteId string } diff --git a/storage-new/models/NotePings.go b/storage-new/models/NoteToPing.go similarity index 52% rename from storage-new/models/NotePings.go rename to storage-new/models/NoteToPing.go index 5e7e90b..582a598 100644 --- a/storage-new/models/NotePings.go +++ b/storage-new/models/NoteToPing.go @@ -1,6 +1,8 @@ package models type NoteToPing struct { - UserId string + Note Note + NoteId string + PingTarget User PingTargetId string } diff --git a/storage-new/models/NoteTags.go b/storage-new/models/NoteToTag.go similarity index 66% rename from storage-new/models/NoteTags.go rename to storage-new/models/NoteToTag.go index 31ee396..cf1fa87 100644 --- a/storage-new/models/NoteTags.go +++ b/storage-new/models/NoteToTag.go @@ -1,6 +1,7 @@ package models type NoteTag struct { - UserId string + Note Note + NoteId string Tag string } diff --git a/storage-new/models/Reaction.go b/storage-new/models/Reaction.go index 43633cb..021f6a7 100644 --- a/storage-new/models/Reaction.go +++ b/storage-new/models/Reaction.go @@ -4,7 +4,10 @@ import "gorm.io/gorm" type Reaction struct { gorm.Model + Note Note NoteId string + Reactor User ReactorId string + Emote Emote EmoteId uint } diff --git a/storage-new/models/UserAuthentication.go b/storage-new/models/UserAuthentication.go index 9cd0ac8..8c27b1e 100644 --- a/storage-new/models/UserAuthentication.go +++ b/storage-new/models/UserAuthentication.go @@ -1,6 +1,7 @@ package models type UserAuthMethod struct { + User User UserId string AuthMethod AuthenticationMethodType `gorm:"type:auth_method_type"` Token []byte diff --git a/storage-new/models/UserBeings.go b/storage-new/models/UserBeings.go index 632d76c..fbdb29b 100644 --- a/storage-new/models/UserBeings.go +++ b/storage-new/models/UserBeings.go @@ -1,6 +1,7 @@ package models type UserBeings struct { + User User UserId string Being BeingType `gorm:"type:being_type"` } diff --git a/storage-new/models/UserInfoField.go b/storage-new/models/UserInfoField.go index a259148..13e4507 100644 --- a/storage-new/models/UserInfoField.go +++ b/storage-new/models/UserInfoField.go @@ -15,5 +15,6 @@ type UserInfoField struct { // of the provided url via the common method of // "Does the target url contain a rel='me' link to the owner's account" Confirmed bool + User User UserId string // Id of account this info field belongs to } diff --git a/storage-new/models/UserRelation.go b/storage-new/models/UserRelation.go index 7b599c7..50da947 100644 --- a/storage-new/models/UserRelation.go +++ b/storage-new/models/UserRelation.go @@ -1,7 +1,9 @@ package models type UserRelation struct { + User User UserId string + TargetUser User TargetUserId string Relation RelationType `gorm:"type:relation_type"` } diff --git a/storage-new/models/UserRemote.go b/storage-new/models/UserRemote.go index a4e24f5..90c0ec7 100644 --- a/storage-new/models/UserRemote.go +++ b/storage-new/models/UserRemote.go @@ -6,6 +6,7 @@ type UserRemoteLinks struct { // ---- Section: gorm // Sets this struct up as a value that an Account may have gorm.Model + User User UserId string // Just about every link here is optional to accomodate for servers with only minimal accounts diff --git a/storage-new/models/UserRoles.go b/storage-new/models/UserRoles.go index 641c5f0..97b52be 100644 --- a/storage-new/models/UserRoles.go +++ b/storage-new/models/UserRoles.go @@ -1,6 +1,8 @@ package models type UserRole struct { + User User UserId string + Role Role RoleId uint } diff --git a/storage-new/models/UserTags.go b/storage-new/models/UserTags.go index 9093551..65fbccd 100644 --- a/storage-new/models/UserTags.go +++ b/storage-new/models/UserTags.go @@ -1,6 +1,7 @@ package models type UserTag struct { + User User UserId string Tag string }