处理一下数据库

This commit is contained in:
shenjack 2024-07-20 00:36:11 +08:00
parent 54ce34cbf9
commit 8a7c18f5ee
Signed by: shenjack
GPG Key ID: 7B1134A979775551
14 changed files with 140 additions and 1168 deletions

33
Cargo.lock generated
View File

@ -341,9 +341,9 @@ checksum = "a12916984aab3fa6e39d655a33e09c0071eb36d6ab3aea5c2d78551f1df6d952"
[[package]]
name = "cc"
version = "1.1.5"
version = "1.1.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "324c74f2155653c90b04f25b2a47a8a631360cb908f92a772695f430c7e31052"
checksum = "2aba8f4e9906c7ce3c73463f62a7f0c65183ada1a2d47e397cc8810827f9694f"
[[package]]
name = "cfg-if"
@ -487,16 +487,6 @@ dependencies = [
"typenum",
]
[[package]]
name = "db_part"
version = "0.1.0"
dependencies = [
"anyhow",
"migration",
"sea-orm",
"tokio",
]
[[package]]
name = "der"
version = "0.7.9"
@ -2483,8 +2473,10 @@ dependencies = [
name = "sr_download"
version = "0.1.0"
dependencies = [
"db_part",
"anyhow",
"migration",
"reqwest",
"sea-orm",
"serde",
"tokio",
"toml",
@ -2605,15 +2597,6 @@ dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "tests"
version = "0.1.0"
dependencies = [
"anyhow",
"reqwest",
"tokio",
]
[[package]]
name = "thiserror"
version = "1.0.63"
@ -2807,7 +2790,7 @@ dependencies = [
"serde",
"serde_spanned",
"toml_datetime",
"winnow 0.6.13",
"winnow 0.6.14",
]
[[package]]
@ -3257,9 +3240,9 @@ dependencies = [
[[package]]
name = "winnow"
version = "0.6.13"
version = "0.6.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "59b5e5f6c299a3c7890b876a2a587f3115162487e704907d9b6cd29473052ba1"
checksum = "374ec40a2d767a3c1b4972d9475ecd557356637be906f2cb3f7fe17a6eb5e22f"
dependencies = [
"memchr",
]

View File

@ -1,3 +1,3 @@
[workspace]
resolver = "2"
members = ["sr_download", "migration", "db_part", "tests"]
members = ["sr_download", "migration"]

View File

@ -1,14 +0,0 @@
[package]
name = "db_part"
version = "0.1.0"
edition = "2021"
[dependencies]
migration = { path = "../migration" }
anyhow = { version = "1.0.86", features = ["backtrace"] }
sea-orm = { version = "0.12.15", features = [
"sqlx-postgres",
"runtime-tokio-rustls",
] }
tokio = { version = "1.38.1", features = ["full"] }

View File

@ -1,18 +0,0 @@
use anyhow::Ok;
use sea_orm::{ConnectOptions, Database};
use migration::{Migrator, MigratorTrait};
pub async fn init_db() -> anyhow::Result<()> {
let mut opt =
ConnectOptions::new("postgres://srdown:srdown@localhost:5432/srdown?currentSchema=srdown");
opt.max_connections(10)
.sqlx_logging(true)
.set_schema_search_path("srdown");
let db = Database::connect(opt).await?;
Migrator::up(&db, None).await?;
Ok(())
}

View File

@ -14,6 +14,6 @@ tokio = { version = "1.38", features = ["full"] }
[dependencies.sea-orm-migration]
version = "0.12.15"
features = [
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
"sqlx-postgres", # `DATABASE_DRIVER` feature
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
"sqlx-postgres", # `DATABASE_DRIVER` feature
]

1104
sr_download/Cargo.lock generated

File diff suppressed because it is too large Load Diff

View File

@ -8,6 +8,12 @@ reqwest = "0.12.5"
tokio = { version = "1.38.1", features = ["full"] }
toml = "0.8.15"
tracing = "0.1.40"
db_part = { path = "../db_part" }
serde = { version = "1.0.204", features = ["serde_derive"] }
anyhow = { version = "1.0.86", features = ["backtrace"] }
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
sea-orm = { version = "0.12.15", features = [
"sqlx-postgres",
"runtime-tokio-rustls",
] }
migration = { path = "../migration" }

View File

@ -1,9 +1,38 @@
use serde::Deserialize;
use std::path::Path;
#[derive(Deserialize)]
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize)]
pub struct ConfigFile {
pub db_url: String,
pub db_schema: String,
pub max_connections: u32,
pub sqlx_logging: bool,
pub schema_search_path: String,
}
impl Default for ConfigFile {
fn default() -> Self {
Self {
db_url: "postgres://srdown:srdown@192.168.3.22:10001/srdown?currentSchema=srdown"
.to_string(),
db_schema: "srdown".to_string(),
max_connections: 10,
sqlx_logging: true,
}
}
}
impl ConfigFile {
pub fn read_from_file(file_path: &Path) -> anyhow::Result<Self> {
let data = std::fs::read_to_string(file_path)?;
let config: ConfigFile = toml::from_str(&data)?;
Ok(config)
}
pub fn write_default_to_file(file_path: &Path) -> anyhow::Result<()> {
let config = ConfigFile::default();
let toml = toml::to_string(&config)?;
std::fs::write(file_path, toml)?;
Ok(())
}
}

26
sr_download/src/db.rs Normal file
View File

@ -0,0 +1,26 @@
use anyhow::Ok;
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
use migration::{Migrator, MigratorTrait};
pub struct DbConfig {
pub db_url: String,
pub db_schema: String,
pub max_connections: u32,
pub sqlx_logging: bool,
}
pub async fn init_db<C>(conf: C) -> anyhow::Result<DatabaseConnection>
where
C: Into<DbConfig>,
{
let conf: DbConfig = conf.into();
let mut opt = ConnectOptions::new(conf.db_url.clone());
opt.max_connections(conf.max_connections)
.sqlx_logging(conf.sqlx_logging)
.set_schema_search_path(conf.db_schema.clone());
let db = Database::connect(opt).await?;
Migrator::up(&db, None).await?;
Ok(db)
}

View File

@ -0,0 +1,17 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "long_data")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub save_id: i32,
pub len: String,
pub text: String,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -0,0 +1,20 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
use super::sea_orm_active_enums::SaveTypeEnum;
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "main_data")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub save_id: i32,
pub save_type: SaveTypeEnum,
pub blake_hash: String,
pub len: i32,
pub short_data: Option<String>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

View File

@ -0,0 +1,7 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
pub mod prelude;
pub mod long_data;
pub mod main_data;
pub mod sea_orm_active_enums;

View File

@ -0,0 +1,4 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
pub use super::long_data::Entity as LongData;
pub use super::main_data::Entity as MainData;

View File

@ -0,0 +1,16 @@
//! `SeaORM` Entity. Generated by sea-orm-codegen 0.12.15
use sea_orm::entity::prelude::*;
#[derive(Debug, Clone, PartialEq, Eq, EnumIter, DeriveActiveEnum)]
#[sea_orm(rs_type = "String", db_type = "Enum", enum_name = "save_type_enum")]
pub enum SaveTypeEnum {
#[sea_orm(string_value = "none")]
None,
#[sea_orm(string_value = "save")]
Save,
#[sea_orm(string_value = "ship")]
Ship,
#[sea_orm(string_value = "unknown")]
Unknown,
}