Encrypted peer-to-peer IM for data security. Own data, own privacy. (Rust+Flutter)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

203 lines
5.9 KiB

use std::path::PathBuf;
use tdn_storage::local::DStorage;
pub mod consensus;
mod account;
mod file;
mod service;
mod session;
use account::ACCOUNT_VERSIONS;
use consensus::CONSENSUS_VERSIONS;
use file::FILE_VERSIONS;
use service::SERVICE_VERSIONS;
use session::SESSION_VERSIONS;
use crate::apps::assistant::ASSISTANT_VERSIONS;
// Account's main database name.
pub(crate) const ACCOUNT_DB: &'static str = "account.db";
/// Account's consensus database name
pub(crate) const CONSENSUS_DB: &'static str = "consensus.db";
/// Account's session database name
pub(crate) const SESSION_DB: &'static str = "session.db";
/// Account's consensus database name
pub(crate) const FILE_DB: &'static str = "file.db";
/// Account's service database name
pub(crate) const SERVICE_DB: &'static str = "service.db";
/// Account's assistant database name
pub(crate) const ASSISTANT_DB: &'static str = "assistant.db";
pub(crate) fn main_migrate(path: &PathBuf) -> std::io::Result<()> {
let mut db_path = path.clone();
db_path.push(ACCOUNT_DB);
if db_path.exists() {
let db = DStorage::open(db_path)?;
// 1. get current version.
let first_matrix =
db.query("SELECT name FROM sqlite_master WHERE type='table' AND name='migrates'")?;
if first_matrix.len() == 0 {
// 2. migrate.
for i in &ACCOUNT_VERSIONS[1..] {
db.execute(i)?;
}
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
ACCOUNT_VERSIONS.len(),
ACCOUNT_DB,
))?;
}
let matrix = db.query("select db_name, version from migrates")?;
for mut values in matrix {
let db_version = values.pop().unwrap().as_i64() as usize;
let db_name = values.pop().unwrap().as_string();
let current_versions = match db_name.as_str() {
ACCOUNT_DB => {
if db_version != ACCOUNT_VERSIONS.len() {
// 2. migrate.
for i in &ACCOUNT_VERSIONS[db_version..] {
db.execute(i)?;
}
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
ACCOUNT_VERSIONS.len(),
db_name,
))?;
}
continue;
}
CONSENSUS_DB => CONSENSUS_VERSIONS.as_ref(),
SESSION_DB => SESSION_VERSIONS.as_ref(),
FILE_DB => FILE_VERSIONS.as_ref(),
SERVICE_DB => SERVICE_VERSIONS.as_ref(),
ASSISTANT_DB => ASSISTANT_VERSIONS.as_ref(),
_ => {
continue;
}
};
if db_version != current_versions.len() {
let mut matrix = db.query("select gid from accounts")?;
while matrix.len() > 0 {
let mut account_path = path.clone();
account_path.push(matrix.pop().unwrap().pop().unwrap().as_str());
account_path.push(&db_name);
let account_db = DStorage::open(account_path)?;
// migrate
for i in &current_versions[db_version..] {
account_db.execute(i)?;
}
account_db.close()?;
}
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
current_versions.len(),
db_name,
))?;
}
}
db.close()?;
} else {
let db = DStorage::open(db_path)?;
// migrate all.
for i in ACCOUNT_VERSIONS.iter() {
db.execute(i)?;
}
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
ACCOUNT_VERSIONS.len(),
ACCOUNT_DB,
))?;
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
CONSENSUS_VERSIONS.len(),
CONSENSUS_DB,
))?;
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
SESSION_VERSIONS.len(),
SESSION_DB,
))?;
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
FILE_VERSIONS.len(),
FILE_DB,
))?;
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
SERVICE_VERSIONS.len(),
SERVICE_DB,
))?;
db.update(&format!(
"UPDATE migrates SET version = {} where db_name = '{}'",
ASSISTANT_VERSIONS.len(),
ASSISTANT_DB,
))?;
db.close()?;
}
Ok(())
}
pub(crate) fn account_init_migrate(path: &PathBuf) -> std::io::Result<()> {
let mut db_path = path.clone();
db_path.push(CONSENSUS_DB);
let db = DStorage::open(db_path)?;
for i in &CONSENSUS_VERSIONS {
db.execute(i)?;
}
db.close()?;
let mut db_path = path.clone();
db_path.push(SESSION_DB);
let db = DStorage::open(db_path)?;
for i in &SESSION_VERSIONS {
db.execute(i)?;
}
db.close()?;
let mut db_path = path.clone();
db_path.push(FILE_DB);
let db = DStorage::open(db_path)?;
for i in &FILE_VERSIONS {
db.execute(i)?;
}
db.close()?;
let mut db_path = path.clone();
db_path.push(SERVICE_DB);
let db = DStorage::open(db_path)?;
for i in &SERVICE_VERSIONS {
db.execute(i)?;
}
db.close()?;
let mut db_path = path.clone();
db_path.push(ASSISTANT_DB);
let db = DStorage::open(db_path)?;
for i in &ASSISTANT_VERSIONS {
db.execute(i)?;
}
db.close()
}