I am not sure if this is doing right
This commit is contained in:
@@ -118,11 +118,12 @@ func (r *MigrationRunner) ApplyMigration(m Migration) (*MigrationResult, error)
|
|||||||
|
|
||||||
// Record migration in history
|
// Record migration in history
|
||||||
if !r.DryRun {
|
if !r.DryRun {
|
||||||
|
now := time.Now().Unix()
|
||||||
history := map[string]interface{}{
|
history := map[string]interface{}{
|
||||||
"migration_number": m.Number,
|
"migration_number": m.Number,
|
||||||
"version": m.Version,
|
"version": m.Version,
|
||||||
"description": m.Description,
|
"description": m.Description,
|
||||||
"applied_at": time.Now(),
|
"applied_at": now,
|
||||||
"applied_by": "migration-runner",
|
"applied_by": "migration-runner",
|
||||||
"execution_time_ms": time.Since(startTime).Milliseconds(),
|
"execution_time_ms": time.Since(startTime).Milliseconds(),
|
||||||
"success": true,
|
"success": true,
|
||||||
@@ -137,7 +138,7 @@ func (r *MigrationRunner) ApplyMigration(m Migration) (*MigrationResult, error)
|
|||||||
version := map[string]interface{}{
|
version := map[string]interface{}{
|
||||||
"version": m.Version,
|
"version": m.Version,
|
||||||
"migration_number": m.Number,
|
"migration_number": m.Number,
|
||||||
"applied_at": time.Now(),
|
"applied_at": now,
|
||||||
"backend_version": config.GetVersion(),
|
"backend_version": config.GetVersion(),
|
||||||
"description": m.Description,
|
"description": m.Description,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ import (
|
|||||||
"io"
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"strings"
|
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
@@ -48,6 +47,11 @@ func NewOrchestrator(db *gorm.DB) *DeploymentOrchestrator {
|
|||||||
// createBackup creates a pre-deployment backup
|
// createBackup creates a pre-deployment backup
|
||||||
// Returns (backupPath, isFreshInstall, error)
|
// Returns (backupPath, isFreshInstall, error)
|
||||||
func (o *DeploymentOrchestrator) createBackup() (string, bool, error) {
|
func (o *DeploymentOrchestrator) createBackup() (string, bool, error) {
|
||||||
|
// Check if this is a fresh installation first
|
||||||
|
if o.isFreshInstallation() {
|
||||||
|
return "", true, nil // Fresh install, no backup needed
|
||||||
|
}
|
||||||
|
|
||||||
// Get current version for backup metadata
|
// Get current version for backup metadata
|
||||||
runner := migrations.NewMigrationRunner(o.DB)
|
runner := migrations.NewMigrationRunner(o.DB)
|
||||||
currentVer, migNum, err := runner.GetCurrentVersion()
|
currentVer, migNum, err := runner.GetCurrentVersion()
|
||||||
@@ -60,10 +64,6 @@ func (o *DeploymentOrchestrator) createBackup() (string, bool, error) {
|
|||||||
backupService := backup.NewBackupService()
|
backupService := backup.NewBackupService()
|
||||||
metadata, err := backupService.CreateJSONBackup(currentVer, migNum)
|
metadata, err := backupService.CreateJSONBackup(currentVer, migNum)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
// Check if this is a fresh installation (no tables exist)
|
|
||||||
if strings.Contains(err.Error(), "doesn't exist") || strings.Contains(err.Error(), "no such table") {
|
|
||||||
return "", true, nil // Fresh install, no backup needed
|
|
||||||
}
|
|
||||||
return "", false, fmt.Errorf("failed to create backup: %w", err)
|
return "", false, fmt.Errorf("failed to create backup: %w", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -144,6 +144,20 @@ func (o *DeploymentOrchestrator) validateDeployment() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// isFreshInstallation checks if this is a fresh database installation
|
||||||
|
func (o *DeploymentOrchestrator) isFreshInstallation() bool {
|
||||||
|
// Check for core tables - if they don't exist, it's a fresh install
|
||||||
|
var count int64
|
||||||
|
|
||||||
|
// Check if characters table exists
|
||||||
|
err := o.DB.Raw("SELECT COUNT(*) FROM information_schema.tables WHERE table_schema = DATABASE() AND table_name = 'characters'").Scan(&count).Error
|
||||||
|
if err != nil || count == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// PrepareDeploymentPackage creates an export of all system and master data
|
// PrepareDeploymentPackage creates an export of all system and master data
|
||||||
func (o *DeploymentOrchestrator) PrepareDeploymentPackage(exportDir string) (*DeploymentPackage, error) {
|
func (o *DeploymentOrchestrator) PrepareDeploymentPackage(exportDir string) (*DeploymentPackage, error) {
|
||||||
logger.Info("═══════════════════════════════════════════════════")
|
logger.Info("═══════════════════════════════════════════════════")
|
||||||
|
|||||||
Reference in New Issue
Block a user