@@ -215,6 +215,56 @@ def test_partitioning_time_daily_apply():
215215 assert table .partitions [6 ].name == "2019_jun_04"
216216
217217
218+ @pytest .mark .postgres_version (lt = 110000 )
219+ def test_partitioning_time_hourly_apply ():
220+ """Tests whether automatically creating new partitions ahead hourly works as
221+ expected."""
222+
223+ model = define_fake_partitioned_model (
224+ {"timestamp" : models .DateTimeField ()}, {"key" : ["timestamp" ]}
225+ )
226+
227+ schema_editor = connection .schema_editor ()
228+ schema_editor .create_partitioned_model (model )
229+
230+ # create partitions for the next 4 hours (including the current)
231+ with freezegun .freeze_time ("2019-1-23 22:00" ):
232+ manager = PostgresPartitioningManager (
233+ [partition_by_current_time (model , hours = 1 , count = 4 )]
234+ )
235+ manager .plan ().apply ()
236+
237+ table = _get_partitioned_table (model )
238+ assert len (table .partitions ) == 4
239+ assert table .partitions [0 ].name == "2019_jan_23_22"
240+ assert table .partitions [1 ].name == "2019_jan_23_23"
241+ assert table .partitions [2 ].name == "2019_jan_24_00"
242+ assert table .partitions [3 ].name == "2019_jan_24_01"
243+
244+ # re-running it with 5, should just create one additional partition
245+ with freezegun .freeze_time ("2019-1-23 22:59" ):
246+ manager = PostgresPartitioningManager (
247+ [partition_by_current_time (model , hours = 1 , count = 5 )]
248+ )
249+ manager .plan ().apply ()
250+
251+ table = _get_partitioned_table (model )
252+ assert len (table .partitions ) == 5
253+ assert table .partitions [4 ].name == "2019_jan_24_02"
254+
255+ # it's june now, we want to partition two hours ahead
256+ with freezegun .freeze_time ("2019-06-03" ):
257+ manager = PostgresPartitioningManager (
258+ [partition_by_current_time (model , hours = 1 , count = 2 )]
259+ )
260+ manager .plan ().apply ()
261+
262+ table = _get_partitioned_table (model )
263+ assert len (table .partitions ) == 7
264+ assert table .partitions [5 ].name == "2019_jun_03_00"
265+ assert table .partitions [6 ].name == "2019_jun_03_01"
266+
267+
218268@pytest .mark .postgres_version (lt = 110000 )
219269def test_partitioning_time_monthly_apply_insert ():
220270 """Tests whether automatically created monthly partitions line up
@@ -333,6 +383,45 @@ def test_partitioning_time_daily_apply_insert():
333383 model .objects .create (timestamp = datetime .date (2019 , 1 , 10 ))
334384
335385
386+ @pytest .mark .postgres_version (lt = 110000 )
387+ def test_partitioning_time_hourly_apply_insert ():
388+ """Tests whether automatically created hourly partitions line up
389+ perfectly."""
390+
391+ model = define_fake_partitioned_model (
392+ {"timestamp" : models .DateTimeField ()}, {"key" : ["timestamp" ]}
393+ )
394+
395+ schema_editor = connection .schema_editor ()
396+ schema_editor .create_partitioned_model (model )
397+
398+ with freezegun .freeze_time ("2019-1-07 13:59" ):
399+ manager = PostgresPartitioningManager (
400+ [partition_by_current_time (model , hours = 2 , count = 2 )]
401+ )
402+ manager .plan ().apply ()
403+
404+ table = _get_partitioned_table (model )
405+ assert len (table .partitions ) == 2
406+
407+ model .objects .create (timestamp = datetime .datetime (2019 , 1 , 7 , 13 ))
408+ model .objects .create (timestamp = datetime .datetime (2019 , 1 , 7 , 16 , 59 ))
409+
410+ with transaction .atomic ():
411+ with pytest .raises (IntegrityError ):
412+ model .objects .create (timestamp = datetime .datetime (2019 , 1 , 7 , 15 ))
413+ model .objects .create (timestamp = datetime .datetime (2019 , 1 , 8 ))
414+
415+ with freezegun .freeze_time ("2019-1-07 13:00" ):
416+ manager = PostgresPartitioningManager (
417+ [partition_by_current_time (model , hours = 2 , count = 4 )]
418+ )
419+ manager .plan ().apply ()
420+
421+ model .objects .create (timestamp = datetime .datetime (2019 , 1 , 7 , 17 ))
422+ model .objects .create (timestamp = datetime .datetime (2019 , 1 , 7 , 20 , 59 ))
423+
424+
336425@pytest .mark .postgres_version (lt = 110000 )
337426@pytest .mark .parametrize (
338427 "kwargs,partition_names" ,
0 commit comments