From 36574b6d68e4cbc2aec9c32a58ccf762046a0ef1 Mon Sep 17 00:00:00 2001 From: kould <2435992353@qq.com> Date: Wed, 13 Dec 2023 15:19:52 +0800 Subject: [PATCH] fix: dao bugs --- .env | 3 + .env.template | 3 + .gitignore | 1 + Cargo.toml | 5 ++ migration/README.md | 2 +- .../src/m20220101_000001_create_table.rs | 34 +++++----- src/entity/{dialog_2_kb.rs => dialog2_kb.rs} | 3 +- src/entity/dialog_info.rs | 12 ++-- src/entity/{doc_2_doc.rs => doc2_doc.rs} | 3 +- src/entity/doc_info.rs | 18 +++--- src/entity/{kb_2_doc.rs => kb2_doc.rs} | 3 +- src/entity/kb_info.rs | 16 +++-- src/entity/mod.rs | 18 +++--- src/entity/{tag_2_doc.rs => tag2_doc.rs} | 3 +- src/entity/tag_info.rs | 10 ++- src/entity/user_info.rs | 2 - src/main.rs | 63 +++++++++++++++---- src/service/dialog_info.rs | 26 ++++++++ src/service/mod.rs | 1 + 19 files changed, 149 insertions(+), 77 deletions(-) create mode 100644 .env create mode 100644 .env.template rename src/entity/{dialog_2_kb.rs => dialog2_kb.rs} (94%) rename src/entity/{doc_2_doc.rs => doc2_doc.rs} (94%) rename src/entity/{kb_2_doc.rs => kb2_doc.rs} (94%) rename src/entity/{tag_2_doc.rs => tag2_doc.rs} (94%) create mode 100644 src/service/dialog_info.rs diff --git a/.env b/.env new file mode 100644 index 000000000..a033ac29a --- /dev/null +++ b/.env @@ -0,0 +1,3 @@ +HOST=127.0.0.1 +PORT=8000 +DATABASE_URL="postgresql://infiniflow:infiniflow@localhost/docgpt" \ No newline at end of file diff --git a/.env.template b/.env.template new file mode 100644 index 000000000..a033ac29a --- /dev/null +++ b/.env.template @@ -0,0 +1,3 @@ +HOST=127.0.0.1 +PORT=8000 +DATABASE_URL="postgresql://infiniflow:infiniflow@localhost/docgpt" \ No newline at end of file diff --git a/.gitignore b/.gitignore index 1a48c9228..da300c870 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,4 @@ Cargo.lock *.pdb .idea/ +.env diff --git a/Cargo.toml b/Cargo.toml index 779323c7b..05d060a5e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,9 +8,14 @@ edition = "2021" [dependencies] actix-web = "4.3.1" actix-rt = "2.8.0" +actix-files = "0.6.2" postgres = "0.19.7" sea-orm = {version = "0.12.9", features = ["sqlx-postgres", "runtime-tokio-native-tls", "macros"]} serde = { version = "1", features = ["derive"] } +tracing-subscriber = "0.3.18" +dotenvy = "0.15.7" +listenfd = "1.0.1" +migration = { path = "./migration" } [[bin]] name = "doc_gpt" diff --git a/migration/README.md b/migration/README.md index 3b438d89e..83c18d5d5 100644 --- a/migration/README.md +++ b/migration/README.md @@ -1,4 +1,4 @@ -# Running Migrator CLI + # Running Migrator CLI - Generate a new migration file ```sh diff --git a/migration/src/m20220101_000001_create_table.rs b/migration/src/m20220101_000001_create_table.rs index e2dfd14b7..5e8721c89 100644 --- a/migration/src/m20220101_000001_create_table.rs +++ b/migration/src/m20220101_000001_create_table.rs @@ -61,8 +61,7 @@ impl MigrationTrait for Migration { .table(Tag2Doc::Table) .if_not_exists() .col(ColumnDef::new(Tag2Doc::TagId).big_integer()) - .col(ColumnDef::new(Tag2Doc::Did).big_integer().comment("doc id, did in docinfo")) - .index(Index::create().col(Tag2Doc::TagId)) + .col(ColumnDef::new(Tag2Doc::Did).big_integer()) .to_owned(), ) .await?; @@ -73,8 +72,7 @@ impl MigrationTrait for Migration { .table(Kb2Doc::Table) .if_not_exists() .col(ColumnDef::new(Kb2Doc::KbId).big_integer()) - .col(ColumnDef::new(Kb2Doc::Did).big_integer().comment("doc id, did in docinfo")) - .index(Index::create().col(Kb2Doc::KbId)) + .col(ColumnDef::new(Kb2Doc::Did).big_integer()) .to_owned(), ) .await?; @@ -86,7 +84,6 @@ impl MigrationTrait for Migration { .if_not_exists() .col(ColumnDef::new(Dialog2Kb::DialogId).big_integer()) .col(ColumnDef::new(Dialog2Kb::KbId).big_integer()) - .index(Index::create().col(Dialog2Kb::DialogId)) .to_owned(), ) .await?; @@ -96,9 +93,8 @@ impl MigrationTrait for Migration { Table::create() .table(Doc2Doc::Table) .if_not_exists() - .col(ColumnDef::new(Doc2Doc::ParentId).big_integer().comment("doc id, did in docinfo")) - .col(ColumnDef::new(Doc2Doc::Did).big_integer().comment("doc id, did in docinfo")) - .index(Index::create().col(Doc2Doc::ParentId)) + .col(ColumnDef::new(Doc2Doc::ParentId).big_integer()) + .col(ColumnDef::new(Doc2Doc::Did).big_integer()) .to_owned(), ) .await?; @@ -108,15 +104,16 @@ impl MigrationTrait for Migration { Table::create() .table(KbInfo::Table) .if_not_exists() - .col(ColumnDef::new(KbInfo::KbId).big_integer().auto_increment().not_null()) + .col(ColumnDef::new(KbInfo::KbId).big_integer() + .auto_increment() + .not_null() + .primary_key()) .col(ColumnDef::new(KbInfo::Uid).big_integer().not_null()) .col(ColumnDef::new(KbInfo::KbName).string().not_null()) .col(ColumnDef::new(KbInfo::Icon).big_integer().default(1)) .col(ColumnDef::new(KbInfo::CreatedAt).date().not_null()) .col(ColumnDef::new(KbInfo::UpdatedAt).date().not_null()) .col(ColumnDef::new(KbInfo::IsDeleted).boolean().default(false)) - .index(Index::create().col(KbInfo::KbId)) - .index(Index::create().col(KbInfo::Uid)) .to_owned(), ) .await?; @@ -126,7 +123,10 @@ impl MigrationTrait for Migration { Table::create() .table(DocInfo::Table) .if_not_exists() - .col(ColumnDef::new(DocInfo::Did).big_integer().auto_increment().not_null()) + .col(ColumnDef::new(DocInfo::Did).big_integer() + .not_null() + .auto_increment() + .primary_key()) .col(ColumnDef::new(DocInfo::Uid).big_integer().not_null()) .col(ColumnDef::new(DocInfo::DocName).string().not_null()) .col(ColumnDef::new(DocInfo::Size).big_integer().not_null()) @@ -135,8 +135,6 @@ impl MigrationTrait for Migration { .col(ColumnDef::new(DocInfo::CreatedAt).date().not_null()) .col(ColumnDef::new(DocInfo::UpdatedAt).date().not_null()) .col(ColumnDef::new(DocInfo::IsDeleted).boolean().default(false)) - .index(Index::create().col(DocInfo::Did)) - .index(Index::create().col(DocInfo::Uid)) .to_owned(), ) .await?; @@ -146,15 +144,17 @@ impl MigrationTrait for Migration { Table::create() .table(DialogInfo::Table) .if_not_exists() - .col(ColumnDef::new(DialogInfo::DialogId).big_integer().auto_increment().not_null()) + .col(ColumnDef::new(DialogInfo::DialogId) + .big_integer() + .not_null() + .auto_increment() + .primary_key()) .col(ColumnDef::new(DialogInfo::Uid).big_integer().not_null()) .col(ColumnDef::new(DialogInfo::DialogName).string().not_null()) .col(ColumnDef::new(DialogInfo::History).string().comment("json")) .col(ColumnDef::new(DialogInfo::CreatedAt).date().not_null()) .col(ColumnDef::new(DialogInfo::UpdatedAt).date().not_null()) .col(ColumnDef::new(DialogInfo::IsDeleted).boolean().default(false)) - .index(Index::create().col(DialogInfo::DialogId)) - .index(Index::create().col(DialogInfo::Uid)) .to_owned(), ) .await?; diff --git a/src/entity/dialog_2_kb.rs b/src/entity/dialog2_kb.rs similarity index 94% rename from src/entity/dialog_2_kb.rs rename to src/entity/dialog2_kb.rs index c4cbf734c..18420f9eb 100644 --- a/src/entity/dialog_2_kb.rs +++ b/src/entity/dialog2_kb.rs @@ -2,9 +2,10 @@ use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)] -#[sea_orm(table_name = "dialog_2_kb")] +#[sea_orm(table_name = "dialog2_kb")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] + #[sea_orm(index)] pub dialog_id: i64, #[sea_orm(primary_key, auto_increment = false)] pub kb_id: i64, diff --git a/src/entity/dialog_info.rs b/src/entity/dialog_info.rs index d426b957f..d0c49359b 100644 --- a/src/entity/dialog_info.rs +++ b/src/entity/dialog_info.rs @@ -6,15 +6,13 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub dialog_id: i64, - #[sea_orm(primary_key, auto_increment = false)] + #[sea_orm(index)] pub uid: i64, pub dialog_name: String, pub history: String, - pub created_at: DateTimeWithTimeZone, - pub updated_at: DateTimeWithTimeZone, - #[sea_orm(soft_delete_column)] - pub is_deleted: bool, + pub created_at: Date, + pub updated_at: Date, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -22,11 +20,11 @@ pub enum Relation {} impl Related for Entity { fn to() -> RelationDef { - super::dialog_2_kb::Relation::KbInfo.def() + super::dialog2_kb::Relation::KbInfo.def() } fn via() -> Option { - Some(super::dialog_2_kb::Relation::DialogInfo.def().rev()) + Some(super::dialog2_kb::Relation::DialogInfo.def().rev()) } } diff --git a/src/entity/doc_2_doc.rs b/src/entity/doc2_doc.rs similarity index 94% rename from src/entity/doc_2_doc.rs rename to src/entity/doc2_doc.rs index a1c0b4595..f987a2d55 100644 --- a/src/entity/doc_2_doc.rs +++ b/src/entity/doc2_doc.rs @@ -2,9 +2,10 @@ use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)] -#[sea_orm(table_name = "doc_2_doc")] +#[sea_orm(table_name = "doc2_doc")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] + #[sea_orm(index)] pub parent_id: i64, #[sea_orm(primary_key, auto_increment = false)] pub did: i64, diff --git a/src/entity/doc_info.rs b/src/entity/doc_info.rs index f7c9ae436..4e5641b20 100644 --- a/src/entity/doc_info.rs +++ b/src/entity/doc_info.rs @@ -6,7 +6,7 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub did: i64, - #[sea_orm(primary_key, auto_increment = false)] + #[sea_orm(index)] pub uid: i64, pub doc_name: String, pub size: i64, @@ -14,8 +14,8 @@ pub struct Model { pub r#type: String, pub kb_progress: f64, - pub created_at: DateTimeWithTimeZone, - pub updated_at: DateTimeWithTimeZone, + pub created_at: Date, + pub updated_at: Date, #[sea_orm(soft_delete_column)] pub is_deleted: bool, } @@ -25,31 +25,31 @@ pub enum Relation {} impl Related for Entity { fn to() -> RelationDef { - super::tag_2_doc::Relation::Tag.def() + super::tag2_doc::Relation::Tag.def() } fn via() -> Option { - Some(super::tag_2_doc::Relation::DocInfo.def().rev()) + Some(super::tag2_doc::Relation::DocInfo.def().rev()) } } impl Related for Entity { fn to() -> RelationDef { - super::kb_2_doc::Relation::KbInfo.def() + super::kb2_doc::Relation::KbInfo.def() } fn via() -> Option { - Some(super::kb_2_doc::Relation::DocInfo.def().rev()) + Some(super::kb2_doc::Relation::DocInfo.def().rev()) } } impl Related for Entity { fn to() -> RelationDef { - super::doc_2_doc::Relation::Parent.def() + super::doc2_doc::Relation::Parent.def() } fn via() -> Option { - Some(super::doc_2_doc::Relation::Child.def().rev()) + Some(super::doc2_doc::Relation::Child.def().rev()) } } diff --git a/src/entity/kb_2_doc.rs b/src/entity/kb2_doc.rs similarity index 94% rename from src/entity/kb_2_doc.rs rename to src/entity/kb2_doc.rs index a6406e3f1..f9d923430 100644 --- a/src/entity/kb_2_doc.rs +++ b/src/entity/kb2_doc.rs @@ -2,9 +2,10 @@ use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)] -#[sea_orm(table_name = "kb_2_doc")] +#[sea_orm(table_name = "kb2_doc")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] + #[sea_orm(index)] pub kb_id: i64, #[sea_orm(primary_key, auto_increment = false)] pub uid: i64, diff --git a/src/entity/kb_info.rs b/src/entity/kb_info.rs index 1ad166987..11cb3b2c2 100644 --- a/src/entity/kb_info.rs +++ b/src/entity/kb_info.rs @@ -6,15 +6,13 @@ use serde::{Deserialize, Serialize}; pub struct Model { #[sea_orm(primary_key, auto_increment = false)] pub kb_id: i64, - #[sea_orm(primary_key, auto_increment = false)] + #[sea_orm(index)] pub uid: i64, pub kn_name: String, pub icon: i64, - pub created_at: DateTimeWithTimeZone, - pub updated_at: DateTimeWithTimeZone, - #[sea_orm(soft_delete_column)] - pub is_deleted: bool, + pub created_at: Date, + pub updated_at: Date, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -22,21 +20,21 @@ pub enum Relation {} impl Related for Entity { fn to() -> RelationDef { - super::kb_2_doc::Relation::DocInfo.def() + super::kb2_doc::Relation::DocInfo.def() } fn via() -> Option { - Some(super::kb_2_doc::Relation::KbInfo.def().rev()) + Some(super::kb2_doc::Relation::KbInfo.def().rev()) } } impl Related for Entity { fn to() -> RelationDef { - super::dialog_2_kb::Relation::DialogInfo.def() + super::dialog2_kb::Relation::DialogInfo.def() } fn via() -> Option { - Some(super::dialog_2_kb::Relation::KbInfo.def().rev()) + Some(super::dialog2_kb::Relation::KbInfo.def().rev()) } } diff --git a/src/entity/mod.rs b/src/entity/mod.rs index 4189e394b..bc48fdc16 100644 --- a/src/entity/mod.rs +++ b/src/entity/mod.rs @@ -1,9 +1,9 @@ -mod user_info; -mod tag_info; -mod tag_2_doc; -mod kb_2_doc; -mod dialog_2_kb; -mod doc_2_doc; -mod kb_info; -mod doc_info; -mod dialog_info; \ No newline at end of file +pub(crate) mod user_info; +pub(crate) mod tag_info; +mod tag2_doc; +mod kb2_doc; +mod dialog2_kb; +mod doc2_doc; +pub(crate) mod kb_info; +pub(crate) mod doc_info; +pub(crate) mod dialog_info; \ No newline at end of file diff --git a/src/entity/tag_2_doc.rs b/src/entity/tag2_doc.rs similarity index 94% rename from src/entity/tag_2_doc.rs rename to src/entity/tag2_doc.rs index 5dba17714..a4bcddca4 100644 --- a/src/entity/tag_2_doc.rs +++ b/src/entity/tag2_doc.rs @@ -2,9 +2,10 @@ use sea_orm::entity::prelude::*; use serde::{Deserialize, Serialize}; #[derive(Clone, Debug, PartialEq, Eq, DeriveEntityModel, Deserialize, Serialize)] -#[sea_orm(table_name = "tag_2_doc")] +#[sea_orm(table_name = "tag2_doc")] pub struct Model { #[sea_orm(primary_key, auto_increment = false)] + #[sea_orm(index)] pub tag_id: i64, #[sea_orm(primary_key, auto_increment = false)] pub uid: i64, diff --git a/src/entity/tag_info.rs b/src/entity/tag_info.rs index 4aacddb89..71a6f01f1 100644 --- a/src/entity/tag_info.rs +++ b/src/entity/tag_info.rs @@ -13,10 +13,8 @@ pub struct Model { pub icon: i64, pub dir: String, - pub created_at: DateTimeWithTimeZone, - pub updated_at: DateTimeWithTimeZone, - #[sea_orm(soft_delete_column)] - pub is_deleted: bool, + pub created_at: Date, + pub updated_at: Date, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] @@ -24,11 +22,11 @@ pub enum Relation {} impl Related for Entity { fn to() -> RelationDef { - super::tag_2_doc::Relation::DocInfo.def() + super::tag2_doc::Relation::DocInfo.def() } fn via() -> Option { - Some(super::tag_2_doc::Relation::Tag.def().rev()) + Some(super::tag2_doc::Relation::Tag.def().rev()) } } diff --git a/src/entity/user_info.rs b/src/entity/user_info.rs index d791c08b1..464d33e63 100644 --- a/src/entity/user_info.rs +++ b/src/entity/user_info.rs @@ -16,8 +16,6 @@ pub struct Model { pub created_at: DateTimeWithTimeZone, pub updated_at: DateTimeWithTimeZone, - #[sea_orm(soft_delete_column)] - pub is_deleted: bool, } #[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)] diff --git a/src/main.rs b/src/main.rs index 6ef93cea5..84a9db010 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,22 +2,59 @@ mod api; mod entity; mod service; -use actix_web::{get, web, App, HttpServer, Responder}; +use std::env; +use actix_files::Files; +use actix_web::{web, App, HttpServer, Responder, middleware}; +use listenfd::ListenFd; +use sea_orm::{Database, DatabaseConnection}; +use migration::{Migrator, MigratorTrait}; -#[get("/")] -async fn index() -> impl Responder { - "Hello, World!" -} - -#[get("/{name}")] -async fn hello(name: web::Path) -> impl Responder { - format!("Hello {}!", &name) +#[derive(Debug, Clone)] +struct AppState { + conn: DatabaseConnection, } #[actix_web::main] async fn main() -> std::io::Result<()> { - HttpServer::new(|| App::new().service(index).service(hello)) - .bind(("127.0.0.1", 9090))? - .run() - .await + std::env::set_var("RUST_LOG", "debug"); + tracing_subscriber::fmt::init(); + + // get env vars + dotenvy::dotenv().ok(); + let db_url = env::var("DATABASE_URL").expect("DATABASE_URL is not set in .env file"); + let host = env::var("HOST").expect("HOST is not set in .env file"); + let port = env::var("PORT").expect("PORT is not set in .env file"); + let server_url = format!("{host}:{port}"); + + // establish connection to database and apply migrations + // -> create post table if not exists + let conn = Database::connect(&db_url).await.unwrap(); + Migrator::up(&conn, None).await.unwrap(); + + let state = AppState { conn }; + + // create server and try to serve over socket if possible + let mut listenfd = ListenFd::from_env(); + let mut server = HttpServer::new(move || { + App::new() + .service(Files::new("/static", "./static")) + .app_data(web::Data::new(state.clone())) + .wrap(middleware::Logger::default()) + .configure(init) + }); + + server = match listenfd.take_tcp_listener(0)? { + Some(listener) => server.listen(listener)?, + None => server.bind(&server_url)?, + }; + + println!("Starting server at {server_url}"); + server.run().await?; + + Ok(()) +} + +fn init(cfg: &mut web::ServiceConfig) { + // cfg.service(index); + // cfg.service(hello); } \ No newline at end of file diff --git a/src/service/dialog_info.rs b/src/service/dialog_info.rs new file mode 100644 index 000000000..b2f9f9d30 --- /dev/null +++ b/src/service/dialog_info.rs @@ -0,0 +1,26 @@ +use sea_orm::{DbConn, DbErr, EntityTrait, PaginatorTrait, QueryOrder}; +use crate::entity::dialog_info; +use crate::entity::dialog_info::Entity; + +pub struct Query; + +impl Query { + pub async fn find_dialog_info_by_id(db: &DbConn, id: i64) -> Result, DbErr> { + Entity::find_by_id(id).one(db).await + } + + pub async fn find_dialog_infos_in_page( + db: &DbConn, + page: u64, + posts_per_page: u64, + ) -> Result<(Vec, u64), DbErr> { + // Setup paginator + let paginator = Entity::find() + .order_by_asc(dialog_info::Column::DialogId) + .paginate(db, posts_per_page); + let num_pages = paginator.num_pages().await?; + + // Fetch paginated posts + paginator.fetch_page(page - 1).await.map(|p| (p, num_pages)) + } +} \ No newline at end of file diff --git a/src/service/mod.rs b/src/service/mod.rs index e69de29bb..58e1c1476 100644 --- a/src/service/mod.rs +++ b/src/service/mod.rs @@ -0,0 +1 @@ +pub(crate) mod dialog_info; \ No newline at end of file