"""Delete history records over 10k

Revision ID: bbe219b77366
Revises: 36274f13329a
Create Date: 2019-01-04 00:44:52.915914

"""

# revision identifiers, used by Alembic.
revision = 'bbe219b77366'
down_revision = '36274f13329a'

import time
from alembic import op
import sqlalchemy as sa
import sqlalchemy.sql as sql

metadata = sa.MetaData()
translation_urls = sa.Table('TranslationUrls', metadata,
    sa.Column('id', sa.Integer()),
    sa.Column('url', sa.Unicode(255)),
)

translation_bundles = sa.Table('TranslationBundles', metadata,
    sa.Column('id', sa.Integer()),
    sa.Column('language', sa.Unicode(20)),
    sa.Column('target', sa.Unicode(20)),
    sa.Column('translation_url_id', sa.Integer()),
)

message_history = sa.Table('TranslationMessageHistory', metadata,
    sa.Column('id', sa.Integer()),
    sa.Column('bundle_id', sa.Integer()),
    sa.Column('key', sa.Unicode(255)),
    sa.Column('datetime', sa.DateTime()),
)

def upgrade():
    all_bundles_stmt = sql.select([
            translation_bundles.c.id, 
            translation_bundles.c.language, 
            translation_bundles.c.target, 
            translation_bundles.c.translation_url_id
        ])

    N = 1000

    for bundle_row in list(op.get_bind().execute(all_bundles_stmt)):
        bundle_id = bundle_row[translation_bundles.c.id]
        count_total = list(op.get_bind().execute(sql.select([sa.func.count(message_history.c.id)], message_history.c.bundle_id == bundle_id)))[0][0]
        if count_total > 10000: # Only focus on the ones with more than 10000
            print "Big bundle. Analyzing"
            print " - Data:", bundle_row, count_total
            keys = [ key for key, in op.get_bind().execute(sql.select([sa.func.distinct(message_history.c.key)], message_history.c.bundle_id == bundle_id)) ]
            print " - Keys: {}".format(len(keys))
            for key in keys:
                records_found = list(op.get_bind().execute(sql.select([sa.func.count(message_history.c.id)], sa.and_(message_history.c.key == unicode(key), message_history.c.bundle_id == bundle_id))))[0][0]
                if records_found > N:
                    print "   - Key {} has {} records".format(key, records_found)

                    while records_found > N:
                        cut_in = records_found - N
                        if cut_in > 200:
                            cut_in = 200

                        records_found = records_found - cut_in
                        ids_to_remove_stmt = sql.select([message_history.c.id, message_history.c.datetime], sa.and_(message_history.c.key == unicode(key), message_history.c.bundle_id == bundle_id)).order_by(sa.asc(message_history.c.datetime)).limit(cut_in)
                        ids_to_remove = [ id_to_remove for id_to_remove, record_datetime in op.get_bind().execute(ids_to_remove_stmt) ]
                        print "     - Deleting {} of them... after: {}".format(cut_in, records_found)
                        op.get_bind().execute(message_history.delete(message_history.c.id.in_(ids_to_remove)))


def downgrade():
    pass

@@ -222,16 +222,27 @@
 3329a'%0A%0A
+import sys%0A
 import t
@@ -245,16 +245,16 @@
 rt time%0A
-
 from ale
@@ -944,16 +944,110 @@
 ()),%0A)%0A%0A
+def dbg(message):%0A    print %22%5B%7B%7D%5D %7B%7D%22.format(time.asctime(), message)%0A    sys.stdout.flush()%0A%0A
 def upgr
@@ -1660,22 +1660,20 @@
         
-print 
+dbg(
 %22Big bun
@@ -1687,16 +1687,17 @@
 alyzing%22
+)
 %0A       
@@ -1697,30 +1697,28 @@
             
-print 
+dbg(
 %22 - Data:%22, 
@@ -1718,11 +1718,23 @@
 ata:
-%22, 
+ %7B%7D %7B%7D%22.format(
 bund
@@ -1752,16 +1752,18 @@
 nt_total
+))
 %0A       
@@ -1925,22 +1925,20 @@
         
-print 
+dbg(
 %22 - Keys
@@ -1960,16 +1960,17 @@
 n(keys))
+)
 %0A       
@@ -2252,30 +2252,28 @@
             
-print 
+dbg(
 %22   - Key %7B%7D
@@ -2315,16 +2315,17 @@
 s_found)
+)
 %0A%0A      
@@ -2977,22 +2977,20 @@
         
-print 
+dbg(
 %22     - 
@@ -3052,16 +3052,17 @@
 s_found)
+)
 %0A       

Be more verbose