From 47ef63adbefc349db58211a1a901819299b2163b Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Thu, 7 Jul 2016 16:30:22 +0100 Subject: [PATCH] add notification_history table table will be used for storing archival versions of notifications. It's an exact duplicate of notifications table, but with the following modifications: * removed _personalisation * removed to * removed content_char_count All foreign keys to other tables still exist. additionally, removed defaults (for id and created_at) since they'll be set when we create, and we should ensure that we don't forget about them when inserting/udpating this table. --- app/models.py | 29 ++++++++- .../versions/0041_notification_history.py | 60 +++++++++++++++++++ 2 files changed, 86 insertions(+), 3 deletions(-) create mode 100644 migrations/versions/0041_notification_history.py diff --git a/app/models.py b/app/models.py index 35b287a6a..ab1bfdd89 100644 --- a/app/models.py +++ b/app/models.py @@ -332,7 +332,7 @@ class VerifyCode(db.Model): NOTIFICATION_STATUS_TYPES = ['created', 'sending', 'delivered', 'pending', 'failed', 'technical-failure', 'temporary-failure', 'permanent-failure'] - +NOTIFICATION_STATUS_TYPES_ENUM = db.Enum(*NOTIFICATION_STATUS_TYPES, name='notify_status_type') class Notification(db.Model): @@ -370,8 +370,7 @@ class Notification(db.Model): unique=False, nullable=True, onupdate=datetime.datetime.utcnow) - status = db.Column( - db.Enum(*NOTIFICATION_STATUS_TYPES, name='notify_status_type'), nullable=False, default='created') + status = db.Column(NOTIFICATION_STATUS_TYPES_ENUM, nullable=False, default='created') reference = db.Column(db.String, nullable=True, index=True) _personalisation = db.Column(db.String, nullable=True) @@ -387,6 +386,30 @@ class Notification(db.Model): self._personalisation = encryption.encrypt(personalisation) +class NotificationHistory(db.Model): + __tablename__ = 'notification_history' + + id = db.Column(UUID(as_uuid=True), primary_key=True) + job_id = db.Column(UUID(as_uuid=True), db.ForeignKey('jobs.id'), index=True, unique=False) + job = db.relationship('Job') + job_row_number = db.Column(db.Integer, nullable=True) + service_id = db.Column(UUID(as_uuid=True), db.ForeignKey('services.id'), index=True, unique=False) + service = db.relationship('Service') + template_id = db.Column(UUID(as_uuid=True), db.ForeignKey('templates.id'), index=True, unique=False) + template = db.relationship('Template') + template_version = db.Column(db.Integer, nullable=False) + api_key_id = db.Column(UUID(as_uuid=True), db.ForeignKey('api_keys.id'), index=True, unique=False) + api_key = db.relationship('ApiKey') + key_type = db.Column(db.String, db.ForeignKey('key_types.name'), index=True, unique=False, nullable=False) + notification_type = db.Column(notification_types, nullable=False) + created_at = db.Column(db.DateTime, index=False, unique=False, nullable=False) + sent_at = db.Column(db.DateTime, index=False, unique=False, nullable=True) + sent_by = db.Column(db.String, nullable=True) + updated_at = db.Column(db.DateTime, index=False, unique=False, nullable=True) + status = db.Column(NOTIFICATION_STATUS_TYPES_ENUM, nullable=False, default='created') + reference = db.Column(db.String, nullable=True, index=True) + + INVITED_USER_STATUS_TYPES = ['pending', 'accepted', 'cancelled'] diff --git a/migrations/versions/0041_notification_history.py b/migrations/versions/0041_notification_history.py new file mode 100644 index 000000000..f44c55fe8 --- /dev/null +++ b/migrations/versions/0041_notification_history.py @@ -0,0 +1,60 @@ +"""empty message + +Revision ID: 807f0b497157 +Revises: 0039_fix_notifications +Create Date: 2016-07-07 13:15:35.503107 + +""" + +# revision identifiers, used by Alembic. +revision = '807f0b497157' +down_revision = '0039_fix_notifications' + +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('notification_history', + sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), + sa.Column('job_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('job_row_number', sa.Integer(), 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('template_version', sa.Integer(), nullable=False), + sa.Column('api_key_id', postgresql.UUID(as_uuid=True), nullable=True), + sa.Column('key_type', sa.String(), nullable=False), + sa.Column('notification_type', sa.Enum('email', 'sms', 'letter', name='notification_type'), nullable=False), + 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('created', 'sending', 'delivered', 'pending', 'failed', 'technical-failure', 'temporary-failure', 'permanent-failure', name='notify_status_type'), nullable=False), + sa.Column('reference', sa.String(), nullable=True), + sa.ForeignKeyConstraint(['api_key_id'], ['api_keys.id'], ), + sa.ForeignKeyConstraint(['job_id'], ['jobs.id'], ), + sa.ForeignKeyConstraint(['key_type'], ['key_types.name'], ), + sa.ForeignKeyConstraint(['service_id'], ['services.id'], ), + sa.ForeignKeyConstraint(['template_id'], ['templates.id'], ), + sa.PrimaryKeyConstraint('id') + ) + op.create_index(op.f('ix_notification_history_api_key_id'), 'notification_history', ['api_key_id'], unique=False) + op.create_index(op.f('ix_notification_history_job_id'), 'notification_history', ['job_id'], unique=False) + op.create_index(op.f('ix_notification_history_key_type'), 'notification_history', ['key_type'], unique=False) + op.create_index(op.f('ix_notification_history_reference'), 'notification_history', ['reference'], unique=False) + op.create_index(op.f('ix_notification_history_service_id'), 'notification_history', ['service_id'], unique=False) + op.create_index(op.f('ix_notification_history_template_id'), 'notification_history', ['template_id'], unique=False) + ### end Alembic commands ### + + +def downgrade(): + ### commands auto generated by Alembic - please adjust! ### + op.drop_index(op.f('ix_notification_history_template_id'), table_name='notification_history') + op.drop_index(op.f('ix_notification_history_service_id'), table_name='notification_history') + op.drop_index(op.f('ix_notification_history_reference'), table_name='notification_history') + op.drop_index(op.f('ix_notification_history_key_type'), table_name='notification_history') + op.drop_index(op.f('ix_notification_history_job_id'), table_name='notification_history') + op.drop_index(op.f('ix_notification_history_api_key_id'), table_name='notification_history') + op.drop_table('notification_history') + ### end Alembic commands ###