WIP checkpoint: Friends/follow/block models + Alembic migration

This commit is contained in:
2026-08-18 15:51:37 +00:00
parent ca06b459ec
commit 10dcd6eff2
4 changed files with 23 additions and 13 deletions
+16 -6
View File
@@ -23,7 +23,7 @@ def upgrade() -> None:
sa.Column("id", sa.Integer(), nullable=False), sa.Column("id", sa.Integer(), nullable=False),
sa.Column("requester_id", sa.Integer(), nullable=False), sa.Column("requester_id", sa.Integer(), nullable=False),
sa.Column("addressee_id", sa.Integer(), nullable=False), sa.Column("addressee_id", sa.Integer(), nullable=False),
sa.Column("status", sa.String(length=20), nullable=False), sa.Column("status", sa.String(length=20), server_default="pending", nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True), sa.Column("updated_at", sa.DateTime(timezone=True), nullable=True),
sa.CheckConstraint( sa.CheckConstraint(
@@ -48,7 +48,7 @@ def upgrade() -> None:
sa.UniqueConstraint( sa.UniqueConstraint(
"requester_id", "requester_id",
"addressee_id", "addressee_id",
name="uq_friendships_pair", name="uq_friendships_requester_addressee",
), ),
) )
op.create_index(op.f("ix_friendships_id"), "friendships", ["id"], unique=False) op.create_index(op.f("ix_friendships_id"), "friendships", ["id"], unique=False)
@@ -57,6 +57,7 @@ def upgrade() -> None:
op.create_table( op.create_table(
"follows", "follows",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("follower_id", sa.Integer(), nullable=False), sa.Column("follower_id", sa.Integer(), nullable=False),
sa.Column("followed_id", sa.Integer(), nullable=False), sa.Column("followed_id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
@@ -74,17 +75,20 @@ def upgrade() -> None:
["users.id"], ["users.id"],
ondelete="CASCADE", ondelete="CASCADE",
), ),
sa.PrimaryKeyConstraint("follower_id", "followed_id"), sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint( sa.UniqueConstraint(
"follower_id", "follower_id",
"followed_id", "followed_id",
name="uq_follows_pair", name="uq_follows_follower_followed",
), ),
) )
op.create_index(op.f("ix_follows_id"), "follows", ["id"], unique=False)
op.create_index(op.f("ix_follows_followed_id"), "follows", ["followed_id"], unique=False) op.create_index(op.f("ix_follows_followed_id"), "follows", ["followed_id"], unique=False)
op.create_index(op.f("ix_follows_follower_id"), "follows", ["follower_id"], unique=False)
op.create_table( op.create_table(
"blocks", "blocks",
sa.Column("id", sa.Integer(), nullable=False),
sa.Column("blocker_id", sa.Integer(), nullable=False), sa.Column("blocker_id", sa.Integer(), nullable=False),
sa.Column("blocked_id", sa.Integer(), nullable=False), sa.Column("blocked_id", sa.Integer(), nullable=False),
sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False), sa.Column("created_at", sa.DateTime(timezone=True), server_default=sa.func.now(), nullable=False),
@@ -102,22 +106,28 @@ def upgrade() -> None:
["users.id"], ["users.id"],
ondelete="CASCADE", ondelete="CASCADE",
), ),
sa.PrimaryKeyConstraint("blocker_id", "blocked_id"), sa.PrimaryKeyConstraint("id"),
sa.UniqueConstraint( sa.UniqueConstraint(
"blocker_id", "blocker_id",
"blocked_id", "blocked_id",
name="uq_blocks_pair", name="uq_blocks_blocker_blocked",
), ),
) )
op.create_index(op.f("ix_blocks_id"), "blocks", ["id"], unique=False)
op.create_index(op.f("ix_blocks_blocked_id"), "blocks", ["blocked_id"], unique=False) op.create_index(op.f("ix_blocks_blocked_id"), "blocks", ["blocked_id"], unique=False)
op.create_index(op.f("ix_blocks_blocker_id"), "blocks", ["blocker_id"], unique=False)
def downgrade() -> None: def downgrade() -> None:
"""Drop friendships, follows, and blocks tables.""" """Drop friendships, follows, and blocks tables."""
op.drop_index(op.f("ix_blocks_blocker_id"), table_name="blocks")
op.drop_index(op.f("ix_blocks_blocked_id"), table_name="blocks") op.drop_index(op.f("ix_blocks_blocked_id"), table_name="blocks")
op.drop_index(op.f("ix_blocks_id"), table_name="blocks")
op.drop_table("blocks") op.drop_table("blocks")
op.drop_index(op.f("ix_follows_follower_id"), table_name="follows")
op.drop_index(op.f("ix_follows_followed_id"), table_name="follows") op.drop_index(op.f("ix_follows_followed_id"), table_name="follows")
op.drop_index(op.f("ix_follows_id"), table_name="follows")
op.drop_table("follows") op.drop_table("follows")
op.drop_index(op.f("ix_friendships_requester_id"), table_name="friendships") op.drop_index(op.f("ix_friendships_requester_id"), table_name="friendships")
+2 -2
View File
@@ -13,7 +13,7 @@ class Block(Base):
UniqueConstraint( UniqueConstraint(
"blocker_id", "blocker_id",
"blocked_id", "blocked_id",
name="uq_blocks_blocker_blocked", name="uq_blocks_pair",
), ),
CheckConstraint( CheckConstraint(
"blocker_id <> blocked_id", "blocker_id <> blocked_id",
@@ -39,7 +39,7 @@ class Block(Base):
blocker = relationship( blocker = relationship(
"User", "User",
foreign_keys=[blocker_id], foreign_keys=[blocker_id],
back_populates="blocks_given", back_populates="blocks_issued",
) )
blocked = relationship( blocked = relationship(
"User", "User",
+2 -2
View File
@@ -13,7 +13,7 @@ class Follow(Base):
UniqueConstraint( UniqueConstraint(
"follower_id", "follower_id",
"followed_id", "followed_id",
name="uq_follows_follower_followed", name="uq_follows_pair",
), ),
CheckConstraint( CheckConstraint(
"follower_id <> followed_id", "follower_id <> followed_id",
@@ -39,7 +39,7 @@ class Follow(Base):
follower = relationship( follower = relationship(
"User", "User",
foreign_keys=[follower_id], foreign_keys=[follower_id],
back_populates="follows_given", back_populates="following",
) )
followed = relationship( followed = relationship(
"User", "User",
+3 -3
View File
@@ -28,7 +28,7 @@ class Friendship(Base):
UniqueConstraint( UniqueConstraint(
"requester_id", "requester_id",
"addressee_id", "addressee_id",
name="uq_friendships_requester_addressee", name="uq_friendships_pair",
), ),
CheckConstraint( CheckConstraint(
"requester_id <> addressee_id", "requester_id <> addressee_id",
@@ -65,10 +65,10 @@ class Friendship(Base):
requester = relationship( requester = relationship(
"User", "User",
foreign_keys=[requester_id], foreign_keys=[requester_id],
back_populates="friendships_sent", back_populates="sent_friend_requests",
) )
addressee = relationship( addressee = relationship(
"User", "User",
foreign_keys=[addressee_id], foreign_keys=[addressee_id],
back_populates="friendships_received", back_populates="received_friend_requests",
) )