This commit is contained in:
parent
daf401a2f7
commit
8d4ba2ecae
14 changed files with 56 additions and 7 deletions
|
@ -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
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue