The first completed version!

This commit is contained in:
Shiroyasha 2025-01-08 18:22:57 +03:00
parent 2844bb9149
commit 42fd8f571e
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
37 changed files with 2320 additions and 952 deletions

8
database/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "database"
version = "0.1.0"
edition = "2021"
publish = false
[dependencies]
sea-orm = { version = "1.1", features = ["macros", "sqlx-sqlite", "runtime-tokio-rustls", "sqlx-postgres", "with-chrono"] }

View file

@ -0,0 +1,26 @@
[package]
name = "migration"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
name = "migration"
path = "src/lib.rs"
[dependencies]
async-std = { version = "1", features = ["attributes", "tokio1"] }
[dependencies.sea-orm-migration]
version = "1.1.0"
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-rustls",
"sqlx-postgres",
"with-uuid",
"with-chrono"
]

View file

@ -0,0 +1,41 @@
# Running Migrator CLI
- Generate a new migration file
```sh
cargo run -- generate MIGRATION_NAME
```
- Apply all pending migrations
```sh
cargo run
```
```sh
cargo run -- up
```
- Apply first 10 pending migrations
```sh
cargo run -- up -n 10
```
- Rollback last applied migrations
```sh
cargo run -- down
```
- Rollback last 10 applied migrations
```sh
cargo run -- down -n 10
```
- Drop all tables from the database, then reapply all migrations
```sh
cargo run -- fresh
```
- Rollback all applied migrations, then reapply all migrations
```sh
cargo run -- refresh
```
- Rollback all applied migrations
```sh
cargo run -- reset
```
- Check the status of all migrations
```sh
cargo run -- status
```

View file

@ -0,0 +1,14 @@
pub use sea_orm_migration::prelude::*;
mod m20241211_182453_create_tables;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20241211_182453_create_tables::Migration),
]
}
}

View file

@ -0,0 +1,190 @@
use sea_orm_migration::{prelude::*, schema::*};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Videos
manager
.create_table(
Table::create()
.table(Videos::Table)
.if_not_exists()
.col(string_len_uniq(Videos::Ytid, 11).primary_key())
.col(string(Videos::Title))
.col(boolean(Videos::Banned).default(Expr::value(false)))
.to_owned(),
)
.await?;
// Requests
manager
.create_table(
Table::create()
.table(Requests::Table)
.if_not_exists()
.col(pk_auto(Requests::Id))
.col(string_len(Requests::Ytid, 11))
.col(timestamp_null(Requests::ViewedAt).default(Expr::value(Keyword::Null)))
.foreign_key(
ForeignKey::create()
.name("fk_videos_ytid_requests")
.from(Requests::Table, Requests::Ytid)
.to(Videos::Table, Videos::Ytid)
.on_delete(ForeignKeyAction::Cascade)
)
.to_owned(),
)
.await?;
// Actions
manager
.create_table(
Table::create()
.table(Actions::Table)
.if_not_exists()
.col(pk_auto(Actions::Id))
.col(integer(Actions::Rid))
.col(big_integer(Actions::Uid))
.col(timestamp(Actions::CreatedAt).default(Expr::current_timestamp()))
.foreign_key(
ForeignKey::create()
.name("fk_requests_rid_actions")
.from(Actions::Table, Actions::Rid)
.to(Requests::Table, Requests::Id)
.on_delete(ForeignKeyAction::Cascade)
)
.to_owned(),
)
.await?;
// Archived
manager
.create_table(
Table::create()
.table(Archived::Table)
.if_not_exists()
.col(pk_auto(Archived::Id))
.col(string_len(Archived::Ytid, 11))
.col(timestamp_null(Archived::ViewedAt))
.col(big_integer(Archived::CreatedBy))
.col(timestamp(Archived::CreatedAt).default(Expr::current_timestamp()))
.col(unsigned(Archived::Contributors))
.foreign_key(
ForeignKey::create()
.name("fk_videos_ytid_archived")
.from(Archived::Table, Archived::Ytid)
.to(Videos::Table, Videos::Ytid)
.on_delete(ForeignKeyAction::NoAction)
)
.to_owned(),
)
.await?;
// Moderators
manager
.create_table(
Table::create()
.table(Moderators::Table)
.if_not_exists()
.col(big_integer_uniq(Moderators::Id).primary_key())
.col(timestamp(Moderators::CreatedAt).default(Expr::current_timestamp()))
.col(boolean(Moderators::Notify).default(Expr::value(true)))
.col(boolean(Moderators::CanAddMods).default(Expr::value(false)))
.to_owned(),
)
.await?;
// Users
manager
.create_table(
Table::create()
.table(Users::Table)
.if_not_exists()
.col(big_integer_uniq(Users::Id).primary_key())
.col(timestamp(Users::CreatedAt).default(Expr::current_timestamp()))
.col(unsigned(Users::Contributions).default(Expr::value(0)))
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Videos
manager
.drop_table(Table::drop().table(Videos::Table).to_owned())
.await?;
// Requests
manager
.drop_table(Table::drop().table(Requests::Table).to_owned())
.await?;
// Actions
manager
.drop_table(Table::drop().table(Actions::Table).to_owned())
.await?;
// Archived
manager
.drop_table(Table::drop().table(Archived::Table).to_owned())
.await?;
// Moderators
manager
.drop_table(Table::drop().table(Moderators::Table).to_owned())
.await?;
// Users
manager
.drop_table(Table::drop().table(Users::Table).to_owned())
.await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum Videos {
Table,
Ytid,
Title,
Banned
}
#[derive(DeriveIden)]
enum Requests {
Table,
Id,
Ytid,
ViewedAt
}
#[derive(DeriveIden)]
enum Actions {
Table,
Id,
Rid,
Uid,
CreatedAt
}
#[derive(DeriveIden)]
enum Archived {
Table,
Id,
Ytid,
ViewedAt,
CreatedBy,
CreatedAt,
Contributors
}
#[derive(DeriveIden)]
enum Moderators {
Table,
Id,
CreatedAt,
Notify,
CanAddMods
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
CreatedAt,
Contributions
}

View file

@ -0,0 +1,6 @@
use sea_orm_migration::prelude::*;
#[async_std::main]
async fn main() {
cli::run_cli(migration::Migrator).await;
}

33
database/src/actions.rs Normal file
View file

@ -0,0 +1,33 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "actions")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub rid: i32,
pub uid: i64,
pub created_at: DateTime,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::requests::Entity",
from = "Column::Rid",
to = "super::requests::Column::Id",
on_update = "NoAction",
on_delete = "Cascade"
)]
Requests,
}
impl Related<super::requests::Entity> for Entity {
fn to() -> RelationDef {
Relation::Requests.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

35
database/src/archived.rs Normal file
View file

@ -0,0 +1,35 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "archived")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub ytid: String,
pub viewed_at: Option<DateTime>,
pub created_by: i64,
pub created_at: DateTime,
pub contributors: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(
belongs_to = "super::videos::Entity",
from = "Column::Ytid",
to = "super::videos::Column::Ytid",
on_update = "NoAction",
on_delete = "NoAction"
)]
Videos,
}
impl Related<super::videos::Entity> for Entity {
fn to() -> RelationDef {
Relation::Videos.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

10
database/src/lib.rs Normal file
View file

@ -0,0 +1,10 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
pub mod prelude;
pub mod actions;
pub mod archived;
pub mod moderators;
pub mod requests;
pub mod users;
pub mod videos;

View file

@ -0,0 +1,18 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "moderators")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i64,
pub created_at: DateTime,
pub notify: bool,
pub can_add_mods: bool,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

8
database/src/prelude.rs Normal file
View file

@ -0,0 +1,8 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
pub use super::actions::Entity as Actions;
pub use super::archived::Entity as Archived;
pub use super::moderators::Entity as Moderators;
pub use super::requests::Entity as Requests;
pub use super::users::Entity as Users;
pub use super::videos::Entity as Videos;

40
database/src/requests.rs Normal file
View file

@ -0,0 +1,40 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "requests")]
pub struct Model {
#[sea_orm(primary_key)]
pub id: i32,
pub ytid: String,
pub viewed_at: Option<DateTime>,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::actions::Entity")]
Actions,
#[sea_orm(
belongs_to = "super::videos::Entity",
from = "Column::Ytid",
to = "super::videos::Column::Ytid",
on_update = "NoAction",
on_delete = "Cascade"
)]
Videos,
}
impl Related<super::actions::Entity> for Entity {
fn to() -> RelationDef {
Relation::Actions.def()
}
}
impl Related<super::videos::Entity> for Entity {
fn to() -> RelationDef {
Relation::Videos.def()
}
}
impl ActiveModelBehavior for ActiveModel {}

17
database/src/users.rs Normal file
View file

@ -0,0 +1,17 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "users")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub id: i64,
pub created_at: DateTime,
pub contributions: i32,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {}
impl ActiveModelBehavior for ActiveModel {}

34
database/src/videos.rs Normal file
View file

@ -0,0 +1,34 @@
//! `SeaORM` Entity, @generated by sea-orm-codegen 1.1.2
use sea_orm::entity::prelude::*;
#[derive(Clone, Debug, PartialEq, DeriveEntityModel, Eq)]
#[sea_orm(table_name = "videos")]
pub struct Model {
#[sea_orm(primary_key, auto_increment = false)]
pub ytid: String,
pub title: String,
pub banned: bool,
}
#[derive(Copy, Clone, Debug, EnumIter, DeriveRelation)]
pub enum Relation {
#[sea_orm(has_many = "super::archived::Entity")]
Archived,
#[sea_orm(has_many = "super::requests::Entity")]
Requests,
}
impl Related<super::archived::Entity> for Entity {
fn to() -> RelationDef {
Relation::Archived.def()
}
}
impl Related<super::requests::Entity> for Entity {
fn to() -> RelationDef {
Relation::Requests.def()
}
}
impl ActiveModelBehavior for ActiveModel {}