github.com/adharshmk96/stk@v1.2.3/pkg/sqlMigrator/migrate.go (about)

     1  package sqlmigrator
     2  
     3  import (
     4  	"fmt"
     5  	"slices"
     6  )
     7  
     8  type migrator struct {
     9  	DBRepo DBRepo
    10  }
    11  
    12  func NewMigrator(dbRepo DBRepo) *migrator {
    13  	return &migrator{
    14  		DBRepo: dbRepo,
    15  	}
    16  }
    17  
    18  func (m *migrator) MigrateUp(ctx *Context, num int) ([]*MigrationFileEntry, error) {
    19  	appliedMigrations := []*MigrationFileEntry{}
    20  	migrationToApply := LoadUnappliedMigrations(ctx)
    21  
    22  	if len(migrationToApply) == 0 {
    23  		fmt.Println("no migrations to apply")
    24  		return appliedMigrations, nil
    25  	}
    26  
    27  	num = min(num, len(migrationToApply))
    28  	if num > 0 {
    29  		migrationToApply = migrationToApply[:num]
    30  	}
    31  
    32  	for _, migration := range migrationToApply {
    33  		if ctx.DryRun {
    34  			displayMigration(migration)
    35  			continue
    36  		}
    37  
    38  		upFileContent, _ := migration.LoadFileContent()
    39  
    40  		err := m.DBRepo.Exec(upFileContent)
    41  		if err != nil {
    42  			return appliedMigrations, err
    43  		}
    44  
    45  		migration.Committed = true
    46  		appliedMigrations = append(appliedMigrations, migration)
    47  
    48  		// commit to db migration table
    49  		dbEntry := &MigrationDBEntry{
    50  			Number:    migration.Number,
    51  			Name:      migration.Name,
    52  			Direction: "up",
    53  		}
    54  
    55  		err = m.DBRepo.PushHistory(dbEntry)
    56  		if err != nil {
    57  			return appliedMigrations, err
    58  		}
    59  	}
    60  
    61  	return appliedMigrations, nil
    62  }
    63  
    64  func (m *migrator) MigrateDown(ctx *Context, num int) ([]*MigrationFileEntry, error) {
    65  	rolledBackMigrations := []*MigrationFileEntry{}
    66  	migrationToApply := LoadAppliedMigrations(ctx)
    67  
    68  	if len(migrationToApply) == 0 {
    69  		fmt.Println("no migrations to rollback")
    70  		return rolledBackMigrations, nil
    71  	}
    72  
    73  	slices.Reverse(migrationToApply)
    74  
    75  	num = min(num, len(migrationToApply))
    76  	if num > 0 {
    77  		migrationToApply = migrationToApply[:num]
    78  	}
    79  
    80  	for _, migration := range migrationToApply {
    81  		if ctx.DryRun {
    82  			displayMigration(migration)
    83  			continue
    84  		}
    85  
    86  		_, downFileContent := migration.LoadFileContent()
    87  
    88  		err := m.DBRepo.Exec(downFileContent)
    89  		if err != nil {
    90  			return rolledBackMigrations, err
    91  		}
    92  
    93  		migration.Committed = false
    94  		rolledBackMigrations = append(rolledBackMigrations, migration)
    95  
    96  		// commit to db migration table
    97  		dbEntry := &MigrationDBEntry{
    98  			Number:    migration.Number,
    99  			Name:      migration.Name,
   100  			Direction: "down",
   101  		}
   102  
   103  		err = m.DBRepo.PushHistory(dbEntry)
   104  		if err != nil {
   105  			return rolledBackMigrations, err
   106  		}
   107  	}
   108  
   109  	return rolledBackMigrations, nil
   110  }
   111  
   112  func (m *migrator) MigrationHistory(ctx *Context) ([]*MigrationDBEntry, error) {
   113  	return m.DBRepo.LoadHistory()
   114  }
   115  
   116  func displayMigration(migration *MigrationFileEntry) {
   117  	fileName := migration.EntryString()
   118  	fmt.Println("up\t:", fileName)
   119  }