From ea0598e507f9664a7d3ccf7da0da2dd948942c33 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Sun, 25 Jun 2023 20:15:15 +0100 Subject: [PATCH] db: move legacy data loading out of main/config put it in loadLegacyData in migrations, which is only called by migrateToBadger. --- config.go | 9 -------- main.go | 53 +--------------------------------------------- migrations.go | 58 ++++++++++++++++++++++++++++++++++++++++++--------- 3 files changed, 49 insertions(+), 71 deletions(-) diff --git a/config.go b/config.go index 7fb388c..7053c12 100644 --- a/config.go +++ b/config.go @@ -157,15 +157,6 @@ func (app *appContext) loadConfig() error { app.MustSetValue("updates", "channel", releaseChannel) } - app.storage.customEmails_path = app.config.Section("files").Key("custom_emails").String() - app.storage.loadCustomEmails() - - app.MustSetValue("user_page", "enabled", "true") - if app.config.Section("user_page").Key("enabled").MustBool(false) { - app.storage.userPage_path = app.config.Section("files").Key("custom_user_page_content").String() - app.storage.loadUserPageContent() - } - substituteStrings = app.config.Section("jellyfin").Key("substitute_jellyfin_strings").MustString("") if substituteStrings != "" { diff --git a/main.go b/main.go index 955ddbd..bfc8e8f 100644 --- a/main.go +++ b/main.go @@ -335,59 +335,8 @@ func start(asDaemon, firstCall bool) { app.debug.Printf("Loaded config file \"%s\"", app.configPath) - app.debug.Println("Loading storage") - - app.storage.invite_path = app.config.Section("files").Key("invites").String() - if err := app.storage.loadInvites(); err != nil { - app.err.Printf("Failed to load Invites: %v", err) - } - app.storage.emails_path = app.config.Section("files").Key("emails").String() - if err := app.storage.loadEmails(); err != nil { - app.err.Printf("Failed to load Emails: %v", err) - err := migrateEmailStorage(app) - if err != nil { - app.err.Printf("Failed to migrate Email storage: %v", err) - } - } - app.storage.policy_path = app.config.Section("files").Key("user_template").String() - if err := app.storage.loadPolicy(); err != nil { - app.err.Printf("Failed to load Policy: %v", err) - } - app.storage.configuration_path = app.config.Section("files").Key("user_configuration").String() - if err := app.storage.loadConfiguration(); err != nil { - app.err.Printf("Failed to load Configuration: %v", err) - } - app.storage.displayprefs_path = app.config.Section("files").Key("user_displayprefs").String() - if err := app.storage.loadDisplayprefs(); err != nil { - app.err.Printf("Failed to load Displayprefs: %v", err) - } - app.storage.users_path = app.config.Section("files").Key("users").String() - if err := app.storage.loadUserExpiries(); err != nil { - app.err.Printf("Failed to load Users: %v", err) - } - app.storage.telegram_path = app.config.Section("files").Key("telegram_users").String() - if err := app.storage.loadTelegramUsers(); err != nil { - app.err.Printf("Failed to load Telegram users: %v", err) - } - app.storage.discord_path = app.config.Section("files").Key("discord_users").String() - if err := app.storage.loadDiscordUsers(); err != nil { - app.err.Printf("Failed to load Discord users: %v", err) - } - app.storage.matrix_path = app.config.Section("files").Key("matrix_users").String() - if err := app.storage.loadMatrixUsers(); err != nil { - app.err.Printf("Failed to load Matrix users: %v", err) - } - app.storage.announcements_path = app.config.Section("files").Key("announcements").String() - if err := app.storage.loadAnnouncements(); err != nil { - app.err.Printf("Failed to load announcement templates: %v", err) - } - - app.storage.profiles_path = app.config.Section("files").Key("user_profiles").String() - app.storage.loadProfiles() - if app.config.Section("ombi").Key("enabled").MustBool(false) { - app.storage.ombi_path = app.config.Section("files").Key("ombi_template").String() - app.storage.loadOmbiTemplate() + app.debug.Printf("Connecting to Ombi") ombiServer := app.config.Section("ombi").Key("server").String() app.ombi = ombi.NewOmbi( ombiServer, diff --git a/migrations.go b/migrations.go index e2e32ab..0b81aea 100644 --- a/migrations.go +++ b/migrations.go @@ -201,6 +201,53 @@ type MigrationStatus struct { Done bool } +func loadLegacyData(app *appContext) { + app.storage.invite_path = app.config.Section("files").Key("invites").String() + if err := app.storage.loadInvites(); err != nil { + app.err.Printf("LegacyData: Failed to load Invites: %v", err) + } + app.storage.emails_path = app.config.Section("files").Key("emails").String() + if err := app.storage.loadEmails(); err != nil { + app.err.Printf("LegacyData: Failed to load Emails: %v", err) + err := migrateEmailStorage(app) + if err != nil { + app.err.Printf("LegacyData: Failed to migrate Email storage: %v", err) + } + } + app.storage.users_path = app.config.Section("files").Key("users").String() + if err := app.storage.loadUserExpiries(); err != nil { + app.err.Printf("LegacyData: Failed to load Users: %v", err) + } + app.storage.telegram_path = app.config.Section("files").Key("telegram_users").String() + if err := app.storage.loadTelegramUsers(); err != nil { + app.err.Printf("LegacyData: Failed to load Telegram users: %v", err) + } + app.storage.discord_path = app.config.Section("files").Key("discord_users").String() + if err := app.storage.loadDiscordUsers(); err != nil { + app.err.Printf("LegacyData: Failed to load Discord users: %v", err) + } + app.storage.matrix_path = app.config.Section("files").Key("matrix_users").String() + if err := app.storage.loadMatrixUsers(); err != nil { + app.err.Printf("LegacyData: Failed to load Matrix users: %v", err) + } + app.storage.announcements_path = app.config.Section("files").Key("announcements").String() + if err := app.storage.loadAnnouncements(); err != nil { + app.err.Printf("LegacyData: Failed to load announcement templates: %v", err) + } + + app.storage.profiles_path = app.config.Section("files").Key("user_profiles").String() + app.storage.loadProfiles() + + app.storage.customEmails_path = app.config.Section("files").Key("custom_emails").String() + app.storage.loadCustomEmails() + + app.MustSetValue("user_page", "enabled", "true") + if app.config.Section("user_page").Key("enabled").MustBool(false) { + app.storage.userPage_path = app.config.Section("files").Key("custom_user_page_content").String() + app.storage.loadUserPageContent() + } +} + func migrateToBadger(app *appContext) { // Check the DB to see if we've already migrated migrated := MigrationStatus{} @@ -209,48 +256,39 @@ func migrateToBadger(app *appContext) { return } app.info.Println("Migrating to Badger(hold)") - app.storage.loadAnnouncements() + loadLegacyData(app) for k, v := range app.storage.deprecatedAnnouncements { app.storage.SetAnnouncementsKey(k, v) } - app.storage.loadDiscordUsers() for jfID, v := range app.storage.deprecatedDiscord { app.storage.SetDiscordKey(jfID, v) } - app.storage.loadTelegramUsers() for jfID, v := range app.storage.deprecatedTelegram { app.storage.SetTelegramKey(jfID, v) } - app.storage.loadMatrixUsers() for jfID, v := range app.storage.deprecatedMatrix { app.storage.SetMatrixKey(jfID, v) } - app.storage.loadEmails() for jfID, v := range app.storage.deprecatedEmails { app.storage.SetEmailsKey(jfID, v) } - app.storage.loadInvites() for k, v := range app.storage.deprecatedInvites { app.storage.SetInvitesKey(k, v) } - app.storage.loadUserExpiries() for k, v := range app.storage.deprecatedUserExpiries { app.storage.SetUserExpiryKey(k, UserExpiry{Expiry: v}) } - app.storage.loadProfiles() for k, v := range app.storage.deprecatedProfiles { app.storage.SetProfileKey(k, v) } - app.storage.loadCustomEmails() - app.storage.loadUserPageContent() if _, ok := app.storage.GetCustomContentKey("UserCreated"); !ok { app.storage.SetCustomContentKey("UserCreated", app.storage.deprecatedCustomEmails.UserCreated) }