Skip to content

Commit bd6a9e4

Browse files
committed
chore(infrastructure): don't index nulls
1 parent 6a76fa2 commit bd6a9e4

File tree

3 files changed

+113
-4
lines changed

3 files changed

+113
-4
lines changed
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
"""
2+
don't index `null`s.
3+
4+
Revision ID: 7ce97d6efd2c
5+
Revises: ba0f7132baef
6+
Create Date: 2025-09-19 11:58:53.188572
7+
8+
"""
9+
10+
from collections.abc import Sequence
11+
12+
from alembic import op
13+
14+
15+
revision: str = "7ce97d6efd2c"
16+
down_revision: str | None = "ba0f7132baef"
17+
branch_labels: str | Sequence[str] | None = None
18+
depends_on: str | Sequence[str] | None = None
19+
20+
21+
def upgrade() -> None:
22+
op.drop_index(op.f("ix_users_selected_emoji_id"), table_name="users")
23+
op.create_index(
24+
op.f("ix_users_selected_emoji_id"),
25+
"users",
26+
["selected_emoji_id"],
27+
unique=False,
28+
postgresql_where="(selected_emoji_id IS NOT NULL)",
29+
)
30+
31+
op.drop_index(op.f("ix_users_current_game_id"), table_name="users")
32+
op.create_index(
33+
op.f("ix_users_current_game_id"),
34+
"users",
35+
["current_game_id"],
36+
unique=False,
37+
postgresql_where="(current_game_id IS NOT NULL)",
38+
)
39+
40+
op.drop_index(op.f("ix_cells_user_filler_id"), table_name="cells")
41+
op.create_index(
42+
op.f("ix_cells_user_filler_id"),
43+
"cells",
44+
["user_filler_id"],
45+
unique=False,
46+
postgresql_where="(user_filler_id IS NOT NULL)",
47+
)
48+
49+
op.drop_index(op.f("ix_cells_ai_filler_id"), table_name="cells")
50+
op.create_index(
51+
op.f("ix_cells_ai_filler_id"),
52+
"cells",
53+
["ai_filler_id"],
54+
unique=False,
55+
postgresql_where="(ai_filler_id IS NOT NULL)",
56+
)
57+
58+
59+
def downgrade() -> None:
60+
op.drop_index(op.f("ix_users_selected_emoji_id"), table_name="users")
61+
op.create_index(
62+
op.f("ix_users_selected_emoji_id"),
63+
"users",
64+
["selected_emoji_id"],
65+
unique=False,
66+
)
67+
68+
op.drop_index(op.f("ix_users_current_game_id"), table_name="users")
69+
op.create_index(
70+
op.f("ix_users_current_game_id"),
71+
"users",
72+
["current_game_id"],
73+
unique=False,
74+
)
75+
76+
op.drop_index(op.f("ix_cells_user_filler_id"), table_name="cells")
77+
op.create_index(
78+
op.f("ix_cells_user_filler_id"),
79+
"cells",
80+
["user_filler_id"],
81+
unique=False,
82+
)
83+
84+
op.drop_index(op.f("ix_cells_ai_filler_id"), table_name="cells")
85+
op.create_index(
86+
op.f("ix_cells_ai_filler_id"),
87+
"cells",
88+
["ai_filler_id"],
89+
unique=False,
90+
)

src/ttt/infrastructure/sqlalchemy/tables/game.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,11 +86,22 @@ class TableCell(Base[Cell]):
8686
user_filler_id: Mapped[int | None] = mapped_column(
8787
BigInteger(),
8888
ForeignKey("users.id", deferrable=True, initially="DEFERRED"),
89-
index=True,
9089
)
9190
ai_filler_id: Mapped[UUID | None] = mapped_column(
9291
ForeignKey("ais.id", deferrable=True, initially="DEFERRED"),
93-
index=True,
92+
)
93+
94+
__table_args__ = (
95+
Index(
96+
"ix_cells_user_filler_id",
97+
user_filler_id,
98+
postgresql_where=(user_filler_id.is_not(None)),
99+
),
100+
Index(
101+
"ix_cells_ai_filler_id",
102+
ai_filler_id,
103+
postgresql_where=(ai_filler_id.is_not(None)),
104+
),
94105
)
95106

96107
def __entity__(self) -> Cell:

src/ttt/infrastructure/sqlalchemy/tables/user.py

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ class TableUser(Base[User]):
7979
account_stars: Mapped[int] = mapped_column(server_default="0")
8080
selected_emoji_id: Mapped[UUID | None] = mapped_column(
8181
ForeignKey("user_emojis.id", deferrable=True, initially="DEFERRED"),
82-
index=True,
8382
)
8483
rating: Mapped[float]
8584
current_game_id: Mapped[UUID | None] = mapped_column(
8685
ForeignKey("games.id", deferrable=True, initially="DEFERRED"),
87-
index=True,
8886
)
8987
admin_right: Mapped[TableAdminRight | None] = mapped_column(admin_right)
9088
admin_right_via_other_admin_admin_id: Mapped[int | None] = mapped_column(
@@ -101,6 +99,16 @@ class TableUser(Base[User]):
10199
)
102100

103101
__table_args__ = (
102+
Index(
103+
"ix_users_selected_emoji_id",
104+
selected_emoji_id,
105+
postgresql_where=(selected_emoji_id.is_not(None)),
106+
),
107+
Index(
108+
"ix_users_current_game_id",
109+
current_game_id,
110+
postgresql_where=(current_game_id.is_not(None)),
111+
),
104112
Index(
105113
"ix_users_admin_right_via_other_admin_admin_id",
106114
admin_right_via_other_admin_admin_id,

0 commit comments

Comments
 (0)