2023-12-26 02:03:42 +08:00
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
use collab_folder::Folder;
|
2024-02-25 07:49:44 +08:00
|
|
|
use collab_plugins::local_storage::kv::{KVTransactionDB, PersistenceError};
|
2024-04-04 13:03:40 +08:00
|
|
|
use semver::Version;
|
2023-12-26 02:03:42 +08:00
|
|
|
use tracing::instrument;
|
|
|
|
|
|
2024-01-05 00:05:38 +08:00
|
|
|
use collab_integrate::{CollabKVAction, CollabKVDB};
|
2024-02-25 07:49:44 +08:00
|
|
|
use flowy_error::FlowyResult;
|
2025-04-18 15:31:50 +08:00
|
|
|
use flowy_user_pub::entities::AuthType;
|
2023-12-26 02:03:42 +08:00
|
|
|
|
|
|
|
|
use crate::migrations::migration::UserDataMigration;
|
|
|
|
|
use crate::migrations::util::load_collab;
|
2024-01-30 05:36:27 +08:00
|
|
|
use flowy_user_pub::session::Session;
|
2023-12-26 02:03:42 +08:00
|
|
|
|
2023-12-27 11:42:39 +08:00
|
|
|
/// Migrate the workspace: { trash: [view_id] } to { trash: { uid: [view_id] } }
|
2023-12-26 02:03:42 +08:00
|
|
|
pub struct WorkspaceTrashMapToSectionMigration;
|
|
|
|
|
|
|
|
|
|
impl UserDataMigration for WorkspaceTrashMapToSectionMigration {
|
|
|
|
|
fn name(&self) -> &str {
|
|
|
|
|
"workspace_trash_map_to_section_migration"
|
|
|
|
|
}
|
|
|
|
|
|
2024-11-01 15:57:58 +08:00
|
|
|
fn run_when(
|
|
|
|
|
&self,
|
|
|
|
|
first_installed_version: &Option<Version>,
|
|
|
|
|
_current_version: &Version,
|
|
|
|
|
) -> bool {
|
2024-10-31 13:02:54 +08:00
|
|
|
match first_installed_version {
|
|
|
|
|
None => true,
|
|
|
|
|
Some(version) => version < &Version::new(0, 4, 0),
|
|
|
|
|
}
|
2024-04-04 13:03:40 +08:00
|
|
|
}
|
|
|
|
|
|
2023-12-26 02:03:42 +08:00
|
|
|
#[instrument(name = "WorkspaceTrashMapToSectionMigration", skip_all, err)]
|
|
|
|
|
fn run(
|
|
|
|
|
&self,
|
|
|
|
|
session: &Session,
|
2024-01-05 00:05:38 +08:00
|
|
|
collab_db: &Arc<CollabKVDB>,
|
2025-04-18 15:31:50 +08:00
|
|
|
_authenticator: &AuthType,
|
2023-12-26 02:03:42 +08:00
|
|
|
) -> FlowyResult<()> {
|
2024-02-25 07:49:44 +08:00
|
|
|
collab_db.with_write_txn(|write_txn| {
|
2024-10-31 13:02:54 +08:00
|
|
|
if let Ok(collab) = load_collab(
|
|
|
|
|
session.user_id,
|
|
|
|
|
write_txn,
|
|
|
|
|
&session.user_workspace.id,
|
|
|
|
|
&session.user_workspace.id,
|
|
|
|
|
) {
|
2024-08-18 05:16:42 +02:00
|
|
|
let mut folder = Folder::open(session.user_id, collab, None)
|
2024-02-25 07:49:44 +08:00
|
|
|
.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);
|
2024-01-05 00:05:38 +08:00
|
|
|
}
|
2024-02-25 07:49:44 +08:00
|
|
|
|
2024-04-03 11:26:48 +08:00
|
|
|
let encode = folder
|
2024-08-18 05:16:42 +02:00
|
|
|
.encode_collab()
|
2024-04-03 11:26:48 +08:00
|
|
|
.map_err(|err| PersistenceError::Internal(err.into()))?;
|
2024-08-21 14:04:53 +08:00
|
|
|
write_txn.flush_doc(
|
2024-02-25 07:49:44 +08:00
|
|
|
session.user_id,
|
|
|
|
|
&session.user_workspace.id,
|
2024-10-31 13:02:54 +08:00
|
|
|
&session.user_workspace.id,
|
2024-08-21 14:04:53 +08:00
|
|
|
encode.state_vector.to_vec(),
|
|
|
|
|
encode.doc_state.to_vec(),
|
2024-02-25 07:49:44 +08:00
|
|
|
)?;
|
|
|
|
|
}
|
|
|
|
|
Ok(())
|
|
|
|
|
})?;
|
2023-12-26 02:03:42 +08:00
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
|
}
|
|
|
|
|
}
|