DBIx-BatchChunker
view release on metacpan or search on metacpan
lib/DBIx/BatchChunker.pm view on Meta::CPAN
package DBIx::BatchChunker;
our $AUTHORITY = 'cpan:GSG';
# ABSTRACT: Run large database changes safely
use version;
our $VERSION = 'v1.0.2'; # VERSION
use Moo;
use MooX::StrictConstructor;
use CLDR::Number;
use Types::Standard qw( Str Bool Undef ArrayRef HashRef CodeRef InstanceOf Tuple Maybe Optional slurpy );
use Types::Numbers qw( NumRange UnsignedInt PerlSafeInt PositiveInt PositiveOrZeroNum );
use Type::Utils;
use List::Util 1.33 (qw( min max sum any first )); # has any/all/etc.
use Math::BigInt upgrade => 'Math::BigFloat';
use Math::BigFloat;
use POSIX qw( ceil );
use Scalar::Util qw( blessed weaken );
use Term::ProgressBar 2.14; # with silent option
use Time::HiRes qw( time sleep );
use DBIx::BatchChunker::LoopState;
# Don't export the above, but don't conflict with StrictConstructor, either
use namespace::clean -except => [qw< new meta >];
# This is now an unused, dummy variable
our $DB_MAX_ID = ~0;
#pod =encoding utf8
#pod
#pod =head1 SYNOPSIS
#pod
#pod use DBIx::BatchChunker;
#pod
#pod my $account_rs = $schema->resultset('Account')->search({
#pod account_type => 'deprecated',
#pod });
#pod
#pod my %params = (
#pod chunk_size => 5000,
#pod target_time => 5,
#pod
#pod rs => $account_rs,
#pod id_name => 'account_id',
#pod
#pod coderef => sub { $_[1]->delete },
#pod sleep => 1,
#pod verbose => 1,
#pod
#pod progress_name => 'Deleting deprecated accounts',
#pod process_past_max => 1,
#pod );
#pod
#pod # EITHER:
#pod # 1) Automatically construct and execute the changes:
#pod
#pod DBIx::BatchChunker->construct_and_execute(%params);
#pod
#pod # OR
#pod # 2) Manually construct and execute the changes:
#pod
#pod my $batch_chunker = DBIx::BatchChunker->new(%params);
#pod
#pod $batch_chunker->calculate_ranges;
#pod $batch_chunker->execute;
#pod
#pod =head1 DESCRIPTION
#pod
#pod This utility class is for running a large batch of DB changes in a manner that doesn't
#pod cause huge locks, outages, and missed transactions. It's highly flexible to allow for
#pod many different kinds of change operations, and dynamically adjusts chunks to its
#pod workload.
#pod
#pod It works by splitting up DB operations into smaller chunks within a loop. These chunks
#pod are transactionalized, either naturally as single-operation bulk work or by the loop
#pod itself. The full range is calculated beforehand to get the right start/end points.
#pod A L<progress bar|/Progress Bar Attributes> will be created to let the deployer know the
( run in 0.600 second using v1.01-cache-2.11-cpan-600a1bdf6e4 )