Browse Source

Drop removed_time column on comments

The log_comments table can take over tracking this.
merge-requests/34/head
Chad Birch 6 years ago
parent
commit
7500e564c7
  1. 57
      tildes/alembic/versions/b424479202f9_drop_removed_time_column_from_comments.py
  2. 20
      tildes/sql/init/triggers/comments/comments.sql
  3. 18
      tildes/tests/test_triggers_comments.py
  4. 2
      tildes/tildes/models/comment/comment.py

57
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();
"""
)

20
tildes/sql/init/triggers/comments/comments.sql

@ -16,23 +16,3 @@ CREATE TRIGGER delete_comment_set_deleted_time_update
FOR EACH ROW FOR EACH ROW
WHEN (OLD.is_deleted IS DISTINCT FROM NEW.is_deleted) WHEN (OLD.is_deleted IS DISTINCT FROM NEW.is_deleted)
EXECUTE PROCEDURE set_comment_deleted_time(); 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();

18
tildes/tests/test_triggers_comments.py

@ -41,24 +41,6 @@ def test_delete_sets_deleted_time(db, comment):
assert not comment.deleted_time 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): def test_remove_delete_single_decrement(db, topic, session_user):
"""Ensure that remove+delete doesn't double-decrement num_comments.""" """Ensure that remove+delete doesn't double-decrement num_comments."""
# add 2 comments # add 2 comments

2
tildes/tildes/models/comment/comment.py

@ -48,7 +48,6 @@ class Comment(DatabaseModel):
"comment.created" or "comment.edited" respectively. "comment.created" or "comment.edited" respectively.
Internal: Internal:
- deleted_time will be set or unset when is_deleted is changed - 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 schema_class = CommentSchema
@ -76,7 +75,6 @@ class Comment(DatabaseModel):
) )
deleted_time: Optional[datetime] = Column(TIMESTAMP(timezone=True)) deleted_time: Optional[datetime] = Column(TIMESTAMP(timezone=True))
is_removed: bool = Column(Boolean, nullable=False, server_default="false") 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)) last_edited_time: Optional[datetime] = Column(TIMESTAMP(timezone=True))
_markdown: str = deferred(Column("markdown", Text, nullable=False)) _markdown: str = deferred(Column("markdown", Text, nullable=False))
rendered_html: str = Column(Text, nullable=False) rendered_html: str = Column(Text, nullable=False)

Loading…
Cancel
Save