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.
This commit is contained in:
Alexey Bezhan
2017-11-08 10:32:45 +00:00
parent 1ace1b59a1
commit 52e0367a13

View File

@@ -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: