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

@ -1,26 +0,0 @@
[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

@ -1,41 +0,0 @@
# 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

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

View file

@ -1,123 +0,0 @@
use sea_orm::{EnumIter, Iterable};
use sea_orm_migration::{prelude::*, schema::*};
use sea_query::extension::postgres::Type;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_type(
Type::create()
.as_enum(Alias::new("status"))
.values(Status::iter())
.to_owned()
)
.await?;
manager
.create_table(
Table::create()
.table(Videos::Table)
.if_not_exists()
.col(pk_auto(Videos::Id))
.col(string(Videos::Title))
.col(string_uniq(Videos::YtId))
.col(timestamp(Videos::CreatedAt))
.col(enumeration(Videos::Status, Alias::new("status"), Status::iter()))
.col(timestamp_null(Videos::UpdatedAt))
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Actions::Table)
.if_not_exists()
.col(pk_auto(Actions::Id))
.col(string_len(Actions::Uid, 36))
.col(integer(Actions::Vid))
.col(timestamp(Actions::CreatedAt))
.foreign_key(
ForeignKey::create()
.name("fk_videos_id")
.from(Actions::Table, Actions::Vid)
.to(Videos::Table, Videos::Id)
.on_delete(ForeignKeyAction::Cascade)
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Moderators::Table)
.if_not_exists()
.col(string_len_uniq(Moderators::Uid, 36).primary_key())
.col(timestamp(Moderators::CreatedAt))
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Actions::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Videos::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Moderators::Table).to_owned())
.await?;
manager
.drop_type(
Type::drop().if_exists().name(Alias::new("status")).cascade().to_owned())
.await?;
// manager
// .drop_foreign_key(
// ForeignKey::drop().name("fk_videos_id").to_owned())
// .await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum Videos {
Table,
Id,
Title,
YtId,
CreatedAt,
Status,
UpdatedAt
}
#[derive(DeriveIden)]
enum Actions {
Table,
Id,
Vid,
Uid,
CreatedAt
}
#[derive(Iden, EnumIter)]
pub enum Status {
#[iden = "Pending"]
Pending,
#[iden = "Viewed"]
Viewed,
#[iden = "Banned"]
Banned
}
#[derive(DeriveIden)]
enum Moderators {
Table,
Uid,
CreatedAt,
}

View file

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