diff options
| author | 2024-04-08 04:39:22 +0100 | |
|---|---|---|
| committer | 2024-04-08 04:39:22 +0100 | |
| commit | 48bcde810febe8dc9f74eb8569e8eb637f9d6c12 (patch) | |
| tree | 1aca285c0b67c20e5d4cada9110782647a71411b | |
| parent | Version 2.5.0 (diff) | |
Track whether a category has been deleted
| -rw-r--r-- | alembic/versions/01b101590e74_add_deleted_column_to_category_model.py | 31 | ||||
| -rw-r--r-- | metricity/exts/event_listeners/guild_listeners.py | 16 | ||||
| -rw-r--r-- | metricity/models.py | 1 | 
3 files changed, 46 insertions, 2 deletions
| diff --git a/alembic/versions/01b101590e74_add_deleted_column_to_category_model.py b/alembic/versions/01b101590e74_add_deleted_column_to_category_model.py new file mode 100644 index 0000000..1325bd2 --- /dev/null +++ b/alembic/versions/01b101590e74_add_deleted_column_to_category_model.py @@ -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 ### diff --git a/metricity/exts/event_listeners/guild_listeners.py b/metricity/exts/event_listeners/guild_listeners.py index 0d25cb2..79cd8f4 100644 --- a/metricity/exts/event_listeners/guild_listeners.py +++ b/metricity/exts/event_listeners/guild_listeners.py @@ -128,11 +128,23 @@ class GuildListeners(commands.Cog):                      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: diff --git a/metricity/models.py b/metricity/models.py index dad6e84..86bb723 100644 --- a/metricity/models.py +++ b/metricity/models.py @@ -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): | 
