Skip to content

Commit aeb2f7c

Browse files
committed
Test for duplicates, add a few tests of reorganize_partition
1 parent e66dd3f commit aeb2f7c

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

partitionmanager/cli.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@
55
import traceback
66

77
from partitionmanager.table_append_partition import (
8-
get_partition_map,
8+
format_sql_reorganize_partition_command,
99
get_autoincrement,
10+
get_partition_map,
11+
parition_name_now,
1012
reorganize_partition,
11-
format_sql_reorganize_partition_command,
1213
)
1314
from partitionmanager.types import SqlInput, toSqlUrl
1415
from partitionmanager.sql import SubprocessDatabaseCommand, IntegratedDatabaseCommand
@@ -47,7 +48,9 @@ def partition_cmd(args):
4748

4849
partitions = get_partition_map(dbcmd, table)
4950

50-
filled_partition_id, partitions = reorganize_partition(partitions, ai)
51+
filled_partition_id, partitions = reorganize_partition(
52+
partitions, parition_name_now(), ai
53+
)
5154

5255
sql_cmd = format_sql_reorganize_partition_command(
5356
table, partition_to_alter=filled_partition_id, partition_list=partitions

partitionmanager/table_append_partition.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from partitionmanager.types import (
2+
DuplicatePartitionException,
23
MismatchedIdException,
34
SqlInput,
45
TableInformationException,
@@ -121,17 +122,20 @@ def parition_name_now():
121122
return datetime.now(tz=timezone.utc).strftime("p_%Y%m%d")
122123

123124

124-
def reorganize_partition(partition_list, auto_increment):
125+
def reorganize_partition(partition_list, new_partition_name, auto_increment):
125126
"""
126127
From a partial partitions list (ending with a single value that indicates MAX VALUE),
127128
add a new partition at the auto_increment number.
128129
"""
129130
last_value = partition_list.pop()
130131
if type(last_value) is not str:
131132
raise UnexpectedPartitionException(last_value)
133+
if last_value == new_partition_name:
134+
raise DuplicatePartitionException(last_value)
135+
132136
reorganized_list = list()
133137
reorganized_list.append((last_value, f"({auto_increment})"))
134-
reorganized_list.append((parition_name_now(), "MAXVALUE"))
138+
reorganized_list.append((new_partition_name, "MAXVALUE"))
135139
return last_value, reorganized_list
136140

137141

partitionmanager/table_append_partition_test.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,18 @@
44
import argparse
55
from partitionmanager.types import (
66
DatabaseCommand,
7+
DuplicatePartitionException,
78
TableInformationException,
89
MismatchedIdException,
910
SqlInput,
11+
UnexpectedPartitionException,
1012
)
1113
from partitionmanager.table_append_partition import (
1214
parse_table_information_schema,
1315
parse_partition_map,
1416
get_autoincrement,
1517
get_partition_map,
18+
reorganize_partition,
1619
)
1720

1821

@@ -174,5 +177,20 @@ def test_okay(self):
174177
SqlInput("zz-table")
175178

176179

180+
class TestReorganizePartitions(unittest.TestCase):
181+
def test_list_without_final_entry(self):
182+
with self.assertRaises(UnexpectedPartitionException):
183+
reorganize_partition([("a", 1), ("b", 2)], "new", 3)
184+
185+
def test_reorganize_with_duplicate(self):
186+
with self.assertRaises(DuplicatePartitionException):
187+
reorganize_partition([("a", 1), "b"], "b", 3)
188+
189+
def test_reorganize(self):
190+
last_value, reorg_list = reorganize_partition([("a", 1), "b"], "c", 2)
191+
self.assertEqual(last_value, "b")
192+
self.assertEqual(reorg_list, [("b", "(2)"), ("c", "MAXVALUE")])
193+
194+
177195
if __name__ == "__main__":
178196
unittest.main()

partitionmanager/types.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,14 @@ class MismatchedIdException(Exception):
4646
pass
4747

4848

49+
class DuplicatePartitionException(Exception):
50+
"""
51+
Raise if a partition being created already exists.
52+
"""
53+
54+
pass
55+
56+
4957
class UnexpectedPartitionException(Exception):
5058
"""
5159
Raised when the partition map is unexpected.

0 commit comments

Comments
 (0)