Kaynağa Gözat

add startup migrations

Mohammed Nafees 3 yıl önce
ebeveyn
işleme
ed0967ce69

+ 37 - 0
cmd/migrate/main.go

@@ -1,18 +1,22 @@
 package main
 
 import (
+	"errors"
 	"log"
 
 	"github.com/porter-dev/porter/api/server/shared/config/envloader"
 	"github.com/porter-dev/porter/cmd/migrate/enable_cluster_preview_envs"
 	"github.com/porter-dev/porter/cmd/migrate/keyrotate"
 	"github.com/porter-dev/porter/cmd/migrate/populate_source_config_display_name"
+	"github.com/porter-dev/porter/cmd/migrate/startup_migrations"
 
 	adapter "github.com/porter-dev/porter/internal/adapter"
+	"github.com/porter-dev/porter/internal/models"
 	"github.com/porter-dev/porter/internal/repository/gorm"
 	lr "github.com/porter-dev/porter/pkg/logger"
 
 	"github.com/joeshaw/envdecode"
+	pgorm "gorm.io/gorm"
 )
 
 func main() {
@@ -49,6 +53,39 @@ func main() {
 		return
 	}
 
+	dbMigration := &models.DbMigration{}
+
+	if err := db.Model(&models.DbMigration{}).First(dbMigration).Error; err != nil {
+		if errors.Is(err, pgorm.ErrRecordNotFound) {
+			dbMigration.Version = 0
+		} else {
+			logger.Fatal().Err(err).Msg("failed to check for db migration version")
+			return
+		}
+	}
+
+	latestMigrationVersion := startup_migrations.LatestMigrationVersion
+
+	if dbMigration.Version < latestMigrationVersion {
+		for ver, fn := range startup_migrations.StartupMigrations {
+			if ver > dbMigration.Version {
+				err := fn(db, logger)
+
+				if err != nil {
+					logger.Fatal().Err(err).Msg("failed to run startup migration script")
+					return
+				}
+			}
+		}
+
+		dbMigration.Version = latestMigrationVersion
+
+		if err := db.Save(dbMigration).Error; err != nil {
+			logger.Fatal().Err(err).Msg("failed to update migration version to latest")
+			return
+		}
+	}
+
 	if shouldRotate, oldKeyStr, newKeyStr := shouldKeyRotate(); shouldRotate {
 		oldKey := [32]byte{}
 		newKey := [32]byte{}

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

@@ -0,0 +1,11 @@
+/*
+                   === 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/enable_cluster_preview_envs"
+	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] = enable_cluster_preview_envs.EnableClusterPreviewEnvs
+}

+ 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

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