2022-01-19 16:00:11 +08:00
|
|
|
use crate::{
|
|
|
|
|
module::WorkspaceDatabase,
|
2022-01-20 23:51:11 +08:00
|
|
|
services::persistence::{AppTableSql, TrashTableSql, ViewTableSql, WorkspaceTableSql},
|
2022-01-19 16:00:11 +08:00
|
|
|
};
|
2022-01-24 21:17:31 +08:00
|
|
|
use flowy_collaboration::{client_folder::FolderPad, entities::revision::md5};
|
2022-01-27 20:39:54 +08:00
|
|
|
use flowy_database::kv::KV;
|
|
|
|
|
use flowy_error::{FlowyError, FlowyResult};
|
|
|
|
|
use flowy_folder_data_model::entities::{
|
2022-01-19 16:00:11 +08:00
|
|
|
app::{App, RepeatedApp},
|
|
|
|
|
view::{RepeatedView, View},
|
|
|
|
|
workspace::Workspace,
|
|
|
|
|
};
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
|
|
|
|
|
pub(crate) const V1_MIGRATION: &str = "FOLDER_V1_MIGRATION";
|
|
|
|
|
|
|
|
|
|
pub(crate) struct FolderMigration {
|
|
|
|
|
user_id: String,
|
|
|
|
|
database: Arc<dyn WorkspaceDatabase>,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FolderMigration {
|
|
|
|
|
pub fn new(user_id: &str, database: Arc<dyn WorkspaceDatabase>) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
user_id: user_id.to_owned(),
|
|
|
|
|
database,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn run_v1_migration(&self) -> FlowyResult<Option<FolderPad>> {
|
|
|
|
|
let key = md5(format!("{}{}", self.user_id, V1_MIGRATION));
|
2022-01-28 10:56:55 +08:00
|
|
|
if KV::get_bool(&key) {
|
2022-01-19 16:00:11 +08:00
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
tracing::trace!("Run folder version 1 migrations");
|
|
|
|
|
let pool = self.database.db_pool()?;
|
|
|
|
|
let conn = &*pool.get()?;
|
|
|
|
|
let workspaces = conn.immediate_transaction::<_, FlowyError, _>(|| {
|
|
|
|
|
let mut workspaces = WorkspaceTableSql::read_workspaces(&self.user_id, None, conn)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(Workspace::from)
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
for workspace in workspaces.iter_mut() {
|
|
|
|
|
let mut apps = AppTableSql::read_workspace_apps(&workspace.id, conn)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(App::from)
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
for app in apps.iter_mut() {
|
|
|
|
|
let views = ViewTableSql::read_views(&app.id, conn)?
|
|
|
|
|
.into_iter()
|
|
|
|
|
.map(View::from)
|
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
|
|
app.belongings = RepeatedView { items: views };
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
workspace.apps = RepeatedApp { items: apps };
|
|
|
|
|
}
|
|
|
|
|
Ok(workspaces)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
if workspaces.is_empty() {
|
2022-01-27 14:06:59 +08:00
|
|
|
KV::set_bool(&key, true);
|
2022-01-19 16:00:11 +08:00
|
|
|
return Ok(None);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let trash = conn.immediate_transaction::<_, FlowyError, _>(|| {
|
|
|
|
|
let trash = TrashTableSql::read_all(conn)?.take_items();
|
|
|
|
|
Ok(trash)
|
|
|
|
|
})?;
|
|
|
|
|
|
|
|
|
|
let folder = FolderPad::new(workspaces, trash)?;
|
|
|
|
|
KV::set_bool(&key, true);
|
|
|
|
|
Ok(Some(folder))
|
|
|
|
|
}
|
|
|
|
|
}
|