Skip to content

Commit

Permalink
feat: ✨ finish migration
Browse files Browse the repository at this point in the history
  • Loading branch information
Eason0729 committed Dec 7, 2023
1 parent f6fac7f commit 09677ac
Show file tree
Hide file tree
Showing 10 changed files with 371 additions and 205 deletions.
463 changes: 299 additions & 164 deletions backend/Cargo.lock

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ features =["json"]

[dependencies.tracing]
version = "0.1.40"
features =["async-await", "log"]
features = ["async-await", "log"]

[dependencies.tokio]
version = "1.34.0"
Expand Down
2 changes: 1 addition & 1 deletion backend/entity/src/submit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub struct Model {
pub upload_at: DateTime,
#[sea_orm(nullable, indexed)]
pub time: Option<u64>,
#[sea_orm(nullable)]
#[sea_orm(nullable)]
pub accuracy: Option<u64>,
#[sea_orm(default_value = "false")]
pub committed: bool,
Expand Down
2 changes: 1 addition & 1 deletion backend/entity/src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub struct Model {
#[sea_orm(primary_key, auto_increment = true)]
pub id: i32,
pub permission: u64,
#[sea_orm(indexed, default_value="0")]
#[sea_orm(indexed, default_value = "0")]
pub score: u32,
pub username: String,
pub password: Vec<u8>,
Expand Down
2 changes: 2 additions & 0 deletions backend/justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@
prepare:
mkdir -p database
mkdir -p config
sea-orm-cli migrate -u sqlite://database/backend.sqlite?mode=rwc

release-docker:
just prepare
cp ../proto/*.proto .
cp ../cert/*.pem .
docker build . --build-arg ARCH=$(uname -m) -t mdoj-backend
Expand Down
21 changes: 17 additions & 4 deletions backend/migration/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,27 @@ name = "migration"
path = "src/lib.rs"

[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
log = "0.4.20"
tracing-subscriber = "0.3"

[dependencies.tracing]
version = "0.1.40"
features = ["async-await", "log"]

[dependencies.async-std]
version = "1"
features = ["attributes", "tokio1"]

[dependencies.sea-orm]
version = "0.12.2"
features = ["debug-print"]

[dependencies.sea-orm-migration]
version = "0.11.0"
version = "0.12.2"
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
# e.g.
# "runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
# "sqlx-postgres", # `DATABASE_DRIVER` feature
"runtime-tokio-native-tls",
"sqlx-sqlite","with-chrono"
]
71 changes: 44 additions & 27 deletions backend/migration/src/m20231207_000001_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ use std::ptr::null;

use sea_orm_migration::{prelude::*, seaql_migrations::PrimaryKey};

// static UPDATE_AT: &str = "DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP";
static UPDATE_AT: &str = "DEFAULT CURRENT_TIMESTAMP";
static CREATE_AT: &str = "DEFAULT CURRENT_TIMESTAMP";

#[derive(Iden)]
enum Announcement {
Table,
Expand Down Expand Up @@ -107,7 +111,6 @@ enum User {
#[derive(Iden)]
enum UserContest {
Table,
Id,
UserId,
ContestId,
Score,
Expand Down Expand Up @@ -136,12 +139,12 @@ impl MigrationTrait for Migration {
.col(
ColumnDef::new(Announcement::CreateAt)
.not_null()
.extra("DEFAULT CURRENT_TIMESTAMP".to_string()),
.extra(CREATE_AT.to_string()),
)
.col(
ColumnDef::new(Announcement::UpdateAt).not_null().extra(
"DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP".to_string(),
),
ColumnDef::new(Announcement::UpdateAt)
.not_null()
.extra(UPDATE_AT.to_string()),
)
.to_owned(),
)
Expand All @@ -159,21 +162,29 @@ impl MigrationTrait for Migration {
.primary_key(),
)
.col(ColumnDef::new(Contest::Hoster).integer().not_null())
.col(ColumnDef::new(Contest::Begin).time().not_null())
.col(ColumnDef::new(Contest::End).time().not_null())
.col(
ColumnDef::new(Contest::Begin)
.timestamp_with_time_zone()
.not_null(),
)
.col(
ColumnDef::new(Contest::End)
.timestamp_with_time_zone()
.not_null(),
)
.col(ColumnDef::new(Contest::Title).text().not_null())
.col(ColumnDef::new(Contest::Content).text().default(""))
.col(ColumnDef::new(Contest::Tags).text().default(""))
.col(ColumnDef::new(Contest::Password).binary().null())
.col(
ColumnDef::new(Contest::CreateAt)
.not_null()
.extra("DEFAULT CURRENT_TIMESTAMP".to_string()),
.extra(CREATE_AT.to_string()),
)
.col(
ColumnDef::new(Contest::UpdateAt).not_null().extra(
"DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP".to_string(),
),
ColumnDef::new(Contest::UpdateAt)
.not_null()
.extra(UPDATE_AT.to_string()),
)
.col(ColumnDef::new(Contest::Public).boolean().default(false))
.to_owned(),
Expand Down Expand Up @@ -220,7 +231,8 @@ impl MigrationTrait for Migration {
ColumnDef::new(Problem::Id)
.integer()
.not_null()
.auto_increment(),
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Problem::UserId).integer().not_null())
.foreign_key(
Expand All @@ -243,12 +255,12 @@ impl MigrationTrait for Migration {
.col(
ColumnDef::new(Problem::CreateAt)
.not_null()
.extra("DEFAULT CURRENT_TIMESTAMP".to_string()),
.extra(CREATE_AT.to_string()),
)
.col(
ColumnDef::new(Problem::UpdateAt).not_null().extra(
"DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP".to_string(),
),
ColumnDef::new(Problem::UpdateAt)
.not_null()
.extra(UPDATE_AT.to_string()),
)
.col(ColumnDef::new(Problem::MatchRule).integer().not_null())
.to_owned(),
Expand All @@ -262,9 +274,9 @@ impl MigrationTrait for Migration {
.col(
ColumnDef::new(Submit::Id)
.integer()
.primary_key()
.not_null()
.auto_increment()
.not_null(),
.primary_key(),
)
.col(ColumnDef::new(Submit::UserId).integer().null())
.foreign_key(
Expand All @@ -283,7 +295,7 @@ impl MigrationTrait for Migration {
.col(
ColumnDef::new(Submit::UploadAt)
.not_null()
.extra("DEFAULT CURRENT_TIMESTAMP".to_string()),
.extra(CREATE_AT.to_string()),
)
.col(ColumnDef::new(Submit::Time).big_unsigned().null())
.col(ColumnDef::new(Submit::Accuracy).big_unsigned().null())
Expand All @@ -305,10 +317,10 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(
ColumnDef::new(Test::Id)
.primary_key()
.integer()
.not_null()
.auto_increment()
.integer(),
.primary_key(),
)
.col(ColumnDef::new(Test::UserId).integer().not_null())
.foreign_key(
Expand Down Expand Up @@ -337,10 +349,10 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(
ColumnDef::new(Token::Id)
.primary_key()
.integer()
.not_null()
.auto_increment()
.integer(),
.primary_key(),
)
.col(ColumnDef::new(Token::UserId).integer().not_null())
.foreign_key(
Expand All @@ -351,7 +363,11 @@ impl MigrationTrait for Migration {
)
.col(ColumnDef::new(Token::Rand).binary().not_null())
.col(ColumnDef::new(Token::Permission).big_unsigned().default(0))
.col(ColumnDef::new(Token::Expiry).time().not_null())
.col(
ColumnDef::new(Token::Expiry)
.timestamp_with_time_zone()
.not_null(),
)
.to_owned(),
)
.await?;
Expand All @@ -362,10 +378,10 @@ impl MigrationTrait for Migration {
.if_not_exists()
.col(
ColumnDef::new(User::Id)
.integer()
.not_null()
.primary_key()
.auto_increment()
.integer(),
.primary_key(),
)
.col(ColumnDef::new(User::Permission).big_unsigned().default(0))
.col(ColumnDef::new(User::Score).unsigned().default(0))
Expand All @@ -374,7 +390,7 @@ impl MigrationTrait for Migration {
.col(
ColumnDef::new(User::CreateAt)
.not_null()
.extra("DEFAULT CURRENT_TIMESTAMP".to_string()),
.extra(CREATE_AT.to_string()),
)
.to_owned(),
)
Expand All @@ -398,6 +414,7 @@ impl MigrationTrait for Migration {
.from(UserContest::Table, UserContest::UserId)
.to(User::Table, User::Id),
)
.col(ColumnDef::new(UserContest::Score).integer().default(0))
.to_owned(),
)
.await?;
Expand Down
10 changes: 5 additions & 5 deletions backend/src/controller/judger/route/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,7 @@ impl std::ops::Deref for ConnGuard {
impl Drop for ConnGuard {
fn drop(&mut self) {
self.upstream.healthy.fetch_add(-2, Ordering::Acquire);
self.upstream
.clients
.push(self.conn.take().unwrap());
self.upstream.clients.push(self.conn.take().unwrap());
}
}

Expand Down Expand Up @@ -172,8 +170,10 @@ impl Router {
Ok(self_)
}
pub async fn get(&self, lang: &Uuid) -> Result<ConnGuard, Error> {

let queue = self.routing_table.get(lang).ok_or(Error::BadArgument("lang"))?;
let queue = self
.routing_table
.get(lang)
.ok_or(Error::BadArgument("lang"))?;

loop {
match queue.pop() {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/init/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ async fn create_table<E>(db: &DatabaseConnection, entity: E)
where
E: EntityTrait,
{
log::info!("Creating table: {}",entity.table_name());
log::info!("Creating table: {}", entity.table_name());
let builder = db.get_database_backend();
let stmt = builder.build(
Schema::new(builder)
Expand Down
1 change: 0 additions & 1 deletion backend/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,3 @@ async fn main() {
log::info!("starting server");
server::Server::start().await;
}

0 comments on commit 09677ac

Please sign in to comment.