2
2
# Copyright 2021 Red Hat Inc.
3
3
# SPDX-License-Identifier: Apache-2.0
4
4
#
5
+ import calendar
5
6
import datetime
6
7
import json
7
8
import uuid
9
+ from datetime import timedelta
8
10
9
11
from django .db import connection as conn
10
12
from django_tenants .utils import schema_context
@@ -22,6 +24,15 @@ def _execute(sql, params=None):
22
24
return cur
23
25
24
26
27
+ def handle_leap_year_overflow (date , years ):
28
+ try :
29
+ new_date = date .replace (year = (date .year + years ))
30
+ except ValueError :
31
+ # If an error occurs, set the day to the last day of the month
32
+ new_date = (date + timedelta (days = 31 )).replace (year = (date .year + years ), day = 1 )
33
+ return new_date
34
+
35
+
25
36
def _get_table_partition_info (schema , table ):
26
37
sql = """
27
38
select c.relkind,
@@ -909,7 +920,7 @@ def test_repartition_table(self):
909
920
"""
910
921
with schema_context (self .schema_name ):
911
922
aws_lids = AWSCostEntryLineItemDailySummary .objects .order_by ("-usage_start" )[0 ]
912
- year10_usage_start = aws_lids . usage_start . replace ( year = ( aws_lids .usage_start . year + 10 ) )
923
+ year10_usage_start = handle_leap_year_overflow ( aws_lids .usage_start , 10 )
913
924
aws_lids .usage_start = year10_usage_start
914
925
aws_lids .save ()
915
926
year10_count = 0
@@ -952,10 +963,11 @@ def test_repartition_all_tables(self):
952
963
"""
953
964
with schema_context (self .schema_name ):
954
965
aws_lids = AWSCostEntryLineItemDailySummary .objects .order_by ("-usage_start" )[0 ]
966
+ aws_lids .usage_start = handle_leap_year_overflow (aws_lids .usage_start , 11 )
955
967
aws_lids .usage_start = aws_lids .usage_start .replace (year = (aws_lids .usage_start .year + 11 ))
956
968
aws_lids .save ()
957
969
ocp_lids = OCPUsageLineItemDailySummary .objects .order_by ("-usage_start" )[0 ]
958
- ocp_lids .usage_start = ocp_lids . usage_start . replace ( year = ( aws_lids .usage_start . year + 11 ) )
970
+ ocp_lids .usage_start = handle_leap_year_overflow ( aws_lids .usage_start , 11 )
959
971
ocp_lids .save ()
960
972
with conn .cursor () as cur :
961
973
cur .execute (
@@ -1019,7 +1031,14 @@ def test_repartition_all_tables(self):
1019
1031
if new_ocp_lids .usage_start .day < 28
1020
1032
else new_ocp_lids .usage_start .day - 1
1021
1033
)
1022
- new_ocp_lids .usage_start = ocp_lids .usage_start .replace (day = new_day )
1034
+ if (
1035
+ ocp_lids .usage_start .month == 2
1036
+ and ocp_lids .usage_start .day == 28
1037
+ and calendar .isleap (ocp_lids .usage_start .year )
1038
+ ):
1039
+ new_ocp_lids .usage_start = ocp_lids .usage_start .replace (day = 29 )
1040
+ else :
1041
+ new_ocp_lids .usage_start = ocp_lids .usage_start .replace (day = new_day )
1023
1042
new_ocp_lids .save ()
1024
1043
1025
1044
with conn .cursor () as cur :
0 commit comments