From e22f3163273ae8f5971872ed713dbfd4dbfbca64 Mon Sep 17 00:00:00 2001 From: Zack Fu Zi Xiang Date: Tue, 10 Sep 2024 12:48:55 +0800 Subject: [PATCH] feat: partially use database body --- src/biz/workspace/publish_dup.rs | 88 ++++++++++++-------------------- 1 file changed, 34 insertions(+), 54 deletions(-) diff --git a/src/biz/workspace/publish_dup.rs b/src/biz/workspace/publish_dup.rs index 2898250c..fe1d744a 100644 --- a/src/biz/workspace/publish_dup.rs +++ b/src/biz/workspace/publish_dup.rs @@ -4,6 +4,7 @@ use collab::core::collab::DataSource; use collab::preclude::Collab; use collab::preclude::MapExt; +use collab_database::database::DatabaseBody; use collab_database::entity::FieldType; use collab_database::views::DatabaseViews; use collab_database::workspace_database::WorkspaceDatabaseBody; @@ -669,7 +670,9 @@ impl PublishCollabDuplicator { ) -> Result<(String, String, bool), AppError> { // collab of database let mut db_collab = collab_from_doc_state(published_db.database_collab.clone(), "")?; - let pub_db_id = get_database_id_from_collab(&db_collab)?; + let db_body = DatabaseBody::from_collab(&db_collab) + .ok_or_else(|| AppError::RecordNotFound("no database body found".to_string()))?; + let pub_db_id = db_body.get_database_id(&db_collab.context.transact()); // check if the database is already duplicated if let Some(db_id) = self.duplicated_refs.get(&pub_db_id).cloned().flatten() { @@ -682,62 +685,39 @@ impl PublishCollabDuplicator { { // handle row relations - // collect all map ref with `database_id` fields - // deep copy those database - // - // db_collab: Object { - // "database": Object { - // "fields": Object { `database_fields` - // "MBaTsr": Object { - // "type_option": Object { `type_option` - // "10": Object { - // "database_id": String("e7a18802-1594-4d44-9542-38c7b471ebc2") - // // we need to duplicate this database and replace the id - // } - // ... - // }, - // }, - // ... - // }, - // }, - // } - // let mut txn = db_collab.context.transact_mut(); - let database_fields: MapRef = db_collab - .data - .get_with_path(&txn, ["database", "fields"]) - .ok_or_else(|| AppError::RecordNotFound("no fields found in database".to_string()))?; - let mut type_option_values = vec![]; - for (_, out) in database_fields.iter(&txn) { - if let Ok(m1) = out.cast::() { - if let Some(type_option) = m1.get(&txn, "type_option") { - if let Ok(m2) = type_option.cast::() { - for (_, out2) in m2.iter(&txn) { - if let Ok(m3) = out2.cast::() { - type_option_values.push(m3); - } + let all_fields = db_body.fields.get_all_fields(&txn); + for mut field in all_fields { + for (key, type_option_value) in field.type_options.iter_mut() { + if *key == FieldType::Relation.type_id() { + if let Some(pub_db_id) = type_option_value.get_mut("database_id") { + if let any::Any::String(pub_db_id_str) = pub_db_id { + if let Some(related_db_view) = + published_db.database_relations.get(pub_db_id_str.as_ref()) + { + if let Some(_dup_view_id) = self + .deep_copy_view(related_db_view, &self.dest_view_id.to_string()) + .await? + { + if let Some(dup_db_id) = self + .duplicated_refs + .get(pub_db_id_str.as_ref()) + .cloned() + .flatten() + { + *pub_db_id = any::Any::String(dup_db_id.into()); + db_body.fields.update_field(&mut txn, &field.id, |f| { + f.set_type_option( + FieldType::Relation.into(), + Some(type_option_value.clone()), + ); + }); + }; + }; + }; } - } - } - } - } - for m in type_option_values { - if let Some(Out::Any(any::Any::String(pub_db_id))) = m.get(&txn, "database_id") { - if let Some(related_db_view) = published_db.database_relations.get(pub_db_id.as_ref()) { - if let Some(_dup_view_id) = self - .deep_copy_view(related_db_view, &self.dest_view_id.to_string()) - .await? - { - if let Some(dup_db_id) = self - .duplicated_refs - .get(pub_db_id.as_ref()) - .cloned() - .flatten() - { - m.insert(&mut txn, "database_id", dup_db_id.as_str()); - }; }; - }; + } } } }