From f8a4171569636bf2a778989bf2a742fb333932af Mon Sep 17 00:00:00 2001 From: Oleg Yurchik Date: Wed, 20 Mar 2024 15:57:03 +0300 Subject: [PATCH 1/2] Speed up get positions --- sapphire/database/migrations/versions/891640b6839e_init.py | 4 ++++ sapphire/database/models/projects.py | 2 ++ 2 files changed, 6 insertions(+) diff --git a/sapphire/database/migrations/versions/891640b6839e_init.py b/sapphire/database/migrations/versions/891640b6839e_init.py index f9b82dbe..f481f43a 100644 --- a/sapphire/database/migrations/versions/891640b6839e_init.py +++ b/sapphire/database/migrations/versions/891640b6839e_init.py @@ -138,6 +138,8 @@ def upgrade() -> None: postgresql_using="hash") op.create_index("positions__specialization_id_idx", "positions", ["specialization_id"], unique=False, postgresql_using="hash") + op.create_index("positions__created_at_idx", "positions", ["created_at"], + unique=False, postgresql_using="btree") op.create_table("profiles", sa.Column("user_id", sa.Uuid(), nullable=False), sa.Column("about", sa.Text(), nullable=True), @@ -161,6 +163,8 @@ def upgrade() -> None: ) op.create_index("projects_history__project_id_idx", "projects_history", ["project_id"], unique=False, postgresql_using="hash") + op.create_index("projects_history__created_at_idx", "projects_history", ["created_at"], + unique=False, postgresql_using="btree") op.create_table("reviews", sa.Column("id", sa.Uuid(), nullable=False), sa.Column("project_id", sa.Uuid(), nullable=False), diff --git a/sapphire/database/models/projects.py b/sapphire/database/models/projects.py index b25e550b..b9dc2e43 100644 --- a/sapphire/database/models/projects.py +++ b/sapphire/database/models/projects.py @@ -85,6 +85,7 @@ class ProjectHistory(Base): __table_args__ = ( Index("projects_history__project_id_idx", "project_id", postgresql_using="hash"), + Index("projects_history__created_at_idx", "created_at", postgresql_using="btree"), ) @@ -119,6 +120,7 @@ class Position(Base): Index("positions__project_id_idx", "project_id", postgresql_using="hash"), Index("positions__specialization_id_idx", "specialization_id", postgresql_using="hash"), + Index("positions__created_at_idx", "created_at", postgresql_using="btree"), ) From 01f161f8f8343630337772ff607f17ee26639ef1 Mon Sep 17 00:00:00 2001 From: Oleg Yurchik Date: Wed, 20 Mar 2024 20:54:23 +0300 Subject: [PATCH 2/2] Fix migration rollback: deleting enums in postgresql --- sapphire/database/migrations/versions/891640b6839e_init.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/sapphire/database/migrations/versions/891640b6839e_init.py b/sapphire/database/migrations/versions/891640b6839e_init.py index f481f43a..7f2d9f3d 100644 --- a/sapphire/database/migrations/versions/891640b6839e_init.py +++ b/sapphire/database/migrations/versions/891640b6839e_init.py @@ -222,6 +222,8 @@ def upgrade() -> None: def downgrade() -> None: # ### commands auto generated by Alembic - please adjust! ### + bind = op.get_bind() + op.drop_index("positions_skills__skill_id_idx", table_name="positions_skills", postgresql_using="hash") op.drop_index("positions_skills__position_id_idx", table_name="positions_skills", @@ -233,6 +235,8 @@ def downgrade() -> None: op.drop_index("participants__position_id_idx", table_name="participants", postgresql_using="hash") op.drop_table("participants") + if bind.engine.name == "postgresql": + op.execute("DROP TYPE participantstatusenum") op.drop_index("reviews__to_user_id_idx", table_name="reviews", postgresql_using="hash") op.drop_index("reviews__project_id_idx", table_name="reviews", postgresql_using="hash") op.drop_index("reviews__from_user_id_idx", table_name="reviews", postgresql_using="hash") @@ -240,6 +244,8 @@ def downgrade() -> None: op.drop_index("projects_history__project_id_idx", table_name="projects_history", postgresql_using="hash") op.drop_table("projects_history") + if bind.engine.name == "postgresql": + op.execute("DROP TYPE projectstatusenum") op.drop_table("profiles") op.drop_index("positions__specialization_id_idx", table_name="positions", postgresql_using="hash")