Skip to content

Commit

Permalink
feat: 初步完成支持把sqlite数据转化为迁移中间数据功能 #138
Browse files Browse the repository at this point in the history
  • Loading branch information
heqingpan committed Oct 20, 2024
1 parent 57097eb commit 180a157
Show file tree
Hide file tree
Showing 8 changed files with 314 additions and 40 deletions.
8 changes: 8 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,12 @@ pub enum Commands {
/// out to sqlite db file
out: String,
},
/// sqlite to transfer middle data
#[command(arg_required_else_help = true)]
SqliteToData {
/// the sqlite db file
file: String,
/// out to transfer middle data file
out: String,
},
}
3 changes: 3 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,7 @@ impl ConfigUtils {
val
}
}
pub fn is_default_tenant(val: &str) -> bool {
val == DEFAULT_TENANT
}
}
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ use rnacos::common::appdata::AppShareData;
use rnacos::openapi::middle::auth_middle::ApiCheckAuth;
use rnacos::raft::NacosRaft;
use rnacos::transfer::data_to_sqlite::data_to_sqlite;
use rnacos::transfer::sqlite_to_data::sqlite_to_data;
use rnacos::web_config::{app_config, console_config};
//#[global_allocator]
//static GLOBAL: MiMalloc = MiMalloc;
Expand Down Expand Up @@ -144,6 +145,10 @@ async fn run_subcommand(commands: Commands) -> Result<(), Box<dyn Error>> {
log::info!("middle data to sqlite, from:{file} to:{out}");
data_to_sqlite(&file, &out).await?;
}
Commands::SqliteToData { file, out } => {
log::info!("sqlite to middle data, from:{file} to:{out}");
sqlite_to_data(&file, &out).await?;
}
}
Ok(())
}
Expand Down
43 changes: 3 additions & 40 deletions src/transfer/data_to_sqlite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,38 +8,10 @@ use crate::transfer::sqlite::dao::config::{ConfigDO, ConfigDao};
use crate::transfer::sqlite::dao::config_history::{ConfigHistoryDO, ConfigHistoryDao};
use crate::transfer::sqlite::dao::tenant::{TenantDO, TenantDao};
use crate::transfer::sqlite::dao::user::{UserDO, UserDao};
use crate::transfer::sqlite::TableSeq;
use crate::user::model::UserDo;
use rusqlite::Connection;

#[derive(Debug, Default)]
pub struct TableSeq {
pub(crate) config_id: i64,
pub(crate) config_history_id: i64,
pub(crate) tenant_id: i64,
pub(crate) user_id: i64,
}

impl TableSeq {
pub fn next_config_id(&mut self) -> i64 {
self.config_id += 1;
self.config_id
}

pub fn next_config_history_id(&mut self) -> i64 {
self.config_history_id += 1;
self.config_history_id
}

pub fn next_tenant_id(&mut self) -> i64 {
self.tenant_id += 1;
self.tenant_id
}
pub fn next_user_id(&mut self) -> i64 {
self.user_id += 1;
self.user_id
}
}

pub async fn data_to_sqlite(data_file: &str, db_path: &str) -> anyhow::Result<()> {
let mut file_reader = TransferFileReader::new(data_file).await?;
let conn = open_init_db(db_path)?;
Expand Down Expand Up @@ -138,17 +110,8 @@ fn insert_user(
record: TransferRecordRef<'_>,
) -> anyhow::Result<()> {
let value_do = UserDo::from_bytes(&record.value)?;
let user_do = UserDO {
id: Some(table_seq.next_user_id()),
username: Some(value_do.username),
nickname: Some(value_do.nickname),
password_hash: value_do.password_hash,
gmt_create: Some(value_do.gmt_create as i64),
gmt_modified: Some(value_do.gmt_modified as i64),
enabled: Some(value_do.enable.to_string()),
roles: Some(serde_json::to_string(&value_do.roles)?),
extend_info: Some(serde_json::to_string(&value_do.extend_info)?),
};
let mut user_do: UserDO = value_do.into();
user_do.id = Some(table_seq.next_user_id());
user_dao.insert(&user_do)?;
Ok(())
}
Expand Down
1 change: 1 addition & 0 deletions src/transfer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ pub mod data_to_sqlite;
pub mod model;
pub mod reader;
pub mod sqlite;
pub mod sqlite_to_data;
pub mod writer;
12 changes: 12 additions & 0 deletions src/transfer/sqlite/dao/config_history.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ impl ConfigHistoryDO {
#[derive(Debug, Default)]
pub struct ConfigHistoryParam {
pub id: Option<i64>,
pub data_id: Option<Arc<String>>,
pub group_id: Option<Arc<String>>,
pub tenant_id: Option<Arc<String>>,
pub limit: Option<i64>,
pub offset: Option<i64>,
}
Expand All @@ -51,6 +54,15 @@ impl ConfigHistorySql {
if let Some(id) = &param.id {
whr.eq("id", id);
}
if let Some(data_id) = &param.data_id {
whr.eq("data_id", data_id);
}
if let Some(group_id) = &param.group_id {
whr.eq("group_id", group_id);
}
if let Some(tenant_id) = &param.tenant_id {
whr.eq("tenant_id", tenant_id);
}
whr
}

Expand Down
81 changes: 81 additions & 0 deletions src/transfer/sqlite/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,83 @@
use crate::common::constant::EMPTY_STR;
use crate::transfer::sqlite::dao::user::UserDO;
use crate::user::model::UserDo;
use std::collections::HashMap;

/// 支持r-nacos导出中间数据文件与sqlite数据库相互转化的模块
pub mod dao;

#[derive(Debug, Default)]
pub struct TableSeq {
pub(crate) config_id: i64,
pub(crate) config_history_id: i64,
pub(crate) tenant_id: i64,
pub(crate) user_id: i64,
}

impl TableSeq {
pub fn next_config_id(&mut self) -> i64 {
self.config_id += 1;
self.config_id
}

pub fn next_config_history_id(&mut self) -> i64 {
self.config_history_id += 1;
self.config_history_id
}

pub fn next_tenant_id(&mut self) -> i64 {
self.tenant_id += 1;
self.tenant_id
}
pub fn next_user_id(&mut self) -> i64 {
self.user_id += 1;
self.user_id
}
}

impl From<UserDo> for UserDO {
fn from(value: UserDo) -> Self {
Self {
id: None,
username: Some(value.username),
nickname: Some(value.nickname),
password_hash: value.password_hash,
gmt_create: Some(value.gmt_create as i64),
gmt_modified: Some(value.gmt_modified as i64),
enabled: Some(value.enable.to_string()),
roles: serde_json::to_string(&value.roles).ok(),
extend_info: serde_json::to_string(&value.extend_info).ok(),
}
}
}

impl From<UserDO> for UserDo {
fn from(v: UserDO) -> Self {
let enable = if let Some(s) = &v.enabled {
s.parse().unwrap_or_default()
} else {
false
};
let roles = if let Some(s) = &v.roles {
serde_json::from_str(s).unwrap_or_default()
} else {
vec![]
};
let extend_info = if let Some(s) = &v.extend_info {
serde_json::from_str(s).unwrap_or_default()
} else {
HashMap::default()
};
Self {
username: v.username.unwrap_or_default(),
password: EMPTY_STR.to_string(),
nickname: v.nickname.unwrap_or_default(),
gmt_create: v.gmt_create.unwrap_or_default() as u32,
gmt_modified: v.gmt_modified.unwrap_or_default() as u32,
enable,
roles,
extend_info,
password_hash: v.password_hash,
}
}
}
Loading

0 comments on commit 180a157

Please sign in to comment.