From 7106eabfd52e81f5671aeb74734f5ef66d17e5cb Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Wed, 18 Nov 2015 21:16:23 +0900 Subject: [PATCH 1/2] support TRUNCATE PARTITION --- lib/MySQL/Partition.pm | 16 +++++++++++++- t/01_basic.t | 2 ++ t/02_mysqld.t | 50 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 1 deletion(-) diff --git a/lib/MySQL/Partition.pm b/lib/MySQL/Partition.pm index ab4f38e..407a928 100644 --- a/lib/MySQL/Partition.pm +++ b/lib/MySQL/Partition.pm @@ -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) = @_; @@ -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) = @_; @@ -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; @@ -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 method which returns L object. @@ -245,6 +257,8 @@ Each method for manipulating partition has C method which returns L +=item C + =back Actually, C<< $mysql_partition->create_partitions(...); >> is a shortcut of following. diff --git a/t/01_basic.t b/t/01_basic.t index b21248e..3788b00 100644 --- a/t/01_basic.t +++ b/t/01_basic.t @@ -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 { diff --git a/t/02_mysqld.t b/t/02_mysqld.t index e5d0b12..4173a53 100644 --- a/t/02_mysqld.t +++ b/t/02_mysqld.t @@ -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'; @@ -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'; @@ -196,6 +237,15 @@ 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']; From fe9139d32e1c681592c0d4743322d494333c1015 Mon Sep 17 00:00:00 2001 From: Ichinose Shogo Date: Wed, 18 Nov 2015 21:18:01 +0900 Subject: [PATCH 2/2] fix name of test --- t/02_mysqld.t | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/t/02_mysqld.t b/t/02_mysqld.t index 4173a53..ef21d50 100644 --- a/t/02_mysqld.t +++ b/t/02_mysqld.t @@ -250,7 +250,7 @@ subtest 'use handle' => 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']; }; };