diff --git a/app/dao/provider_details_dao.py b/app/dao/provider_details_dao.py index 275d74e5a..257858eee 100644 --- a/app/dao/provider_details_dao.py +++ b/app/dao/provider_details_dao.py @@ -1,6 +1,6 @@ from sqlalchemy import asc from app.dao.dao_utils import transactional -from app.models import ProviderDetails +from app.models import ProviderDetails, ProviderDetailsHistory from app import db @@ -20,4 +20,7 @@ def get_provider_details_by_notification_type(notification_type): @transactional def dao_update_provider_details(provider_details): + provider_details.version += 1 + history = ProviderDetailsHistory.from_original(provider_details) db.session.add(provider_details) + db.session.add(history) diff --git a/app/models.py b/app/models.py index a13eecc8b..35fac79f1 100644 --- a/app/models.py +++ b/app/models.py @@ -367,17 +367,19 @@ class ProviderDetails(db.Model): priority = db.Column(db.Integer, nullable=False) notification_type = db.Column(notification_types, nullable=False) active = db.Column(db.Boolean, default=False, nullable=False) + version = db.Column(db.Integer, default=1, nullable=False) class ProviderDetailsHistory(db.Model, HistoryModel): __tablename__ = 'provider_details_history' - id = db.Column(UUID(as_uuid=True), primary_key=True) + id = db.Column(UUID(as_uuid=True), primary_key=True, nullable=False) display_name = db.Column(db.String, nullable=False) identifier = db.Column(db.String, nullable=False) priority = db.Column(db.Integer, nullable=False) notification_type = db.Column(notification_types, nullable=False) active = db.Column(db.Boolean, nullable=False) + version = db.Column(db.Integer, primary_key=True, nullable=False) JOB_STATUS_PENDING = 'pending' diff --git a/migrations/versions/0062_provider_details_history.py b/migrations/versions/0062_provider_details_history.py index c4898970c..6ffc92927 100644 --- a/migrations/versions/0062_provider_details_history.py +++ b/migrations/versions/0062_provider_details_history.py @@ -16,9 +16,13 @@ from sqlalchemy.dialects import postgresql def upgrade(): op.get_bind() - op.execute('UPDATE provider_details SET active = false WHERE active is null') - op.alter_column('provider_details', 'active', existing_type=sa.BOOLEAN(), nullable=False) + op.execute('UPDATE provider_details SET active = false WHERE active is null') + op.alter_column('provider_details', 'active', nullable=False) + + op.add_column('provider_details', sa.Column('version', sa.Integer())) + op.execute('UPDATE provider_details SET version = 1') + op.alter_column('provider_details', 'version', nullable=False) op.create_table('provider_details_history', sa.Column('id', postgresql.UUID(as_uuid=True), nullable=False), @@ -27,12 +31,18 @@ def upgrade(): sa.Column('priority', sa.Integer(), nullable=False), sa.Column('notification_type', postgresql.ENUM('email', 'sms', 'letter', name='notification_type', create_type=False), nullable=False), sa.Column('active', sa.Boolean(), nullable=False), + sa.Column('version', sa.Integer(), nullable=False), sa.PrimaryKeyConstraint('id') ) + op.execute( + 'INSERT INTO provider_details_history' + + ' (id, display_name, identifier, priority, notification_type, active, version)' + + ' SELECT id, display_name, identifier, priority, notification_type, active, version FROM provider_details' + ) def downgrade(): op.drop_table('provider_details_history') - op.alter_column('provider_details_history', 'active', existing_type=sa.BOOLEAN(), nullable=True) op.alter_column('provider_details', 'active', existing_type=sa.BOOLEAN(), nullable=True) + op.drop_column('provider_details', 'version')