Skip to content

Commit

Permalink
feat: 🚧 sertup database immgration
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Dec 7, 2023
1 parent 89a417d commit b2ae9f9
Show file tree
Hide file tree
Showing 10 changed files with 355 additions and 101 deletions.
3 changes: 2 additions & 1 deletion backend/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@
/config
*.sqlite
/key.pem
/cert.pem
/cert.pem
/a.log
2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ entity = { path = "./entity" }
chrono = "0.4.26"
thiserror = "1.0.44"
ring = "^0.17"
lockfree = "0.5.1"
lockfree = "0.5.1"
derive_builder = "0.12.0"
futures = "0.3.29"
bincode = "1.3.3"
Expand Down
1 change: 0 additions & 1 deletion backend/src/controller/judger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use futures::Future;
use leaky_bucket::RateLimiter;
use sea_orm::{ActiveModelTrait, ActiveValue, EntityTrait, QueryOrder};
use thiserror::Error;
use tokio::sync::OnceCell;
use tokio_stream::StreamExt;
use tonic::Status;
use uuid::Uuid;
Expand Down
32 changes: 29 additions & 3 deletions backend/src/init/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use std::path::PathBuf;

use ring::digest;
use sea_orm::{ActiveModelTrait, ActiveValue, Database, DatabaseConnection};
use sea_orm::{ActiveModelTrait, ActiveValue, Database, DatabaseConnection, Schema, EntityTrait, ConnectionTrait};
use tokio::fs;
use tokio::sync::OnceCell;

Expand All @@ -18,7 +18,7 @@ pub async fn init(config: &GlobalConfig) {
DB.set(db).unwrap();
}
Err(_) => {
println!("Database connection failed, creating database");
log::info!("Database connection failed, creating database");

fs::File::create(PathBuf::from(config.database.path.clone()))
.await
Expand All @@ -27,9 +27,10 @@ pub async fn init(config: &GlobalConfig) {
let db: DatabaseConnection = Database::connect(&uri).await.unwrap();

first_migration(config,&db).await;

DB.set(db).unwrap();

println!("Database created");
log::info!("Database created");
}
}
}
Expand All @@ -42,7 +43,32 @@ fn hash(config: &GlobalConfig, src: &str) -> Vec<u8> {
.to_vec()
}

async fn create_table<E>(db: &DatabaseConnection, entity: E)
where
E: EntityTrait,
{
let builder = db.get_database_backend();
let stmt = builder.build(Schema::new(builder).create_table_from_entity(entity).if_not_exists());

match db.execute(stmt).await {
Ok(_) => log::info!("Migrated {}", entity.table_name()),
Err(e) => log::info!("Error: {}", e),
}
}

pub async fn first_migration(config: &GlobalConfig,db:&DatabaseConnection) {
// create tables
create_table(db, entity::user::Entity).await;
create_table(db, entity::token::Entity).await;
create_table(db, entity::announcement::Entity).await;
create_table(db, entity::contest::Entity).await;
create_table(db, entity::education::Entity).await;
create_table(db, entity::problem::Entity).await;
create_table(db, entity::submit::Entity).await;
create_table(db, entity::test::Entity).await;
create_table(db, entity::user_contest::Entity).await;

// generate admin@admin
let mut perm = UserPermBytes::default();

perm.grant_link(true);
Expand Down
4 changes: 2 additions & 2 deletions backend/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ impl Server {
pub async fn start() {
let config = config::init().await;
log::info!("Loading TLS certificate...");
let cert = fs::read_to_string(&config.grpc.public_pem).await.unwrap();
let key = fs::read_to_string(&config.grpc.private_pem).await.unwrap();
let cert = fs::read_to_string(&config.grpc.public_pem).await.expect("public key.pem not found");
let key = fs::read_to_string(&config.grpc.private_pem).await.expect("privite key.pem not found");
let identity = transport::Identity::from_pem(cert, key);

log::info!("Constructing server...");
Expand Down
Loading

0 comments on commit b2ae9f9

Please sign in to comment.