准备自己写migration了

This commit is contained in:
shenjack 2024-08-30 12:21:19 +08:00
parent 3a5b43ae1e
commit cebaa3caee
Signed by: shenjack
GPG Key ID: 7B1134A979775551
7 changed files with 131 additions and 103 deletions

View File

@ -1,16 +1,18 @@
use blake3::Hasher; use blake3::Hasher;
use colored::Colorize;
use sea_orm::{ use sea_orm::{
ActiveModelTrait, ColumnTrait, ConnectOptions, ConnectionTrait, Database, DatabaseConnection, ActiveModelTrait, ColumnTrait, ConnectionTrait, DatabaseConnection, EntityTrait,
DbErr, EntityTrait, IntoActiveModel, ModelTrait, QueryFilter, QueryOrder, QuerySelect, IntoActiveModel, ModelTrait, QueryFilter, QuerySelect, Statement, TransactionTrait,
Statement, TransactionTrait,
}; };
use tracing::{event, Level}; use tracing::{event, Level};
use crate::config::ConfigFile;
use crate::model; use crate::model;
use crate::model::sea_orm_active_enums::SaveType; use crate::model::sea_orm_active_enums::SaveType;
use migration::{Migrator, MigratorTrait, SaveId, FULL_DATA_VIEW, TEXT_DATA_MAX_LEN}; use migration::{SaveId, FULL_DATA_VIEW, TEXT_DATA_MAX_LEN};
pub mod search;
pub mod utils;
pub use utils::{connect, connect_server, migrate};
#[allow(unused)] #[allow(unused)]
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
@ -79,96 +81,6 @@ impl DbData {
} }
} }
pub async fn connect(conf: &ConfigFile) -> anyhow::Result<DatabaseConnection> {
let mut opt = ConnectOptions::new(conf.db.url.clone());
opt.max_connections(conf.db.max_connections)
.set_schema_search_path(conf.db.schema.clone())
.sqlx_logging(conf.db.sqlx_logging);
event!(Level::INFO, "正在连接数据库");
let db: DatabaseConnection = Database::connect(opt).await?;
db.ping().await?;
event!(Level::INFO, "{}", "已经连接数据库".blue());
Ok(db)
}
pub async fn connect_server(conf: &ConfigFile) -> anyhow::Result<DatabaseConnection> {
let mut opt = ConnectOptions::new(conf.db.url.clone());
opt.max_connections(conf.serve.db_max_connect)
.set_schema_search_path(conf.db.schema.clone())
.sqlx_logging(conf.db.sqlx_logging);
event!(Level::INFO, "服务器正在连接数据库");
let db: DatabaseConnection = Database::connect(opt).await?;
db.ping().await?;
event!(Level::INFO, "{}", "服务器已经连接数据库".blue());
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(())
}
/// 找到最大的数据的 id
pub async fn find_max_id(db: &DatabaseConnection) -> SaveId {
// SELECT save_id from main_data ORDER BY save_id DESC LIMIT 1
// 我丢你老母, 有这时间写这个, 我都写完 sql 语句了
let data: Result<Option<i32>, DbErr> = model::main_data::Entity::find()
.order_by_desc(model::main_data::Column::SaveId)
.filter(model::main_data::Column::Len.gt(0))
.filter(model::main_data::Column::SaveType.ne(SaveType::None))
.select_only()
.column(model::main_data::Column::SaveId)
.limit(1)
.into_tuple()
.one(db)
.await;
match data {
Ok(model) => match model {
Some(model) => model as SaveId,
None => 0,
},
Err(e) => {
event!(Level::WARN, "Error when find_max_id: {:?}", e);
0
}
}
}
/// 找到最大的存档
pub async fn find_max_save(db: &DatabaseConnection) -> Option<DbData> {
// SELECT * from main_data ORDER BY save_id DESC WHERE save_type = save LIMIT 1
let data = model::main_data::Entity::find()
.order_by_desc(model::main_data::Column::SaveId)
.filter(model::main_data::Column::SaveType.eq(SaveType::Save))
.one(db)
.await;
match data {
Ok(model) => model.map(|model| model.into()),
Err(e) => {
event!(Level::WARN, "Error when find_max_save: {:?}", e);
None
}
}
}
/// 找到最大的飞船
pub async fn find_max_ship(db: &DatabaseConnection) -> Option<DbData> {
// SELECT * from main_data ORDER BY save_id DESC WHERE save_type = ship LIMIT 1
let data = model::main_data::Entity::find()
.order_by_desc(model::main_data::Column::SaveId)
.filter(model::main_data::Column::SaveType.eq(SaveType::Ship))
.one(db)
.await;
match data {
Ok(model) => model.map(|model| model.into()),
Err(e) => {
event!(Level::WARN, "Error when find_max_ship: {:?}", e);
None
}
}
}
/// 直接从数据库中查询数据, 这里数据库已经准备好了根据长度区分过的数据 /// 直接从数据库中查询数据, 这里数据库已经准备好了根据长度区分过的数据
/// 可以从 full view 里直接选数据 /// 可以从 full view 里直接选数据
@ -303,6 +215,7 @@ where
blake_hash: hash, blake_hash: hash,
len: data_len as i64, len: data_len as i64,
short_data: None, short_data: None,
xml_tested: None,
}; };
let long_data = model::long_data::Model { let long_data = model::long_data::Model {
save_id: save_id as i32, save_id: save_id as i32,
@ -321,6 +234,7 @@ where
blake_hash: hash, blake_hash: hash,
len: data_len as i64, len: data_len as i64,
short_data: Some(data), short_data: Some(data),
xml_tested: None,
}; };
new_data.into_active_model().insert(db).await?; new_data.into_active_model().insert(db).await?;
} }

View File

@ -0,0 +1,68 @@
use sea_orm::{ColumnTrait, DatabaseConnection, DbErr, EntityTrait, QueryFilter, QueryOrder, QuerySelect};
use tracing::{event, Level};
use crate::model;
use crate::model::sea_orm_active_enums::SaveType;
use migration::SaveId;
use super::DbData;
/// 找到最大的数据的 id
pub async fn max_id(db: &DatabaseConnection) -> SaveId {
// SELECT save_id from main_data ORDER BY save_id DESC LIMIT 1
// 我丢你老母, 有这时间写这个, 我都写完 sql 语句了
let data: Result<Option<i32>, DbErr> = model::main_data::Entity::find()
.order_by_desc(model::main_data::Column::SaveId)
.filter(model::main_data::Column::Len.gt(0))
.filter(model::main_data::Column::SaveType.ne(SaveType::None))
.select_only()
.column(model::main_data::Column::SaveId)
.limit(1)
.into_tuple()
.one(db)
.await;
match data {
Ok(model) => match model {
Some(model) => model as SaveId,
None => 0,
},
Err(e) => {
event!(Level::WARN, "Error when find_max_id: {:?}", e);
0
}
}
}
/// 找到最大的存档
pub async fn max_save(db: &DatabaseConnection) -> Option<DbData> {
// SELECT * from main_data ORDER BY save_id DESC WHERE save_type = save LIMIT 1
let data = model::main_data::Entity::find()
.order_by_desc(model::main_data::Column::SaveId)
.filter(model::main_data::Column::SaveType.eq(SaveType::Save))
.one(db)
.await;
match data {
Ok(model) => model.map(|model| model.into()),
Err(e) => {
event!(Level::WARN, "Error when find_max_save: {:?}", e);
None
}
}
}
/// 找到最大的飞船
pub async fn max_ship(db: &DatabaseConnection) -> Option<DbData> {
// SELECT * from main_data ORDER BY save_id DESC WHERE save_type = ship LIMIT 1
let data = model::main_data::Entity::find()
.order_by_desc(model::main_data::Column::SaveId)
.filter(model::main_data::Column::SaveType.eq(SaveType::Ship))
.one(db)
.await;
match data {
Ok(model) => model.map(|model| model.into()),
Err(e) => {
event!(Level::WARN, "Error when find_max_ship: {:?}", e);
None
}
}
}

View File

@ -0,0 +1,37 @@
use colored::Colorize;
use sea_orm::{ConnectOptions, Database, DatabaseConnection};
use tracing::{event, Level};
use crate::config::ConfigFile;
use migration::{Migrator, MigratorTrait};
pub async fn connect(conf: &ConfigFile) -> anyhow::Result<DatabaseConnection> {
let mut opt = ConnectOptions::new(conf.db.url.clone());
opt.max_connections(conf.db.max_connections)
.set_schema_search_path(conf.db.schema.clone())
.sqlx_logging(conf.db.sqlx_logging);
event!(Level::INFO, "正在连接数据库");
let db: DatabaseConnection = Database::connect(opt).await?;
db.ping().await?;
event!(Level::INFO, "{}", "已经连接数据库".blue());
Ok(db)
}
pub async fn connect_server(conf: &ConfigFile) -> anyhow::Result<DatabaseConnection> {
let mut opt = ConnectOptions::new(conf.db.url.clone());
opt.max_connections(conf.serve.db_max_connect)
.set_schema_search_path(conf.db.schema.clone())
.sqlx_logging(conf.db.sqlx_logging);
event!(Level::INFO, "服务器正在连接数据库");
let db: DatabaseConnection = Database::connect(opt).await?;
db.ping().await?;
event!(Level::INFO, "{}", "服务器已经连接数据库".blue());
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(())
}

View File

@ -85,7 +85,7 @@ async fn serve_mode(mut stop_receiver: Receiver<()>) -> anyhow::Result<()> {
let db_connect = db_part::connect(&conf).await?; let db_connect = db_part::connect(&conf).await?;
db_part::migrate(&db_connect).await?; db_part::migrate(&db_connect).await?;
let mut db_max_id = db_part::find_max_id(&db_connect).await; let mut db_max_id = db_part::search::max_id(&db_connect).await;
let mut web_waiter = None; let mut web_waiter = None;
if conf.serve.enable { if conf.serve.enable {

View File

@ -0,0 +1,9 @@
# 数据格式部分
生成的, 别动
生成新的:
```powershell
sea-orm-cli generate entity --database-url
```

View File

@ -195,10 +195,10 @@ async fn jump_to_dashboard_from_root() -> impl IntoResponse {
const INFO_PAGE: &str = include_str!("info.html"); const INFO_PAGE: &str = include_str!("info.html");
async fn dashboard_page(State(db): State<DatabaseConnection>) -> Html<String> { async fn dashboard_page(State(db): State<DatabaseConnection>) -> Html<String> {
let max_id = db_part::find_max_id(&db).await; let max_id = db_part::search::max_id(&db).await;
let max_id_data = db_part::get_raw_data(max_id, &db).await.unwrap(); let max_id_data = db_part::get_raw_data(max_id, &db).await.unwrap();
let max_ship = db_part::find_max_ship(&db).await; let max_ship = db_part::search::max_ship(&db).await;
let max_save = db_part::find_max_save(&db).await; let max_save = db_part::search::max_save(&db).await;
let mut page_content = INFO_PAGE let mut page_content = INFO_PAGE
.replace("|MAX_ID|", &max_id_data.save_id.to_string()) .replace("|MAX_ID|", &max_id_data.save_id.to_string())

View File

@ -11,7 +11,7 @@ pub trait FromDb {
impl FromDb for LastData { impl FromDb for LastData {
async fn from_db(db: &DatabaseConnection) -> Option<Self> { async fn from_db(db: &DatabaseConnection) -> Option<Self> {
let id = db_part::find_max_id(db).await; let id = db_part::search::max_id(db).await;
let data = db_part::get_raw_data(id, db).await?; let data = db_part::get_raw_data(id, db).await?;
Some(Self { Some(Self {
save_id: data.save_id, save_id: data.save_id,
@ -24,7 +24,7 @@ impl FromDb for LastData {
impl FromDb for LastSave { impl FromDb for LastSave {
async fn from_db(db: &DatabaseConnection) -> Option<Self> { async fn from_db(db: &DatabaseConnection) -> Option<Self> {
let data = db_part::find_max_save(db).await?; let data = db_part::search::max_save(db).await?;
Some(Self { Some(Self {
save_id: data.save_id, save_id: data.save_id,
len: data.len, len: data.len,
@ -35,7 +35,7 @@ impl FromDb for LastSave {
impl FromDb for LastShip { impl FromDb for LastShip {
async fn from_db(db: &DatabaseConnection) -> Option<Self> { async fn from_db(db: &DatabaseConnection) -> Option<Self> {
let data = db_part::find_max_ship(db).await?; let data = db_part::search::max_ship(db).await?;
Some(Self { Some(Self {
save_id: data.save_id, save_id: data.save_id,
len: data.len, len: data.len,