feat: partially use database body

This commit is contained in:
Zack Fu Zi Xiang 2024-09-10 12:48:55 +08:00
parent 0dbd8fd633
commit e22f316327
No known key found for this signature in database
1 changed files with 34 additions and 54 deletions

View File

@ -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::<MapRef>() {
if let Some(type_option) = m1.get(&txn, "type_option") {
if let Ok(m2) = type_option.cast::<MapRef>() {
for (_, out2) in m2.iter(&txn) {
if let Ok(m3) = out2.cast::<MapRef>() {
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());
};
};
};
}
}
}
}