Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion lib/MySQL/Partition.pm
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ sub new {
bless \%args, $sub_class;
}

__PACKAGE__->_grow_methods(qw/create_partitions add_partitions drop_partitions/);
__PACKAGE__->_grow_methods(qw/create_partitions add_partitions drop_partitions truncate_partitions/);

sub retrieve_partitions {
my ($self, $table) = @_;
Expand Down Expand Up @@ -105,6 +105,12 @@ sub _build_drop_partitions_sql {
sprintf 'ALTER TABLE %s DROP PARTITION %s', $self->table, join(', ', @partition_names);
}

sub _build_truncate_partitions_sql {
my ($self, @partition_names) = @_;

sprintf 'ALTER TABLE %s TRUNCATE PARTITION %s', $self->table, join(', ', @partition_names);
}

sub _grow_methods {
my ($class, @methods) = @_;

Expand Down Expand Up @@ -169,6 +175,10 @@ MySQL::Partition - Utility for MySQL partitioning
print $handle->statement;
$handle->execute;

$list_partition->truncate_partitions('p1');
$handle = $list_partition->prepare_truncate_partitions('p2_3');
$handle->execute;

$list_partition->drop_partitions('p1');
$handle = $list_partition->prepare_drop_partitions('p2_3');
$handle->execute;
Expand Down Expand Up @@ -233,6 +243,8 @@ Returns the table has a specified partition name or not.

=head3 C<< $mysql_partition->drop_partitions(@partition_names) >>

=head3 C<< $mysql_partition->truncate_partitions(@partition_names) >>

=head2 Methods for MySQL::Partition::Handle

Each method for manipulating partition has C<prepare_*> method which returns L<MySQL::Partition::Handle> object.
Expand All @@ -245,6 +257,8 @@ Each method for manipulating partition has C<prepare_*> method which returns L<M

=item C<prepare_drop_partitions>

=item C<prepare_truncate_partitions>

=back

Actually, C<< $mysql_partition->create_partitions(...); >> is a shortcut of following.
Expand Down
2 changes: 2 additions & 0 deletions t/01_basic.t
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ subtest list => sub {
'ALTER TABLE test ADD PARTITION (PARTITION p2 VALUES IN (2, 3))';
is $list_partition->_build_drop_partitions_sql('p1'),
'ALTER TABLE test DROP PARTITION p1';
is $list_partition->_build_truncate_partitions_sql('p1'),
'ALTER TABLE test TRUNCATE PARTITION p1';
};

subtest range => sub {
Expand Down
52 changes: 51 additions & 1 deletion t/02_mysqld.t
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ subtest list => sub {
is_deeply \@partitions, ['p1', 'p2'];
};

subtest 'truncate_partition' => sub {
$dbh->do(q[INSERT INTO `test` (`event_id`) VALUES (1), (2)]);
is_deeply $dbh->selectrow_arrayref(q[SELECT COUNT(*) FROM `test` WHERE `event_id` = 1]), [1];
is_deeply $dbh->selectrow_arrayref(q[SELECT COUNT(*) FROM `test` WHERE `event_id` = 2]), [1];
$list_partition->truncate_partitions('p1');
pass 'truncate_partition ok';
is_deeply $dbh->selectrow_arrayref(q[SELECT COUNT(*) FROM `test` WHERE `event_id` = 1]), [0];
is_deeply $dbh->selectrow_arrayref(q[SELECT COUNT(*) FROM `test` WHERE `event_id` = 2]), [1];
ok $list_partition->has_partition('p1');
my @partitions = $list_partition->retrieve_partitions;
is_deeply \@partitions, ['p1', 'p2'];
};

subtest 'drop_partition' => sub {
$list_partition->drop_partitions('p1');
pass 'drop_partition ok';
Expand Down Expand Up @@ -95,6 +108,34 @@ subtest 'range columns' => sub {
is_deeply \@partitions, ['p20100101', 'p20110101', 'p20120101'];
};

subtest 'truncate_partition' => sub {
$dbh->do(q[INSERT INTO `test2` (`created_at`) VALUES
("2010-01-01 00:00:00"), ("2010-12-31 23:59:59"),
("2011-01-01 00:00:00"), ("2011-12-31 23:59:59")
]);
is_deeply $dbh->selectrow_arrayref(q[
SELECT COUNT(*) FROM `test2`
WHERE `created_at` BETWEEN "2010-01-01 00:00:00" AND "2010-12-31 23:59:59"
]), [2];
is_deeply $dbh->selectrow_arrayref(q[
SELECT COUNT(*) FROM `test2`
WHERE `created_at` BETWEEN "2011-01-01 00:00:00" AND "2011-12-31 23:59:59"
]), [2];
$range_partition->truncate_partitions('p20110101');
pass 'truncate_partition ok';
is_deeply $dbh->selectrow_arrayref(q[
SELECT COUNT(*) FROM `test2`
WHERE `created_at` BETWEEN "2010-01-01 00:00:00" AND "2010-12-31 23:59:59"
]), [0];
is_deeply $dbh->selectrow_arrayref(q[
SELECT COUNT(*) FROM `test2`
WHERE `created_at` BETWEEN "2011-01-01 00:00:00" AND "2011-12-31 23:59:59"
]), [2];
ok $range_partition->has_partition('p20110101');
my @partitions = $range_partition->retrieve_partitions;
is_deeply \@partitions, ['p20100101', 'p20110101', 'p20120101'];
};

subtest 'drop_partition' => sub {
$range_partition->drop_partitions('p20110101');
pass 'drop_partition ok';
Expand Down Expand Up @@ -196,11 +237,20 @@ subtest 'use handle' => sub {
is_deeply [$list_partition->retrieve_partitions], ['p1', 'p2'];
};

subtest 'truncate_partition' => sub {
$dbh->do(q[INSERT INTO `test5` (`event_id`) VALUES (1)]);
my $handle = $list_partition->prepare_truncate_partitions('p1');
is_deeply $dbh->selectrow_arrayref(q[SELECT COUNT(*) FROM `test5` WHERE `event_id` = 1]), [1];
$handle->execute;
is_deeply $dbh->selectrow_arrayref(q[SELECT COUNT(*) FROM `test5` WHERE `event_id` = 1]), [0];
pass 'truncate_partitions ok';
};

subtest 'drop_partition' => sub {
my $handle = $list_partition->prepare_drop_partitions('p1');
is_deeply [$list_partition->retrieve_partitions], ['p1', 'p2'];
$handle->execute;
pass 'add_partitions ok';
pass 'drop_partitions ok';
is_deeply [$list_partition->retrieve_partitions], ['p2'];
};
};
Expand Down