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 @@ 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: 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):