Merge pull request #970 from alphagov/server-command-for-db-update

Create a server command to run custom db scripts.
This commit is contained in:
Rebecca Law
2017-05-24 09:16:44 +01:00
committed by GitHub
4 changed files with 32 additions and 2 deletions

View File

@@ -2,7 +2,10 @@ import uuid
from datetime import datetime
from decimal import Decimal
from flask.ext.script import Command, Manager, Option
from app.models import (PROVIDERS, Service, User)
from app import db
from app.models import (PROVIDERS, Service, User, NotificationHistory)
from app.dao.services_dao import (
delete_service_and_all_associated_db_objects,
dao_fetch_all_services_by_user
@@ -60,3 +63,29 @@ class PurgeFunctionalTestDataCommand(Command):
else:
delete_user_verify_codes(usr)
delete_model_user(usr)
class CustomDbScript(Command):
def run(self):
self.update_notification_international_flag()
def update_notification_international_flag(self):
# 250,000 rows takes 30 seconds to update.
subq = "select id from notifications where international is null limit 250000"
update = "update notifications set international = False where id in ({})".format(subq)
result = db.session.execute(subq).fetchall()
while len(result) > 0:
db.session.execute(update)
print('commit 250000 updates at {}'.format(datetime.utcnow()))
db.session.commit()
result = db.session.execute(subq).fetchall()
# Now update notification_history
subq_history = "select id from notification_history where international is null limit 250000"
update_history = "update notification_history set international = False where id in ({})".format(subq_history)
result_history = db.session.execute(subq_history).fetchall()
while len(result_history) > 0:
db.session.execute(update_history)
print('commit 250000 updates at {}'.format(datetime.utcnow()))
db.session.commit()
result_history = db.session.execute(subq_history).fetchall()

View File

@@ -15,6 +15,7 @@ migrate = Migrate(application, db)
manager.add_command('db', MigrateCommand)
manager.add_command('create_provider_rate', commands.CreateProviderRateCommand)
manager.add_command('purge_functional_test_data', commands.PurgeFunctionalTestDataCommand)
manager.add_command('custom_db_script', commands.CustomDbScript)
@manager.command

View File

@@ -1 +0,0 @@
9772

View File

@@ -25,6 +25,7 @@ manager = Manager(application)
migrate = Migrate(application, db)
manager.add_command('db', MigrateCommand)
manager.add_command('purge_functional_test_data', commands.PurgeFunctionalTestDataCommand)
manager.add_command('custom_db_script', commands.CustomDbScript)
if __name__ == '__main__':
manager.run()