diff options
Diffstat (limited to 'internal/services/migration_query_service.go')
| -rw-r--r-- | internal/services/migration_query_service.go | 90 |
1 files changed, 0 insertions, 90 deletions
diff --git a/internal/services/migration_query_service.go b/internal/services/migration_query_service.go deleted file mode 100644 index f505727..0000000 --- a/internal/services/migration_query_service.go +++ /dev/null @@ -1,90 +0,0 @@ -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 -} |