mmandrille

Untitled

Jan 8th, 2021 (edited)
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.27 KB | None | 0 0
  1. #Django import
  2. from django.db import transaction
  3. from django.db.models import Min, Max
  4. #Proyect imports
  5. from core.models import Cert
  6.  
  7.  
  8. def test_for_update():
  9.    
  10.         #We retrieve certs to work with them
  11.         certs = Cert.objects.all().order_by('pk')
  12.         certs = certs.select_for_update()# Thats the magic!
  13.         total_rows = certs.count()
  14.         print('{0} rows in total'.format(total_rows))
  15.  
  16.         #get batch size
  17.         batch_size = int(input('Define Batch Size: '))
  18.  
  19.         print('It should need {0} selects to finish.'.format(total_rows/batch_size))
  20.  
  21.         with transaction.atomic():
  22.             full = True
  23.             while full:
  24.                 #Select for update
  25.                 batch = certs[batch_size]
  26.                 #We collect data to show:
  27.                 data = batch.aggregate(Min('pk'), Max('pk'))
  28.                 batch_count = batch.count()
  29.                 #We print
  30.                 print('{0} to {1} with {2} rows...'.format(
  31.                     data['pk__min'],
  32.                     data['pk__max'],
  33.                     batch_count
  34.                 ))
  35.                 #condition to exit:
  36.                 if batch_count < batch_size:
  37.                     full = False
  38.            
  39.             print('Finished process, success!')
Add Comment
Please, Sign In to add comment