From 5e32c6b43125cc5ab89bb51372830ce15bf49fb1 Mon Sep 17 00:00:00 2001 From: Alex <$(pass /github/email)> Date: Sun, 13 Oct 2024 13:43:03 +0200 Subject: [PATCH] mod: db migration --- internal/database/db.go | 41 ++++++++++++++++++++++++++--------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/internal/database/db.go b/internal/database/db.go index 5372d51..c6a6b3a 100644 --- a/internal/database/db.go +++ b/internal/database/db.go @@ -37,21 +37,9 @@ func Open(dbPath string, adminMail string) error { logger.Info.Print("Opened DB") - var count int64 - db.Model(&models.User{}).Count(&count) - if count == 0 { - subscriptionModels := createSubscriptionModels() - for _, model := range subscriptionModels { - result := db.Create(&model) - if result.Error != nil { - return result.Error - } - } - var createdModel models.SubscriptionModel - if err := db.First(&createdModel).Error; err != nil { - return err - } - + var categoriesCount int64 + db.Model(&models.Category{}).Count(&categoriesCount) + if categoriesCount == 0 { categories := createLicenceCategories() for _, model := range categories { result := db.Create(&model) @@ -59,6 +47,28 @@ func Open(dbPath string, adminMail string) error { return result.Error } } + } + + var subscriptionsCount int64 + db.Model(&models.SubscriptionModel{}).Count(&subscriptionsCount) + if subscriptionsCount == 0 { + subscriptionModels := createSubscriptionModels() + for _, model := range subscriptionModels { + result := db.Create(&model) + if result.Error != nil { + return result.Error + } + } + } + + var userCount int64 + db.Model(&models.User{}).Count(&userCount) + if userCount == 0 { + var createdModel models.SubscriptionModel + if err := db.First(&createdModel).Error; err != nil { + return err + } + admin, err := createAdmin(adminMail, createdModel.ID) if err != nil { return err @@ -68,6 +78,7 @@ func Open(dbPath string, adminMail string) error { return result.Error } } + return nil }