Create a server command to run custom db scripts.

This commit is contained in:
Rebecca Law
2017-05-19 17:04:39 +01:00
parent 35e9abe8b5
commit 2e864411af
3 changed files with 22 additions and 1 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,19 @@ 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 notification_history where international is null limit 250000"
update = "update notification_history set international = False where id in ({})".format(subq)
result = db.session.execute(subq).fetchall()
while len(result) > 0:
db.session.execute(update)
print('commit 10000 updates at {}'.format(datetime.utcnow()))
db.session.commit()
result = db.session.execute(subq).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

@@ -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()