github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/internal/database/migration_service.go (about)

     1  package database
     2  
     3  import (
     4  	"github.com/sharovik/devbot/internal/log"
     5  )
     6  
     7  // BaseMigrationInterface the interface for all migration files
     8  type BaseMigrationInterface interface {
     9  	GetName() string
    10  	Execute() error
    11  }
    12  
    13  // MigrationService the service for migrations based on GoLang run.
    14  // This can be useful if you want to use abstraction of our SQL client in your migration
    15  type MigrationService struct {
    16  	Logger     log.LoggerInstance
    17  	Dictionary BaseDatabaseInterface
    18  	migrations []BaseMigrationInterface
    19  }
    20  
    21  // SetMigration method loads the migration into memory. Use this method to prepare your migration for execution
    22  func (s *MigrationService) SetMigration(newMigration BaseMigrationInterface) {
    23  	if s.migrations == nil {
    24  		s.migrations = []BaseMigrationInterface{}
    25  	}
    26  
    27  	for _, m := range s.migrations {
    28  		if m.GetName() == newMigration.GetName() {
    29  			return
    30  		}
    31  	}
    32  
    33  	s.migrations = append(s.migrations, newMigration)
    34  }
    35  
    36  func (s *MigrationService) finish() {
    37  	s.migrations = nil
    38  }
    39  
    40  // RunMigrations method will run all migrations, which are set to migrations variable
    41  func (s MigrationService) RunMigrations() error {
    42  	for _, migration := range s.migrations {
    43  		isMigrationAlreadyExecuted, err := s.Dictionary.IsMigrationAlreadyExecuted(migration.GetName())
    44  		if err != nil {
    45  			return err
    46  		}
    47  
    48  		if isMigrationAlreadyExecuted {
    49  			continue
    50  		}
    51  
    52  		s.Logger.Info().Str("migration_name", migration.GetName()).Msg("Running migration")
    53  		if err := migration.Execute(); err != nil {
    54  			log.Logger().AddError(err).Str("migration_name", migration.GetName()).Msg("Failed to execute the migration")
    55  			continue
    56  		}
    57  
    58  		if err := s.Dictionary.MarkMigrationExecuted(migration.GetName()); err != nil {
    59  			return err
    60  		}
    61  	}
    62  
    63  	s.finish()
    64  	return nil
    65  }