This commit is contained in:
shenjack 2024-10-23 22:24:01 +08:00
parent bc4e426b02
commit 5f3a501387
Signed by: shenjack
GPG Key ID: 7B1134A979775551
8 changed files with 458 additions and 394 deletions

754
Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
[package]
name = "sr_download"
version = "1.2.6"
version = "1.2.7"
edition = "2021"
default-run = "sr_download"

View File

@ -3,9 +3,10 @@ use sea_orm::{
ActiveModelTrait, ColumnTrait, ConnectionTrait, DatabaseConnection, EntityTrait,
IntoActiveModel, ModelTrait, QueryFilter, QuerySelect, Statement, TransactionTrait,
};
// use tracing::{event, Level};
use tracing::{event, Level};
use crate::model;
use crate::config::ConfigFile;
pub use crate::model::sea_orm_active_enums::SaveType;
use migration::{SaveId, FULL_DATA_VIEW, TEXT_DATA_MAX_LEN};
@ -16,6 +17,20 @@ pub mod utils;
pub use utils::{connect, connect_server, migrate};
pub async fn full_update(db: &DatabaseConnection, conf: &ConfigFile) {
// sea_orm 的迁移
if let Err(e) = migrate(db).await {
event!(Level::ERROR, "sea_orm 迁移失败: {:?}", e);
};
// 自己的迁移
updates::update_db(db, conf).await;
// 数据更新
utils::check_null_data(db).await;
utils::update_xml_tested(db).await;
}
#[allow(unused)]
#[derive(Debug, Clone)]
pub struct DbData {

View File

@ -1 +1,55 @@
use sea_orm::{ConnectionTrait, DatabaseBackend, DatabaseConnection, Statement, TransactionTrait};
use tracing::{event, Level};
use crate::config::ConfigFile;
pub mod pre_local {
use super::*;
use crate::db_part::defines::check_sea_orm_exists;
pub async fn try_merge(db: &DatabaseConnection, conf: &ConfigFile) {
event!(Level::INFO, "尝试从 sea_orm 表迁移数据");
if !check_sea_orm_exists(db, conf).await {
// 如果没有这个表, 那就说明已经是 merge 过了
event!(Level::DEBUG, "sea_orm 表不存在, 不需要迁移");
}
event!(Level::DEBUG, "sea_orm 表存在, 开始迁移");
// 先开个事物
let transaction = match db.begin().await {
Ok(t) => t,
Err(e) => {
event!(Level::ERROR, "无法开启事务: {:?}", e);
return;
}
};
// 迁移数据
// 提交事务
if let Err(e) = transaction.commit().await {
event!(Level::ERROR, "无法提交事务: {:?}", e);
}
}
}
pub async fn update_db(db: &DatabaseConnection, conf: &ConfigFile) {
event!(Level::INFO, "开始更新数据库");
// 开启全局事务
let global_transaction = match db.begin().await {
Ok(t) => t,
Err(e) => {
event!(Level::ERROR, "无法开启全局事务: {:?}", e);
return;
}
};
pre_local::try_merge(db, conf).await;
// 提交全局事务
if let Err(e) = global_transaction.commit().await {
event!(Level::ERROR, "无法提交全局事务, 更新失败: {:?}", e);
}
event!(Level::INFO, "更新完成");
}

View File

@ -79,9 +79,7 @@ pub async fn main(mut stop_receiver: Receiver<()>) -> anyhow::Result<()> {
let conf = config::ConfigFile::try_read()?;
let db_connect = db_part::connect(&conf).await?;
db_part::migrate(&db_connect).await?;
db_part::utils::check_null_data(&db_connect).await;
db_part::utils::update_xml_tested(&db_connect).await;
db_part::full_update(&db_connect, &conf).await;
tokio::time::sleep(std::time::Duration::from_secs(2)).await;

View File

@ -93,15 +93,10 @@ async fn async_main(run_mode: RunMode) -> anyhow::Result<()> {
stop_sender.send(()).unwrap();
});
let job_waiter;
match run_mode {
RunMode::Serve => {
job_waiter = tokio::spawn(serve_mode::main(stop_receiver));
}
RunMode::Fast => {
job_waiter = tokio::spawn(fast_mode::main(stop_receiver));
}
}
let job_waiter = match run_mode {
RunMode::Serve => tokio::spawn(serve_mode::main(stop_receiver)),
RunMode::Fast => tokio::spawn(fast_mode::main(stop_receiver)),
};
job_waiter.await??;
let _ = stop_waiter.await;
Ok(())

View File

@ -14,9 +14,7 @@ pub async fn main(mut stop_receiver: Receiver<()>) -> anyhow::Result<()> {
let conf = config::ConfigFile::try_read()?;
let db_connect = db_part::connect(&conf).await?;
db_part::migrate(&db_connect).await?;
db_part::utils::check_null_data(&db_connect).await;
db_part::utils::update_xml_tested(&db_connect).await;
db_part::full_update(&db_connect, &conf).await;
let mut db_max_id = db_part::search::max_id(&db_connect).await;
let mut web_waiter = None;

View File

@ -1,5 +1,9 @@
# v1.0
## 1.2.7
开始写自己的迁移脚本
## 1.2.6
1.3 会迁移到自己写的脚本的