Skip to content

Commit

Permalink
Merge pull request #144 from python-discord/jb3/deleted-category-attr…
Browse files Browse the repository at this point in the history
…ibute

Category deletion tracking
  • Loading branch information
jb3 authored Apr 8, 2024
2 parents 13c6a31 + 48bcde8 commit 9d30baa
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
"""
Add deleted column to Category model.
Revision ID: 01b101590e74
Revises: c09a64cac3cb
Create Date: 2024-04-08 04:39:00.198882
"""
import sqlalchemy as sa

from alembic import op

# revision identifiers, used by Alembic.
revision = "01b101590e74"
down_revision = "c09a64cac3cb"
branch_labels = None
depends_on = None


def upgrade() -> None:
"""Apply the current migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.add_column("categories", sa.Column("deleted", sa.Boolean(), nullable=False))
# ### end Alembic commands ###


def downgrade() -> None:
"""Revert the current migration."""
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("categories", "deleted")
# ### end Alembic commands ###
16 changes: 14 additions & 2 deletions metricity/exts/event_listeners/guild_listeners.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,23 @@ async def sync_channels(self, guild: discord.Guild) -> None:
if existing_cat := await sess.get(models.Category, str(channel.id)):
existing_cat.name = channel.name
else:
sess.add(models.Category(id=str(channel.id), name=channel.name))
sess.add(models.Category(id=str(channel.id), name=channel.name, deleted=False))

await sess.commit()

log.info("Category synchronisation process complete, synchronising channels")
log.info("Category synchronisation process complete, synchronising deleted categories")

async with async_session() as sess:
await sess.execute(
update(models.Category)
.where(~models.Category.id.in_(
[str(channel.id) for channel in guild.channels if isinstance(channel, discord.CategoryChannel)],
))
.values(deleted=True),
)
await sess.commit()

log.info("Deleted category synchronisation process complete, synchronising channels")

async with async_session() as sess:
for channel in guild.channels:
Expand Down
1 change: 1 addition & 0 deletions metricity/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Category(Base):

id: Mapped[str] = mapped_column(primary_key=True)
name: Mapped[str]
deleted: Mapped[bool] = mapped_column(default=False)


class Channel(Base):
Expand Down

0 comments on commit 9d30baa

Please sign in to comment.