Просмотр исходного кода

startup migrations will be run automatically when version mismatches

Mohammed Nafees 3 лет назад
Родитель
Сommit
fc6ae04134

+ 33 - 0
api/server/shared/config/loader/loader.go

@@ -1,6 +1,7 @@
 package loader
 
 import (
+	"errors"
 	"fmt"
 	"io/ioutil"
 	"net/http"
@@ -12,6 +13,7 @@ import (
 	"github.com/porter-dev/porter/api/server/shared/config/env"
 	"github.com/porter-dev/porter/api/server/shared/config/envloader"
 	"github.com/porter-dev/porter/api/server/shared/websocket"
+	"github.com/porter-dev/porter/cmd/migrate/startup_migrations"
 	"github.com/porter-dev/porter/internal/adapter"
 	"github.com/porter-dev/porter/internal/analytics"
 	"github.com/porter-dev/porter/internal/auth/sessionstore"
@@ -19,6 +21,7 @@ import (
 	"github.com/porter-dev/porter/internal/billing"
 	"github.com/porter-dev/porter/internal/helm/urlcache"
 	"github.com/porter-dev/porter/internal/integrations/powerdns"
+	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/notifier"
 	"github.com/porter-dev/porter/internal/notifier/sendgrid"
 	"github.com/porter-dev/porter/internal/oauth"
@@ -79,6 +82,36 @@ func (e *EnvConfigLoader) LoadConfig() (res *config.Config, err error) {
 		return nil, err
 	}
 
+	dbMigration := &models.DbMigration{}
+
+	if err := InstanceDB.Model(&models.DbMigration{}).First(dbMigration).Error; err != nil {
+		if errors.Is(err, pgorm.ErrRecordNotFound) {
+			dbMigration.Version = 0
+		} else {
+			return nil, err
+		}
+	}
+
+	latestMigrationVersion := startup_migrations.LatestMigrationVersion
+
+	if dbMigration.Version < latestMigrationVersion {
+		for ver, fn := range startup_migrations.StartupMigrations {
+			if ver > dbMigration.Version {
+				err := fn(InstanceDB, res.Logger)
+
+				if err != nil {
+					return nil, err
+				}
+			}
+		}
+
+		dbMigration.Version = latestMigrationVersion
+
+		if err := InstanceDB.Save(dbMigration).Error; err != nil {
+			return nil, err
+		}
+	}
+
 	var key [32]byte
 
 	for i, b := range []byte(envConf.DBConf.EncryptionKey) {

+ 13 - 0
cmd/migrate/startup_migrations/doc.go

@@ -0,0 +1,13 @@
+/*
+
+                   === Mandatory Migrations at Startup ===
+
+   This package contains the migrations that are run at startup. Such migrations are
+   mandatory by nature, especially for self-hosted customers.
+
+   A globally accessible map structure shall be maintained and updated with the respective
+   migration scripts (functions) attached with the migration version they should be run with.
+
+*/
+
+package startup_migrations

+ 17 - 0
cmd/migrate/startup_migrations/global_map.go

@@ -0,0 +1,17 @@
+package startup_migrations
+
+import (
+	"github.com/porter-dev/porter/cmd/migrate/migrate_legacy_rbac"
+	lr "github.com/porter-dev/porter/pkg/logger"
+	"gorm.io/gorm"
+)
+
+const LatestMigrationVersion uint = 1
+
+type MigrationFunc func(db *gorm.DB, logger *lr.Logger) error
+
+var StartupMigrations = make(map[uint]MigrationFunc)
+
+func init() {
+	StartupMigrations[1] = migrate_legacy_rbac.MigrateFromLegacyRBAC
+}

+ 9 - 0
internal/models/db_migration.go

@@ -0,0 +1,9 @@
+package models
+
+import "gorm.io/gorm"
+
+type DbMigration struct {
+	gorm.Model
+
+	Version uint
+}

+ 1 - 0
internal/repository/gorm/migrate.go

@@ -57,6 +57,7 @@ func AutoMigrate(db *gorm.DB, debug bool) error {
 		&models.StackResource{},
 		&models.StackSourceConfig{},
 		&models.StackEnvGroup{},
+		&models.DbMigration{},
 		&ints.KubeIntegration{},
 		&ints.BasicIntegration{},
 		&ints.OIDCIntegration{},