// Down a list of migrations
Down a list of migrations
// Down a list of migrations
func (s Service) Down(ctx context.Context, ms MigrationsList, quantity int) error {
	t, err := s.repo.BeginTx(ctx)
	if err != nil {
		return errors.Wrapf(err, "migration.Service.Down: transaction begin error")
	}
	list, err := s.repo.QueryTx(ctx, t, nil, 0, 0)
	if err != nil {
		if errors.Is(err, apperror.ErrNotFound) {
			return err
		}
		return errors.Wrapf(apperror.ErrInternal, "migration.Service.Down: get list logs of migrations error: %v", err)
	}
	gl			:= GroupLogsByStatus(list)
	migrations	:= MigrationsListFilterExistsByKeys(ms, gl[StatusApplied])
	ids			:= migrations.IDs()
	sort.Sort(sort.Reverse(sort.IntSlice(ids)))
	if quantity < 1 {
		quantity = DefaultDownQuantity
	}
	if len(ids) < quantity {
		quantity = len(ids)
	}
	if quantity == 0 {
		return apperror.ErrNotFound
	}
	migrationsLogsForUpdate, _, er := s.downProceed(ctx, migrations, ids[:quantity])
	err = s.repo.BatchUpdateTx(ctx, t, migrationsLogsForUpdate)
	if err != nil {
		err = t.Rollback()
		if err != nil {
			return errors.Wrapf(err, "migration.Service.Down: transaction rollback error")
		}
		return errors.Wrapf(err, "migration.Service.Down: batch update error")
	}
	err = t.Commit()
	if err != nil {
		return errors.Wrapf(err, "migration.Service.Down: transaction commit error")
	}
	return er
}
func ( s Service ) Down ( ctx context . Context , ms MigrationsList , quantity int ) error { t , err := s . repo . BeginTx ( ctx ) if err != nil { return errors . Wrapf ( err , " " ) } list , err := s . repo . QueryTx ( ctx , t , nil , 0 , 0 ) if err != nil { if errors . Is ( err , apperror . ErrNotFound ) { return err } return errors . Wrapf ( apperror . ErrInternal , " " , err ) } gl := GroupLogsByStatus ( list ) migrations := MigrationsListFilterExistsByKeys ( ms , gl [ StatusApplied ] ) ids := migrations . IDs ( ) sort . Sort ( sort . Reverse ( sort . IntSlice ( ids ) ) ) if quantity < 1 { quantity = DefaultDownQuantity } if len ( ids ) < quantity { quantity = len ( ids ) } if quantity == 0 { return apperror . ErrNotFound } migrationsLogsForUpdate , _ , er := s . downProceed ( ctx , migrations , ids [ : quantity ] ) err = s . repo . BatchUpdateTx ( ctx , t , migrationsLogsForUpdate ) if err != nil { err = t . Rollback ( ) if err != nil { return errors . Wrapf ( err , " " ) } return errors . Wrapf ( err , " " ) } err = t . Commit ( ) if err != nil { return errors . Wrapf ( err , " " ) } return er }
Down a list of migrations
Down a list of migrations