From 7500e564c7ab3ce9e57544930bd56462d26bee58 Mon Sep 17 00:00:00 2001 From: Chad Birch Date: Mon, 27 Aug 2018 15:21:54 -0600 Subject: [PATCH] Drop removed_time column on comments The log_comments table can take over tracking this. --- ..._drop_removed_time_column_from_comments.py | 57 +++++++++++++++++++ .../sql/init/triggers/comments/comments.sql | 20 ------- tildes/tests/test_triggers_comments.py | 18 ------ tildes/tildes/models/comment/comment.py | 2 - 4 files changed, 57 insertions(+), 40 deletions(-) create mode 100644 tildes/alembic/versions/b424479202f9_drop_removed_time_column_from_comments.py diff --git a/tildes/alembic/versions/b424479202f9_drop_removed_time_column_from_comments.py b/tildes/alembic/versions/b424479202f9_drop_removed_time_column_from_comments.py new file mode 100644 index 0000000..83a69e4 --- /dev/null +++ b/tildes/alembic/versions/b424479202f9_drop_removed_time_column_from_comments.py @@ -0,0 +1,57 @@ +"""Drop removed_time column from comments + +Revision ID: b424479202f9 +Revises: 347859b0355e +Create Date: 2018-08-27 21:08:06.656876 + +""" +from alembic import op +import sqlalchemy as sa +from sqlalchemy.dialects import postgresql + +# revision identifiers, used by Alembic. +revision = "b424479202f9" +down_revision = "347859b0355e" +branch_labels = None +depends_on = None + + +def upgrade(): + op.drop_column("comments", "removed_time") + + op.execute("DROP TRIGGER remove_comment_set_removed_time_update ON comments") + op.execute("DROP FUNCTION set_comment_removed_time") + + +def downgrade(): + op.add_column( + "comments", + sa.Column( + "removed_time", + postgresql.TIMESTAMP(timezone=True), + autoincrement=False, + nullable=True, + ), + ) + + op.execute( + """ + CREATE OR REPLACE FUNCTION set_comment_removed_time() RETURNS TRIGGER AS $$ + BEGIN + IF (NEW.is_removed = TRUE) THEN + NEW.removed_time := current_timestamp; + ELSE + NEW.removed_time := NULL; + END IF; + + RETURN NEW; + END; + $$ LANGUAGE plpgsql; + + CREATE TRIGGER remove_comment_set_removed_time_update + BEFORE UPDATE ON comments + FOR EACH ROW + WHEN (OLD.is_removed IS DISTINCT FROM NEW.is_removed) + EXECUTE PROCEDURE set_comment_removed_time(); + """ + ) diff --git a/tildes/sql/init/triggers/comments/comments.sql b/tildes/sql/init/triggers/comments/comments.sql index 0ff787e..46427ab 100644 --- a/tildes/sql/init/triggers/comments/comments.sql +++ b/tildes/sql/init/triggers/comments/comments.sql @@ -16,23 +16,3 @@ CREATE TRIGGER delete_comment_set_deleted_time_update FOR EACH ROW WHEN (OLD.is_deleted IS DISTINCT FROM NEW.is_deleted) EXECUTE PROCEDURE set_comment_deleted_time(); - - --- set comment.removed_time when is_removed changes -CREATE OR REPLACE FUNCTION set_comment_removed_time() RETURNS TRIGGER AS $$ -BEGIN - IF (NEW.is_removed = TRUE) THEN - NEW.removed_time := current_timestamp; - ELSE - NEW.removed_time := NULL; - END IF; - - RETURN NEW; -END; -$$ LANGUAGE plpgsql; - -CREATE TRIGGER remove_comment_set_removed_time_update - BEFORE UPDATE ON comments - FOR EACH ROW - WHEN (OLD.is_removed IS DISTINCT FROM NEW.is_removed) - EXECUTE PROCEDURE set_comment_removed_time(); diff --git a/tildes/tests/test_triggers_comments.py b/tildes/tests/test_triggers_comments.py index 56e7b81..7289f0b 100644 --- a/tildes/tests/test_triggers_comments.py +++ b/tildes/tests/test_triggers_comments.py @@ -41,24 +41,6 @@ def test_delete_sets_deleted_time(db, comment): assert not comment.deleted_time -def test_remove_sets_removed_time(db, comment): - """Ensure a removed comment gets its removed_time set and unset.""" - assert not comment.is_removed - assert not comment.removed_time - - comment.is_removed = True - db.commit() - db.refresh(comment) - - assert comment.removed_time - - comment.is_removed = False - db.commit() - db.refresh(comment) - - assert not comment.removed_time - - def test_remove_delete_single_decrement(db, topic, session_user): """Ensure that remove+delete doesn't double-decrement num_comments.""" # add 2 comments diff --git a/tildes/tildes/models/comment/comment.py b/tildes/tildes/models/comment/comment.py index 3860ba6..580049d 100644 --- a/tildes/tildes/models/comment/comment.py +++ b/tildes/tildes/models/comment/comment.py @@ -48,7 +48,6 @@ class Comment(DatabaseModel): "comment.created" or "comment.edited" respectively. Internal: - deleted_time will be set or unset when is_deleted is changed - - removed_time will be set or unset when is_removed is changed """ schema_class = CommentSchema @@ -76,7 +75,6 @@ class Comment(DatabaseModel): ) deleted_time: Optional[datetime] = Column(TIMESTAMP(timezone=True)) is_removed: bool = Column(Boolean, nullable=False, server_default="false") - removed_time: Optional[datetime] = Column(TIMESTAMP(timezone=True)) last_edited_time: Optional[datetime] = Column(TIMESTAMP(timezone=True)) _markdown: str = deferred(Column("markdown", Text, nullable=False)) rendered_html: str = Column(Text, nullable=False)