From 8ffd6d00500ecff6abf4b7810cf25f71384e1ea9 Mon Sep 17 00:00:00 2001 From: mstar Date: Thu, 27 Mar 2025 15:55:40 +0100 Subject: [PATCH] Fix enum type migration and more relations - Fixed bad SQL for creating sql enum types - Noted more relations, mostly x to media --- storage-new/migrations.go | 7 ++++--- storage-new/models/Note.go | 2 +- storage-new/models/RemoteServer.go | 5 +++-- storage-new/models/User.go | 21 ++++++++++++--------- 4 files changed, 20 insertions(+), 15 deletions(-) diff --git a/storage-new/migrations.go b/storage-new/migrations.go index 1d43181..4482e76 100644 --- a/storage-new/migrations.go +++ b/storage-new/migrations.go @@ -82,7 +82,7 @@ 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 { + if err := db.Exec("DROP TYPE IF EXISTS " + name + " CASCADE;").Error; err != nil { return other.Error( "storage", fmt.Sprintf("Failed to remove old type %s (if it exists)", name), @@ -90,9 +90,9 @@ func migrateEnum(db *gorm.DB, name string, values []string) error { ) } queryBuilder := strings.Builder{} - queryBuilder.WriteString("CREATE TYPE") + queryBuilder.WriteString("CREATE TYPE ") queryBuilder.WriteString(name) - queryBuilder.WriteString("AS ENUM (") + queryBuilder.WriteString(" AS ENUM (") blen := len(values) for i, btype := range values { queryBuilder.WriteString("'" + string(btype) + "'") @@ -101,6 +101,7 @@ func migrateEnum(db *gorm.DB, name string, values []string) error { queryBuilder.WriteString(",") } } + queryBuilder.WriteString(");") if err := db.Exec(queryBuilder.String()).Error; err != nil { return err } diff --git a/storage-new/models/Note.go b/storage-new/models/Note.go index f0d6df9..f5e8e0e 100644 --- a/storage-new/models/Note.go +++ b/storage-new/models/Note.go @@ -22,7 +22,7 @@ type Note struct { // Soft delete means that this entry still exists in the db, but gorm won't include it anymore unless specifically told to // If not null, this entry is marked as deleted DeletedAt gorm.DeletedAt `gorm:"index"` - // Creator Account // `gorm:"foreignKey:CreatorId;references:ID"` // Account that created the post + Creator User CreatorId string Remote bool // Whether the note is originally a remote one and just "cached" // Raw content of the note. So without additional formatting applied diff --git a/storage-new/models/RemoteServer.go b/storage-new/models/RemoteServer.go index 6324697..be24b57 100644 --- a/storage-new/models/RemoteServer.go +++ b/storage-new/models/RemoteServer.go @@ -9,6 +9,7 @@ type RemoteServer struct { ServerType ServerSoftwareType // What software the server is running. Useful for formatting Domain string // `gorm:"primaryKey"` // Domain the server exists under. Additional primary key Name string // What the server wants to be known as (usually same as url) - Icon string // ID of a media file - IsSelf bool // Whether this server is yours truly + Icon MediaMetadata + IconId string // ID of a media file + IsSelf bool // Whether this server is yours truly } diff --git a/storage-new/models/User.go b/storage-new/models/User.go index 250cd11..37f546a 100644 --- a/storage-new/models/User.go +++ b/storage-new/models/User.go @@ -32,15 +32,18 @@ type User struct { // If not null, this entry is marked as deleted DeletedAt gorm.DeletedAt `gorm:"index"` // Server RemoteServer // `gorm:"foreignKey:ServerId;references:ID"` // The server this user is from - ServerId uint // Id of the server this user is from, needed for including RemoteServer - DisplayName string // The display name of the user. Can be different from the handle - Description string // The description of a user account - IsBot bool // Whether to mark this account as a script controlled one - Icon string // ID of a media file used as icon - Background *string // ID of a media file used as background image - Banner *string // ID of a media file used as banner - Indexable bool // Whether this account can be found by crawlers - PublicKey []byte // The public key of the account + ServerId uint // Id of the server this user is from, needed for including RemoteServer + DisplayName string // The display name of the user. Can be different from the handle + Description string // The description of a user account + IsBot bool // Whether to mark this account as a script controlled one + Icon MediaMetadata + IconId string // ID of a media file used as icon + Background *MediaMetadata + BackgroundId *string // ID of a media file used as background image + Banner *MediaMetadata + BannerId *string // ID of a media file used as banner + Indexable bool // Whether this account can be found by crawlers + PublicKey []byte // The public key of the account // Whether this account restricts following // If true, the owner must approve of a follow request first RestrictedFollow bool