aboutsummaryrefslogtreecommitdiffstats
path: root/internal/services/migration_query_service.go
blob: f5057274bdc3fed8bc4098412557bca5d6368ed4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package services

import (
	"fmt"

	"git.sr.ht/~eugenetriguba/bolt/internal/models"
	"git.sr.ht/~eugenetriguba/bolt/internal/repositories"
)

type SortOrder int

const (
	SortOrderDesc SortOrder = iota
	SortOrderAsc
)

// MigrationQueryService handles querying and listing migrations from
// both the filesystem and database sources.
type MigrationQueryService struct {
	dbRepo   repositories.MigrationDBRepo
	fsRepo   repositories.MigrationFsRepo
	depGraph *MigrationDependencyGraph
}

func NewMigrationQueryService(
	dbRepo repositories.MigrationDBRepo,
	fsRepo repositories.MigrationFsRepo,
	depGraph *MigrationDependencyGraph,
) *MigrationQueryService {
	return &MigrationQueryService{
		dbRepo:   dbRepo,
		fsRepo:   fsRepo,
		depGraph: depGraph,
	}
}

// ListMigrations retrieves and combines migrations from both the filesystem and database,
// returning them in the specified order (topological).
func (s *MigrationQueryService) ListMigrations(order SortOrder) ([]*models.Migration, error) {
	// Assumption: All migration versions that have been applied
	// to the database exist locally in the list of filesystem migrations.
	// This would hold true unless someone decided to apply a migration and,
	// for whatever reason, delete it later from their filesystem. If there is
	// an applied migration that doesn't exist locally, it would not be shown
	// here.
	localMigrations, err := s.fsRepo.List()
	if err != nil {
		return nil, fmt.Errorf("unable to list out local filesystem migrations: %w", err)
	}

	appliedMigrations, err := s.dbRepo.List()
	if err != nil {
		return nil, fmt.Errorf(
			"unable to list out applied migrations from remote db: %w",
			err,
		)
	}

	return s.CombineMigrations(localMigrations, appliedMigrations, order)
}

// CombineMigrations combines local filesystem migrations with applied database migrations,
// marking migrations as applied where appropriate and sorting them in the specified order.
func (s *MigrationQueryService) CombineMigrations(
	localMigrations map[string]*models.Migration,
	appliedMigrations map[string]*models.Migration,
	order SortOrder,
) ([]*models.Migration, error) {
	migrations := make(map[string]*models.Migration, 0)
	for _, localMigration := range localMigrations {
		_, ok := appliedMigrations[localMigration.Version]
		if ok {
			localMigration.Applied = true
		}
		migrations[localMigration.Version] = localMigration
	}

	var sorted []*models.Migration
	var err error
	if order == SortOrderAsc {
		sorted, err = s.depGraph.TopologicalSort(migrations)
	} else {
		sorted, err = s.depGraph.ReverseTopologicalSort(migrations)
	}
	if err != nil {
		return nil, err
	}

	return sorted, nil
}