// Up a list of migrations
Up a list of migrations
// Up a list of migrations
func (s Service) Up(ctx context.Context, ms MigrationsList, quantity int) error {
	t, err := s.repo.BeginTx(ctx)
	if err != nil {
		return errors.Wrapf(err, "migration.Service.Up: transaction begin error")
	}
	list, err := s.repo.QueryTx(ctx, t, nil, 0, 0)
	if err != nil {
		return errors.Wrapf(apperror.ErrInternal, "migration.Service.Down: get list logs of migrations error: %v", err)
	}
	gl			:= GroupLogsByStatus(list)
	migrations	:= MigrationsListFilterExceptByKeys(ms, gl[StatusApplied])
	ids			:= migrations.IDs()
	sort.Ints(ids)
	if quantity < 1 {
		quantity = len(ids)
	}
	if len(ids) < quantity {
		quantity = len(ids)
	}
	if quantity == 0 {
		return apperror.ErrNotFound
	}
	appliedMigrationsLogs, idErr, err := s.upProceed(ctx, migrations, ids[:quantity])
	migrationsLogsForUpdate	:= MigrationsLogsFilterExistsByKeys(appliedMigrationsLogs, gl[StatusNotApplied])
	migrationsLogsForCreate := MigrationsLogsFilterExceptByKeys(appliedMigrationsLogs, gl[StatusNotApplied])
	if err != nil {
		if mLog, ok := gl[StatusNotApplied][idErr]; ok {
			mLog.Status = StatusError
			migrationsLogsForUpdate[idErr] = mLog
		} else {
			mLog = *migrations[idErr].Log(StatusError)
			migrationsLogsForCreate[idErr] = mLog
		}
	}
	err = s.repo.BatchUpdateTx(ctx, t, migrationsLogsForUpdate)
	if err != nil {
		err = t.Rollback()
		if err != nil {
			return errors.Wrapf(err, "migration.Service.Up: transaction rollback error")
		}
		return errors.Wrapf(err, "migration.Service.Up: batch update error")
	}
	err = s.repo.BatchCreateTx(ctx, t, migrationsLogsForCreate)
	if err != nil {
		err = t.Rollback()
		if err != nil {
			return errors.Wrapf(err, "migration.Service.Up: transaction rollback error")
		}
		return errors.Wrapf(err, "migration.Service.Up: batch create error")
	}
	err = t.Commit()
	if err != nil {
		return errors.Wrapf(err, "migration.Service.Up: transaction commit error")
	}
	return nil
}
func ( s Service ) Up ( 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 { return errors . Wrapf ( apperror . ErrInternal , " " , err ) } gl := GroupLogsByStatus ( list ) migrations := MigrationsListFilterExceptByKeys ( ms , gl [ StatusApplied ] ) ids := migrations . IDs ( ) sort . Ints ( ids ) if quantity < 1 { quantity = len ( ids ) } if len ( ids ) < quantity { quantity = len ( ids ) } if quantity == 0 { return apperror . ErrNotFound } appliedMigrationsLogs , idErr , err := s . upProceed ( ctx , migrations , ids [ : quantity ] ) migrationsLogsForUpdate := MigrationsLogsFilterExistsByKeys ( appliedMigrationsLogs , gl [ StatusNotApplied ] ) migrationsLogsForCreate := MigrationsLogsFilterExceptByKeys ( appliedMigrationsLogs , gl [ StatusNotApplied ] ) if err != nil { if mLog , ok := gl [ StatusNotApplied ] [ idErr ] ; ok { mLog . Status = StatusError migrationsLogsForUpdate [ idErr ] = mLog } else { mLog = * migrations [ idErr ] . Log ( StatusError ) migrationsLogsForCreate [ idErr ] = mLog } } 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 = s . repo . BatchCreateTx ( ctx , t , migrationsLogsForCreate ) 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 nil }
Up a list of migrations
Up a list of migrations