Add server_commands and update readme.md

Update command to search for services from the user.
This commit is contained in:
Nicholas Staples
2016-05-11 15:52:49 +01:00
parent 55ed143889
commit 15607c0977
6 changed files with 63 additions and 17 deletions

View File

@@ -73,3 +73,19 @@ scripts/run_celery.sh
``` ```
scripts/run_celery_beat.sh scripts/run_celery_beat.sh
``` ```
## To remove functional test data
NOTE: There is assumption that both the server name prefix and user name prefix are followed by a uuid.
The script will search for all services/users with that prefix and only remove it if the prefix is followed by a uuid otherwise it will be skipped.
Locally
```
python application.py purge_functional_test_data -u <functional tests user name prefix> # Remove the user and associated services.
```
On the server
```
python server_commands.py purge_functional_test_data -u <functional tests user name prefix> # Remove the user and associated services.
```

View File

@@ -3,7 +3,10 @@ from datetime import datetime
from decimal import Decimal from decimal import Decimal
from flask.ext.script import Command, Manager, Option from flask.ext.script import Command, Manager, Option
from app.models import (PROVIDERS, Service, User) from app.models import (PROVIDERS, Service, User)
from app.dao.services_dao import delete_service_and_all_associated_db_objects from app.dao.services_dao import (
delete_service_and_all_associated_db_objects,
dao_fetch_all_services_by_user
)
from app.dao.provider_rates_dao import create_provider_rates from app.dao.provider_rates_dao import create_provider_rates
from app.dao.users_dao import (delete_model_user, delete_user_verify_codes) from app.dao.users_dao import (delete_model_user, delete_user_verify_codes)
@@ -36,22 +39,10 @@ class CreateProviderRateCommand(Command):
class PurgeFunctionalTestDataCommand(Command): class PurgeFunctionalTestDataCommand(Command):
option_list = ( option_list = (
Option('-n', '-service-name-prefix', dest="service_name_prefix", help='Functional service name prefix.'), Option('-u', '-user-email-prefix', dest='user_email_prefix', help="Functional test user email prefix."),
Option('-u', '-user-email-prefix', dest='user_email_prefix', help="Functional test user email prefix.")
) )
def run(self, service_name_prefix=None, user_email_prefix=None): def run(self, service_name_prefix=None, user_email_prefix=None):
if service_name_prefix:
services = Service.query.filter(Service.name.like("{}%".format(service_name_prefix))).all()
for service in services:
# Make sure the second part of the service name is a uuid.
# Just in case someone decides to create a service with that name included in it.
try:
uuid.UUID(service.name.split(service_name_prefix)[1])
except ValueError:
print("Skipping {} as the service name doesn't contain a UUID.".format(service.name))
else:
delete_service_and_all_associated_db_objects(service)
if user_email_prefix: if user_email_prefix:
users = User.query.filter(User.email_address.like("{}%".format(user_email_prefix))).all() users = User.query.filter(User.email_address.like("{}%".format(user_email_prefix))).all()
for usr in users: for usr in users:
@@ -62,5 +53,10 @@ class PurgeFunctionalTestDataCommand(Command):
except ValueError: except ValueError:
print("Skipping {} as the user email doesn't contain a UUID.".format(usr.email_address)) print("Skipping {} as the user email doesn't contain a UUID.".format(usr.email_address))
else: else:
delete_user_verify_codes(usr) services = dao_fetch_all_services_by_user(usr.id)
delete_model_user(usr) if services:
for service in services:
delete_service_and_all_associated_db_objects(service)
else:
delete_user_verify_codes(usr)
delete_model_user(usr)

View File

@@ -50,7 +50,6 @@ def get_unsigned_secret(key_id):
def _generate_secret(token=None): def _generate_secret(token=None):
import uuid
if not token: if not token:
token = uuid.uuid4() token = uuid.uuid4()
serializer = URLSafeSerializer(current_app.config.get('SECRET_KEY')) serializer = URLSafeSerializer(current_app.config.get('SECRET_KEY'))

View File

@@ -95,9 +95,11 @@ def delete_service_and_all_associated_db_objects(service):
_delete_commit(InvitedUser.query.filter_by(service=service)) _delete_commit(InvitedUser.query.filter_by(service=service))
_delete_commit(Permission.query.filter_by(service=service)) _delete_commit(Permission.query.filter_by(service=service))
_delete_commit(ApiKey.query.filter_by(service=service)) _delete_commit(ApiKey.query.filter_by(service=service))
_delete_commit(ApiKey.get_history_model().query.filter_by(service_id=service.id))
_delete_commit(Notification.query.filter_by(service=service)) _delete_commit(Notification.query.filter_by(service=service))
_delete_commit(Job.query.filter_by(service=service)) _delete_commit(Job.query.filter_by(service=service))
_delete_commit(Template.query.filter_by(service=service)) _delete_commit(Template.query.filter_by(service=service))
_delete_commit(Template.get_history_model().query.filter_by(service_id=service.id))
verify_codes = VerifyCode.query.join(User).filter(User.id.in_([x.id for x in service.users])) verify_codes = VerifyCode.query.join(User).filter(User.id.in_([x.id for x in service.users]))
list(map(db.session.delete, verify_codes)) list(map(db.session.delete, verify_codes))
@@ -105,6 +107,7 @@ def delete_service_and_all_associated_db_objects(service):
users = [x for x in service.users] users = [x for x in service.users]
map(service.users.remove, users) map(service.users.remove, users)
[service.users.remove(x) for x in users] [service.users.remove(x) for x in users]
_delete_commit(Service.get_history_model().query.filter_by(id=service.id))
db.session.delete(service) db.session.delete(service)
db.session.commit() db.session.commit()
list(map(db.session.delete, users)) list(map(db.session.delete, users))

29
server_commands.py Normal file
View File

@@ -0,0 +1,29 @@
from flask.ext.script import Manager, Server
from flask_migrate import Migrate, MigrateCommand
from app import (create_app, db, commands)
from credstash import getAllSecrets
import os
default_env_file = '/home/ubuntu/environment'
environment = 'live'
if os.path.isfile(default_env_file):
with open(default_env_file, 'r') as environment_file:
environment = environment_file.readline().strip()
# on aws get secrets and export to env
os.environ.update(getAllSecrets(region="eu-west-1"))
from config import configs
os.environ['NOTIFY_API_ENVIRONMENT'] = configs[environment]
application = create_app()
manager = Manager(application)
migrate = Migrate(application, db)
manager.add_command('db', MigrateCommand)
manager.add_command('purge_functional_test_data', commands.PurgeFunctionalTestDataCommand)
if __name__ == '__main__':
manager.run()

View File

@@ -323,10 +323,13 @@ def test_delete_service_and_associated_objects(notify_db,
assert ProviderStatistics.query.count() == 0 assert ProviderStatistics.query.count() == 0
assert VerifyCode.query.count() == 0 assert VerifyCode.query.count() == 0
assert ApiKey.query.count() == 0 assert ApiKey.query.count() == 0
assert ApiKey.get_history_model().query.count() == 0
assert Template.query.count() == 0 assert Template.query.count() == 0
assert Template.get_history_model().query.count() == 0
assert Job.query.count() == 0 assert Job.query.count() == 0
assert Notification.query.count() == 0 assert Notification.query.count() == 0
assert Permission.query.count() == 0 assert Permission.query.count() == 0
assert User.query.count() == 0 assert User.query.count() == 0
assert InvitedUser.query.count() == 0 assert InvitedUser.query.count() == 0
assert Service.query.count() == 0 assert Service.query.count() == 0
assert Service.get_history_model().query.count() == 0