diff --git a/migration/src/lib.rs b/migration/src/lib.rs index ddf6b1b..8b2fd7c 100644 --- a/migration/src/lib.rs +++ b/migration/src/lib.rs @@ -1,11 +1,14 @@ pub use sea_orm_migration::prelude::*; -mod m20240719_00001_create_main_data_table; -mod m20240719_00002_create_long_data_table; -mod m20240721_221623_create_indexs; +pub mod m20240719_00001_create_main_data_table; +pub mod m20240719_00002_create_long_data_table; +pub mod m20240721_221623_create_indexs; pub use m20240721_221623_create_indexs::FULL_DATA_VIEW; +pub const TEXT_DATA_MAX_LEN: usize = 1024; + +pub type SaveId = u32; pub struct Migrator; #[async_trait::async_trait] diff --git a/migration/src/m20240719_00001_create_main_data_table.rs b/migration/src/m20240719_00001_create_main_data_table.rs index 396e8e1..1e9d92a 100644 --- a/migration/src/m20240719_00001_create_main_data_table.rs +++ b/migration/src/m20240719_00001_create_main_data_table.rs @@ -2,6 +2,8 @@ use sea_orm::{EnumIter, Iterable}; use sea_orm_migration::prelude::extension::postgres::Type; use sea_orm_migration::prelude::*; +use crate::TEXT_DATA_MAX_LEN; + #[derive(DeriveMigrationName)] pub struct Migration; @@ -56,7 +58,7 @@ impl MigrationTrait for Migration { // len = 64 .col(ColumnDef::new(MainData::BlakeHash).char_len(64).not_null()) .col(ColumnDef::new(MainData::Len).big_integer().not_null()) - .col(ColumnDef::new(MainData::ShortData).string_len(1024)) + .col(ColumnDef::new(MainData::ShortData).string_len(TEXT_DATA_MAX_LEN as u32)) .to_owned(), ) .await?; diff --git a/sr_download/Cargo.toml b/sr_download/Cargo.toml index aa9efe9..55662d1 100644 --- a/sr_download/Cargo.toml +++ b/sr_download/Cargo.toml @@ -3,6 +3,10 @@ name = "sr_download" version = "0.1.0" edition = "2021" +[[bin]] +name = "get-save" +path = "./src/get-save.rs" + [dependencies] reqwest = "0.12.5" tokio = { version = "1.38.1", features = ["full"] } diff --git a/sr_download/src/config.rs b/sr_download/src/config.rs index e41a911..833ed0c 100644 --- a/sr_download/src/config.rs +++ b/sr_download/src/config.rs @@ -2,7 +2,7 @@ use std::path::Path; use serde::{Deserialize, Serialize}; -use crate::SaveId; +use migration::SaveId; #[derive(Serialize, Deserialize)] pub struct ConfigFile { diff --git a/sr_download/src/db_part.rs b/sr_download/src/db_part.rs index edaf7c2..36d5726 100644 --- a/sr_download/src/db_part.rs +++ b/sr_download/src/db_part.rs @@ -6,10 +6,10 @@ use sea_orm::{ }; use tracing::{event, Level}; +use crate::config::ConfigFile; +use crate::model; use crate::model::sea_orm_active_enums::SaveType; -use crate::{config::ConfigFile, SaveId}; -use crate::{model, TEXT_DATA_MAX_LEN}; -use migration::{Migrator, MigratorTrait, FULL_DATA_VIEW}; +use migration::{Migrator, MigratorTrait, SaveId, FULL_DATA_VIEW, TEXT_DATA_MAX_LEN}; #[derive(Debug, Clone)] pub struct DbData { @@ -83,12 +83,16 @@ pub async fn connect(conf: &ConfigFile) -> anyhow::Result { event!(Level::INFO, "Connecting to database"); let db: DatabaseConnection = Database::connect(opt).await?; db.ping().await?; - event!(Level::INFO, "Connected to database, starting migration"); - Migrator::up(&db, None).await?; - event!(Level::INFO, "Migration finished"); Ok(db) } +pub async fn migrate(db: &DatabaseConnection) -> anyhow::Result<()> { + event!(Level::INFO, "Starting migration"); + Migrator::up(db, None).await?; + event!(Level::INFO, "Migration finished"); + Ok(()) +} + pub async fn find_max_id(db: &DatabaseConnection) -> SaveId { // SELECT save_id from main_data ORDER BY save_id DESC LIMIT 1 // 我丢你老母, 有这时间写这个, 我都写完 sql 语句了 @@ -112,11 +116,22 @@ pub async fn find_max_id(db: &DatabaseConnection) -> SaveId { } } +#[allow(non_snake_case)] +pub fn SaveType_from_str(str: String) -> Option { + match str.to_lowercase().as_str() { + "save" => Some(SaveType::Save), + "ship" => Some(SaveType::Ship), + "none" => Some(SaveType::None), + "unknown" => Some(SaveType::Unknown), + _ => None + } +} + /// 直接从数据库中查询数据, 这里数据库已经准备好了根据长度区分过的数据 /// 可以从 full view 里直接选数据 pub async fn get_raw_data(save_id: SaveId, db: &DatabaseConnection) -> Option { let sql = format!( - "SELECT data, save_type, blake_hash FROM {} WHERE save_id = {}", + "SELECT data, save_type::text FROM {} WHERE save_id = {}", FULL_DATA_VIEW, save_id ); let datas = db @@ -126,9 +141,8 @@ pub async fn get_raw_data(save_id: SaveId, db: &DatabaseConnection) -> Option anyhow::Result<()> { + let config = config::ConfigFile::read_from_file(Path::new("config.toml")).unwrap(); + let db = db_part::connect(&config).await.unwrap(); + + let want_get_id = std::env::args().nth(1).ok_or(anyhow::anyhow!("No input"))?.parse::()?; + + let data = db_part::get_raw_data(want_get_id, &db).await.ok_or(anyhow::anyhow!("No data"))?; + + println!("{}", data.text.ok_or(anyhow::anyhow!("No text"))?); + + Ok(()) +} diff --git a/sr_download/src/main.rs b/sr_download/src/main.rs index 24222ba..621e2c6 100644 --- a/sr_download/src/main.rs +++ b/sr_download/src/main.rs @@ -10,12 +10,10 @@ mod db_part; mod model; mod net; -pub type SaveId = u32; -pub const TEXT_DATA_MAX_LEN: usize = 1024; - use model::sea_orm_active_enums::SaveType; use crate::db_part::CoverStrategy; +use migration::SaveId; async fn big_worker( db: sea_orm::DatabaseConnection, @@ -89,6 +87,7 @@ async fn main_works(mut stop_receiver: Receiver<()>) -> anyhow::Result<()> { }; let db_connect = db_part::connect(&conf).await?; + db_part::migrate(&db_connect).await?; let db_max_id = db_part::find_max_id(&db_connect).await; event!(