
# revision identifiers, used by Alembic.
from datetime import datetime

revision = '0082_set_international'
down_revision = '0080_fix_rate_start_date'

from alembic import op
import sqlalchemy as sa


def upgrade():
    conn = op.get_bind()
    start = datetime.utcnow()
    all_notifications = "select id from notification_history where international is null limit 10000"

    results = conn.execute(all_notifications)
    res = results.fetchall()

    conn.execute("update notifications set international = False where id in ({})".format(all_notifications))
    conn.execute("update notification_history set international = False where id in ({})".format(all_notifications))

    while len(res) > 0:
        conn.execute("update notifications set international = False where id in ({})".format(all_notifications))
        conn.execute("update notification_history set international = False where id in ({})".format(all_notifications))
        results = conn.execute(all_notifications)
        res = results.fetchall()
    end = datetime.utcnow()
    print("Started at: {}   ended at: {}".format(start, end))

def downgrade():
    # There is no way to downgrade this update.
    pass
from datetime import datetime
from alembic import op

# revision identifiers, used by Alembic.
revision = '0082_set_international'
down_revision = '0081_noti_status_as_enum'


def upgrade():
    conn = op.get_bind()
    start = datetime.utcnow()
    notification_history = "select id from notification_history where international is null limit 10000"

    results = conn.execute(notification_history)
    res = results.fetchall()

    while len(res) > 0:
        conn.execute("update notification_history set international = False where id in ({})".format(
            notification_history))
        results = conn.execute(notification_history)
        res = results.fetchall()

    notifications = "select id from notifications where international is null limit 10000"
    results2 = conn.execute(notifications)
    res2 = results2.fetchall()
    while len(res2) > 0:
        conn.execute("update notifications set international = False where id in ({})".format(notifications))

        results2 = conn.execute(notifications)
        res2 = results2.fetchall()

    end = datetime.utcnow()
    print("Started at: {}   ended at: {}".format(start, end))


def downgrade():
    # There is no way to downgrade this update.
    pass

Update the script to set the international flag to do the notifications and notification_history in separate loops. It takes about 1.5 minutes to update 27,000 notifications and 27,000 notification_history. The update is a row level lock so will only affect updates to the same row. This is unlikely as the data being updated should be older than 3 days. The second scripts updates the table to set international as not null, to make the model.
Update the script to set the international flag to do the notifications and notification_history in separate loops.
It takes about 1.5 minutes to update 27,000 notifications and 27,000 notification_history. The update is a row level lock so will only affect updates to the same row.
This is unlikely as the data being updated should be older than 3 days.
The second scripts updates the table to set international as not null, to make the model.

+ from datetime import datetime
+ from alembic import op
  
  # revision identifiers, used by Alembic.
- from datetime import datetime
- 
  revision = '0082_set_international'
+ down_revision = '0081_noti_status_as_enum'
- down_revision = '0080_fix_rate_start_date'
- 
- from alembic import op
- import sqlalchemy as sa
  
  
  def upgrade():
      conn = op.get_bind()
      start = datetime.utcnow()
-     all_notifications = "select id from notification_history where international is null limit 10000"
+     notification_history = "select id from notification_history where international is null limit 10000"
  
-     results = conn.execute(all_notifications)
+     results = conn.execute(notification_history)
      res = results.fetchall()
  
-     conn.execute("update notifications set international = False where id in ({})".format(all_notifications))
+     while len(res) > 0:
-     conn.execute("update notification_history set international = False where id in ({})".format(all_notifications))
+         conn.execute("update notification_history set international = False where id in ({})".format(
+             notification_history))
+         results = conn.execute(notification_history)
+         res = results.fetchall()
  
+     notifications = "select id from notifications where international is null limit 10000"
+     results2 = conn.execute(notifications)
+     res2 = results2.fetchall()
-     while len(res) > 0:
+     while len(res2) > 0:
-         conn.execute("update notifications set international = False where id in ({})".format(all_notifications))
+         conn.execute("update notifications set international = False where id in ({})".format(notifications))
-         conn.execute("update notification_history set international = False where id in ({})".format(all_notifications))
+ 
-         results = conn.execute(all_notifications)
+         results2 = conn.execute(notifications)
-         res = results.fetchall()
+         res2 = results2.fetchall()
+ 
      end = datetime.utcnow()
      print("Started at: {}   ended at: {}".format(start, end))
+ 
  
  def downgrade():
      # There is no way to downgrade this update.
      pass
+ 
Update the script to set the international flag to do the notifications and notification_history in separate loops. It takes about 1.5 minutes to update 27,000 notifications and 27,000 notification_history. The update is a row level lock so will only affect updates to the same row. This is unlikely as the data being updated should be older than 3 days. The second scripts updates the table to set international as not null, to make the model.
## Code Before:

# revision identifiers, used by Alembic.
from datetime import datetime

revision = '0082_set_international'
down_revision = '0080_fix_rate_start_date'

from alembic import op
import sqlalchemy as sa


def upgrade():
    conn = op.get_bind()
    start = datetime.utcnow()
    all_notifications = "select id from notification_history where international is null limit 10000"

    results = conn.execute(all_notifications)
    res = results.fetchall()

    conn.execute("update notifications set international = False where id in ({})".format(all_notifications))
    conn.execute("update notification_history set international = False where id in ({})".format(all_notifications))

    while len(res) > 0:
        conn.execute("update notifications set international = False where id in ({})".format(all_notifications))
        conn.execute("update notification_history set international = False where id in ({})".format(all_notifications))
        results = conn.execute(all_notifications)
        res = results.fetchall()
    end = datetime.utcnow()
    print("Started at: {}   ended at: {}".format(start, end))

def downgrade():
    # There is no way to downgrade this update.
    pass
## Instruction:
Update the script to set the international flag to do the notifications and notification_history in separate loops. It takes about 1.5 minutes to update 27,000 notifications and 27,000 notification_history. The update is a row level lock so will only affect updates to the same row. This is unlikely as the data being updated should be older than 3 days. The second scripts updates the table to set international as not null, to make the model.
## Code After:
from datetime import datetime
from alembic import op

# revision identifiers, used by Alembic.
revision = '0082_set_international'
down_revision = '0081_noti_status_as_enum'


def upgrade():
    conn = op.get_bind()
    start = datetime.utcnow()
    notification_history = "select id from notification_history where international is null limit 10000"

    results = conn.execute(notification_history)
    res = results.fetchall()

    while len(res) > 0:
        conn.execute("update notification_history set international = False where id in ({})".format(
            notification_history))
        results = conn.execute(notification_history)
        res = results.fetchall()

    notifications = "select id from notifications where international is null limit 10000"
    results2 = conn.execute(notifications)
    res2 = results2.fetchall()
    while len(res2) > 0:
        conn.execute("update notifications set international = False where id in ({})".format(notifications))

        results2 = conn.execute(notifications)
        res2 = results2.fetchall()

    end = datetime.utcnow()
    print("Started at: {}   ended at: {}".format(start, end))


def downgrade():
    # There is no way to downgrade this update.
    pass
