From 52e0367a13afd2e3e84b2aebdc88e864123c29fa Mon Sep 17 00:00:00 2001 From: Alexey Bezhan Date: Wed, 8 Nov 2017 10:32:45 +0000 Subject: [PATCH] Replace Notifications.template_history with Notifications.template Each notification instance is connected to a specific version of the template that was used to generate notification content. Template versions are stored in TemplateHistory, so that's the table we need to use for Notifications.template relationship. Currently, using Notifications.template returns the latest template version, which leads to incorrect content being returned by the API if the template has been updated. This change removes Notifications.template_history attribute and replaces template relationship to refer to TemplateHistory instance based on the notification's template ID and version. --- app/models.py | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/app/models.py b/app/models.py index 409fe3018..9d16c4968 100644 --- a/app/models.py +++ b/app/models.py @@ -909,9 +909,9 @@ class Notification(db.Model): 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_id = db.Column(UUID(as_uuid=True), index=True, unique=False) template_version = db.Column(db.Integer, nullable=False) + template = db.relationship('TemplateHistory') 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) @@ -947,11 +947,6 @@ class Notification(db.Model): client_reference = db.Column(db.String, index=True, nullable=True) _personalisation = db.Column(db.String, nullable=True) - template_history = db.relationship('TemplateHistory', primaryjoin=and_( - foreign(template_id) == remote(TemplateHistory.id), - foreign(template_version) == remote(TemplateHistory.version) - )) - scheduled_notification = db.relationship('ScheduledNotification', uselist=False) client_reference = db.Column(db.String, index=True, nullable=True) @@ -963,6 +958,14 @@ class Notification(db.Model): created_by = db.relationship('User') created_by_id = db.Column(UUID(as_uuid=True), db.ForeignKey('users.id'), nullable=True) + __table_args__ = ( + db.ForeignKeyConstraint( + ['template_id', 'template_version'], + ['templates_history.id', 'templates_history.version'], + ), + {} + ) + @property def personalisation(self): if self._personalisation: