chore: fix migrate empty document (#4087)

* chore: fix migrate empty document

* chore: update collab rev
This commit is contained in:
Nathan.fooo 2023-12-04 20:25:15 -08:00 committed by GitHub
parent 1491fbfb0d
commit 2d7a373d77
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 81 additions and 69 deletions

View File

@ -881,7 +881,7 @@ dependencies = [
[[package]]
name = "collab"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"async-trait",
@ -900,7 +900,7 @@ dependencies = [
[[package]]
name = "collab-database"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"async-trait",
@ -930,7 +930,7 @@ dependencies = [
[[package]]
name = "collab-derive"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"proc-macro2",
"quote",
@ -942,7 +942,7 @@ dependencies = [
[[package]]
name = "collab-document"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"collab",
@ -961,7 +961,7 @@ dependencies = [
[[package]]
name = "collab-entity"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"bytes",
@ -975,7 +975,7 @@ dependencies = [
[[package]]
name = "collab-folder"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"chrono",
@ -1017,7 +1017,7 @@ dependencies = [
[[package]]
name = "collab-persistence"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"async-trait",
@ -1038,7 +1038,7 @@ dependencies = [
[[package]]
name = "collab-plugins"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"async-trait",
@ -1064,7 +1064,7 @@ dependencies = [
[[package]]
name = "collab-user"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"collab",

View File

@ -67,14 +67,14 @@ client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "f13
# To switch to the local path, run:
# scripts/tool/update_collab_source.sh
# ⚠️⚠️⚠️️
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-plugins = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-persistence = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-plugins = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-persistence = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }

View File

@ -731,7 +731,7 @@ dependencies = [
[[package]]
name = "collab"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"async-trait",
@ -750,7 +750,7 @@ dependencies = [
[[package]]
name = "collab-database"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"async-trait",
@ -780,7 +780,7 @@ dependencies = [
[[package]]
name = "collab-derive"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"proc-macro2",
"quote",
@ -792,7 +792,7 @@ dependencies = [
[[package]]
name = "collab-document"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"collab",
@ -811,7 +811,7 @@ dependencies = [
[[package]]
name = "collab-entity"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"bytes",
@ -825,7 +825,7 @@ dependencies = [
[[package]]
name = "collab-folder"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"chrono",
@ -867,7 +867,7 @@ dependencies = [
[[package]]
name = "collab-persistence"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"async-trait",
@ -888,7 +888,7 @@ dependencies = [
[[package]]
name = "collab-plugins"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"async-trait",
@ -914,7 +914,7 @@ dependencies = [
[[package]]
name = "collab-user"
version = "0.1.0"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=e5c8f4a9a095f7a453e5ced42888bdf248c55358#e5c8f4a9a095f7a453e5ced42888bdf248c55358"
source = "git+https://github.com/AppFlowy-IO/AppFlowy-Collab?rev=c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1#c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1"
dependencies = [
"anyhow",
"collab",

View File

@ -109,11 +109,11 @@ client-api = { git = "https://github.com/AppFlowy-IO/AppFlowy-Cloud", rev = "f13
# To switch to the local path, run:
# scripts/tool/update_collab_source.sh
# ⚠️⚠️⚠️️
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-plugins = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab-persistence = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "e5c8f4a9a095f7a453e5ced42888bdf248c55358" }
collab = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-folder = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-document = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-database = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-plugins = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-user = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-entity = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }
collab-persistence = { git = "https://github.com/AppFlowy-IO/AppFlowy-Collab", rev = "c6e9d6b9a0aed7e2d1b8de9328d64e3f8b04ded1" }

View File

@ -4,11 +4,12 @@ use collab::core::collab::MutexCollab;
use collab::core::origin::{CollabClient, CollabOrigin};
use collab_document::document::Document;
use collab_document::document_data::default_document_data;
use collab_folder::Folder;
use collab_document::error::DocumentError;
use collab_folder::{Folder, View};
use tracing::{event, instrument};
use collab_integrate::{RocksCollabDB, YrsDocAction};
use flowy_error::{internal_error, FlowyResult};
use collab_integrate::{PersistenceError, RocksCollabDB, YrsDocAction};
use flowy_error::{internal_error, FlowyError, FlowyResult};
use crate::migrations::migration::UserDataMigration;
use crate::migrations::util::load_collab;
@ -26,39 +27,25 @@ impl UserDataMigration for HistoricalEmptyDocumentMigration {
fn run(&self, session: &Session, collab_db: &Arc<RocksCollabDB>) -> FlowyResult<()> {
let write_txn = collab_db.write_txn();
let origin = CollabOrigin::Client(CollabClient::new(session.user_id, "phantom"));
// Deserialize the folder from the raw data
if let Ok(folder_collab) = load_collab(session.user_id, &write_txn, &session.user_workspace.id)
{
let folder = Folder::open(session.user_id, folder_collab, None)?;
let folder_collab = match load_collab(session.user_id, &write_txn, &session.user_workspace.id) {
Ok(fc) => fc,
Err(_) => return Ok(()),
};
// Migration the first level documents of the workspace. The first level documents do not have
// any updates. So when calling load_collab, it will return error.
let migration_views = folder.get_workspace_views();
for view in migration_views {
if load_collab(session.user_id, &write_txn, &view.id).is_err() {
// Create a document with default data
let document_data = default_document_data();
let collab = Arc::new(MutexCollab::new(origin.clone(), &view.id, vec![]));
if let Ok(document) = Document::create_with_data(collab.clone(), document_data) {
// Remove all old updates and then insert the new update
let encode = document.get_collab().encode_collab_v1();
if let Err(err) = write_txn.flush_doc_with(
session.user_id,
&view.id,
&encode.doc_state,
&encode.state_vector,
) {
event!(
tracing::Level::ERROR,
"Failed to migrate document {}, error: {}",
view.id,
err
);
} else {
event!(tracing::Level::INFO, "Did migrate document {}", view.id);
}
}
}
let folder = Folder::open(session.user_id, folder_collab, None)?;
let migration_views = folder.get_workspace_views();
// For historical reasons, the first level documents are empty. So migrate them by inserting
// the default document data.
for view in migration_views {
if let Err(_) = migrate_empty_document(&write_txn, &origin, &view, session.user_id) {
event!(
tracing::Level::ERROR,
"Failed to migrate document {}",
view.id
);
} else {
event!(tracing::Level::INFO, "Did migrate document {}", view.id);
}
}
@ -67,3 +54,28 @@ impl UserDataMigration for HistoricalEmptyDocumentMigration {
Ok(())
}
}
fn migrate_empty_document<'a, W>(
write_txn: &W,
origin: &CollabOrigin,
view: &View,
user_id: i64,
) -> Result<(), FlowyError>
where
W: YrsDocAction<'a>,
PersistenceError: From<W::Error>,
{
let collab = Arc::new(MutexCollab::new(origin.clone(), &view.id, vec![]));
let result = Document::open(collab.clone());
if let Err(DocumentError::NoRequiredData) = result {
let document_data = default_document_data();
let document = Document::create_with_data(collab, document_data)?;
let encode = document.get_collab().encode_collab_v1();
write_txn.flush_doc_with(user_id, &view.id, &encode.doc_state, &encode.state_vector)?;
} else {
result?;
}
Ok(())
}