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

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;
}