74 lines
2.3 KiB
Rust
Raw Normal View History

2022-07-20 18:27:12 +08:00
use crate::manager::GridUser;
use crate::services::persistence::GridDatabase;
use flowy_database::kv::KV;
use flowy_error::FlowyResult;
use flowy_grid_data_model::revision::GridRevision;
2022-08-16 11:37:34 +08:00
use flowy_revision::disk::SQLiteGridRevisionPersistence;
use flowy_revision::reset::{RevisionResettable, RevisionStructReset};
2022-07-20 18:27:12 +08:00
use flowy_sync::client_grid::{make_grid_rev_json_str, GridRevisionPad};
use flowy_sync::entities::revision::Revision;
2022-08-16 11:37:34 +08:00
use flowy_sync::util::md5;
2022-07-20 18:27:12 +08:00
use std::sync::Arc;
2022-08-16 11:37:34 +08:00
const V1_MIGRATION: &str = "GRID_V1_MIGRATION";
2022-07-20 18:27:12 +08:00
pub(crate) struct GridMigration {
user: Arc<dyn GridUser>,
database: Arc<dyn GridDatabase>,
}
impl GridMigration {
pub fn new(user: Arc<dyn GridUser>, database: Arc<dyn GridDatabase>) -> Self {
Self { user, database }
}
2022-08-16 11:37:34 +08:00
pub async fn run_v1_migration(&self, grid_id: &str) -> FlowyResult<()> {
2022-07-20 18:27:12 +08:00
let user_id = self.user.user_id()?;
2022-08-16 11:37:34 +08:00
let key = migration_flag_key(&user_id, V1_MIGRATION, grid_id);
if KV::get_bool(&key) {
return Ok(());
}
let _ = self.migration_grid_rev_struct(grid_id).await?;
tracing::trace!("Run grid:{} v1 migration", grid_id);
KV::set_bool(&key, true);
2022-07-20 18:27:12 +08:00
Ok(())
}
2022-08-16 11:37:34 +08:00
pub async fn migration_grid_rev_struct(&self, grid_id: &str) -> FlowyResult<()> {
let object = GridRevisionResettable {
2022-07-20 18:27:12 +08:00
grid_id: grid_id.to_owned(),
};
let user_id = self.user.user_id()?;
2022-08-16 11:37:34 +08:00
let pool = self.database.db_pool()?;
2022-07-20 18:27:12 +08:00
let disk_cache = SQLiteGridRevisionPersistence::new(&user_id, pool);
2022-08-16 11:37:34 +08:00
let reset = RevisionStructReset::new(&user_id, object, Arc::new(disk_cache));
reset.run().await
2022-07-20 18:27:12 +08:00
}
}
2022-08-16 11:37:34 +08:00
fn migration_flag_key(user_id: &str, version: &str, grid_id: &str) -> String {
md5(format!("{}{}{}", user_id, version, grid_id,))
}
pub struct GridRevisionResettable {
2022-07-20 18:27:12 +08:00
grid_id: String,
}
2022-08-16 11:37:34 +08:00
impl RevisionResettable for GridRevisionResettable {
fn target_id(&self) -> &str {
&self.grid_id
}
2022-07-20 18:27:12 +08:00
2022-08-16 11:37:34 +08:00
fn target_reset_rev_str(&self, revisions: Vec<Revision>) -> FlowyResult<String> {
let pad = GridRevisionPad::from_revisions(revisions)?;
let json = pad.json_str()?;
Ok(json)
2022-07-20 18:27:12 +08:00
}
2022-08-16 11:37:34 +08:00
fn default_target_rev_str(&self) -> FlowyResult<String> {
let grid_rev = GridRevision::default();
let json = make_grid_rev_json_str(&grid_rev)?;
Ok(json)
2022-07-20 18:27:12 +08:00
}
}