Skip to content

Commit 64fe6c6

Browse files
James Youngbloodmarkharding
authored andcommitted
Add indices for event keys in feed queries
Changelog: enchancement
1 parent 054811c commit 64fe6c6

2 files changed

Lines changed: 102 additions & 0 deletions

File tree

services/server/migration/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ mod m20260604_000001_add_content_label_table;
1414
mod m20260617_000001_add_notification_table;
1515
mod m20260617_000002_drop_push_token_table;
1616
mod m20260618_000001_timestamps_to_timestamptz;
17+
mod m20260625_000001_add_event_key_indices;
1718

1819
pub struct Migrator;
1920

@@ -35,6 +36,7 @@ impl MigratorTrait for Migrator {
3536
Box::new(m20260617_000001_add_notification_table::Migration),
3637
Box::new(m20260617_000002_drop_push_token_table::Migration),
3738
Box::new(m20260618_000001_timestamps_to_timestamptz::Migration),
39+
Box::new(m20260625_000001_add_event_key_indices::Migration),
3840
]
3941
}
4042
}
Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
use ::entity::{
2+
content_delete_model, content_label_model, content_reaction_model,
3+
};
4+
use sea_orm_migration::prelude::*;
5+
6+
/// `content_delete`, `content_reaction`, and `content_label` are all
7+
/// queried to match events, but have no index over the `event_key_*`
8+
/// columns (see `tombstone::list_tombstones_for_event_keys`). We add
9+
/// an index so that matching events does not require a sequential scan.
10+
///
11+
/// Each index mirrors the column order of the existing
12+
/// `idx-events-event_key` unique index on the `events` table
13+
/// (collection, identity, public_key_type, public_key, sequence) but is
14+
/// **non-unique** because a single target event can have multiple
15+
/// delete / reaction / label rows.
16+
#[derive(DeriveMigrationName)]
17+
pub struct Migration;
18+
19+
#[async_trait::async_trait]
20+
impl MigrationTrait for Migration {
21+
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
22+
manager
23+
.create_index(
24+
Index::create()
25+
.if_not_exists()
26+
.name("content_delete_event_key_idx")
27+
.table(content_delete_model::Entity)
28+
.col(content_delete_model::Column::EventKeyCollection)
29+
.col(content_delete_model::Column::EventKeyIdentity)
30+
.col(content_delete_model::Column::EventKeyPublicKeyType)
31+
.col(content_delete_model::Column::EventKeyPublicKey)
32+
.col(content_delete_model::Column::EventKeySequence)
33+
.to_owned(),
34+
)
35+
.await?;
36+
37+
manager
38+
.create_index(
39+
Index::create()
40+
.if_not_exists()
41+
.name("content_reaction_event_key_idx")
42+
.table(content_reaction_model::Entity)
43+
.col(content_reaction_model::Column::EventKeyCollection)
44+
.col(content_reaction_model::Column::EventKeyIdentity)
45+
.col(content_reaction_model::Column::EventKeyPublicKeyType)
46+
.col(content_reaction_model::Column::EventKeyPublicKey)
47+
.col(content_reaction_model::Column::EventKeySequence)
48+
.to_owned(),
49+
)
50+
.await?;
51+
52+
manager
53+
.create_index(
54+
Index::create()
55+
.if_not_exists()
56+
.name("content_label_event_key_idx")
57+
.table(content_label_model::Entity)
58+
.col(content_label_model::Column::EventKeyCollection)
59+
.col(content_label_model::Column::EventKeyIdentity)
60+
.col(content_label_model::Column::EventKeyPublicKeyType)
61+
.col(content_label_model::Column::EventKeyPublicKey)
62+
.col(content_label_model::Column::EventKeySequence)
63+
.to_owned(),
64+
)
65+
.await?;
66+
67+
Ok(())
68+
}
69+
70+
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
71+
manager
72+
.drop_index(
73+
Index::drop()
74+
.name("content_label_event_key_idx")
75+
.table(content_label_model::Entity)
76+
.to_owned(),
77+
)
78+
.await?;
79+
80+
manager
81+
.drop_index(
82+
Index::drop()
83+
.name("content_reaction_event_key_idx")
84+
.table(content_reaction_model::Entity)
85+
.to_owned(),
86+
)
87+
.await?;
88+
89+
manager
90+
.drop_index(
91+
Index::drop()
92+
.name("content_delete_event_key_idx")
93+
.table(content_delete_model::Entity)
94+
.to_owned(),
95+
)
96+
.await?;
97+
98+
Ok(())
99+
}
100+
}

0 commit comments

Comments
 (0)