78 lines
2.1 KiB
Rust
Raw Normal View History

use std::sync::Arc;
use collab_folder::Folder;
use collab_plugins::local_storage::kv::{KVTransactionDB, PersistenceError};
2024-04-04 13:03:40 +08:00
use semver::Version;
use tracing::instrument;
use collab_integrate::{CollabKVAction, CollabKVDB};
use flowy_error::FlowyResult;
2025-04-18 15:31:50 +08:00
use flowy_user_pub::entities::AuthType;
use crate::migrations::migration::UserDataMigration;
use crate::migrations::util::load_collab;
use flowy_user_pub::session::Session;
/// Migrate the workspace: { trash: [view_id] } to { trash: { uid: [view_id] } }
pub struct WorkspaceTrashMapToSectionMigration;
impl UserDataMigration for WorkspaceTrashMapToSectionMigration {
fn name(&self) -> &str {
"workspace_trash_map_to_section_migration"
}
fn run_when(
&self,
first_installed_version: &Option<Version>,
_current_version: &Version,
) -> bool {
match first_installed_version {
None => true,
Some(version) => version < &Version::new(0, 4, 0),
}
2024-04-04 13:03:40 +08:00
}
#[instrument(name = "WorkspaceTrashMapToSectionMigration", skip_all, err)]
fn run(
&self,
session: &Session,
collab_db: &Arc<CollabKVDB>,
2025-04-18 15:31:50 +08:00
_authenticator: &AuthType,
) -> FlowyResult<()> {
collab_db.with_write_txn(|write_txn| {
if let Ok(collab) = load_collab(
session.user_id,
write_txn,
&session.user_workspace.id,
&session.user_workspace.id,
) {
let mut folder = Folder::open(session.user_id, collab, None)
.map_err(|err| PersistenceError::Internal(err.into()))?;
let trash_ids = folder
.get_trash_v1()
.into_iter()
.map(|fav| fav.id)
.collect::<Vec<String>>();
if !trash_ids.is_empty() {
2024-03-22 16:15:18 +07:00
folder.add_trash_view_ids(trash_ids);
}
let encode = folder
.encode_collab()
.map_err(|err| PersistenceError::Internal(err.into()))?;
write_txn.flush_doc(
session.user_id,
&session.user_workspace.id,
&session.user_workspace.id,
encode.state_vector.to_vec(),
encode.doc_state.to_vec(),
)?;
}
Ok(())
})?;
Ok(())
}
}