Implement final move, link, unlink db APIs
This commit is contained in:
parent
7fa68a9aa4
commit
9afb67f2cf
8 changed files with 160 additions and 174 deletions
|
@ -48,7 +48,6 @@ impl Database {
|
|||
.insert(&*tx)
|
||||
.await?;
|
||||
|
||||
let channel_paths_stmt;
|
||||
if let Some(parent) = parent {
|
||||
let sql = r#"
|
||||
INSERT INTO channel_paths
|
||||
|
@ -60,7 +59,7 @@ impl Database {
|
|||
WHERE
|
||||
channel_id = $3
|
||||
"#;
|
||||
channel_paths_stmt = Statement::from_sql_and_values(
|
||||
let channel_paths_stmt = Statement::from_sql_and_values(
|
||||
self.pool.get_database_backend(),
|
||||
sql,
|
||||
[
|
||||
|
@ -796,6 +795,8 @@ impl Database {
|
|||
return Err(anyhow!("Cannot create a channel cycle").into());
|
||||
}
|
||||
}
|
||||
|
||||
// Now insert all of the new paths
|
||||
let sql = r#"
|
||||
INSERT INTO channel_paths
|
||||
(id_path, channel_id)
|
||||
|
@ -832,6 +833,21 @@ impl Database {
|
|||
}
|
||||
}
|
||||
|
||||
// If we're linking a channel, remove any root edges for the channel
|
||||
{
|
||||
let sql = r#"
|
||||
DELETE FROM channel_paths
|
||||
WHERE
|
||||
id_path = '/' || $1 || '/'
|
||||
"#;
|
||||
let channel_paths_stmt = Statement::from_sql_and_values(
|
||||
self.pool.get_database_backend(),
|
||||
sql,
|
||||
[channel.to_proto().into()],
|
||||
);
|
||||
tx.execute(channel_paths_stmt).await?;
|
||||
}
|
||||
|
||||
if let Some(channel) = from_descendants.get_mut(&channel) {
|
||||
// Remove the other parents
|
||||
channel.clear();
|
||||
|
@ -849,7 +865,7 @@ impl Database {
|
|||
&self,
|
||||
user: UserId,
|
||||
channel: ChannelId,
|
||||
from: Option<ChannelId>,
|
||||
from: ChannelId,
|
||||
) -> Result<()> {
|
||||
self.transaction(|tx| async move {
|
||||
// Note that even with these maxed permissions, this linking operation
|
||||
|
@ -927,10 +943,6 @@ impl Database {
|
|||
self.unlink_channel_internal(user, channel, from, &*tx)
|
||||
.await?;
|
||||
|
||||
dbg!(channel_path::Entity::find().all(&*tx).await);
|
||||
|
||||
dbg!(&moved_channels);
|
||||
|
||||
Ok(moved_channels)
|
||||
})
|
||||
.await
|
||||
|
|
|
@ -664,7 +664,7 @@ async fn test_channels_moving(db: &Arc<Database>) {
|
|||
.unlink_channel(
|
||||
a_id,
|
||||
livestreaming_dag_sub_id,
|
||||
Some(livestreaming_id),
|
||||
livestreaming_id,
|
||||
)
|
||||
.await
|
||||
.unwrap();
|
||||
|
@ -688,7 +688,7 @@ async fn test_channels_moving(db: &Arc<Database>) {
|
|||
|
||||
// ========================================================================
|
||||
// Test unlinking in a complex DAG by removing the inner link
|
||||
db.unlink_channel(a_id, livestreaming_id, Some(gpui2_id))
|
||||
db.unlink_channel(a_id, livestreaming_id, gpui2_id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -709,7 +709,7 @@ async fn test_channels_moving(db: &Arc<Database>) {
|
|||
|
||||
// ========================================================================
|
||||
// Test moving DAG nodes by moving livestreaming to be below gpui2
|
||||
db.move_channel(a_id, livestreaming_id, Some(crdb_id), gpui2_id)
|
||||
db.move_channel(a_id, livestreaming_id, crdb_id, gpui2_id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -746,7 +746,7 @@ async fn test_channels_moving(db: &Arc<Database>) {
|
|||
|
||||
// ========================================================================
|
||||
// Unlinking a channel from it's parent should automatically promote it to a root channel
|
||||
db.unlink_channel(a_id, crdb_id, Some(zed_id))
|
||||
db.unlink_channel(a_id, crdb_id, zed_id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -764,29 +764,9 @@ async fn test_channels_moving(db: &Arc<Database>) {
|
|||
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
|
||||
]);
|
||||
|
||||
// ========================================================================
|
||||
// Unlinking a root channel should not have any effect
|
||||
db.unlink_channel(a_id, crdb_id, None)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
// DAG is now:
|
||||
// crdb
|
||||
// zed
|
||||
// \- livestreaming - livestreaming_dag - livestreaming_dag_sub
|
||||
//
|
||||
let result = db.get_channels_for_user(a_id).await.unwrap();
|
||||
assert_dag(result.channels, &[
|
||||
(zed_id, None),
|
||||
(crdb_id, None),
|
||||
(livestreaming_id, Some(zed_id)),
|
||||
(livestreaming_dag_id, Some(livestreaming_id)),
|
||||
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
|
||||
]);
|
||||
|
||||
// ========================================================================
|
||||
// You should be able to move a root channel into a non-root channel
|
||||
db.move_channel(a_id, crdb_id, None, zed_id)
|
||||
db.link_channel(a_id, crdb_id, zed_id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -805,8 +785,8 @@ async fn test_channels_moving(db: &Arc<Database>) {
|
|||
|
||||
|
||||
// ========================================================================
|
||||
// Moving a non-root channel without a parent id should be the equivalent of a link operation
|
||||
db.move_channel(a_id, livestreaming_id, None, crdb_id)
|
||||
// Prep for DAG deletion test
|
||||
db.link_channel(a_id, livestreaming_id, crdb_id)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
|
@ -824,10 +804,10 @@ async fn test_channels_moving(db: &Arc<Database>) {
|
|||
(livestreaming_dag_sub_id, Some(livestreaming_dag_id)),
|
||||
]);
|
||||
|
||||
// ========================================================================
|
||||
// Deleting a parent of a DAG should delete the whole DAG:
|
||||
// Deleting the parent of a DAG should delete the whole DAG:
|
||||
db.delete_channel(zed_id, a_id).await.unwrap();
|
||||
let result = db.get_channels_for_user(a_id).await.unwrap();
|
||||
|
||||
assert!(
|
||||
result.channels.is_empty()
|
||||
)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue