From c4b316bde6837d30586189c3c58830d242e53ca5 Mon Sep 17 00:00:00 2001 From: Nicholas Staples Date: Fri, 8 Apr 2016 13:34:46 +0100 Subject: [PATCH] Rebased migrations, all tests working. --- app/models.py | 29 ++- app/notifications/rest.py | 1 + app/schemas.py | 25 +- app/service/rest.py | 4 +- app/template/rest.py | 4 +- app/user/rest.py | 18 +- migrations/versions/0001_initialise_data.py | 52 ---- .../versions/0001_restart_migrations.py | 225 ++++++++++++++++++ migrations/versions/0002_add_templates.py | 38 --- migrations/versions/0003_create_tokens.py | 36 --- migrations/versions/0004_create_jobs.py | 40 ---- migrations/versions/0005_add_job_details.py | 28 --- migrations/versions/0006_add_user_details.py | 40 ---- .../versions/0007_change_to_api_keys.py | 47 ---- migrations/versions/0008_add_verify_codes.py | 38 --- .../0009_unique_service_to_key_name.py | 26 -- .../0010_add_queue_name_to_service.py | 28 --- migrations/versions/0011_uuid_service_id.py | 121 ---------- migrations/versions/0012_add_status_to_job.py | 34 --- migrations/versions/0013_add_notifications.py | 47 ---- migrations/versions/0014_job_id_nullable.py | 30 --- migrations/versions/0015_add_permissions.py | 26 -- migrations/versions/0015_add_subject_line.py | 26 -- migrations/versions/0016_add_email_from.py | 23 -- migrations/versions/0017_emailfrom_notnull.py | 26 -- migrations/versions/0018_unique_emailfrom.py | 22 -- .../versions/0019_unique_servicename.py | 22 -- migrations/versions/0020_email_has_subject.py | 26 -- migrations/versions/0021_add_job_metadata.py | 29 --- migrations/versions/0022_add_invite_users.py | 42 ---- migrations/versions/0023_drop_token.py | 26 -- .../versions/0024_uix_user_to_service.py | 26 -- .../versions/0025_add_processing_dates.py | 26 -- migrations/versions/0026_add_sender.py | 26 -- .../versions/0027_add_service_permission.py | 42 ---- .../versions/0028_add_default_permissions.py | 44 ---- .../0029_add_permissions_to_invite.py | 26 -- .../versions/0030_add_template_permission.py | 36 --- .../0031_add_manage_team_permission.py | 41 ---- .../0032_update_permission_to_enum.py | 48 ---- .../versions/0033_correct_permission_enums.py | 81 ------- migrations/versions/0034_job_sent_count.py | 26 -- .../versions/0035_default_sent_count.py | 27 --- .../versions/0036_notification_stats.py | 37 --- migrations/versions/0037_more_job_states.py | 30 --- migrations/versions/0038_reduce_limits.py | 21 -- .../versions/0039_more_notification_states.py | 31 --- migrations/versions/0040_add_reference.py | 24 -- migrations/versions/0041_platform_admin.py | 33 --- .../versions/0042_default_stats_to_zero.py | 45 ---- migrations/versions/0043_view_activity.py | 47 ---- .../versions/0044_add_template_stats.py | 42 ---- .../0045_template_stats_update_timestamp.py | 32 --- migrations/versions/0046_remove_bucketname.py | 28 --- tests/app/conftest.py | 5 + tests/app/dao/test_api_key_dao.py | 14 +- tests/app/dao/test_templates_dao.py | 18 +- tests/app/dao/test_users_dao.py | 4 +- tests/app/invite/test_invite_rest.py | 15 +- tests/app/job/test_rest.py | 18 +- tests/app/notifications/test_rest.py | 34 +-- tests/app/permissions/test_rest.py | 6 +- tests/app/service/test_rest.py | 19 +- tests/app/template/test_rest.py | 27 +-- tests/app/user/test_rest.py | 37 ++- tests/app/user/test_rest_verify.py | 13 +- tests/conftest.py | 34 +-- 67 files changed, 392 insertions(+), 1850 deletions(-) delete mode 100644 migrations/versions/0001_initialise_data.py create mode 100644 migrations/versions/0001_restart_migrations.py delete mode 100644 migrations/versions/0002_add_templates.py delete mode 100644 migrations/versions/0003_create_tokens.py delete mode 100644 migrations/versions/0004_create_jobs.py delete mode 100644 migrations/versions/0005_add_job_details.py delete mode 100644 migrations/versions/0006_add_user_details.py delete mode 100644 migrations/versions/0007_change_to_api_keys.py delete mode 100644 migrations/versions/0008_add_verify_codes.py delete mode 100644 migrations/versions/0009_unique_service_to_key_name.py delete mode 100644 migrations/versions/0010_add_queue_name_to_service.py delete mode 100644 migrations/versions/0011_uuid_service_id.py delete mode 100644 migrations/versions/0012_add_status_to_job.py delete mode 100644 migrations/versions/0013_add_notifications.py delete mode 100644 migrations/versions/0014_job_id_nullable.py delete mode 100644 migrations/versions/0015_add_permissions.py delete mode 100644 migrations/versions/0015_add_subject_line.py delete mode 100644 migrations/versions/0016_add_email_from.py delete mode 100644 migrations/versions/0017_emailfrom_notnull.py delete mode 100644 migrations/versions/0018_unique_emailfrom.py delete mode 100644 migrations/versions/0019_unique_servicename.py delete mode 100644 migrations/versions/0020_email_has_subject.py delete mode 100644 migrations/versions/0021_add_job_metadata.py delete mode 100644 migrations/versions/0022_add_invite_users.py delete mode 100644 migrations/versions/0023_drop_token.py delete mode 100644 migrations/versions/0024_uix_user_to_service.py delete mode 100644 migrations/versions/0025_add_processing_dates.py delete mode 100644 migrations/versions/0026_add_sender.py delete mode 100644 migrations/versions/0027_add_service_permission.py delete mode 100644 migrations/versions/0028_add_default_permissions.py delete mode 100644 migrations/versions/0029_add_permissions_to_invite.py delete mode 100644 migrations/versions/0030_add_template_permission.py delete mode 100644 migrations/versions/0031_add_manage_team_permission.py delete mode 100644 migrations/versions/0032_update_permission_to_enum.py delete mode 100644 migrations/versions/0033_correct_permission_enums.py delete mode 100644 migrations/versions/0034_job_sent_count.py delete mode 100644 migrations/versions/0035_default_sent_count.py delete mode 100644 migrations/versions/0036_notification_stats.py delete mode 100644 migrations/versions/0037_more_job_states.py delete mode 100644 migrations/versions/0038_reduce_limits.py delete mode 100644 migrations/versions/0039_more_notification_states.py delete mode 100644 migrations/versions/0040_add_reference.py delete mode 100644 migrations/versions/0041_platform_admin.py delete mode 100644 migrations/versions/0042_default_stats_to_zero.py delete mode 100644 migrations/versions/0043_view_activity.py delete mode 100644 migrations/versions/0044_add_template_stats.py delete mode 100644 migrations/versions/0045_template_stats_update_timestamp.py delete mode 100644 migrations/versions/0046_remove_bucketname.py diff --git a/app/models.py b/app/models.py index c41cbac97..1729aad7d 100644 --- a/app/models.py +++ b/app/models.py @@ -1,7 +1,7 @@ import uuid import datetime -from sqlalchemy import UniqueConstraint, Sequence +from sqlalchemy import UniqueConstraint, Sequence, CheckConstraint from . import db from sqlalchemy.dialects.postgresql import ( @@ -24,7 +24,7 @@ def filter_null_value_fields(obj): class User(db.Model): __tablename__ = 'users' - id = db.Column(db.Integer, primary_key=True) + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = db.Column(db.String, nullable=False, index=True, unique=False) email_address = db.Column(db.String(255), nullable=False, index=True, unique=True) created_at = db.Column( @@ -62,7 +62,7 @@ class User(db.Model): user_to_service = db.Table( 'user_to_service', db.Model.metadata, - db.Column('user_id', db.Integer, db.ForeignKey('users.id')), + db.Column('user_id', UUID(as_uuid=True), db.ForeignKey('users.id')), db.Column('service_id', UUID(as_uuid=True), db.ForeignKey('services.id')), UniqueConstraint('user_id', 'service_id', name='uix_user_to_service') @@ -74,7 +74,6 @@ class Service(db.Model): __tablename__ = 'services' id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) - old_id = db.Column(db.Integer, Sequence('services_id_seq'), nullable=False) name = db.Column(db.String(255), nullable=False, unique=True) created_at = db.Column( db.DateTime, @@ -99,9 +98,9 @@ class Service(db.Model): class ApiKey(db.Model): - __tablename__ = 'api_key' + __tablename__ = 'api_keys' - id = db.Column(db.Integer, primary_key=True) + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = db.Column(db.String(255), nullable=False) secret = db.Column(db.String(255), unique=True, nullable=False) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False) @@ -116,7 +115,7 @@ class ApiKey(db.Model): class NotificationStatistics(db.Model): __tablename__ = 'notification_statistics' - id = db.Column(db.Integer, primary_key=True) + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) day = db.Column(db.String(255), nullable=False) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, nullable=False) service = db.relationship('Service', backref=db.backref('service_notification_stats', lazy='dynamic')) @@ -142,7 +141,7 @@ TEMPLATE_TYPES = [TEMPLATE_TYPE_SMS, TEMPLATE_TYPE_EMAIL, TEMPLATE_TYPE_LETTER] class Template(db.Model): __tablename__ = 'templates' - id = db.Column(db.Integer, primary_key=True) + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) name = db.Column(db.String(255), nullable=False) template_type = db.Column(db.Enum(*TEMPLATE_TYPES, name='template_type'), nullable=False) created_at = db.Column( @@ -173,7 +172,7 @@ class Job(db.Model): original_file_name = db.Column(db.String, nullable=False) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False) service = db.relationship('Service', backref=db.backref('jobs', lazy='dynamic')) - template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, unique=False) + template_id = db.Column(UUID(as_uuid=True), db.ForeignKey('templates.id'), index=True, unique=False) template = db.relationship('Template', backref=db.backref('jobs', lazy='dynamic')) created_at = db.Column( db.DateTime, @@ -208,8 +207,8 @@ VERIFY_CODE_TYPES = ['email', 'sms'] class VerifyCode(db.Model): __tablename__ = 'verify_codes' - id = db.Column(db.Integer, primary_key=True) - user_id = db.Column(db.Integer, db.ForeignKey('users.id'), index=True, nullable=False) + id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) + user_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False) user = db.relationship('User', backref=db.backref('verify_codes', lazy='dynamic')) _code = db.Column(db.String, nullable=False) code_type = db.Column(db.Enum(*VERIFY_CODE_TYPES, name='verify_code_types'), @@ -248,7 +247,7 @@ class Notification(db.Model): job = db.relationship('Job', backref=db.backref('notifications', lazy='dynamic')) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False) service = db.relationship('Service') - template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, unique=False) + template_id = db.Column(UUID(as_uuid=True), db.ForeignKey('templates.id'), index=True, unique=False) template = db.relationship('Template') created_at = db.Column( db.DateTime, @@ -281,7 +280,7 @@ class InvitedUser(db.Model): id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) email_address = db.Column(db.String(255), nullable=False) - user_id = db.Column(db.Integer, db.ForeignKey('users.id'), index=True, nullable=False) + user_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False) from_user = db.relationship('User') service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False) service = db.relationship('Service') @@ -332,7 +331,7 @@ class Permission(db.Model): # Service id is optional, if the service is omitted we will assume the permission is not service specific. service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=True) service = db.relationship('Service') - user_id = db.Column(db.Integer, db.ForeignKey('users.id'), index=True, nullable=False) + user_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), index=True, nullable=False) user = db.relationship('User') permission = db.Column( db.Enum(*PERMISSION_LIST, name='permission_types'), @@ -356,7 +355,7 @@ class TemplateStatistics(db.Model): id = db.Column(UUID(as_uuid=True), primary_key=True, default=uuid.uuid4) service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False, nullable=False) service = db.relationship('Service', backref=db.backref('template_statistics', lazy='dynamic')) - template_id = db.Column(db.BigInteger, db.ForeignKey('templates.id'), index=True, nullable=False, unique=False) + template_id = db.Column(UUID(as_uuid=True), db.ForeignKey('templates.id'), index=True, nullable=False, unique=False) template = db.relationship('Template') usage_count = db.Column(db.BigInteger, index=False, unique=False, nullable=False, default=1) day = db.Column(db.Date, index=True, nullable=False, unique=False, default=datetime.date.today) diff --git a/app/notifications/rest.py b/app/notifications/rest.py index 5c4b3121e..ede349d5d 100644 --- a/app/notifications/rest.py +++ b/app/notifications/rest.py @@ -195,6 +195,7 @@ def get_all_notifications(): api_user['client'], filter_dict=data, page=page) + return jsonify( notifications=notification_status_schema.dump(pagination.items, many=True).data, links=pagination_links( diff --git a/app/schemas.py b/app/schemas.py index 0f440a771..fce025b95 100644 --- a/app/schemas.py +++ b/app/schemas.py @@ -91,12 +91,23 @@ class NotificationModelSchema(BaseSchema): model = models.Notification -class TemplateSchema(BaseSchema): +class BaseTemplateSchema(BaseSchema): class Meta: model = models.Template exclude = ("updated_at", "created_at", "service_id", "jobs") +class TemplateSchema(BaseTemplateSchema): + + @validates_schema + def validate_type(self, data): + template_type = data.get('template_type') + if template_type and template_type == 'email': + subject = data.get('subject') + if not subject or subject.strip() == '': + raise ValidationError('Invalid template subject', 'subject') + + class NotificationsStatisticsSchema(BaseSchema): class Meta: model = models.NotificationStatistics @@ -140,7 +151,7 @@ class SmsNotificationSchema(NotificationSchema): class EmailNotificationSchema(NotificationSchema): to = fields.Str(required=True) - template = fields.Int(required=True) + template = fields.Str(required=True) @validates('to') def validate_to(self, value): @@ -151,17 +162,17 @@ class EmailNotificationSchema(NotificationSchema): class SmsTemplateNotificationSchema(SmsNotificationSchema): - template = fields.Int(required=True) + template = fields.Str(required=True) job = fields.String() class JobSmsTemplateNotificationSchema(SmsNotificationSchema): - template = fields.Int(required=True) + template = fields.Str(required=True) job = fields.String(required=True) class JobEmailTemplateNotificationSchema(EmailNotificationSchema): - template = fields.Int(required=True) + template = fields.Str(required=True) job = fields.String(required=True) @@ -220,8 +231,8 @@ class EmailDataSchema(ma.Schema): class NotificationsFilterSchema(ma.Schema): - template_type = fields.Nested(TemplateSchema, only='template_type', many=True) - status = fields.Nested(NotificationModelSchema, only='status', many=True) + template_type = fields.Nested(BaseTemplateSchema, only=['template_type'], many=True) + status = fields.Nested(NotificationModelSchema, only=['status'], many=True) page = fields.Int(required=False) @pre_load diff --git a/app/service/rest.py b/app/service/rest.py index b396317d9..ae643ac3f 100644 --- a/app/service/rest.py +++ b/app/service/rest.py @@ -108,7 +108,7 @@ def renew_api_key(service_id=None): return jsonify(data=unsigned_api_key), 201 -@service.route('//api-key/revoke/', methods=['POST']) +@service.route('//api-key/revoke/', methods=['POST']) def revoke_api_key(service_id, api_key_id): service_api_key = get_model_api_keys(service_id=service_id, id=api_key_id) @@ -117,7 +117,7 @@ def revoke_api_key(service_id, api_key_id): @service.route('//api-keys', methods=['GET']) -@service.route('//api-keys/', methods=['GET']) +@service.route('//api-keys/', methods=['GET']) def get_api_keys(service_id, key_id=None): dao_fetch_service_by_id(service_id=service_id) diff --git a/app/template/rest.py b/app/template/rest.py index 5c5f6f271..15a4bcd51 100644 --- a/app/template/rest.py +++ b/app/template/rest.py @@ -47,7 +47,7 @@ def create_template(service_id): return jsonify(data=template_schema.dump(new_template).data), 201 -@template.route('/', methods=['POST']) +@template.route('/', methods=['POST']) def update_template(service_id, template_id): fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id) @@ -70,7 +70,7 @@ def get_all_templates_for_service(service_id): return jsonify(data=data) -@template.route('/', methods=['GET']) +@template.route('/', methods=['GET']) def get_template_by_id_and_service_id(service_id, template_id): fetched_template = dao_get_template_by_id_and_service_id(template_id=template_id, service_id=service_id) data, errors = template_schema.dump(fetched_template) diff --git a/app/user/rest.py b/app/user/rest.py index fec2ca31b..21440ed5b 100644 --- a/app/user/rest.py +++ b/app/user/rest.py @@ -51,7 +51,7 @@ def create_user(): return jsonify(data=user_schema.dump(user_to_create).data), 201 -@user.route('/', methods=['PUT']) +@user.route('/', methods=['PUT']) def update_user(user_id): user_to_update = get_model_users(user_id=user_id) req_json = request.get_json() @@ -68,7 +68,7 @@ def update_user(user_id): return jsonify(data=user_schema.dump(user_to_update).data), status_code -@user.route('//verify/password', methods=['POST']) +@user.route('//verify/password', methods=['POST']) def verify_user_password(user_id): user_to_verify = get_model_users(user_id=user_id) @@ -89,7 +89,7 @@ def verify_user_password(user_id): return jsonify(result='error', message={'password': ['Incorrect password']}), 400 -@user.route('//verify/code', methods=['POST']) +@user.route('//verify/code', methods=['POST']) def verify_user_code(user_id): user_to_verify = get_model_users(user_id=user_id) @@ -116,7 +116,7 @@ def verify_user_code(user_id): return jsonify({}), 204 -@user.route('//sms-code', methods=['POST']) +@user.route('//sms-code', methods=['POST']) def send_user_sms_code(user_id): user_to_send_to = get_model_users(user_id=user_id) verify_code, errors = request_verify_code_schema.load(request.get_json()) @@ -135,7 +135,7 @@ def send_user_sms_code(user_id): return jsonify({}), 204 -@user.route('//email-code', methods=['POST']) +@user.route('//email-code', methods=['POST']) def send_user_email_code(user_id): user_to_send_to = get_model_users(user_id=user_id) verify_code, errors = request_verify_code_schema.load(request.get_json()) @@ -154,7 +154,7 @@ def send_user_email_code(user_id): return jsonify({}), 204 -@user.route('//email-verification', methods=['POST']) +@user.route('//email-verification', methods=['POST']) def send_user_email_verification(user_id): user_to_send_to = get_model_users(user_id=user_id) verify_code, errors = request_verify_code_schema.load(request.get_json()) @@ -176,7 +176,7 @@ def send_user_email_verification(user_id): return jsonify({}), 204 -@user.route('/', methods=['GET']) +@user.route('/', methods=['GET']) @user.route('', methods=['GET']) def get_user(user_id=None): users = get_model_users(user_id=user_id) @@ -184,7 +184,7 @@ def get_user(user_id=None): return jsonify(data=result.data) -@user.route('//service//permission', methods=['POST']) +@user.route('//service//permission', methods=['POST']) def set_permissions(user_id, service_id): # TODO fix security hole, how do we verify that the user # who is making this request has permission to make the request. @@ -240,7 +240,7 @@ def _create_reset_password_url(email): def _create_verification_url(user, secret_code): from utils.url_safe_token import generate_token import json - data = json.dumps({'user_id': user.id, 'email': user.email_address, 'secret_code': secret_code}) + data = json.dumps({'user_id': str(user.id), 'email': user.email_address, 'secret_code': secret_code}) token = generate_token(data, current_app.config['SECRET_KEY'], current_app.config['DANGEROUS_SALT']) return current_app.config['ADMIN_BASE_URL'] + '/verify-email/' + token diff --git a/migrations/versions/0001_initialise_data.py b/migrations/versions/0001_initialise_data.py deleted file mode 100644 index a1f5ab250..000000000 --- a/migrations/versions/0001_initialise_data.py +++ /dev/null @@ -1,52 +0,0 @@ -"""empty message - -Revision ID: 0001_initialise_data -Revises: None -Create Date: 2016-01-12 09:33:29.249042 - -""" - -# revision identifiers, used by Alembic. -revision = '0001_initialise_data' -down_revision = None - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('services', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(length=255), nullable=False), - sa.Column('created_at', sa.DateTime(), nullable=False), - sa.Column('updated_at', sa.DateTime(), nullable=True), - sa.Column('active', sa.Boolean(), nullable=False), - sa.Column('limit', sa.BigInteger(), nullable=False), - sa.Column('restricted', sa.Boolean(), nullable=False), - sa.PrimaryKeyConstraint('id') - ) - op.create_table('users', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('email_address', sa.String(length=255), nullable=False), - sa.Column('created_at', sa.DateTime(), nullable=False), - sa.Column('updated_at', sa.DateTime(), nullable=True), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_users_email_address'), 'users', ['email_address'], unique=True) - op.create_table('user_to_service', - sa.Column('user_id', sa.Integer(), nullable=True), - sa.Column('service_id', sa.Integer(), nullable=True), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.ForeignKeyConstraint(['user_id'], ['users.id'], ) - ) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_table('user_to_service') - op.drop_index(op.f('ix_users_email_address'), table_name='users') - op.drop_table('users') - op.drop_table('services') - ### end Alembic commands ### diff --git a/migrations/versions/0001_restart_migrations.py b/migrations/versions/0001_restart_migrations.py new file mode 100644 index 000000000..fe4499c6f --- /dev/null +++ b/migrations/versions/0001_restart_migrations.py @@ -0,0 +1,225 @@ +"""empty message + +Revision ID: 0001_restart_migrations +Revises: None +Create Date: 2016-04-07 17:22:12.147542 + +""" + +# revision identifiers, used by Alembic. +revision = '0001_restart_migrations' +down_revision = None + +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +def upgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.create_table('services', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('name', sa.String(length=255), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('active', sa.Boolean(), nullable=False), + sa.Column('limit', sa.BigInteger(), nullable=False), + sa.Column('restricted', sa.Boolean(), nullable=False), + sa.Column('email_from', sa.Text(), nullable=False), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('email_from'), + sa.UniqueConstraint('name') + ) + op.create_table('users', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('name', sa.String(), nullable=False), + sa.Column('email_address', sa.String(length=255), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('_password', sa.String(), nullable=False), + sa.Column('mobile_number', sa.String(), nullable=False), + sa.Column('password_changed_at', sa.DateTime(), nullable=True), + sa.Column('logged_in_at', sa.DateTime(), nullable=True), + sa.Column('failed_login_count', sa.Integer(), nullable=False), + sa.Column('state', sa.String(), nullable=False), + sa.Column('platform_admin', sa.Boolean(), nullable=False), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_users_email_address'), 'users', ['email_address'], unique=True) + op.create_index(op.f('ix_users_name'), 'users', ['name'], unique=False) + op.create_table('api_keys', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('name', sa.String(length=255), nullable=False), + sa.Column('secret', sa.String(length=255), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('expiry_date', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('secret'), + sa.UniqueConstraint('service_id', 'name', name='uix_service_to_key_name') + ) + op.create_index(op.f('ix_api_keys_service_id'), 'api_keys', ['service_id'], unique=False) + op.create_table('invited_users', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('email_address', sa.String(length=255), nullable=False), + sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('status', sa.Enum('pending', 'accepted', 'cancelled', name='invited_users_status_types'), nullable=False), + sa.Column('permissions', sa.String(), nullable=False), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_invited_users_service_id'), 'invited_users', ['service_id'], unique=False) + op.create_index(op.f('ix_invited_users_user_id'), 'invited_users', ['user_id'], unique=False) + op.create_table('notification_statistics', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('day', sa.String(length=255), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('emails_requested', sa.BigInteger(), nullable=False), + sa.Column('emails_delivered', sa.BigInteger(), nullable=False), + sa.Column('emails_error', sa.BigInteger(), nullable=False), + sa.Column('sms_requested', sa.BigInteger(), nullable=False), + sa.Column('sms_delivered', sa.BigInteger(), nullable=False), + sa.Column('sms_error', sa.BigInteger(), nullable=False), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('service_id', 'day', name='uix_service_to_day') + ) + op.create_index(op.f('ix_notification_statistics_service_id'), 'notification_statistics', ['service_id'], unique=False) + op.create_table('permissions', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('permission', sa.Enum('manage_users', 'manage_templates', 'manage_settings', 'send_texts', 'send_emails', 'send_letters', 'manage_api_keys', 'platform_admin', 'view_activity', name='permission_types'), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('service_id', 'user_id', 'permission', name='uix_service_user_permission') + ) + op.create_index(op.f('ix_permissions_service_id'), 'permissions', ['service_id'], unique=False) + op.create_index(op.f('ix_permissions_user_id'), 'permissions', ['user_id'], unique=False) + op.create_table('templates', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('name', sa.String(length=255), nullable=False), + sa.Column('template_type', sa.Enum('sms', 'email', 'letter', name='template_type'), nullable=False), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('content', sa.Text(), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('subject', sa.Text(), nullable=True), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.PrimaryKeyConstraint('id'), + sa.UniqueConstraint('subject') + ) + op.create_index(op.f('ix_templates_service_id'), 'templates', ['service_id'], unique=False) + op.create_table('user_to_service', + sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.UniqueConstraint('user_id', 'service_id', name='uix_user_to_service') + ) + op.create_table('verify_codes', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('user_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('_code', sa.String(), nullable=False), + sa.Column('code_type', sa.Enum('email', 'sms', name='verify_code_types'), nullable=False), + sa.Column('expiry_datetime', sa.DateTime(), nullable=False), + sa.Column('code_used', sa.Boolean(), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_verify_codes_user_id'), 'verify_codes', ['user_id'], unique=False) + op.create_table('jobs', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('original_file_name', sa.String(), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('template_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('status', sa.Enum('pending', 'in progress', 'finished', 'sending limits exceeded', name='job_status_types'), nullable=False), + sa.Column('notification_count', sa.Integer(), nullable=False), + sa.Column('notifications_sent', sa.Integer(), nullable=False), + sa.Column('processing_started', sa.DateTime(), nullable=True), + sa.Column('processing_finished', sa.DateTime(), nullable=True), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False) + op.create_index(op.f('ix_jobs_template_id'), 'jobs', ['template_id'], unique=False) + op.create_table('template_statistics', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('template_id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('usage_count', sa.BigInteger(), nullable=False), + sa.Column('day', sa.Date(), nullable=False), + sa.Column('updated_at', sa.DateTime(), nullable=False), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_template_statistics_day'), 'template_statistics', ['day'], unique=False) + op.create_index(op.f('ix_template_statistics_service_id'), 'template_statistics', ['service_id'], unique=False) + op.create_index(op.f('ix_template_statistics_template_id'), 'template_statistics', ['template_id'], unique=False) + op.create_table('notifications', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('to', sa.String(), nullable=False), + sa.Column('job_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('template_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('created_at', sa.DateTime(), nullable=False), + sa.Column('sent_at', sa.DateTime(), nullable=True), + sa.Column('sent_by', sa.String(), nullable=True), + sa.Column('updated_at', sa.DateTime(), nullable=True), + sa.Column('status', sa.Enum('sent', 'delivered', 'failed', 'complaint', 'bounce', name='notification_status_types'), nullable=False), + sa.Column('reference', sa.String(), nullable=True), + sa.ForeignKeyConstraint(['job_id'], ['jobs.id'], ), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_notifications_job_id'), 'notifications', ['job_id'], unique=False) + op.create_index(op.f('ix_notifications_reference'), 'notifications', ['reference'], unique=False) + op.create_index(op.f('ix_notifications_service_id'), 'notifications', ['service_id'], unique=False) + op.create_index(op.f('ix_notifications_template_id'), 'notifications', ['template_id'], unique=False) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_notifications_template_id'), table_name='notifications') + op.drop_index(op.f('ix_notifications_service_id'), table_name='notifications') + op.drop_index(op.f('ix_notifications_reference'), table_name='notifications') + op.drop_index(op.f('ix_notifications_job_id'), table_name='notifications') + op.drop_table('notifications') + op.drop_index(op.f('ix_template_statistics_template_id'), table_name='template_statistics') + op.drop_index(op.f('ix_template_statistics_service_id'), table_name='template_statistics') + op.drop_index(op.f('ix_template_statistics_day'), table_name='template_statistics') + op.drop_table('template_statistics') + op.drop_index(op.f('ix_jobs_template_id'), table_name='jobs') + op.drop_index(op.f('ix_jobs_service_id'), table_name='jobs') + op.drop_table('jobs') + op.drop_index(op.f('ix_verify_codes_user_id'), table_name='verify_codes') + op.drop_table('verify_codes') + op.drop_table('user_to_service') + op.drop_index(op.f('ix_templates_service_id'), table_name='templates') + op.drop_table('templates') + op.drop_index(op.f('ix_permissions_user_id'), table_name='permissions') + op.drop_index(op.f('ix_permissions_service_id'), table_name='permissions') + op.drop_table('permissions') + op.drop_index(op.f('ix_notification_statistics_service_id'), table_name='notification_statistics') + op.drop_table('notification_statistics') + op.drop_index(op.f('ix_invited_users_user_id'), table_name='invited_users') + op.drop_index(op.f('ix_invited_users_service_id'), table_name='invited_users') + op.drop_table('invited_users') + op.drop_index(op.f('ix_api_keys_service_id'), table_name='api_keys') + op.drop_table('api_keys') + op.drop_index(op.f('ix_users_name'), table_name='users') + op.drop_index(op.f('ix_users_email_address'), table_name='users') + op.drop_table('users') + op.drop_table('services') + ### end Alembic commands ### diff --git a/migrations/versions/0002_add_templates.py b/migrations/versions/0002_add_templates.py deleted file mode 100644 index 30f4073f4..000000000 --- a/migrations/versions/0002_add_templates.py +++ /dev/null @@ -1,38 +0,0 @@ -"""empty message - -Revision ID: 0002_add_templates -Revises: 0001_initialise_data -Create Date: 2016-01-13 10:10:37.303109 - -""" - -# revision identifiers, used by Alembic. -revision = '0002_add_templates' -down_revision = '0001_initialise_data' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('templates', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(length=255), nullable=False), - sa.Column('template_type', sa.Enum('sms', 'email', 'letter', name='template_type'), nullable=False), - sa.Column('created_at', sa.DateTime(), nullable=False), - sa.Column('updated_at', sa.DateTime(), nullable=True), - sa.Column('content', sa.Text(), nullable=False), - sa.Column('service_id', sa.BigInteger(), nullable=True), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_templates_service_id'), 'templates', ['service_id'], unique=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_templates_service_id'), table_name='templates') - op.drop_table('templates') - ### end Alembic commands ### diff --git a/migrations/versions/0003_create_tokens.py b/migrations/versions/0003_create_tokens.py deleted file mode 100644 index b850980e3..000000000 --- a/migrations/versions/0003_create_tokens.py +++ /dev/null @@ -1,36 +0,0 @@ -"""empty message - -Revision ID: 0003_create_tokens -Revises: 0001_initialise_data -Create Date: 2016-01-13 17:07:49.061776 - -""" - -# revision identifiers, used by Alembic. -revision = '0003_create_tokens' -down_revision = '0002_add_templates' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('tokens', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('token', sa.String(length=255), nullable=False), - sa.Column('service_id', sa.Integer(), nullable=False), - sa.Column('expiry_date', sa.DateTime(), nullable=True), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('token') - ) - op.create_index(op.f('ix_tokens_service_id'), 'tokens', ['service_id'], unique=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_tokens_service_id'), table_name='tokens') - op.drop_table('tokens') - ### end Alembic commands ### diff --git a/migrations/versions/0004_create_jobs.py b/migrations/versions/0004_create_jobs.py deleted file mode 100644 index d5f1cafb6..000000000 --- a/migrations/versions/0004_create_jobs.py +++ /dev/null @@ -1,40 +0,0 @@ -"""empty message - -Revision ID: 0004_create_jobs -Revises: 0003_create_tokens -Create Date: 2016-01-15 10:12:02.381160 - -""" - -# revision identifiers, used by Alembic. -revision = '0004_create_jobs' -down_revision = '0003_create_tokens' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('jobs', - sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('original_file_name', sa.String(), nullable=False), - sa.Column('service_id', sa.BigInteger(), nullable=True), - sa.Column('template_id', sa.BigInteger(), nullable=True), - sa.Column('created_at', sa.DateTime(), nullable=False), - sa.Column('updated_at', sa.DateTime(), nullable=True), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False) - op.create_index(op.f('ix_jobs_template_id'), 'jobs', ['template_id'], unique=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_jobs_template_id'), table_name='jobs') - op.drop_index(op.f('ix_jobs_service_id'), table_name='jobs') - op.drop_table('jobs') - ### end Alembic commands ### diff --git a/migrations/versions/0005_add_job_details.py b/migrations/versions/0005_add_job_details.py deleted file mode 100644 index 78fe3efdb..000000000 --- a/migrations/versions/0005_add_job_details.py +++ /dev/null @@ -1,28 +0,0 @@ -"""empty message - -Revision ID: 0005_add_job_details -Revises: 0004_create_jobs -Create Date: 2016-01-15 15:57:38.022562 - -""" - -# revision identifiers, used by Alembic. -revision = '0005_add_job_details' -down_revision = '0004_create_jobs' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('jobs', sa.Column('bucket_name', sa.String(), nullable=False)) - op.add_column('jobs', sa.Column('file_name', sa.String(), nullable=False)) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('jobs', 'file_name') - op.drop_column('jobs', 'bucket_name') - ### end Alembic commands ### diff --git a/migrations/versions/0006_add_user_details.py b/migrations/versions/0006_add_user_details.py deleted file mode 100644 index a789a348c..000000000 --- a/migrations/versions/0006_add_user_details.py +++ /dev/null @@ -1,40 +0,0 @@ -"""empty message - -Revision ID: 0006_add_user_details -Revises: 0005_add_job_details -Create Date: 2016-01-19 11:16:06.518285 - -""" - -# revision identifiers, used by Alembic. -revision = '0006_add_user_details' -down_revision = '0005_add_job_details' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('users', sa.Column('_password', sa.String(), nullable=False)) - op.add_column('users', sa.Column('failed_login_count', sa.Integer(), nullable=False)) - op.add_column('users', sa.Column('logged_in_at', sa.DateTime(), nullable=True)) - op.add_column('users', sa.Column('mobile_number', sa.String(), nullable=False)) - op.add_column('users', sa.Column('name', sa.String(), nullable=False)) - op.add_column('users', sa.Column('password_changed_at', sa.DateTime(), nullable=True)) - op.add_column('users', sa.Column('state', sa.String(), nullable=False)) - op.create_index(op.f('ix_users_name'), 'users', ['name'], unique=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_users_name'), table_name='users') - op.drop_column('users', 'state') - op.drop_column('users', 'password_changed_at') - op.drop_column('users', 'name') - op.drop_column('users', 'mobile_number') - op.drop_column('users', 'logged_in_at') - op.drop_column('users', 'failed_login_count') - op.drop_column('users', '_password') - ### end Alembic commands ### diff --git a/migrations/versions/0007_change_to_api_keys.py b/migrations/versions/0007_change_to_api_keys.py deleted file mode 100644 index d63c111d6..000000000 --- a/migrations/versions/0007_change_to_api_keys.py +++ /dev/null @@ -1,47 +0,0 @@ -"""empty message - -Revision ID: 0007_change_to_api_keys -Revises: 0005_add_job_details -Create Date: 2016-01-19 10:50:46.269618 - -""" - -# revision identifiers, used by Alembic. -revision = '0007_change_to_api_keys' -down_revision = '0006_add_user_details' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('api_key', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('name', sa.String(length=255), nullable=False), - sa.Column('secret', sa.String(length=255), nullable=False), - sa.Column('service_id', sa.Integer(), nullable=False), - sa.Column('expiry_date', sa.DateTime(), nullable=True), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('secret') - ) - op.create_index(op.f('ix_api_key_service_id'), 'api_key', ['service_id'], unique=False) - op.drop_table('tokens') - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('tokens', - sa.Column('id', sa.INTEGER(), nullable=False), - sa.Column('token', sa.VARCHAR(length=255), autoincrement=False, nullable=False), - sa.Column('service_id', sa.INTEGER(), autoincrement=False, nullable=False), - sa.Column('expiry_date', postgresql.TIMESTAMP(), autoincrement=False, nullable=True), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], name='tokens_service_id_fkey'), - sa.PrimaryKeyConstraint('id', name='tokens_pkey'), - sa.UniqueConstraint('token', name='tokens_token_key') - ) - op.drop_index(op.f('ix_api_key_service_id'), table_name='api_key') - op.drop_table('api_key') - ### end Alembic commands ### diff --git a/migrations/versions/0008_add_verify_codes.py b/migrations/versions/0008_add_verify_codes.py deleted file mode 100644 index 47d79be9e..000000000 --- a/migrations/versions/0008_add_verify_codes.py +++ /dev/null @@ -1,38 +0,0 @@ -"""empty message - -Revision ID: 0008_add_verify_codes -Revises: 0007_change_to_api_keys -Create Date: 2016-01-21 16:59:05.818017 - -""" - -# revision identifiers, used by Alembic. -revision = '0008_add_verify_codes' -down_revision = '0007_change_to_api_keys' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('verify_codes', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('user_id', sa.Integer(), nullable=False), - sa.Column('_code', sa.String(), nullable=False), - sa.Column('code_type', sa.Enum('email', 'sms', name='verify_code_types'), nullable=False), - sa.Column('expiry_datetime', sa.DateTime(), nullable=False), - sa.Column('code_used', sa.Boolean(), nullable=True), - sa.Column('created_at', sa.DateTime(), nullable=False), - sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_verify_codes_user_id'), 'verify_codes', ['user_id'], unique=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_verify_codes_user_id'), table_name='verify_codes') - op.drop_table('verify_codes') - ### end Alembic commands ### diff --git a/migrations/versions/0009_unique_service_to_key_name.py b/migrations/versions/0009_unique_service_to_key_name.py deleted file mode 100644 index 1185131bb..000000000 --- a/migrations/versions/0009_unique_service_to_key_name.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0009_unique_service_to_key_name -Revises: 0007_change_to_api_keys -Create Date: 2016-01-21 16:14:51.773001 - -""" - -# revision identifiers, used by Alembic. -revision = '0009_unique_service_to_key_name' -down_revision = '0008_add_verify_codes' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_unique_constraint('uix_service_to_key_name', 'api_key', ['service_id', 'name']) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint('uix_service_to_key_name', 'api_key', type_='unique') - ### end Alembic commands ### diff --git a/migrations/versions/0010_add_queue_name_to_service.py b/migrations/versions/0010_add_queue_name_to_service.py deleted file mode 100644 index 636847cd3..000000000 --- a/migrations/versions/0010_add_queue_name_to_service.py +++ /dev/null @@ -1,28 +0,0 @@ -"""empty message - -Revision ID: 0010_add_queue_name_to_service -Revises: 0009_unique_service_to_key_name -Create Date: 2016-01-27 13:53:34.717916 - -""" - -# revision identifiers, used by Alembic. - -revision = '0010_add_queue_name_to_service' -down_revision = '0009_unique_service_to_key_name' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('services', sa.Column('queue_name', postgresql.UUID(as_uuid=True), nullable=True)) - op.get_bind() - op.execute('update services set queue_name = (SELECT uuid_in(md5(random()::text || now()::text)::cstring))') - ### end Alembic commands ### - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('services', 'queue_name') - ### end Alembic commands ### diff --git a/migrations/versions/0011_uuid_service_id.py b/migrations/versions/0011_uuid_service_id.py deleted file mode 100644 index d332930ef..000000000 --- a/migrations/versions/0011_uuid_service_id.py +++ /dev/null @@ -1,121 +0,0 @@ -"""empty message - -Revision ID: 0011_uuid_service_id -Revises: 0010_add_queue_name_to_service -Create Date: 2016-02-01 15:47:30.553052 - -""" - -# revision identifiers, used by Alembic. -from sqlalchemy.dialects import postgresql - -revision = '0011_uuid_service_id' -down_revision = '0010_add_queue_name_to_service' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - - # op.execute('update jobs set new_service_id = (SELECT queue_name from services where service_id = services.id)') - - op.add_column('user_to_service', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True)) - op.get_bind() - op.execute('update user_to_service '\ - 'set new_service_id = (SELECT queue_name from services where service_id = services.id)') - - op.drop_column('user_to_service', 'service_id') - op.alter_column('user_to_service', 'new_service_id', new_column_name='service_id') - - op.add_column('jobs', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True)) - op.execute('update jobs '\ - 'set new_service_id = (SELECT queue_name from services where service_id = services.id)') - - op.drop_column('jobs', 'service_id') - op.alter_column('jobs', 'new_service_id', new_column_name='service_id', nullable=False) - - op.add_column('api_key', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True)) - op.execute('update api_key '\ - 'set new_service_id = (SELECT queue_name from services where service_id = services.id)') - - op.drop_column('api_key', 'service_id') - op.alter_column('api_key', 'new_service_id', new_column_name='service_id', nullable=False) - - op.add_column('templates', sa.Column('new_service_id', postgresql.UUID(as_uuid=True), nullable=True)) - op.execute('update templates '\ - 'set new_service_id = (SELECT queue_name from services where service_id = services.id)') - - op.drop_column('templates', 'service_id') - op.alter_column('templates', 'new_service_id', new_column_name='service_id', nullable=False) - - op.drop_constraint('services_pkey', 'services') - op.alter_column('services', 'id', new_column_name='old_id') - op.alter_column('services', 'queue_name', new_column_name='id', nullable=False) - - op.create_primary_key('services_pkey', 'services', ['id']) - - op.create_foreign_key('user_to_service_service_id_fkey', 'user_to_service', 'services', ['service_id'], ['id']) - op.create_foreign_key('api_key_service_id_fkey', 'api_key', 'services', ['service_id'], ['id']) - op.create_foreign_key('jobs_service_id_fkey', 'jobs', 'services', ['service_id'], ['id']) - op.create_foreign_key('templates_service_id_fkey', 'templates', 'services', ['service_id'], ['id']) - - op.create_index(op.f('ix_templates_service_id'), 'templates', ['service_id'], unique=False) - op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False) - op.create_index(op.f('ix_api_key_service_id'), 'api_key', ['service_id'], unique=False) - op.create_unique_constraint('uix_service_to_key_name', 'api_key', ['service_id', 'name']) - - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - - op.add_column('user_to_service', sa.Column('new_service_id', sa.Integer, nullable=True)) - op.get_bind() - op.execute('update user_to_service '\ - 'set new_service_id = (SELECT old_id from services where service_id = services.id)') - - op.drop_column('user_to_service', 'service_id') - op.alter_column('user_to_service', 'new_service_id', new_column_name='service_id', nullable=False) - - op.add_column('jobs', sa.Column('new_service_id', sa.Integer, nullable=True)) - op.execute('update jobs '\ - 'set new_service_id = (SELECT old_id from services where service_id = services.id)') - - op.drop_column('jobs', 'service_id') - op.alter_column('jobs', 'new_service_id', new_column_name='service_id', nullable=False) - - op.add_column('api_key', sa.Column('new_service_id', sa.Integer, nullable=True)) - op.execute('update api_key '\ - 'set new_service_id = (SELECT old_id from services where service_id = services.id)') - - op.drop_column('api_key', 'service_id') - op.alter_column('api_key', 'new_service_id', new_column_name='service_id', nullable=False) - - op.add_column('templates', sa.Column('new_service_id', sa.Integer, nullable=True)) - op.execute('update templates '\ - 'set new_service_id = (SELECT old_id from services where service_id = services.id)') - - op.drop_column('templates', 'service_id') - op.alter_column('templates', 'new_service_id', new_column_name='service_id', nullable=False) - - op.drop_constraint('services_pkey', 'services') - - op.alter_column('services', 'id', new_column_name='queue_name', nullable=False) - op.alter_column('services', 'old_id', new_column_name='id', nullable=True) - - op.create_primary_key('services_pkey', 'services', ['id']) - - op.create_foreign_key('user_to_service_service_id_fkey', 'user_to_service', 'services', ['service_id'], ['id']) - op.create_foreign_key('api_key_service_id_fkey', 'api_key', 'services', ['service_id'], ['id']) - op.create_foreign_key('jobs_service_id_fkey', 'jobs', 'services', ['service_id'], ['id']) - op.create_foreign_key('templates_service_id_fkey', 'templates', 'services', ['service_id'], ['id']) - - op.create_index(op.f('ix_api_key_service_id'), 'api_key', ['service_id'], unique=False) - op.create_unique_constraint('uix_service_to_key_name', 'api_key', ['service_id', 'name']) - op.create_index(op.f('ix_jobs_service_id'), 'jobs', ['service_id'], unique=False) - op.create_index(op.f('ix_templates_service_id'), 'templates', ['service_id'], unique=False) - - ### end Alembic commands ### diff --git a/migrations/versions/0012_add_status_to_job.py b/migrations/versions/0012_add_status_to_job.py deleted file mode 100644 index e5ca0a3f1..000000000 --- a/migrations/versions/0012_add_status_to_job.py +++ /dev/null @@ -1,34 +0,0 @@ -"""empty message - -Revision ID: 0012_add_status_to_job -Revises: 0011_uuid_service_id -Create Date: 2016-02-02 11:25:34.402864 - -""" - -# revision identifiers, used by Alembic. -revision = '0012_add_status_to_job' -down_revision = '0011_uuid_service_id' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - job_status_types = sa.Enum('pending', 'in progress', 'finished', name='job_status_types') - job_status_types.create(op.get_bind()) - op.add_column('jobs', sa.Column('status', job_status_types, nullable=True)) - op.get_bind() - op.execute("update jobs set status='pending'") - op.alter_column('jobs', 'status', nullable=False) - - - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('jobs', 'status') - op.execute('DROP TYPE job_status_types') - ### end Alembic commands ### diff --git a/migrations/versions/0013_add_notifications.py b/migrations/versions/0013_add_notifications.py deleted file mode 100644 index 61fd55a66..000000000 --- a/migrations/versions/0013_add_notifications.py +++ /dev/null @@ -1,47 +0,0 @@ -"""empty message - -Revision ID: 0013_add_notifications -Revises: 0012_add_status_to_job -Create Date: 2016-02-09 11:14:46.708551 - -""" - -# revision identifiers, used by Alembic. -revision = '0013_add_notifications' -down_revision = '0012_add_status_to_job' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('notifications', - sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('to', sa.String(), nullable=False), - sa.Column('job_id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True), - sa.Column('template_id', sa.BigInteger(), nullable=True), - sa.Column('created_at', sa.DateTime(), nullable=False), - sa.Column('updated_at', sa.DateTime(), nullable=True), - sa.Column('status', sa.Enum('sent', 'failed', name='notification_status_types'), nullable=False), - sa.ForeignKeyConstraint(['job_id'], ['jobs.id'], ), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_notifications_job_id'), 'notifications', ['job_id'], unique=False) - op.create_index(op.f('ix_notifications_service_id'), 'notifications', ['service_id'], unique=False) - op.create_index(op.f('ix_notifications_template_id'), 'notifications', ['template_id'], unique=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_notifications_template_id'), table_name='notifications') - op.drop_index(op.f('ix_notifications_service_id'), table_name='notifications') - op.drop_index(op.f('ix_notifications_job_id'), table_name='notifications') - op.drop_table('notifications') - op.get_bind() - op.execute("drop type notification_status_types") - ### end Alembic commands ### diff --git a/migrations/versions/0014_job_id_nullable.py b/migrations/versions/0014_job_id_nullable.py deleted file mode 100644 index 8c27b3ce1..000000000 --- a/migrations/versions/0014_job_id_nullable.py +++ /dev/null @@ -1,30 +0,0 @@ -"""empty message - -Revision ID: 0014_job_id_nullable -Revises: 0013_add_notifications -Create Date: 2016-02-10 10:57:39.414061 - -""" - -# revision identifiers, used by Alembic. -revision = '0014_job_id_nullable' -down_revision = '0013_add_notifications' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.alter_column('notifications', 'job_id', - existing_type=postgresql.UUID(), - nullable=True) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.alter_column('notifications', 'job_id', - existing_type=postgresql.UUID(), - nullable=False) - ### end Alembic commands ### diff --git a/migrations/versions/0015_add_permissions.py b/migrations/versions/0015_add_permissions.py deleted file mode 100644 index 507a5effa..000000000 --- a/migrations/versions/0015_add_permissions.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0015_add_permissions -Revises: 0014_job_id_nullable -Create Date: 2016-02-19 14:16:59.359493 - -""" - -# revision identifiers, used by Alembic. -revision = '0015_add_permissions' -down_revision = '0014_job_id_nullable' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('users', sa.Column('permissions', postgresql.ARRAY(sa.String()), nullable=True)) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('users', 'permissions') - ### end Alembic commands ### diff --git a/migrations/versions/0015_add_subject_line.py b/migrations/versions/0015_add_subject_line.py deleted file mode 100644 index 88102327d..000000000 --- a/migrations/versions/0015_add_subject_line.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0015_add_subject_line -Revises: 0015_add_permissions -Create Date: 2016-02-18 09:43:29.282804 - -""" - -# revision identifiers, used by Alembic. -revision = '0015_add_subject_line' -down_revision = '0015_add_permissions' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - pass - op.add_column('templates', sa.Column('subject', sa.Text(), nullable=True)) - op.create_unique_constraint(None, 'templates', ['subject']) - - -def downgrade(): - pass - op.drop_constraint(None, 'templates', type_='unique') - op.drop_column('templates', 'subject') diff --git a/migrations/versions/0016_add_email_from.py b/migrations/versions/0016_add_email_from.py deleted file mode 100644 index 053d65765..000000000 --- a/migrations/versions/0016_add_email_from.py +++ /dev/null @@ -1,23 +0,0 @@ -"""empty message - -Revision ID: 0016_add_email_from -Revises: 0015_add_subject_line -Create Date: 2016-02-18 11:25:37.915137 - -""" - -# revision identifiers, used by Alembic. -revision = '0016_add_email_from' -down_revision = '0015_add_subject_line' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - op.add_column('services', sa.Column('email_from', sa.Text(), nullable=True)) - op.execute("UPDATE services SET email_from=name") - - -def downgrade(): - op.drop_column('services', 'email_from') diff --git a/migrations/versions/0017_emailfrom_notnull.py b/migrations/versions/0017_emailfrom_notnull.py deleted file mode 100644 index f30ac105d..000000000 --- a/migrations/versions/0017_emailfrom_notnull.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0017_emailfrom_notnull -Revises: 0016_add_email_from -Create Date: 2016-02-18 11:41:25.753694 - -""" - -# revision identifiers, used by Alembic. -revision = '0017_emailfrom_notnull' -down_revision = '0016_add_email_from' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - op.alter_column('services', 'email_from', - existing_type=sa.TEXT(), - nullable=False) - - -def downgrade(): - op.alter_column('services', 'email_from', - existing_type=sa.TEXT(), - nullable=True) diff --git a/migrations/versions/0018_unique_emailfrom.py b/migrations/versions/0018_unique_emailfrom.py deleted file mode 100644 index f36d61bce..000000000 --- a/migrations/versions/0018_unique_emailfrom.py +++ /dev/null @@ -1,22 +0,0 @@ -"""empty message - -Revision ID: 0018_unique_emailfrom -Revises: 0017_emailfrom_notnull -Create Date: 2016-02-18 11:42:18.246280 - -""" - -# revision identifiers, used by Alembic. -revision = '0018_unique_emailfrom' -down_revision = '0017_emailfrom_notnull' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - op.create_unique_constraint(None, 'services', ['email_from']) - - -def downgrade(): - op.drop_constraint(None, 'services', type_='unique') diff --git a/migrations/versions/0019_unique_servicename.py b/migrations/versions/0019_unique_servicename.py deleted file mode 100644 index 1c8d83976..000000000 --- a/migrations/versions/0019_unique_servicename.py +++ /dev/null @@ -1,22 +0,0 @@ -"""empty message - -Revision ID: 0019_unique_servicename -Revises: 0018_unique_emailfrom -Create Date: 2016-02-18 11:45:29.102891 - -""" - -# revision identifiers, used by Alembic. -revision = '0019_unique_servicename' -down_revision = '0018_unique_emailfrom' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - op.create_unique_constraint(None, 'services', ['name']) - - -def downgrade(): - op.drop_constraint(None, 'services', type_='unique') diff --git a/migrations/versions/0020_email_has_subject.py b/migrations/versions/0020_email_has_subject.py deleted file mode 100644 index 2290b83bf..000000000 --- a/migrations/versions/0020_email_has_subject.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0020_email_has_subject -Revises: 0019_unique_servicename -Create Date: 2016-02-18 11:45:29.102891 - -""" - -# revision identifiers, used by Alembic. -revision = '0020_email_has_subject' -down_revision = '0019_unique_servicename' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - op.create_check_constraint( - "ch_email_template_has_subject", - "templates", - "((template_type='email' and subject is not null) or (template_type!='email' and subject is null))" - ) - - -def downgrade(): - op.drop_constraint('ch_email_template_has_subject', 'templates', type_='check') diff --git a/migrations/versions/0021_add_job_metadata.py b/migrations/versions/0021_add_job_metadata.py deleted file mode 100644 index a72d86c23..000000000 --- a/migrations/versions/0021_add_job_metadata.py +++ /dev/null @@ -1,29 +0,0 @@ -"""empty message - -Revision ID: 0021_add_job_metadata -Revises: 0020_email_has_subject -Create Date: 2016-02-22 12:33:02.360780 - -""" - -# revision identifiers, used by Alembic. -revision = '0021_add_job_metadata' -down_revision = '0020_email_has_subject' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('jobs', sa.Column('notification_count', sa.Integer(), nullable=True)) - op.get_bind() - op.execute('update jobs set notification_count = 0') - op.alter_column('jobs', 'notification_count', nullable=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('jobs', 'notification_count') - ### end Alembic commands ### diff --git a/migrations/versions/0022_add_invite_users.py b/migrations/versions/0022_add_invite_users.py deleted file mode 100644 index b77be1d73..000000000 --- a/migrations/versions/0022_add_invite_users.py +++ /dev/null @@ -1,42 +0,0 @@ -"""empty message - -Revision ID: 0022_add_invite_users -Revises: 0021_add_job_metadata -Create Date: 2016-02-23 16:41:40.481468 - -""" - -# revision identifiers, used by Alembic. -revision = '0022_add_invite_users' -down_revision = '0021_add_job_metadata' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('invited_users', - sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('email_address', sa.String(length=255), nullable=False), - sa.Column('user_id', sa.Integer(), nullable=False), - sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True), - sa.Column('_token', sa.String(), nullable=False), - sa.Column('created_at', sa.DateTime(), nullable=False), - sa.Column('status', sa.Enum('pending', 'accepted', 'cancelled', name='invited_users_status_types'), nullable=False), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_invited_users_service_id'), 'invited_users', ['service_id'], unique=False) - op.create_index(op.f('ix_invited_users_user_id'), 'invited_users', ['user_id'], unique=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_invited_users_user_id'), table_name='invited_users') - op.drop_index(op.f('ix_invited_users_service_id'), table_name='invited_users') - op.drop_table('invited_users') - op.execute('DROP TYPE invited_users_status_types') - ### end Alembic commands ### diff --git a/migrations/versions/0023_drop_token.py b/migrations/versions/0023_drop_token.py deleted file mode 100644 index bf6f705d7..000000000 --- a/migrations/versions/0023_drop_token.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0023_drop_token -Revises: 0022_add_invite_users -Create Date: 2016-02-24 13:58:04.440296 - -""" - -# revision identifiers, used by Alembic. -revision = '0023_drop_token' -down_revision = '0022_add_invite_users' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('invited_users', '_token') - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('invited_users', sa.Column('_token', sa.VARCHAR(), autoincrement=False, nullable=False)) - ### end Alembic commands ### diff --git a/migrations/versions/0024_uix_user_to_service.py b/migrations/versions/0024_uix_user_to_service.py deleted file mode 100644 index 63b131c68..000000000 --- a/migrations/versions/0024_uix_user_to_service.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0024_uix_user_to_service -Revises: 0023_drop_token -Create Date: 2016-02-25 11:15:06.088508 - -""" - -# revision identifiers, used by Alembic. -revision = '0024_uix_user_to_service' -down_revision = '0023_drop_token' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_unique_constraint('uix_user_to_service', 'user_to_service', ['user_id', 'service_id']) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_constraint('uix_user_to_service', 'user_to_service', type_='unique') - ### end Alembic commands ### diff --git a/migrations/versions/0025_add_processing_dates.py b/migrations/versions/0025_add_processing_dates.py deleted file mode 100644 index 850658148..000000000 --- a/migrations/versions/0025_add_processing_dates.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0025_add_processing_dates -Revises: 0024_uix_user_to_service -Create Date: 2016-02-24 17:15:47.457200 - -""" - -# revision identifiers, used by Alembic. -revision = '0025_add_processing_dates' -down_revision = '0024_uix_user_to_service' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - op.add_column('jobs', sa.Column('processing_finished', sa.DateTime(), nullable=True)) - op.add_column('jobs', sa.Column('processing_started', sa.DateTime(), nullable=True)) - op.add_column('notifications', sa.Column('sent_at', sa.DateTime(), nullable=True)) - - -def downgrade(): - op.drop_column('notifications', 'sent_at') - op.drop_column('jobs', 'processing_started') - op.drop_column('jobs', 'processing_finished') diff --git a/migrations/versions/0026_add_sender.py b/migrations/versions/0026_add_sender.py deleted file mode 100644 index ffe86f920..000000000 --- a/migrations/versions/0026_add_sender.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0026_add_sender -Revises: 0025_add_processing_dates -Create Date: 2016-02-24 17:18:21.942772 - -""" - -# revision identifiers, used by Alembic. -revision = '0026_add_sender' -down_revision = '0025_add_processing_dates' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('notifications', sa.Column('sent_by', sa.String(), nullable=True)) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('notifications', 'sent_by') - ### end Alembic commands ### diff --git a/migrations/versions/0027_add_service_permission.py b/migrations/versions/0027_add_service_permission.py deleted file mode 100644 index fe3149588..000000000 --- a/migrations/versions/0027_add_service_permission.py +++ /dev/null @@ -1,42 +0,0 @@ -"""empty message - -Revision ID: 0027_add_service_permission -Revises: 0026_add_sender -Create Date: 2016-02-26 10:33:20.536362 - -""" - -# revision identifiers, used by Alembic. -revision = '0027_add_service_permission' -down_revision = '0026_add_sender' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('permissions', - sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=True), - sa.Column('user_id', sa.Integer(), nullable=False), - sa.Column('permission', sa.String(length=255), nullable=False), - sa.Column('created_at', sa.DateTime(), nullable=False), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.ForeignKeyConstraint(['user_id'], ['users.id'], ), - sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('service_id', 'user_id', 'permission', name='uix_service_user_permission') - ) - op.create_index(op.f('ix_permissions_service_id'), 'permissions', ['service_id'], unique=False) - op.create_index(op.f('ix_permissions_user_id'), 'permissions', ['user_id'], unique=False) - op.drop_column('users', 'permissions') - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('users', sa.Column('permissions', postgresql.ARRAY(VARCHAR()), autoincrement=False, nullable=True)) - op.drop_index(op.f('ix_permissions_user_id'), table_name='permissions') - op.drop_index(op.f('ix_permissions_service_id'), table_name='permissions') - op.drop_table('permissions') - ### end Alembic commands ### diff --git a/migrations/versions/0028_add_default_permissions.py b/migrations/versions/0028_add_default_permissions.py deleted file mode 100644 index 893f4f851..000000000 --- a/migrations/versions/0028_add_default_permissions.py +++ /dev/null @@ -1,44 +0,0 @@ -"""empty message - -Revision ID: 0028_add_default_permissions -Revises: 0027_add_service_permission -Create Date: 2016-02-26 10:33:20.536362 - -""" - -# revision identifiers, used by Alembic. -revision = '0028_add_default_permissions' -down_revision = '0027_add_service_permission' -import uuid -from datetime import datetime -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - user_services = conn.execute("SELECT * FROM user_to_service").fetchall() - for entry in user_services: - id_ = uuid.uuid4() - created_at = datetime.now().isoformat().replace('T', ' ') - conn.execute(( - "INSERT INTO permissions (id, user_id, service_id, permission, created_at)" - " VALUES ('{}', '{}', '{}', 'manage_service', '{}')").format(id_, entry[0], entry[1], created_at)) - id_ = uuid.uuid4() - conn.execute(( - "INSERT INTO permissions (id, user_id, service_id, permission, created_at)" - " VALUES ('{}', '{}', '{}', 'send_messages', '{}')").format(id_, entry[0], entry[1], created_at)) - id_ = uuid.uuid4() - conn.execute(( - "INSERT INTO permissions (id, user_id, service_id, permission, created_at)" - " VALUES ('{}', '{}', '{}', 'manage_api_keys', '{}')").format(id_, entry[0], entry[1], created_at)) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - conn.execute("DELETE FROM permissions") - - ### end Alembic commands ### \ No newline at end of file diff --git a/migrations/versions/0029_add_permissions_to_invite.py b/migrations/versions/0029_add_permissions_to_invite.py deleted file mode 100644 index b00cbee62..000000000 --- a/migrations/versions/0029_add_permissions_to_invite.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0029_add_permissions_to_invite -Revises: 0028_add_default_permissions -Create Date: 2016-02-26 16:17:30.612924 - -""" - -# revision identifiers, used by Alembic. -revision = '0029_add_permissions_to_invite' -down_revision = '0028_add_default_permissions' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('invited_users', sa.Column('permissions', sa.String(), nullable=False)) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('invited_users', 'permissions') - ### end Alembic commands ### diff --git a/migrations/versions/0030_add_template_permission.py b/migrations/versions/0030_add_template_permission.py deleted file mode 100644 index 7eb2795c3..000000000 --- a/migrations/versions/0030_add_template_permission.py +++ /dev/null @@ -1,36 +0,0 @@ -"""empty message - -Revision ID: 0030_add_template_permission -Revises: 0029_add_permissions_to_invite -Create Date: 2016-02-26 10:33:20.536362 - -""" - -# revision identifiers, used by Alembic. -revision = '0030_add_template_permission' -down_revision = '0029_add_permissions_to_invite' -import uuid -from datetime import datetime -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - user_services = conn.execute("SELECT * FROM user_to_service").fetchall() - for entry in user_services: - id_ = uuid.uuid4() - created_at = datetime.now().isoformat().replace('T', ' ') - conn.execute(( - "INSERT INTO permissions (id, user_id, service_id, permission, created_at)" - " VALUES ('{}', '{}', '{}', 'manage_templates', '{}')").format(id_, entry[0], entry[1], created_at)) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - conn.execute("DELETE FROM permissions where permission='manage_templates'") - - ### end Alembic commands ### \ No newline at end of file diff --git a/migrations/versions/0031_add_manage_team_permission.py b/migrations/versions/0031_add_manage_team_permission.py deleted file mode 100644 index 05413da45..000000000 --- a/migrations/versions/0031_add_manage_team_permission.py +++ /dev/null @@ -1,41 +0,0 @@ -"""empty message - -Revision ID: 0031_add_manage_team_permission -Revises: 0030_add_template_permission -Create Date: 2016-02-26 10:33:20.536362 - -""" - -# revision identifiers, used by Alembic. -revision = '0031_add_manage_team_permission' -down_revision = '0030_add_template_permission' -import uuid -from datetime import datetime -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - user_services = conn.execute("SELECT * FROM user_to_service").fetchall() - for entry in user_services: - id_ = uuid.uuid4() - created_at = datetime.now().isoformat().replace('T', ' ') - conn.execute(( - "INSERT INTO permissions (id, user_id, service_id, permission, created_at)" - " VALUES ('{}', '{}', '{}', 'manage_team', '{}')").format(id_, entry[0], entry[1], created_at)) - id_ = uuid.uuid4() - conn.execute(( - "INSERT INTO permissions (id, user_id, service_id, permission, created_at)" - " VALUES ('{}', '{}', '{}', 'view_activity', '{}')").format(id_, entry[0], entry[1], created_at)) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - conn.execute("DELETE FROM permissions where permission='manage_team'") - conn.execute("DELETE FROM permissions where permission='view_activity'") - - ### end Alembic commands ### \ No newline at end of file diff --git a/migrations/versions/0032_update_permission_to_enum.py b/migrations/versions/0032_update_permission_to_enum.py deleted file mode 100644 index 0bf9d0ea4..000000000 --- a/migrations/versions/0032_update_permission_to_enum.py +++ /dev/null @@ -1,48 +0,0 @@ -"""empty message - -Revision ID: 0032_update_permission_to_enum -Revises: 0031_add_manage_team_permission -Create Date: 2016-03-01 17:08:12.184393 - -""" - -# revision identifiers, used by Alembic. -revision = '0032_update_permission_to_enum' -down_revision = '0031_add_manage_team_permission' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - permissions = conn.execute("SELECT id, permission FROM permissions").fetchall() - op.drop_constraint('uix_service_user_permission', 'permissions', type_='unique') - op.drop_column('permissions', 'permission') - permission_types = sa.Enum('manage_service', 'send_messages', 'manage_api_keys', 'manage_templates', 'manage_team', 'view_activity', name='permission_types') - permission_types.create(op.get_bind()) - op.add_column('permissions', sa.Column('permission', permission_types, nullable=True)) - for p in permissions: - conn.execute("UPDATE permissions SET permission='{}' WHERE id='{}'".format(str(p[1]), str(p[0]))) - op.create_unique_constraint('uix_service_user_permission', 'permissions', ['service_id', 'user_id', 'permission']) - op.alter_column('permissions', 'permission', nullable=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - permissions = conn.execute("SELECT id, permission FROM permissions").fetchall() - op.drop_constraint('uix_service_user_permission', 'permissions', type_='unique') - op.drop_column('permissions', 'permission') - try: - sa.Enum(name='permission_types').drop(conn, checkfirst=False) - except: - pass - op.add_column('permissions', sa.Column('permission', sa.VARCHAR(length=255), autoincrement=False, nullable=True)) - for p in permissions: - conn.execute("UPDATE permissions SET permission='{}' WHERE ID='{}'".format(str(p[1]), str(p[0]))) - op.create_unique_constraint('uix_service_user_permission', 'permissions', ['service_id', 'user_id', 'permission']) - op.alter_column('permissions', 'permission', nullable=False) - ### end Alembic commands ### diff --git a/migrations/versions/0033_correct_permission_enums.py b/migrations/versions/0033_correct_permission_enums.py deleted file mode 100644 index 52989b6c4..000000000 --- a/migrations/versions/0033_correct_permission_enums.py +++ /dev/null @@ -1,81 +0,0 @@ -"""empty message - -Revision ID: 0033_correct_permission_enums -Revises: 0032_update_permission_to_enum -Create Date: 2016-03-02 15:00:25.358153 - -""" - -# revision identifiers, used by Alembic. -revision = '0033_correct_permission_enums' -down_revision = '0032_update_permission_to_enum' - -import uuid -from datetime import datetime -from alembic import op -import sqlalchemy as sa - - -def add_default_permissions(conn, permissions): - user_services = conn.execute("SELECT * FROM user_to_service").fetchall() - for entry in user_services: - for p in permissions: - id_ = uuid.uuid4() - created_at = datetime.now().isoformat().replace('T', ' ') - conn.execute(( - "INSERT INTO permissions (id, user_id, service_id, permission, created_at)" - " VALUES ('{}', '{}', '{}', '{}', '{}')").format(id_, entry[0], entry[1], p, created_at)) - - -def upgrade(): - # Since there are no specific permissions set for services yet - # we can just remove all and re-add all. - ### commands auto generated by Nick - please adjust! ### - new_permissions = ['manage_users', - 'manage_templates', - 'manage_settings', - 'send_texts', - 'send_emails', - 'send_letters', - 'manage_api_keys', - 'access_developer_docs'] - conn = op.get_bind() - conn.execute("DELETE FROM permissions") - op.drop_constraint('uix_service_user_permission', 'permissions', type_='unique') - op.drop_column('permissions', 'permission') - try: - sa.Enum(name='permission_types').drop(conn, checkfirst=False) - except: - pass - permission_types = sa.Enum(*new_permissions, name='permission_types') - permission_types.create(op.get_bind()) - op.add_column('permissions', sa.Column('permission', permission_types, nullable=False)) - add_default_permissions(conn, new_permissions) - op.alter_column('permissions', 'permission', nullable=False) - op.create_unique_constraint('uix_service_user_permission', 'permissions', ['service_id', 'user_id', 'permission']) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Nick - please adjust! ### - old_permissions = ['manage_service', - 'send_messages', - 'manage_api_keys', - 'manage_templates', - 'manage_team', - 'view_activity'] - conn = op.get_bind() - conn.execute("DELETE FROM permissions") - op.drop_constraint('uix_service_user_permission', 'permissions', type_='unique') - op.drop_column('permissions', 'permission') - try: - sa.Enum(name='permission_types').drop(conn, checkfirst=False) - except: - pass - permission_types = sa.Enum(*old_permissions, name='permission_types') - permission_types.create(op.get_bind()) - op.add_column('permissions', sa.Column('permission', permission_types, nullable=False)) - add_default_permissions(conn, old_permissions) - op.alter_column('permissions', 'permission', nullable=False) - op.create_unique_constraint('uix_service_user_permission', 'permissions', ['service_id', 'user_id', 'permission']) - ### end Alembic commands ### diff --git a/migrations/versions/0034_job_sent_count.py b/migrations/versions/0034_job_sent_count.py deleted file mode 100644 index d741d1b88..000000000 --- a/migrations/versions/0034_job_sent_count.py +++ /dev/null @@ -1,26 +0,0 @@ -"""empty message - -Revision ID: 0034_job_sent_count -Revises: 0033_correct_permission_enums -Create Date: 2016-03-04 13:44:32.217058 - -""" - -# revision identifiers, used by Alembic. -revision = '0034_job_sent_count' -down_revision = '0033_correct_permission_enums' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('jobs', sa.Column('notifications_sent', sa.Integer(), nullable=True)) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('jobs', 'notifications_sent') - ### end Alembic commands ### diff --git a/migrations/versions/0035_default_sent_count.py b/migrations/versions/0035_default_sent_count.py deleted file mode 100644 index aac9524a6..000000000 --- a/migrations/versions/0035_default_sent_count.py +++ /dev/null @@ -1,27 +0,0 @@ -"""empty message - -Revision ID: 0035_default_sent_count -Revises: 0034_job_sent_count -Create Date: 2016-03-08 09:08:55.721654 - -""" - -# revision identifiers, used by Alembic. -revision = '0035_default_sent_count' -down_revision = '0034_job_sent_count' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - op.execute('update jobs set notifications_sent = notification_count') - op.alter_column('jobs', 'notifications_sent', - existing_type=sa.INTEGER(), - nullable=False) - - -def downgrade(): - op.alter_column('jobs', 'notifications_sent', - existing_type=sa.INTEGER(), - nullable=True) diff --git a/migrations/versions/0036_notification_stats.py b/migrations/versions/0036_notification_stats.py deleted file mode 100644 index 33c6d3f90..000000000 --- a/migrations/versions/0036_notification_stats.py +++ /dev/null @@ -1,37 +0,0 @@ -"""empty message - -Revision ID: 0036_notification_stats -Revises: 0035_default_sent_count -Create Date: 2016-03-08 11:16:25.659463 - -""" - -# revision identifiers, used by Alembic. -revision = '0036_notification_stats' -down_revision = '0035_default_sent_count' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - op.create_table('notification_statistics', - sa.Column('id', sa.Integer(), nullable=False), - sa.Column('day', sa.String(length=255), nullable=False), - sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('emails_requested', sa.BigInteger(), nullable=False), - sa.Column('emails_delivered', sa.BigInteger(), nullable=True), - sa.Column('emails_error', sa.BigInteger(), nullable=True), - sa.Column('sms_requested', sa.BigInteger(), nullable=False), - sa.Column('sms_delivered', sa.BigInteger(), nullable=True), - sa.Column('sms_error', sa.BigInteger(), nullable=True), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.PrimaryKeyConstraint('id'), - sa.UniqueConstraint('service_id', 'day', name='uix_service_to_day') - ) - op.create_index(op.f('ix_service_notification_stats_service_id'), 'notification_statistics', ['service_id'], unique=False) - - -def downgrade(): - op.drop_index(op.f('ix_service_notification_stats_service_id'), table_name='notification_statistics') - op.drop_table('notification_statistics') diff --git a/migrations/versions/0037_more_job_states.py b/migrations/versions/0037_more_job_states.py deleted file mode 100644 index ef6e5671b..000000000 --- a/migrations/versions/0037_more_job_states.py +++ /dev/null @@ -1,30 +0,0 @@ -"""empty message - -Revision ID: 0037_more_job_states -Revises: 0036_notification_stats -Create Date: 2016-03-08 11:16:25.659463 - -""" - -# revision identifiers, used by Alembic. -revision = '0037_more_job_states' -down_revision = '0036_notification_stats' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - -def upgrade(): - op.drop_column('jobs', 'status') - op.execute('DROP TYPE job_status_types') - job_status_types = sa.Enum('pending', 'in progress', 'finished', 'sending limits exceeded', name='job_status_types') - job_status_types.create(op.get_bind()) - op.add_column('jobs', sa.Column('status', job_status_types, nullable=True)) - op.get_bind() - op.execute("update jobs set status='finished'") - op.alter_column('jobs', 'status', nullable=False) - - -def downgrade(): - op.drop_column('jobs', 'status') - op.execute('DROP TYPE job_status_types') diff --git a/migrations/versions/0038_reduce_limits.py b/migrations/versions/0038_reduce_limits.py deleted file mode 100644 index e9d9fd1f3..000000000 --- a/migrations/versions/0038_reduce_limits.py +++ /dev/null @@ -1,21 +0,0 @@ -"""empty message - -Revision ID: 0038_reduce_limits -Revises: 0037_more_job_states -Create Date: 2016-03-08 11:16:25.659463 - -""" - -# revision identifiers, used by Alembic. -revision = '0038_reduce_limits' -down_revision = '0037_more_job_states' - -from alembic import op - - -def upgrade(): - op.execute('update services set "limit" = 50') - - -def downgrade(): - pass \ No newline at end of file diff --git a/migrations/versions/0039_more_notification_states.py b/migrations/versions/0039_more_notification_states.py deleted file mode 100644 index 7ac99e6ab..000000000 --- a/migrations/versions/0039_more_notification_states.py +++ /dev/null @@ -1,31 +0,0 @@ -"""empty message - -Revision ID: 0039_more_notification_states -Revises: 0038_reduce_limits -Create Date: 2016-03-08 11:16:25.659463 - -""" - -# revision identifiers, used by Alembic. -revision = '0039_more_notification_states' -down_revision = '0038_reduce_limits' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - - -def upgrade(): - op.drop_column('notifications', 'status') - op.execute('DROP TYPE notification_status_types') - notification_status_types = sa.Enum('sent', 'delivered', 'failed', 'complaint', 'bounce', name='notification_status_types') - notification_status_types.create(op.get_bind()) - op.add_column('notifications', sa.Column('status', notification_status_types, nullable=True)) - op.get_bind() - op.execute("update notifications set status='delivered'") - op.alter_column('notifications', 'status', nullable=False) - - -def downgrade(): - op.drop_column('notifications', 'status') - op.execute('DROP TYPE notification_status_types') diff --git a/migrations/versions/0040_add_reference.py b/migrations/versions/0040_add_reference.py deleted file mode 100644 index 65a6ad785..000000000 --- a/migrations/versions/0040_add_reference.py +++ /dev/null @@ -1,24 +0,0 @@ -"""empty message - -Revision ID: 0040_add_reference -Revises: 0039_more_notification_states -Create Date: 2016-03-11 09:15:57.900192 - -""" - -# revision identifiers, used by Alembic. -revision = '0040_add_reference' -down_revision = '0039_more_notification_states' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - op.add_column('notifications', sa.Column('reference', sa.String(), nullable=True)) - op.create_index(op.f('ix_notifications_reference'), 'notifications', ['reference'], unique=False) - - -def downgrade(): - op.drop_index(op.f('ix_notifications_reference'), table_name='notifications') - op.drop_column('notifications', 'reference') diff --git a/migrations/versions/0041_platform_admin.py b/migrations/versions/0041_platform_admin.py deleted file mode 100644 index d75c8f5c0..000000000 --- a/migrations/versions/0041_platform_admin.py +++ /dev/null @@ -1,33 +0,0 @@ -"""empty message - -Revision ID: 0041_platform_admin -Revises: 0040_add_reference -Create Date: 2016-03-16 16:33:15.279429 - -""" - -# revision identifiers, used by Alembic. -revision = '0041_platform_admin' -down_revision = '0040_add_reference' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_index(op.f('ix_notification_statistics_service_id'), 'notification_statistics', ['service_id'], unique=False) - op.drop_index('ix_service_notification_stats_service_id', table_name='notification_statistics') - op.add_column('users', sa.Column('platform_admin', sa.Boolean(), nullable=True, default=False)) - op.get_bind() - op.execute('update users set platform_admin = False') - op.alter_column('users', 'platform_admin', nullable=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('users', 'platform_admin') - op.create_index('ix_service_notification_stats_service_id', 'notification_statistics', ['service_id'], unique=False) - op.drop_index(op.f('ix_notification_statistics_service_id'), table_name='notification_statistics') - ### end Alembic commands ### diff --git a/migrations/versions/0042_default_stats_to_zero.py b/migrations/versions/0042_default_stats_to_zero.py deleted file mode 100644 index 4d862c903..000000000 --- a/migrations/versions/0042_default_stats_to_zero.py +++ /dev/null @@ -1,45 +0,0 @@ -"""empty message - -Revision ID: 0042_default_stats_to_zero -Revises: 0041_platform_admin -Create Date: 2016-03-17 11:09:17.906910 - -""" - -# revision identifiers, used by Alembic. -revision = '0042_default_stats_to_zero' -down_revision = '0041_platform_admin' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.get_bind() - op.execute('update notification_statistics set emails_delivered = 0 where emails_delivered is Null') - op.execute('update notification_statistics set emails_error = 0 where emails_error is Null') - op.execute('update notification_statistics set sms_delivered = 0 where sms_delivered is Null') - op.execute('update notification_statistics set sms_error = 0 where sms_error is Null') - op.alter_column('notification_statistics', 'emails_requested', server_default='0') - op.alter_column('notification_statistics', 'emails_delivered', server_default='0', nullable=False) - op.alter_column('notification_statistics', 'emails_error', server_default='0', nullable=False) - op.alter_column('notification_statistics', 'sms_requested', server_default='0') - op.alter_column('notification_statistics', 'sms_delivered', server_default='0', nullable=False) - op.alter_column('notification_statistics', 'sms_error', server_default='0', nullable=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.alter_column('notification_statistics', 'emails_requested', server_default=None) - op.alter_column('notification_statistics', 'emails_delivered', server_default=None, nullable=True) - op.alter_column('notification_statistics', 'emails_error', server_default=None, nullable=True) - op.alter_column('notification_statistics', 'sms_requested', server_default=None) - op.alter_column('notification_statistics', 'sms_delivered', server_default=None, nullable=True) - op.alter_column('notification_statistics', 'sms_error', server_default=None, nullable=True) - op.execute('update notification_statistics set emails_delivered = Null where emails_delivered = 0') - op.execute('update notification_statistics set emails_error = Null where emails_error = 0') - op.execute('update notification_statistics set sms_delivered = Null where sms_delivered = 0') - op.execute('update notification_statistics set sms_error = Null where sms_error = 0') - ### end Alembic commands ### diff --git a/migrations/versions/0043_view_activity.py b/migrations/versions/0043_view_activity.py deleted file mode 100644 index c69089603..000000000 --- a/migrations/versions/0043_view_activity.py +++ /dev/null @@ -1,47 +0,0 @@ -"""empty message - -Revision ID: 0043_add_view_activity -Revises: 0042_default_stats_to_zero -Create Date: 2016-03-29 13:46:36.219549 - -""" - -# revision identifiers, used by Alembic. -import uuid - -revision = '0043_add_view_activity' -down_revision = '0042_default_stats_to_zero' - -from alembic import op - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - conn.execute('COMMIT') - conn.execute("alter type permission_types add value IF NOT EXISTS 'view_activity'") - user_services = conn.execute("SELECT * FROM user_to_service where user_id " - "not in (select user_id from permissions " - "where permission='view_activity')").fetchall() - for user_service in user_services: - conn.execute( - "insert into permissions (id, service_id, user_id, created_at, permission) " - "values('{0}', '{1}', {2}, now(), 'view_activity')".format( - uuid.uuid4(), user_service.service_id, user_service.user_id)) - conn.execute("delete from permissions where permission = 'access_developer_docs'") - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - conn = op.get_bind() - conn.execute("delete from permissions where permission = 'view_activity'") - conn.execute('COMMIT') - conn.execute("alter type permission_types add value IF NOT EXISTS 'access_developer_docs'") - manage_api_key_users = conn.execute("SELECT * FROM permissions where permission='manage_api_keys'").fetchall() - for user_service in manage_api_key_users: - conn.execute( - "insert into permissions (id, service_id, user_id, created_at, permission) " - "values('{0}', '{1}', {2}, now(), 'access_developer_docs')".format( - uuid.uuid4(), user_service.service_id, user_service.user_id)) - ### end Alembic commands ### diff --git a/migrations/versions/0044_add_template_stats.py b/migrations/versions/0044_add_template_stats.py deleted file mode 100644 index d85d8fd72..000000000 --- a/migrations/versions/0044_add_template_stats.py +++ /dev/null @@ -1,42 +0,0 @@ -"""empty message - -Revision ID: 0044_add_template_stats -Revises: 0043_add_view_activity -Create Date: 2016-03-31 12:05:19.630792 - -""" - -# revision identifiers, used by Alembic. -revision = '0044_add_template_stats' -down_revision = '0043_add_view_activity' - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.dialects import postgresql - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.create_table('template_statistics', - sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('service_id', postgresql.UUID(as_uuid=True), nullable=False), - sa.Column('template_id', sa.BigInteger(), nullable=False), - sa.Column('usage_count', sa.BigInteger(), nullable=False), - sa.Column('day', sa.Date(), nullable=False), - sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), - sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ), - sa.PrimaryKeyConstraint('id') - ) - op.create_index(op.f('ix_template_statistics_day'), 'template_statistics', ['day'], unique=False) - op.create_index(op.f('ix_template_statistics_service_id'), 'template_statistics', ['service_id'], unique=False) - op.create_index(op.f('ix_template_statistics_template_id'), 'template_statistics', ['template_id'], unique=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_index(op.f('ix_template_statistics_template_id'), table_name='template_statistics') - op.drop_index(op.f('ix_template_statistics_service_id'), table_name='template_statistics') - op.drop_index(op.f('ix_template_statistics_day'), table_name='template_statistics') - op.drop_table('template_statistics') - ### end Alembic commands ### diff --git a/migrations/versions/0045_template_stats_update_timestamp.py b/migrations/versions/0045_template_stats_update_timestamp.py deleted file mode 100644 index fb9d8d0b1..000000000 --- a/migrations/versions/0045_template_stats_update_timestamp.py +++ /dev/null @@ -1,32 +0,0 @@ -"""empty message - -Revision ID: 0045_template_stats_update_time -Revises: 0044_add_template_stats -Create Date: 2016-04-05 14:32:45.165755 - -""" - -# revision identifiers, used by Alembic. -revision = '0045_template_stats_update_time' -down_revision = '0044_add_template_stats' - -import datetime - -from alembic import op -import sqlalchemy as sa -from sqlalchemy.sql import table, column - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('template_statistics', sa.Column('updated_at', sa.DateTime(), nullable=True)) - updated_at = table('template_statistics', column('updated_at')) - op.execute(updated_at.update().values(updated_at=datetime.datetime.utcnow())) - op.alter_column('template_statistics', 'updated_at', nullable=False) - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('template_statistics', 'updated_at') - ### end Alembic commands ### diff --git a/migrations/versions/0046_remove_bucketname.py b/migrations/versions/0046_remove_bucketname.py deleted file mode 100644 index d5cfd2377..000000000 --- a/migrations/versions/0046_remove_bucketname.py +++ /dev/null @@ -1,28 +0,0 @@ -"""empty message - -Revision ID: 0046_remove_bucketname -Revises: 0045_template_stats_update_time -Create Date: 2016-04-07 12:23:55.050714 - -""" - -# revision identifiers, used by Alembic. -revision = '0046_remove_bucketname' -down_revision = '0045_template_stats_update_time' - -from alembic import op -import sqlalchemy as sa - - -def upgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.drop_column('jobs', 'file_name') - op.drop_column('jobs', 'bucket_name') - ### end Alembic commands ### - - -def downgrade(): - ### commands auto generated by Alembic - please adjust! ### - op.add_column('jobs', sa.Column('bucket_name', sa.VARCHAR(), autoincrement=False, nullable=False)) - op.add_column('jobs', sa.Column('file_name', sa.VARCHAR(), autoincrement=False, nullable=False)) - ### end Alembic commands ### diff --git a/tests/app/conftest.py b/tests/app/conftest.py index fa67cb573..43551d6ee 100644 --- a/tests/app/conftest.py +++ b/tests/app/conftest.py @@ -421,3 +421,8 @@ def sample_service_permission(notify_db, db.session.add(p_model) db.session.commit() return p_model + + +@pytest.fixture(scope='function') +def fake_uuid(): + return "6ce466d0-fd6a-11e5-82f5-e0accb9d11a6" diff --git a/tests/app/dao/test_api_key_dao.py b/tests/app/dao/test_api_key_dao.py index fbb33e517..b70bb2172 100644 --- a/tests/app/dao/test_api_key_dao.py +++ b/tests/app/dao/test_api_key_dao.py @@ -42,10 +42,13 @@ def test_save_api_key_should_update_the_api_key(notify_api, notify_db, notify_db assert all_api_keys[0].service_id == saved_api_key.service_id -def test_get_api_key_should_raise_exception_when_api_key_does_not_exist(notify_api, notify_db, notify_db_session, - sample_service): +def test_get_api_key_should_raise_exception_when_api_key_does_not_exist(notify_api, + notify_db, + notify_db_session, + sample_service, + fake_uuid): try: - get_model_api_keys(sample_service.id, id=123) + get_model_api_keys(sample_service.id, id=fake_uuid) fail("Should have thrown a NoResultFound exception") except NoResultFound: pass @@ -78,9 +81,10 @@ def test_get_unsigned_secret_returns_key(notify_api, def test_should_not_allow_duplicate_key_names_per_service(notify_api, notify_db, notify_db_session, - sample_api_key): + sample_api_key, + fake_uuid): api_key = ApiKey( - **{'id': sample_api_key.id + 1, 'service_id': sample_api_key.service_id, 'name': sample_api_key.name}) + **{'id': fake_uuid, 'service_id': sample_api_key.service_id, 'name': sample_api_key.name}) try: save_model_api_key(api_key) fail("should throw IntegrityError") diff --git a/tests/app/dao/test_templates_dao.py b/tests/app/dao/test_templates_dao.py index a3d74afb2..20c6b3c32 100644 --- a/tests/app/dao/test_templates_dao.py +++ b/tests/app/dao/test_templates_dao.py @@ -42,20 +42,6 @@ def test_create_email_template(sample_service): assert dao_get_all_templates_for_service(sample_service.id)[0].name == 'Sample Template' -def test_create_email_template_fails_if_no_subject(sample_service): - data = { - 'name': 'Sample Template', - 'template_type': "email", - 'content': "Template content", - 'service': sample_service - } - template = Template(**data) - - with pytest.raises(IntegrityError) as e: - dao_create_template(template) - assert 'new row for relation "templates" violates check constraint "ch_email_template_has_subject"' in str(e.value) - - def test_update_template(sample_service): data = { 'name': 'Sample Template', @@ -154,7 +140,7 @@ def test_get_template_by_id_and_service(notify_db, notify_db_session, sample_ser assert Template.query.count() == 1 -def test_get_template_by_id_and_service_returns_none_if_no_template(sample_service): +def test_get_template_by_id_and_service_returns_none_if_no_template(sample_service, fake_uuid): with pytest.raises(NoResultFound) as e: - dao_get_template_by_id_and_service_id(template_id=999, service_id=sample_service.id) + dao_get_template_by_id_and_service_id(template_id=fake_uuid, service_id=sample_service.id) assert 'No row was found for one' in str(e.value) diff --git a/tests/app/dao/test_users_dao.py b/tests/app/dao/test_users_dao.py index 35dce1b60..2a36bf1ba 100644 --- a/tests/app/dao/test_users_dao.py +++ b/tests/app/dao/test_users_dao.py @@ -54,9 +54,9 @@ def test_get_user(notify_api, notify_db, notify_db_session): assert get_model_users(user_id=another_user.id).email_address == email -def test_get_user_not_exists(notify_api, notify_db, notify_db_session): +def test_get_user_not_exists(notify_api, notify_db, notify_db_session, fake_uuid): try: - get_model_users(user_id="12345") + get_model_users(user_id=fake_uuid) pytest.fail("NoResultFound exception not thrown.") except NoResultFound as e: pass diff --git a/tests/app/invite/test_invite_rest.py b/tests/app/invite/test_invite_rest.py index 78c6223d4..4f2ad9b69 100644 --- a/tests/app/invite/test_invite_rest.py +++ b/tests/app/invite/test_invite_rest.py @@ -19,7 +19,7 @@ def test_create_invited_user(notify_api, sample_service, mocker): data = { 'service': str(sample_service.id), 'email_address': email_address, - 'from_user': invite_from.id, + 'from_user': str(invite_from.id), 'permissions': 'send_messages,manage_service,manage_api_keys' } @@ -41,7 +41,7 @@ def test_create_invited_user(notify_api, sample_service, mocker): assert json_resp['data']['service'] == str(sample_service.id) assert json_resp['data']['email_address'] == email_address - assert json_resp['data']['from_user'] == invite_from.id + assert json_resp['data']['from_user'] == str(invite_from.id) assert json_resp['data']['permissions'] == 'send_messages,manage_service,manage_api_keys' assert json_resp['data']['id'] invitation_expiration_days = notify_api.config['INVITATION_EXPIRATION_DAYS'] @@ -70,7 +70,7 @@ def test_create_invited_user_invalid_email(notify_api, sample_service, mocker): data = { 'service': str(sample_service.id), 'email_address': email_address, - 'from_user': invite_from.id, + 'from_user': str(invite_from.id), 'permissions': 'send_messages,manage_service,manage_api_keys' } @@ -128,7 +128,7 @@ def test_get_all_invited_users_by_service(notify_api, notify_db, notify_db_sessi for invite in json_resp['data']: assert invite['service'] == str(sample_service.id) - assert invite['from_user'] == invite_from.id + assert invite['from_user'] == str(invite_from.id) assert invite['id'] @@ -176,7 +176,7 @@ def test_get_invited_user_by_service_and_id(notify_api, sample_service, sample_i assert json_resp['data']['service'] == str(sample_service.id) assert json_resp['data']['email_address'] == invite_email_address - assert json_resp['data']['from_user'] == invite_from.id + assert json_resp['data']['from_user'] == str(invite_from.id) assert json_resp['data']['id'] @@ -218,12 +218,11 @@ def test_update_invited_user_set_status_to_cancelled(notify_api, sample_invited_ assert json_resp['status'] == 'cancelled' -def test_update_invited_user_for_wrong_service_returns_404(notify_api, sample_invited_user): +def test_update_invited_user_for_wrong_service_returns_404(notify_api, sample_invited_user, fake_uuid): with notify_api.test_request_context(): with notify_api.test_client() as client: data = {'status': 'cancelled'} - bad_service_id = uuid.uuid4() - url = '/service/{0}/invite/{1}'.format(bad_service_id, sample_invited_user.id) + url = '/service/{0}/invite/{1}'.format(fake_uuid, sample_invited_user.id) auth_header = create_authorization_header( path=url, method='POST', diff --git a/tests/app/job/test_rest.py b/tests/app/job/test_rest.py index c2468fa8a..a604a346f 100644 --- a/tests/app/job/test_rest.py +++ b/tests/app/job/test_rest.py @@ -54,12 +54,11 @@ def test_get_job_with_invalid_job_id_returns404(notify_api, sample_template): assert resp_json['message'] == 'No result found' -def test_get_job_with_unknown_id_returns404(notify_api, sample_template): - random_id = str(uuid.uuid4()) +def test_get_job_with_unknown_id_returns404(notify_api, sample_template, fake_uuid): service_id = sample_template.service.id with notify_api.test_request_context(): with notify_api.test_client() as client: - path = '/service/{}/job/{}'.format(service_id, random_id) + path = '/service/{}/job/{}'.format(service_id, fake_uuid) auth_header = create_authorization_header( service_id=sample_template.service.id, path=path, @@ -89,15 +88,14 @@ def test_get_job_by_id(notify_api, sample_job): assert resp_json['data']['id'] == job_id -def test_create_job(notify_api, sample_template, mocker): +def test_create_job(notify_api, sample_template, mocker, fake_uuid): with notify_api.test_request_context(): with notify_api.test_client() as client: mocker.patch('app.celery.tasks.process_job.apply_async') - job_id = uuid.uuid4() data = { - 'id': str(job_id), + 'id': fake_uuid, 'service': str(sample_template.service.id), - 'template': sample_template.id, + 'template': str(sample_template.id), 'original_file_name': 'thisisatest.csv', 'notification_count': 1 } @@ -115,15 +113,15 @@ def test_create_job(notify_api, sample_template, mocker): assert response.status_code == 201 app.celery.tasks.process_job.apply_async.assert_called_once_with( - ([str(job_id)]), + ([str(fake_uuid)]), queue="process-job" ) resp_json = json.loads(response.get_data(as_text=True)) - assert resp_json['data']['id'] == str(job_id) + assert resp_json['data']['id'] == fake_uuid assert resp_json['data']['service'] == str(sample_template.service.id) - assert resp_json['data']['template'] == sample_template.id + assert resp_json['data']['template'] == str(sample_template.id) assert resp_json['data']['original_file_name'] == 'thisisatest.csv' diff --git a/tests/app/notifications/test_rest.py b/tests/app/notifications/test_rest.py index f4226a656..4b5804b79 100644 --- a/tests/app/notifications/test_rest.py +++ b/tests/app/notifications/test_rest.py @@ -30,7 +30,7 @@ def test_get_notification_by_id(notify_api, sample_notification): notification = json.loads(response.get_data(as_text=True))['data']['notification'] assert notification['status'] == 'sent' assert notification['template'] == { - 'id': sample_notification.template.id, + 'id': str(sample_notification.template.id), 'name': sample_notification.template.name, 'template_type': sample_notification.template.template_type} assert notification['job'] == { @@ -76,7 +76,7 @@ def test_get_all_notifications(notify_api, sample_notification): notifications = json.loads(response.get_data(as_text=True)) assert notifications['notifications'][0]['status'] == 'sent' assert notifications['notifications'][0]['template'] == { - 'id': sample_notification.template.id, + 'id': str(sample_notification.template.id), 'name': sample_notification.template.name, 'template_type': sample_notification.template.template_type} assert notifications['notifications'][0]['job'] == { @@ -504,14 +504,14 @@ def test_should_reject_bad_phone_numbers(notify_api, sample_template, mocker): assert response.status_code == 400 -def test_send_notification_invalid_template_id(notify_api, sample_template, mocker): +def test_send_notification_invalid_template_id(notify_api, sample_template, mocker, fake_uuid): with notify_api.test_request_context(): with notify_api.test_client() as client: mocker.patch('app.celery.tasks.send_sms.apply_async') data = { 'to': '+447700900855', - 'template': 9999 + 'template': fake_uuid } auth_header = create_authorization_header( service_id=sample_template.service.id, @@ -707,7 +707,7 @@ def test_should_allow_valid_sms_notification(notify_api, sample_template, mocker data = { 'to': '07700 900 855', - 'template': sample_template.id + 'template': str(sample_template.id) } auth_header = create_authorization_header( @@ -767,7 +767,7 @@ def test_should_reject_email_notification_with_bad_email(notify_api, sample_emai to_address = "bad-email" data = { 'to': to_address, - 'template': sample_email_template.service.id + 'template': str(sample_email_template.service.id) } auth_header = create_authorization_header( service_id=sample_email_template.service.id, @@ -788,13 +788,13 @@ def test_should_reject_email_notification_with_bad_email(notify_api, sample_emai def test_should_reject_email_notification_with_template_id_that_cant_be_found( - notify_api, sample_email_template, mocker): + notify_api, sample_email_template, mocker, fake_uuid): with notify_api.test_request_context(): with notify_api.test_client() as client: mocker.patch('app.celery.tasks.send_email.apply_async') data = { 'to': 'ok@ok.com', - 'template': 1234 + 'template': fake_uuid } auth_header = create_authorization_header( service_id=sample_email_template.service.id, @@ -829,7 +829,7 @@ def test_should_not_allow_email_template_from_another_service(notify_api, servic data = { 'to': sample_user.email_address, - 'template': service_2_templates[0].id + 'template': str(service_2_templates[0].id) } auth_header = create_authorization_header( @@ -861,7 +861,7 @@ def test_should_not_send_email_if_restricted_and_not_a_service_user(notify_api, data = { 'to': "not-someone-we-trust@email-address.com", - 'template': sample_email_template.id + 'template': str(sample_email_template.id) } auth_header = create_authorization_header( @@ -896,8 +896,8 @@ def test_should_not_send_email_for_job_if_restricted_and_not_a_service_user( data = { 'to': "not-someone-we-trust@email-address.com", - 'template': sample_job.template.id, - 'job': sample_job.id + 'template': str(sample_job.template.id), + 'job': (sample_job.id) } auth_header = create_authorization_header( @@ -927,7 +927,7 @@ def test_should_allow_valid_email_notification(notify_api, sample_email_template data = { 'to': 'ok@ok.com', - 'template': sample_email_template.id + 'template': str(sample_email_template.id) } auth_header = create_authorization_header( @@ -971,7 +971,7 @@ def test_should_block_api_call_if_over_day_limit(notify_db, notify_db_session, n data = { 'to': 'ok@ok.com', - 'template': email_template.id + 'template': str(email_template.id) } auth_header = create_authorization_header( @@ -1007,7 +1007,7 @@ def test_no_limit_for_live_service(notify_api, data = { 'to': 'ok@ok.com', - 'template': sample_email_template.id + 'template': str(sample_email_template.id) } auth_header = create_authorization_header( @@ -1041,7 +1041,7 @@ def test_should_block_api_call_if_over_day_limit_regardless_of_type(notify_db, n data = { 'to': '+447234123123', - 'template': sms_template.id + 'template': str(sms_template.id) } auth_header = create_authorization_header( @@ -1074,7 +1074,7 @@ def test_should_allow_api_call_if_under_day_limit_regardless_of_type(notify_db, data = { 'to': '+447634123123', - 'template': sms_template.id + 'template': str(sms_template.id) } auth_header = create_authorization_header( diff --git a/tests/app/permissions/test_rest.py b/tests/app/permissions/test_rest.py index 79fadd20c..16787bb6d 100644 --- a/tests/app/permissions/test_rest.py +++ b/tests/app/permissions/test_rest.py @@ -22,7 +22,7 @@ def test_get_permission_list(notify_api, notify_db, notify_db_session, sample_pe assert len(json_resp['data']) == 1 expected = { "permission": sample_permission.permission, - "user": sample_permission.user.id, + "user": str(sample_permission.user.id), "id": str(sample_permission.id), "service": None } @@ -52,7 +52,7 @@ def test_get_permission_filter(notify_api, service_id=str(sample_service.id)).first() expected = { "permission": another_permission.permission, - "user": sample_user.id, + "user": str(sample_user.id), "id": str(another_permission.id), "service": str(sample_service.id) } @@ -75,7 +75,7 @@ def test_get_permission(notify_api, notify_db, notify_db_session, sample_permiss json_resp = json.loads(response.get_data(as_text=True)) expected = { "permission": sample_permission.permission, - "user": sample_permission.user.id, + "user": str(sample_permission.user.id), "id": str(sample_permission.id), "service": None } diff --git a/tests/app/service/test_rest.py b/tests/app/service/test_rest.py index 60f1ed3cd..1d73f9fcb 100644 --- a/tests/app/service/test_rest.py +++ b/tests/app/service/test_rest.py @@ -177,7 +177,7 @@ def test_create_service(notify_api, sample_user): with notify_api.test_client() as client: data = { 'name': 'created service', - 'user_id': sample_user.id, + 'user_id': str(sample_user.id), 'limit': 1000, 'restricted': False, 'active': False, @@ -238,12 +238,15 @@ def test_should_not_create_service_with_missing_user_id_field(notify_api): assert 'Missing data for required field.' in json_resp['message']['user_id'] -def test_should_not_create_service_with_missing_if_user_id_is_not_in_database(notify_api, notify_db, notify_db_session): +def test_should_not_create_service_with_missing_if_user_id_is_not_in_database(notify_api, + notify_db, + notify_db_session, + fake_uuid): with notify_api.test_request_context(): with notify_api.test_client() as client: data = { 'email_from': 'service', - 'user_id': 1234, + 'user_id': fake_uuid, 'name': 'created service', 'limit': 1000, 'restricted': False, @@ -269,7 +272,7 @@ def test_should_not_create_service_if_missing_data(notify_api, sample_user): with notify_api.test_request_context(): with notify_api.test_client() as client: data = { - 'user_id': sample_user.id + 'user_id': str(sample_user.id) } auth_header = create_authorization_header( path='/service', @@ -299,7 +302,7 @@ def test_should_not_create_service_with_duplicate_name(notify_api, with notify_api.test_client() as client: data = { 'name': sample_service.name, - 'user_id': sample_service.users[0].id, + 'user_id': str(sample_service.users[0].id), 'limit': 1000, 'restricted': False, 'active': False, @@ -327,7 +330,7 @@ def test_create_service_should_throw_duplicate_key_constraint_for_existing_email with notify_api.test_client() as client: data = { 'name': 'First SERVICE', - 'user_id': first_service.users[0].id, + 'user_id': str(first_service.users[0].id), 'limit': 1000, 'restricted': False, 'active': False, @@ -543,7 +546,7 @@ def test_default_permissions_are_added_for_user_service(notify_api, with notify_api.test_client() as client: data = { 'name': 'created service', - 'user_id': sample_user.id, + 'user_id': str(sample_user.id), 'limit': 1000, 'restricted': False, 'active': False, @@ -650,7 +653,7 @@ def test_add_existing_user_to_another_service_with_all_permissions(notify_api, ) assert resp.status_code == 200 json_resp = json.loads(resp.get_data(as_text=True)) - assert user_to_add.id in json_resp['data']['users'] + assert str(user_to_add.id) in json_resp['data']['users'] # check user has all permissions auth_header = create_authorization_header( diff --git a/tests/app/template/test_rest.py b/tests/app/template/test_rest.py index fbb3cd6d2..13551afa9 100644 --- a/tests/app/template/test_rest.py +++ b/tests/app/template/test_rest.py @@ -67,25 +67,24 @@ def test_should_create_a_new_email_template_for_a_service(notify_api, sample_ser assert json_resp['data']['id'] -def test_should_be_error_if_service_does_not_exist_on_create(notify_api): +def test_should_be_error_if_service_does_not_exist_on_create(notify_api, fake_uuid): with notify_api.test_request_context(): with notify_api.test_client() as client: - bad_id = str(uuid.uuid4()) data = { 'name': 'my template', 'template_type': 'sms', 'content': 'template content', - 'service': bad_id + 'service': fake_uuid } data = json.dumps(data) auth_header = create_authorization_header( - path='/service/{}/template'.format(bad_id), + path='/service/{}/template'.format(fake_uuid), method='POST', request_body=data ) response = client.post( - '/service/{}/template'.format(bad_id), + '/service/{}/template'.format(fake_uuid), headers=[('Content-Type', 'application/json'), auth_header], data=data ) @@ -95,22 +94,21 @@ def test_should_be_error_if_service_does_not_exist_on_create(notify_api): assert json_resp['message'] == 'No result found' -def test_should_be_error_if_service_does_not_exist_on_update(notify_api): +def test_should_be_error_if_service_does_not_exist_on_update(notify_api, fake_uuid): with notify_api.test_request_context(): with notify_api.test_client() as client: - bad_id = str(uuid.uuid4()) data = { 'name': 'my template' } data = json.dumps(data) auth_header = create_authorization_header( - path='/service/{}/template/123'.format(bad_id), + path='/service/{}/template/{}'.format(fake_uuid, fake_uuid), method='POST', request_body=data ) response = client.post( - '/service/{}/template/123'.format(bad_id), + '/service/{}/template/{}'.format(fake_uuid, fake_uuid), headers=[('Content-Type', 'application/json'), auth_header], data=data ) @@ -142,9 +140,9 @@ def test_must_have_a_subject_on_an_email_template(notify_api, sample_service): data=data ) json_resp = json.loads(response.get_data(as_text=True)) - assert response.status_code == 500 + assert response.status_code == 400 assert json_resp['result'] == 'error' - assert json_resp['message'] == 'Failed to create template' + assert json_resp['message'] == {'subject': ['Invalid template subject']} def test_must_have_a_uniqe_subject_on_an_email_template(notify_api, sample_service): @@ -393,18 +391,17 @@ def test_should_return_empty_array_if_no_templates_for_service(notify_api, sampl assert len(json_resp['data']) == 0 -def test_should_return_404_if_no_templates_for_service_with_id(notify_api, sample_service): +def test_should_return_404_if_no_templates_for_service_with_id(notify_api, sample_service, fake_uuid): with notify_api.test_request_context(): with notify_api.test_client() as client: - uuid_ = uuid.uuid4() auth_header = create_authorization_header( - path='/service/{}/template/{}'.format(sample_service.id, 9999), + path='/service/{}/template/{}'.format(sample_service.id, fake_uuid), method='GET' ) response = client.get( - '/service/{}/template/{}'.format(sample_service.id, 9999), + '/service/{}/template/{}'.format(sample_service.id, fake_uuid), headers=[auth_header] ) diff --git a/tests/app/user/test_rest.py b/tests/app/user/test_rest.py index 1679e0c17..49674bf9e 100644 --- a/tests/app/user/test_rest.py +++ b/tests/app/user/test_rest.py @@ -27,7 +27,7 @@ def test_get_user_list(notify_api, notify_db, notify_db_session, sample_service) expected_permissions = default_service_permissions fetched = json_resp['data'][0] - assert sample_user.id == fetched['id'] + assert str(sample_user.id) == fetched['id'] assert sample_user.name == fetched['name'] assert sample_user.mobile_number == fetched['mobile_number'] assert sample_user.email_address == fetched['email_address'] @@ -53,7 +53,7 @@ def test_get_user(notify_api, notify_db, notify_db_session, sample_service): expected_permissions = default_service_permissions fetched = json_resp['data'] - assert sample_user.id == fetched['id'] + assert str(sample_user.id) == fetched['id'] assert sample_user.name == fetched['name'] assert sample_user.mobile_number == fetched['mobile_number'] assert sample_user.email_address == fetched['email_address'] @@ -90,9 +90,8 @@ def test_post_user(notify_api, notify_db, notify_db_session): assert resp.status_code == 201 user = User.query.filter_by(email_address='user@digital.cabinet-office.gov.uk').first() json_resp = json.loads(resp.get_data(as_text=True)) - json_resp['data'] == {"email_address": user.email_address, "id": user.id} assert json_resp['data']['email_address'] == user.email_address - assert json_resp['data']['id'] == user.id + assert json_resp['data']['id'] == str(user.id) def test_post_user_missing_attribute_email(notify_api, notify_db, notify_db_session): @@ -186,7 +185,7 @@ def test_put_user(notify_api, notify_db, notify_db_session, sample_service): expected_permissions = default_service_permissions fetched = json_resp['data'] - assert sample_user.id == fetched['id'] + assert str(sample_user.id) == fetched['id'] assert sample_user.name == fetched['name'] assert sample_user.mobile_number == fetched['mobile_number'] assert new_email == fetched['email_address'] @@ -230,13 +229,13 @@ def test_put_user_update_password(notify_api, request_body=json.dumps(data)) headers = [('Content-Type', 'application/json'), auth_header] resp = client.post( - url_for('user.verify_user_password', user_id=sample_user.id), + url_for('user.verify_user_password', user_id=str(sample_user.id)), data=json.dumps(data), headers=headers) assert resp.status_code == 204 -def test_put_user_not_exists(notify_api, notify_db, notify_db_session, sample_user): +def test_put_user_not_exists(notify_api, notify_db, notify_db_session, sample_user, fake_uuid): """ Tests PUT endpoint '/' to update a user doesn't exist. """ @@ -245,17 +244,17 @@ def test_put_user_not_exists(notify_api, notify_db, notify_db_session, sample_us assert User.query.count() == 1 new_email = 'new@digital.cabinet-office.gov.uk' data = {'email_address': new_email} - auth_header = create_authorization_header(path=url_for('user.update_user', user_id="9999"), + auth_header = create_authorization_header(path=url_for('user.update_user', user_id=fake_uuid), method='PUT', request_body=json.dumps(data)) headers = [('Content-Type', 'application/json'), auth_header] resp = client.put( - url_for('user.update_user', user_id="9999"), + url_for('user.update_user', user_id=fake_uuid), data=json.dumps(data), headers=headers) assert resp.status_code == 404 assert User.query.count() == 1 - user = User.query.filter_by(id=sample_user.id).first() + user = User.query.filter_by(id=str(sample_user.id)).first() json_resp = json.loads(resp.get_data(as_text=True)) assert json_resp['result'] == "error" assert json_resp['message'] == 'No result found' @@ -278,7 +277,7 @@ def test_get_user_by_email(notify_api, notify_db, notify_db_session, sample_serv expected_permissions = default_service_permissions fetched = json_resp['data'] - assert sample_user.id == fetched['id'] + assert str(sample_user.id) == fetched['id'] assert sample_user.name == fetched['name'] assert sample_user.mobile_number == fetched['mobile_number'] assert sample_user.email_address == fetched['email_address'] @@ -325,9 +324,9 @@ def test_get_user_with_permissions(notify_api, with notify_api.test_request_context(): with notify_api.test_client() as client: header = create_authorization_header( - path=url_for('user.get_user', user_id=sample_service_permission.user.id), + path=url_for('user.get_user', user_id=str(sample_service_permission.user.id)), method='GET') - response = client.get(url_for('user.get_user', user_id=sample_service_permission.user.id), + response = client.get(url_for('user.get_user', user_id=str(sample_service_permission.user.id)), headers=[header]) assert response.status_code == 200 permissions = json.loads(response.get_data(as_text=True))['data']['permissions'] @@ -345,7 +344,7 @@ def test_set_user_permissions(notify_api, header = create_authorization_header( path=url_for( 'user.set_permissions', - user_id=sample_user.id, + user_id=str(sample_user.id), service_id=str(sample_service.id)), method='POST', request_body=data) @@ -353,7 +352,7 @@ def test_set_user_permissions(notify_api, response = client.post( url_for( 'user.set_permissions', - user_id=sample_user.id, + user_id=str(sample_user.id), service_id=str(sample_service.id)), headers=headers, data=data) @@ -376,7 +375,7 @@ def test_set_user_permissions_multiple(notify_api, header = create_authorization_header( path=url_for( 'user.set_permissions', - user_id=sample_user.id, + user_id=str(sample_user.id), service_id=str(sample_service.id)), method='POST', request_body=data) @@ -384,7 +383,7 @@ def test_set_user_permissions_multiple(notify_api, response = client.post( url_for( 'user.set_permissions', - user_id=sample_user.id, + user_id=str(sample_user.id), service_id=str(sample_service.id)), headers=headers, data=data) @@ -411,7 +410,7 @@ def test_set_user_permissions_remove_old(notify_api, header = create_authorization_header( path=url_for( 'user.set_permissions', - user_id=sample_user.id, + user_id=str(sample_user.id), service_id=str(sample_service.id)), method='POST', request_body=data) @@ -419,7 +418,7 @@ def test_set_user_permissions_remove_old(notify_api, response = client.post( url_for( 'user.set_permissions', - user_id=sample_user.id, + user_id=str(sample_user.id), service_id=str(sample_service.id)), headers=headers, data=data) diff --git a/tests/app/user/test_rest_verify.py b/tests/app/user/test_rest_verify.py index e666c0140..d386085b5 100644 --- a/tests/app/user/test_rest_verify.py +++ b/tests/app/user/test_rest_verify.py @@ -336,7 +336,10 @@ def test_send_user_email_code(notify_api, queue='email-code') -def test_send_user_email_code_returns_404_for_when_user_does_not_exist(notify_api, notify_db, notify_db_session): +def test_send_user_email_code_returns_404_for_when_user_does_not_exist(notify_api, + notify_db, + notify_db_session, + fake_uuid): """ Tests POST endpoint /user//email-code return 404 for missing user """ @@ -344,11 +347,11 @@ def test_send_user_email_code_returns_404_for_when_user_does_not_exist(notify_ap with notify_api.test_client() as client: data = json.dumps({}) auth_header = create_authorization_header( - path=url_for('user.send_user_email_code', user_id=1), + path=url_for('user.send_user_email_code', user_id=fake_uuid), method='POST', request_body=data) resp = client.post( - url_for('user.send_user_email_code', user_id=1), + url_for('user.send_user_email_code', user_id=fake_uuid), data=data, headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 404 @@ -364,11 +367,11 @@ def test_send_user_email_verification(notify_api, with notify_api.test_client() as client: data = json.dumps({}) auth_header = create_authorization_header( - path=url_for('user.send_user_email_verification', user_id=sample_email_code.user.id), + path=url_for('user.send_user_email_verification', user_id=str(sample_email_code.user.id)), method='POST', request_body=data) resp = client.post( - url_for('user.send_user_email_verification', user_id=sample_email_code.user.id), + url_for('user.send_user_email_verification', user_id=str(sample_email_code.user.id)), data=data, headers=[('Content-Type', 'application/json'), auth_header]) assert resp.status_code == 204 diff --git a/tests/conftest.py b/tests/conftest.py index 1908447f0..bc25b6cc4 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -27,24 +27,28 @@ def notify_api(request): @pytest.fixture(scope='session') def notify_db(notify_api, request): - Migrate(notify_api, db) - Manager(db, MigrateCommand) - BASE_DIR = os.path.dirname(os.path.dirname(__file__)) - ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations') - config = Config(ALEMBIC_CONFIG + '/alembic.ini') - config.set_main_option("script_location", ALEMBIC_CONFIG) + try: + Migrate(notify_api, db) + Manager(db, MigrateCommand) + BASE_DIR = os.path.dirname(os.path.dirname(__file__)) + ALEMBIC_CONFIG = os.path.join(BASE_DIR, 'migrations') + config = Config(ALEMBIC_CONFIG + '/alembic.ini') + config.set_main_option("script_location", ALEMBIC_CONFIG) - with notify_api.app_context(): - upgrade(config, 'head') + with notify_api.app_context(): + upgrade(config, 'head') - def teardown(): - db.session.remove() - db.engine.execute("drop sequence services_id_seq cascade") - db.drop_all() - db.engine.execute("drop table alembic_version") - db.get_engine(notify_api).dispose() + def teardown(): + db.session.remove() + db.engine.execute("drop sequence services_id_seq cascade") + db.drop_all() + db.engine.execute("drop table alembic_version") + db.get_engine(notify_api).dispose() - request.addfinalizer(teardown) + request.addfinalizer(teardown) + except: + import traceback + traceback.print_exc() return db