From e5658d1f61415054029b66cd2f703f1f40138460 Mon Sep 17 00:00:00 2001 From: Eugene Triguba Date: Sat, 18 Oct 2025 10:43:39 -0400 Subject: Add support for migration dependencies --- internal/services/migration_query_service.go | 90 ++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 internal/services/migration_query_service.go (limited to 'internal/services/migration_query_service.go') diff --git a/internal/services/migration_query_service.go b/internal/services/migration_query_service.go new file mode 100644 index 0000000..f505727 --- /dev/null +++ b/internal/services/migration_query_service.go @@ -0,0 +1,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 +} -- cgit v1.2.3