From 51e2a99f09554ca2f9bf7a4b6063e2eacd33dece Mon Sep 17 00:00:00 2001 From: Adam Coldrick Date: Tue, 21 Jun 2016 15:31:21 +0000 Subject: [PATCH] Store history of a comment when editing At the moment it is possible to edit comments via the API, but this causes the previous content of the comment to be permanently lost. This commit fixes this problem by keeping a record of the previous version of a comment each time it is edited. Change-Id: I0bcf4ba3555c8d96e91497742f424056e3123312 --- storyboard/db/api/comments.py | 6 +++ .../059_add_a_table_for_comment_history.py | 49 +++++++++++++++++++ storyboard/db/models.py | 9 ++++ 3 files changed, 64 insertions(+) create mode 100644 storyboard/db/migration/alembic_migrations/versions/059_add_a_table_for_comment_history.py diff --git a/storyboard/db/api/comments.py b/storyboard/db/api/comments.py index b0a070c8..4a76ea65 100644 --- a/storyboard/db/api/comments.py +++ b/storyboard/db/api/comments.py @@ -26,6 +26,12 @@ def comment_create(values): def comment_update(comment_id, values): + comment = api_base.entity_get(models.Comment, comment_id) + old_dict = { + 'comment_id': comment_id, + 'content': comment.content + } + api_base.entity_create(models.HistoricalComment, old_dict) return api_base.entity_update(models.Comment, comment_id, values) diff --git a/storyboard/db/migration/alembic_migrations/versions/059_add_a_table_for_comment_history.py b/storyboard/db/migration/alembic_migrations/versions/059_add_a_table_for_comment_history.py new file mode 100644 index 00000000..388c0393 --- /dev/null +++ b/storyboard/db/migration/alembic_migrations/versions/059_add_a_table_for_comment_history.py @@ -0,0 +1,49 @@ +# Licensed under the Apache License, Version 2.0 (the "License"); you may +# not use this file except in compliance with the License. You may obtain +# a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# + +"""Add a table for comment history + +Revision ID: 059 +Revises: 058 +Create Date: 2016-06-21 14:00:20.515139 + +""" + +# revision identifiers, used by Alembic. +revision = '059' +down_revision = '058' + + +from alembic import op +import sqlalchemy as sa + +from storyboard.db.decorators import UTCDateTime +from storyboard.db.models import MYSQL_MEDIUM_TEXT + + +def upgrade(active_plugins=None, options=None): + op.create_table( + 'comments_history', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('created_at', UTCDateTime(), nullable=True), + sa.Column('updated_at', UTCDateTime(), nullable=True), + sa.Column('content', MYSQL_MEDIUM_TEXT, nullable=True), + sa.Column('comment_id', sa.Integer(), nullable=False), + sa.ForeignKeyConstraint(['comment_id'], ['comments.id'], + name='fk_comment_id'), + sa.PrimaryKeyConstraint('id') + ) + + +def downgrade(active_plugins=None, options=None): + op.drop_table('comments_history') diff --git a/storyboard/db/models.py b/storyboard/db/models.py index 302b970c..018a0e0f 100644 --- a/storyboard/db/models.py +++ b/storyboard/db/models.py @@ -508,6 +508,15 @@ class Comment(FullText, ModelBuilder, Base): parent = relationship('Comment', remote_side=[id], backref='children') +class HistoricalComment(FullText, ModelBuilder, Base): + __tablename__ = 'comments_history' + __fulltext_columns__ = ['content'] + + content = Column(MYSQL_MEDIUM_TEXT) + comment_id = Column(Integer, ForeignKey('comments.id'), nullable=False) + current = relationship(Comment, backref='history') + + # Subscription and notifications class Subscription(ModelBuilder, Base):