package storage import ( "git.mstar.dev/mstar/goutils/other" "github.com/rs/zerolog" "github.com/rs/zerolog/log" "gorm.io/gorm" "git.mstar.dev/mstar/linstrom/storage-new/dbgen" ) const CurrentMigrationVersion = 1 // Auto-migrate all tables and types used func Migrate(db *gorm.DB) error { // Shut up gorm's queries during automigrate by setting log level to info during migration // and then back to the previous value on exit currentLogLevel := zerolog.GlobalLevel() zerolog.SetGlobalLevel(zerolog.InfoLevel) defer zerolog.SetGlobalLevel(currentLogLevel) 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 := createActitiystreamsObjectType(db); err != nil { return other.Error("storage", "Failed to create Activitystreams Object type", err) } if err := createActitiystreamsActivityType(db); err != nil { return other.Error("storage", "Failed to create Activitystreams Activity type", err) } if err := createCollectionTarget(db); err != nil { return other.Error("storage", "Failed to create collections target type", err) } if err := preTypeMigrations(db); err != nil { return other.Error("storage", "Failed to run pre-type migrations", err) } if err := migrateTypes(db); err != nil { return other.Error("storage", "Failed to automigrate data structs", err) } return nil } func preTypeMigrations(db *gorm.DB) error { genTmp := dbgen.Use(db) meta, err := genTmp. ServerMetadata.Select(dbgen.ServerMetadata.LastMigrationVersion). First() if err != nil { return nil } if meta.LastMigrationVersion == CurrentMigrationVersion { return nil } log.Error().Msg("custom schema migrations not implemented yet") return nil }