History

implement recursive channel query

Mikayla Maki committed 3 years ago

Commit 09984401 parent 15631a65 files · +191 −138Browse files

Unified · Split

5 files
  1. crates/collab/migrations.sqlite/20221109000000_test_schema.sql +0 −3
  2. crates/collab/src/db.rs +125 −131
  3. crates/collab/src/db/channel.rs +0 −1
  4. crates/collab/src/db/channel_parent.rs +3 −0
  5. crates/collab/src/tests/channel_tests.rs +63 −3
crates/collab/migrations.sqlite/20221109000000_test_schema.sql+0−3
@@ -187,7 +187,6 @@ CREATE INDEX "index_followers_on_room_id" ON "followers" ("room_id"); · 186 unmodified lines
187 187
188CREATE TABLE "channels" (188CREATE TABLE "channels" (
189 "id" INTEGER PRIMARY KEY AUTOINCREMENT,189 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
190 -- "id_path" TEXT NOT NULL,
191 "name" VARCHAR NOT NULL,190 "name" VARCHAR NOT NULL,
192 "room_id" INTEGER REFERENCES rooms (id) ON DELETE SET NULL,191 "room_id" INTEGER REFERENCES rooms (id) ON DELETE SET NULL,
193 "created_at" TIMESTAMP NOT NULL DEFAULT now192 "created_at" TIMESTAMP NOT NULL DEFAULT now
@@ -199,8 +198,6 @@ CREATE TABLE "channel_parents" ( · 5 unmodified lines
199 PRIMARY KEY(child_id, parent_id)198 PRIMARY KEY(child_id, parent_id)
200);199);
201 200
202-- CREATE UNIQUE INDEX "index_channels_on_id_path" ON "channels" ("id_path");
203
204CREATE TABLE "channel_members" (201CREATE TABLE "channel_members" (
205 "id" INTEGER PRIMARY KEY AUTOINCREMENT,202 "id" INTEGER PRIMARY KEY AUTOINCREMENT,
206 "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,203 "channel_id" INTEGER NOT NULL REFERENCES channels (id) ON DELETE CASCADE,
crates/collab/src/db.rs+125−131
@@ -1,7 +1,7 @@
1mod access_token;1mod access_token;
2mod channel;2mod channel;
3mod channel_member;3mod channel_member;
4// mod channel_parent;4mod channel_parent;
5mod contact;5mod contact;
6mod follower;6mod follower;
7mod language_server;7mod language_server;
@@ -39,7 +39,10 @@ use sea_orm::{ · 31 unmodified lines
39 DbErr, FromQueryResult, IntoActiveModel, IsolationLevel, JoinType, QueryOrder, QuerySelect,39 DbErr, FromQueryResult, IntoActiveModel, IsolationLevel, JoinType, QueryOrder, QuerySelect,
40 Statement, TransactionTrait,40 Statement, TransactionTrait,
41};41};
42use sea_query::{Alias, Expr, OnConflict, Query, SelectStatement};42use sea_query::{
43 Alias, ColumnRef, CommonTableExpression, Expr, OnConflict, Order, Query, QueryStatementWriter,
44 SelectStatement, UnionType, WithClause,
45};
43use serde::{Deserialize, Serialize};46use serde::{Deserialize, Serialize};
44pub use signup::{Invite, NewSignup, WaitlistSummary};47pub use signup::{Invite, NewSignup, WaitlistSummary};
45use sqlx::migrate::{Migrate, Migration, MigrationSource};48use sqlx::migrate::{Migrate, Migration, MigrationSource};
@@ -3032,7 +3035,11 @@ impl Database { · 2986 unmodified lines
3032 3035
3033 // channels3036 // channels
3034 3037
3035 pub async fn create_channel(&self, name: &str) -> Result<ChannelId> {3038 pub async fn create_root_channel(&self, name: &str) -> Result<ChannelId> {
3039 self.create_channel(name, None).await
3040 }
3041
3042 pub async fn create_channel(&self, name: &str, parent: Option<ChannelId>) -> Result<ChannelId> {
3036 self.transaction(move |tx| async move {3043 self.transaction(move |tx| async move {
3037 let tx = tx;3044 let tx = tx;
3038 3045
@@ -3043,10 +3050,21 @@ impl Database { · 4 unmodified lines
3043 3050
3044 let channel = channel.insert(&*tx).await?;3051 let channel = channel.insert(&*tx).await?;
3045 3052
3053 if let Some(parent) = parent {
3054 channel_parent::ActiveModel {
3055 child_id: ActiveValue::Set(channel.id),
3056 parent_id: ActiveValue::Set(parent),
3057 }
3058 .insert(&*tx)
3059 .await?;
3060 }
3061
3046 Ok(channel.id)3062 Ok(channel.id)
3047 }).await3063 })
3064 .await
3048 }3065 }
3049 3066
3067 // Property: Members are only
3050 pub async fn add_channel_member(&self, channel_id: ChannelId, user_id: UserId) -> Result<()> {3068 pub async fn add_channel_member(&self, channel_id: ChannelId, user_id: UserId) -> Result<()> {
3051 self.transaction(move |tx| async move {3069 self.transaction(move |tx| async move {
3052 let tx = tx;3070 let tx = tx;
@@ -3060,139 +3078,108 @@ impl Database { · 7 unmodified lines
3060 channel_membership.insert(&*tx).await?;3078 channel_membership.insert(&*tx).await?;
3061 3079
3062 Ok(())3080 Ok(())
3063 }).await3081 })
3082 .await
3064 }3083 }
3065 3084
3066 pub async fn get_channels(&self, user_id: UserId) -> Vec<ChannelId> {3085 pub async fn get_channels(&self, user_id: UserId) -> Result<Vec<Channel>> {
3067 self.transaction(|tx| async move {3086 self.transaction(|tx| async move {
3068 let tx = tx;3087 let tx = tx;
3069 3088
3089 // This is the SQL statement we want to generate:
3090 let sql = r#"
3091 WITH RECURSIVE channel_tree(child_id, parent_id, depth) AS (
3092 SELECT channel_id as child_id, NULL as parent_id, 0
3093 FROM channel_members
3094 WHERE user_id = ?
3095 UNION ALL
3096 SELECT channel_parents.child_id, channel_parents.parent_id, channel_tree.depth + 1
3097 FROM channel_parents, channel_tree
3098 WHERE channel_parents.parent_id = channel_tree.child_id
3099 )
3100 SELECT channel_tree.child_id as id, channels.name, channel_tree.parent_id
3101 FROM channel_tree
3102 JOIN channels ON channels.id = channel_tree.child_id
3103 ORDER BY channel_tree.depth;
3104 "#;
3105
3106 // let root_channel_ids_query = SelectStatement::new()
3107 // .column(channel_member::Column::ChannelId)
3108 // .expr(Expr::value("NULL"))
3109 // .from(channel_member::Entity.table_ref())
3110 // .and_where(
3111 // Expr::col(channel_member::Column::UserId)
3112 // .eq(Expr::cust_with_values("?", vec![user_id])),
3113 // );
3114
3115 // let build_tree_query = SelectStatement::new()
3116 // .column(channel_parent::Column::ChildId)
3117 // .column(channel_parent::Column::ParentId)
3118 // .expr(Expr::col(Alias::new("channel_tree.depth")).add(1i32))
3119 // .from(Alias::new("channel_tree"))
3120 // .and_where(
3121 // Expr::col(channel_parent::Column::ParentId)
3122 // .equals(Alias::new("channel_tree"), Alias::new("child_id")),
3123 // )
3124 // .to_owned();
3125
3126 // let common_table_expression = CommonTableExpression::new()
3127 // .query(
3128 // root_channel_ids_query
3129 // .union(UnionType::Distinct, build_tree_query)
3130 // .to_owned(),
3131 // )
3132 // .column(Alias::new("child_id"))
3133 // .column(Alias::new("parent_id"))
3134 // .column(Alias::new("depth"))
3135 // .table_name(Alias::new("channel_tree"))
3136 // .to_owned();
3137
3138 // let select = SelectStatement::new()
3139 // .expr_as(
3140 // Expr::col(Alias::new("channel_tree.child_id")),
3141 // Alias::new("id"),
3142 // )
3143 // .column(channel::Column::Name)
3144 // .column(Alias::new("channel_tree.parent_id"))
3145 // .from(Alias::new("channel_tree"))
3146 // .inner_join(
3147 // channel::Entity.table_ref(),
3148 // Expr::eq(
3149 // channel::Column::Id.into_expr(),
3150 // Expr::tbl(Alias::new("channel_tree"), Alias::new("child_id")),
3151 // ),
3152 // )
3153 // .order_by(Alias::new("channel_tree.child_id"), Order::Asc)
3154 // .to_owned();
3155
3156 // let with_clause = WithClause::new()
3157 // .recursive(true)
3158 // .cte(common_table_expression)
3159 // .to_owned();
3160
3161 // let query = select.with(with_clause);
3162
3163 // let query = SelectStatement::new()
3164 // .column(ColumnRef::Asterisk)
3165 // .from_subquery(query, Alias::new("channel_tree")
3166 // .to_owned();
3167
3168 // let stmt = self.pool.get_database_backend().build(&query);
3169
3170 let stmt = Statement::from_sql_and_values(
3171 self.pool.get_database_backend(),
3172 sql,
3173 vec![user_id.into()],
3174 );
3175
3176 Ok(channel_parent::Entity::find()
3177 .from_raw_sql(stmt)
3178 .into_model::<Channel>()
3179 .all(&*tx)
3180 .await?)
3070 })3181 })
3071 // let user = user::Model {3182 .await
3072 // id: user_id,
3073 // ..Default::default()
3074 // };
3075 // let mut channel_ids = user
3076 // .find_related(channel_member::Entity)
3077 // .select_only()
3078 // .column(channel_member::Column::ChannelId)
3079 // .all(&*tx)
3080 // .await;
3081
3082 // // let descendants = Alias::new("descendants");
3083 // // let cte_referencing = SelectStatement::new()
3084 // // .column(channel_parent::Column::ChildId)
3085 // // .from(channel::Entity)
3086 // // .and_where(
3087 // // Expr::col(channel_parent::Column::ParentId)
3088 // // .in_subquery(SelectStatement::new().from(descendants).take())
3089 // // );
3090
3091 // // /*
3092 // // WITH RECURSIVE descendant_ids(id) AS (
3093 // // $1
3094 // // UNION ALL
3095 // // SELECT child_id as id FROM channel_parents WHERE parent_id IN descendants
3096 // // )
3097 // // SELECT * from channels where id in descendant_ids
3098 // // */
3099
3100
3101 // // // WITH RECURSIVE descendants(id) AS (
3102 // // // // SQL QUERY FOR SELECTING Initial IDs
3103 // // // UNION
3104 // // // SELECT id FROM ancestors WHERE p.parent = id
3105 // // // )
3106 // // // SELECT * FROM descendants;
3107
3108
3109
3110 // // // let descendant_channel_ids =
3111
3112
3113
3114 // // // let query = sea_query::Query::with().recursive(true);
3115
3116
3117 // // for id_path in id_paths {
3118 // // //
3119 // // }
3120
3121
3122 // // // zed/public/plugins
3123 // // // zed/public/plugins/js
3124 // // // zed/zed-livekit
3125 // // // livekit/zed-livekit
3126 // // // zed - 101
3127 // // // livekit - 500
3128 // // // zed-livekit - 510
3129 // // // public - 150
3130 // // // plugins - 200
3131 // // // js - 300
3132 // // //
3133 // // // Channel, Parent - edges
3134 // // // 510 - 500
3135 // // // 510 - 101
3136 // // //
3137 // // // Given the channel 'Zed' (101)
3138 // // // Select * from EDGES where parent = 101 => 510
3139 // // //
3140
3141
3142 // // "SELECT * from channels where id_path like '$1?'"
3143
3144 // // // https://www.postgresql.org/docs/current/queries-with.html
3145 // // // https://www.sqlite.org/lang_with.html
3146
3147 // // "SELECT channel_id from channel_ancestors where ancestor_id IN $()"
3148
3149 // // // | channel_id | ancestor_ids |
3150 // // // 150 150
3151 // // // 150 101
3152 // // // 200 101
3153 // // // 300 101
3154 // // // 200 150
3155 // // // 300 150
3156 // // // 300 200
3157 // // //
3158 // // // // | channel_id | ancestor_ids |
3159 // // // 150 101
3160 // // // 200 101
3161 // // // 300 101
3162 // // // 200 150
3163 // // // 300 [150, 200]
3164
3165 // // channel::Entity::find()
3166 // // .filter(channel::Column::IdPath.like(id_paths.unwrap()))
3167
3168 // // dbg!(&id_paths.unwrap()[0].id_path);
3169
3170 // // // let mut channel_members_by_channel_id = HashMap::new();
3171 // // // for channel_member in channel_members {
3172 // // // channel_members_by_channel_id
3173 // // // .entry(channel_member.channel_id)
3174 // // // .or_insert_with(Vec::new)
3175 // // // .push(channel_member);
3176 // // // }
3177
3178 // // // let mut channel_messages = channel_message::Entity::find()
3179 // // // .filter(channel_message::Column::ChannelId.in_selection(channel_ids))
3180 // // // .all(&*tx)
3181 // // // .await?;
3182
3183 // // // let mut channel_messages_by_channel_id = HashMap::new();
3184 // // // for channel_message in channel_messages {
3185 // // // channel_messages_by_channel_id
3186 // // // .entry(channel_message.channel_id)
3187 // // // .or_insert_with(Vec::new)
3188 // // // .push(channel_message);
3189 // // // }
3190
3191 // // todo!();
3192 // // // Ok(channels)
3193 // Err(Error("not implemented"))
3194 // })
3195 // .await
3196 }3183 }
3197 3184
3198 async fn transaction<F, Fut, T>(&self, f: F) -> Result<T>3185 async fn transaction<F, Fut, T>(&self, f: F) -> Result<T>
@@ -3440,6 +3427,13 @@ pub struct NewUserResult { · 241 unmodified lines
3440 pub signup_device_id: Option<String>,3427 pub signup_device_id: Option<String>,
3441}3428}
3442 3429
3430#[derive(FromQueryResult, Debug, PartialEq)]
3431pub struct Channel {
3432 pub id: ChannelId,
3433 pub name: String,
3434 pub parent_id: Option<ChannelId>,
3435}
3436
3443fn random_invite_code() -> String {3437fn random_invite_code() -> String {
3444 nanoid::nanoid!(16)3438 nanoid::nanoid!(16)
3445}3439}
crates/collab/src/db/channel.rs+0−1
@@ -8,7 +8,6 @@ pub struct Model { · 7 unmodified lines
8 pub id: ChannelId,8 pub id: ChannelId,
9 pub name: String,9 pub name: String,
10 pub room_id: Option<RoomId>,10 pub room_id: Option<RoomId>,
11 // pub id_path: String,
12}11}
13 12
14impl ActiveModelBehavior for ActiveModel {}13impl ActiveModelBehavior for ActiveModel {}
crates/collab/src/db/channel_parent.rs+3−0
@@ -11,3 +11,6 @@ pub struct Model { · 10 unmodified lines
11}11}
12 12
13impl ActiveModelBehavior for ActiveModel {}13impl ActiveModelBehavior for ActiveModel {}
14
15#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
16pub enum Relation {}
crates/collab/src/tests/channel_tests.rs+63−3
@@ -1,6 +1,8 @@
1use gpui::{executor::Deterministic, TestAppContext};1use gpui::{executor::Deterministic, TestAppContext};
2use std::sync::Arc;2use std::sync::Arc;
3 3
4use crate::db::Channel;
5
4use super::TestServer;6use super::TestServer;
5 7
6#[gpui::test]8#[gpui::test]
@@ -11,13 +13,71 @@ async fn test_basic_channels(deterministic: Arc<Deterministic>, cx: &mut TestApp · 4 unmodified lines
11 let a_id = crate::db::UserId(client_a.user_id().unwrap() as i32);13 let a_id = crate::db::UserId(client_a.user_id().unwrap() as i32);
12 let db = server._test_db.db();14 let db = server._test_db.db();
13 15
14 let zed_id = db.create_channel("zed").await.unwrap();16 let zed_id = db.create_root_channel("zed").await.unwrap();
17 let crdb_id = db.create_channel("crdb", Some(zed_id)).await.unwrap();
18 let livestreaming_id = db
19 .create_channel("livestreaming", Some(zed_id))
20 .await
21 .unwrap();
22 let replace_id = db.create_channel("replace", Some(zed_id)).await.unwrap();
23 let rust_id = db.create_root_channel("rust").await.unwrap();
24 let cargo_id = db.create_channel("cargo", Some(rust_id)).await.unwrap();
15 25
16 db.add_channel_member(zed_id, a_id).await.unwrap();26 db.add_channel_member(zed_id, a_id).await.unwrap();
27 db.add_channel_member(rust_id, a_id).await.unwrap();
28
29 let channels = db.get_channels(a_id).await.unwrap();
30 assert_eq!(
31 channels,
32 vec![
33 Channel {
34 id: zed_id,
35 name: "zed".to_string(),
36 parent_id: None,
37 },
38 Channel {
39 id: rust_id,
40 name: "rust".to_string(),
41 parent_id: None,
42 },
43 Channel {
44 id: crdb_id,
45 name: "crdb".to_string(),
46 parent_id: Some(zed_id),
47 },
48 Channel {
49 id: livestreaming_id,
50 name: "livestreaming".to_string(),
51 parent_id: Some(zed_id),
52 },
53 Channel {
54 id: replace_id,
55 name: "replace".to_string(),
56 parent_id: Some(zed_id),
57 },
58 Channel {
59 id: cargo_id,
60 name: "cargo".to_string(),
61 parent_id: Some(rust_id),
62 }
63 ]
64 );
65}
17 66
18 let channels = db.get_channels(a_id).await;67#[gpui::test]
68async fn test_block_cycle_creation(deterministic: Arc<Deterministic>, cx: &mut TestAppContext) {
69 deterministic.forbid_parking();
70 let mut server = TestServer::start(&deterministic).await;
71 let client_a = server.create_client(cx, "user_a").await;
72 let a_id = crate::db::UserId(client_a.user_id().unwrap() as i32);
73 let db = server._test_db.db();
19 74
20 assert_eq!(channels, vec![zed_id]);75 let zed_id = db.create_root_channel("zed").await.unwrap();
76 let first_id = db.create_channel("first", Some(zed_id)).await.unwrap();
77 let second_id = db
78 .create_channel("second_id", Some(first_id))
79 .await
80 .unwrap();
21}81}
22 82
23/*83/*

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t