Skip to content

Commit

Permalink
chore: migrate private section
Browse files Browse the repository at this point in the history
  • Loading branch information
LucasXu0 committed Apr 3, 2024
1 parent 58fb529 commit f279da5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 0 deletions.
1 change: 1 addition & 0 deletions frontend/rust-lib/flowy-user/src/migrations/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ pub mod migration;
pub mod session_migration;
mod util;
pub mod workspace_and_favorite_v1;
pub mod workspace_private_v1;
pub mod workspace_trash_v1;

#[derive(Clone, Debug)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
use std::sync::Arc;

use collab_folder::Folder;
use collab_plugins::local_storage::kv::{KVTransactionDB, PersistenceError};
use tracing::instrument;

use collab_integrate::{CollabKVAction, CollabKVDB};
use flowy_error::FlowyResult;
use flowy_user_pub::entities::Authenticator;

use crate::migrations::migration::UserDataMigration;
use crate::migrations::util::load_collab;
use flowy_user_pub::session::Session;

/// Migrate the workspace: Add all the view_ids in the view_map into the private section
pub struct WorkspacePrivateSectionMigration;

impl UserDataMigration for WorkspacePrivateSectionMigration {
fn name(&self) -> &str {
"workspace_private_section_migration"
}

#[instrument(name = "WorkspacePrivateSectionMigration", skip_all, err)]
fn run(
&self,
session: &Session,
collab_db: &Arc<CollabKVDB>,
_authenticator: &Authenticator,
) -> FlowyResult<()> {
collab_db.with_write_txn(|write_txn| {
if let Ok(collab) = load_collab(session.user_id, write_txn, &session.user_workspace.id) {
let folder = Folder::open(session.user_id, collab, None)
.map_err(|err| PersistenceError::Internal(err.into()))?;

let view_ids = folder
.get_workspace_views()
.into_iter()
.map(|view| view.id.clone())
.collect::<Vec<String>>();

if !view_ids.is_empty() {
folder.add_private_view_ids(view_ids);
}

let encode = folder
.encode_collab_v1()
.map_err(|err| PersistenceError::Internal(err.into()))?;
write_txn.flush_doc_with(
session.user_id,
&session.user_workspace.id,
&encode.doc_state,
&encode.state_vector,
)?;
}
Ok(())
})?;

Ok(())
}
}
2 changes: 2 additions & 0 deletions frontend/rust-lib/flowy-user/src/user_manager/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ use crate::event_map::{DefaultUserStatusCallback, UserStatusCallback};
use crate::migrations::document_empty_content::HistoricalEmptyDocumentMigration;
use crate::migrations::migration::{UserDataMigration, UserLocalDataMigration};
use crate::migrations::workspace_and_favorite_v1::FavoriteV1AndWorkspaceArrayMigration;
use crate::migrations::workspace_private_v1::WorkspacePrivateSectionMigration;
use crate::migrations::workspace_trash_v1::WorkspaceTrashMapToSectionMigration;
use crate::migrations::AnonUser;
use crate::services::authenticate_user::AuthenticateUser;
Expand Down Expand Up @@ -838,6 +839,7 @@ pub(crate) fn run_collab_data_migration(
Box::new(HistoricalEmptyDocumentMigration),
Box::new(FavoriteV1AndWorkspaceArrayMigration),
Box::new(WorkspaceTrashMapToSectionMigration),
Box::new(WorkspacePrivateSectionMigration),
];
match UserLocalDataMigration::new(session.clone(), collab_db, sqlite_pool)
.run(migrations, &user.authenticator)
Expand Down

0 comments on commit f279da5

Please sign in to comment.