1- use crate :: migrations:: session_migration:: migrate_session_with_user_uuid;
2-
31use crate :: manager:: run_collab_data_migration;
4-
2+ use crate :: migrations :: session_migration :: migrate_session_with_user_uuid ;
53use crate :: services:: data_import:: importer:: load_collab_by_oid;
64use crate :: services:: db:: UserDBPath ;
75use crate :: services:: entities:: { Session , UserPaths } ;
@@ -102,6 +100,7 @@ pub(crate) fn import_appflowy_data_folder(
102100
103101 let mut database_view_ids_by_database_id: HashMap < String , Vec < String > > = HashMap :: new ( ) ;
104102 let row_object_ids = Mutex :: new ( HashSet :: new ( ) ) ;
103+ let row_document_object_ids = Mutex :: new ( HashSet :: new ( ) ) ;
105104 let document_object_ids = Mutex :: new ( HashSet :: new ( ) ) ;
106105 let database_object_ids = Mutex :: new ( HashSet :: new ( ) ) ;
107106 let import_container_view_id = match & container_name {
@@ -156,8 +155,17 @@ pub(crate) fn import_appflowy_data_folder(
156155 & mut all_imported_object_ids,
157156 & imported_collab_by_oid,
158157 & row_object_ids,
158+ & row_document_object_ids,
159159 ) ?;
160160
161+ debug ! (
162+ "import row document ids: {:?}" ,
163+ row_document_object_ids
164+ . lock( )
165+ . iter( )
166+ . collect:: <Vec <& String >>( )
167+ ) ;
168+
161169 // the object ids now only contains the document collab object ids
162170 for object_id in & all_imported_object_ids {
163171 if let Some ( imported_collab) = imported_collab_by_oid. get ( object_id) {
@@ -174,15 +182,18 @@ pub(crate) fn import_appflowy_data_folder(
174182 }
175183
176184 // create a root view that contains all the views
177- let child_views = import_workspace_views (
185+ let ( mut child_views, orphan_views ) = import_workspace_views (
178186 & import_container_view_id,
179187 & mut old_to_new_id_map. lock ( ) ,
180188 & imported_session,
181189 & imported_collab_read_txn,
182190 ) ?;
183191
184192 match container_name {
185- None => Ok ( child_views) ,
193+ None => {
194+ child_views. extend ( orphan_views) ;
195+ Ok ( child_views)
196+ } ,
186197 Some ( container_name) => {
187198 let name = if container_name. is_empty ( ) {
188199 format ! (
@@ -207,15 +218,18 @@ pub(crate) fn import_appflowy_data_folder(
207218 document_object_ids
208219 . lock ( )
209220 . insert ( import_container_view_id. clone ( ) ) ;
210- let import_container_view =
211- ViewBuilder :: new ( session. user_id , session. user_workspace . id . clone ( ) )
212- . with_view_id ( import_container_view_id)
213- . with_layout ( ViewLayout :: Document )
214- . with_name ( name)
215- . with_child_views ( child_views)
216- . build ( ) ;
217-
218- Ok ( vec ! [ import_container_view] )
221+ let mut import_container_views =
222+ vec ! [
223+ ViewBuilder :: new( session. user_id, session. user_workspace. id. clone( ) )
224+ . with_view_id( import_container_view_id)
225+ . with_layout( ViewLayout :: Document )
226+ . with_name( name)
227+ . with_child_views( child_views)
228+ . build( ) ,
229+ ] ;
230+
231+ import_container_views. extend ( orphan_views) ;
232+ Ok ( import_container_views)
219233 } ,
220234 }
221235 } ) ?;
@@ -280,6 +294,7 @@ fn migrate_databases<'a, W>(
280294 imported_object_ids : & mut Vec < String > ,
281295 imported_collab_by_oid : & HashMap < String , Collab > ,
282296 row_object_ids : & Mutex < HashSet < String > > ,
297+ row_document_object_ids : & Mutex < HashSet < String > > ,
283298) -> Result < ( ) , PersistenceError >
284299where
285300 W : CollabKVAction < ' a > ,
@@ -312,6 +327,8 @@ where
312327 let old_row_id = String :: from ( row_order. id . clone ( ) ) ;
313328 let old_row_document_id = database_row_document_id_from_row_id ( & old_row_id) ;
314329 let new_row_id = old_to_new_id_map. lock ( ) . renew_id ( & old_row_id) ;
330+ // The row document might not exist in the database row. But by querying the old_row_document_id,
331+ // we can know the document of the row is exist or not.
315332 let new_row_document_id = database_row_document_id_from_row_id ( & new_row_id) ;
316333
317334 old_to_new_id_map
@@ -328,7 +345,6 @@ where
328345 . iter ( )
329346 . map ( |order| order. id . clone ( ) . into_inner ( ) )
330347 . collect :: < Vec < String > > ( ) ;
331-
332348 row_object_ids. lock ( ) . extend ( new_row_ids) ;
333349 } ) ;
334350
@@ -369,18 +385,18 @@ where
369385 ) ;
370386 }
371387
388+ // imported_collab_by_oid contains all the collab object ids, including the row document collab object ids.
389+ // So, if the id exist in the imported_collab_by_oid, it means the row document collab object is exist.
372390 let imported_row_document_id = database_row_document_id_from_row_id ( imported_row_id) ;
373391 if imported_collab_by_oid
374392 . get ( & imported_row_document_id)
375393 . is_some ( )
376394 {
377395 let new_row_document_id = old_to_new_id_map. lock ( ) . renew_id ( & imported_row_document_id) ;
378- info ! (
379- "map row document from: {}, to: {}" ,
380- imported_row_document_id, new_row_document_id,
381- ) ;
396+ row_document_object_ids. lock ( ) . insert ( new_row_document_id) ;
382397 }
383398 }
399+
384400 Ok ( ( ) )
385401}
386402
@@ -436,7 +452,7 @@ fn import_workspace_views<'a, W>(
436452 old_to_new_id_map : & mut OldToNewIdMap ,
437453 other_session : & Session ,
438454 other_collab_read_txn : & W ,
439- ) -> Result < Vec < ParentChildViews > , PersistenceError >
455+ ) -> Result < ( Vec < ParentChildViews > , Vec < ParentChildViews > ) , PersistenceError >
440456where
441457 W : CollabKVAction < ' a > ,
442458 PersistenceError : From < W :: Error > ,
@@ -523,7 +539,17 @@ where
523539 )
524540 . collect :: < Vec < ParentChildViews > > ( ) ;
525541
526- Ok ( parent_views)
542+ // the views in the all_views_map now, should be the orphan views
543+ debug ! ( "create orphan views: {:?}" , all_views_map. keys( ) ) ;
544+ let mut orphan_views = vec ! [ ] ;
545+ for orphan_view in all_views_map. into_values ( ) {
546+ orphan_views. push ( ParentChildViews {
547+ parent_view : orphan_view,
548+ child_views : vec ! [ ] ,
549+ } ) ;
550+ }
551+
552+ Ok ( ( parent_views, orphan_views) )
527553}
528554
529555fn parent_view_from_view (
0 commit comments