This commit is contained in:
Shiroyasha 2025-06-10 00:54:51 +03:00
commit 1c273e1fc0
Signed by: shiroyashik
GPG key ID: E4953D3940D7860A
46 changed files with 7589 additions and 0 deletions

22
migration/Cargo.toml Normal file
View file

@ -0,0 +1,22 @@
[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 = "0.12.14"
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
]

41
migration/README.md Normal file
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
```

20
migration/src/lib.rs Normal file
View file

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

View file

@ -0,0 +1,68 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(User::Table)
.if_not_exists()
.col(
ColumnDef::new(User::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(User::Name)
.string_len(50)
.not_null()
.unique_key(),
)
.col(ColumnDef::new(User::PasswordHash).string_len(128).not_null())
.col(ColumnDef::new(User::PasswordSalt).string_len(32))
.col(ColumnDef::new(User::Email).string_len(64))
.col(ColumnDef::new(User::Rank).string_len(32).not_null())
.col(ColumnDef::new(User::CreationTime).timestamp().not_null())
.col(ColumnDef::new(User::LastLoginTime).timestamp())
.col(ColumnDef::new(User::AvatarStyle).string_len(32).not_null())
.col(ColumnDef::new(User::Version).integer().not_null())
.col(ColumnDef::new(User::PasswordRevision).small_integer().not_null())
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(User::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
pub(super) enum User {
Table,
Id,
Name,
#[sea_orm(iden = "password_hash")]
PasswordHash,
#[sea_orm(iden = "password_salt")]
PasswordSalt,
Email,
Rank,
#[sea_orm(iden = "creation_time")]
CreationTime,
#[sea_orm(iden = "last_login_time")]
LastLoginTime,
#[sea_orm(iden = "avatar_style")]
AvatarStyle,
Version,
#[sea_orm(iden = "password_revision")]
PasswordRevision,
}

View file

@ -0,0 +1,82 @@
use sea_orm_migration::prelude::*;
use crate::m20240225_224934_create_user::User;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Post::Table)
.if_not_exists()
.col(
ColumnDef::new(Post::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Post::UserId).integer())
.col(ColumnDef::new(Post::CreationTime).timestamp().not_null())
.col(ColumnDef::new(Post::LastEditTime).timestamp())
.col(ColumnDef::new(Post::Safety).string_len(32).not_null())
.col(ColumnDef::new(Post::Type).string_len(32).not_null())
.col(ColumnDef::new(Post::Checksum).string_len(64).not_null())
.col(ColumnDef::new(Post::Source).string_len(2048))
.col(ColumnDef::new(Post::FileSize).big_integer())
.col(ColumnDef::new(Post::ImageWidth).integer())
.col(ColumnDef::new(Post::ImageHeight).integer())
.col(ColumnDef::new(Post::MimeType).string_len(32).not_null())
.col(ColumnDef::new(Post::Version).integer().not_null())
.col(ColumnDef::new(Post::Flags).string_len(32))
.col(ColumnDef::new(Post::ChecksumMD5).string_len(32))
.foreign_key(
ForeignKey::create()
.name("FK_posts_userid")
.from(Post::Table, Post::UserId)
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Post::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Post {
Table,
Id,
#[sea_orm(iden = "user_id")]
UserId,
#[sea_orm(iden = "creation_time")]
CreationTime,
#[sea_orm(iden = "last_edit_time")]
LastEditTime,
Safety,
Type,
Checksum,
Source,
#[sea_orm(iden = "file_size")]
FileSize,
#[sea_orm(iden = "image_width")]
ImageWidth,
#[sea_orm(iden = "image_height")]
ImageHeight,
#[sea_orm(iden = "mime-type")]
MimeType,
Version,
Flags,
#[sea_orm(iden = "checksum_md5")]
ChecksumMD5,
}

View file

@ -0,0 +1,65 @@
use sea_orm_migration::prelude::*;
use crate::m20240225_224934_create_user::User;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(Snapshot::Table)
.if_not_exists()
.col(
ColumnDef::new(Snapshot::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Snapshot::CreationTime).timestamp().not_null())
.col(ColumnDef::new(Snapshot::ResourceType).string_len(32).not_null())
.col(ColumnDef::new(Snapshot::Operation).string_len(16).not_null())
.col(ColumnDef::new(Snapshot::UserId).integer())
.col(ColumnDef::new(Snapshot::Data).binary())
.col(ColumnDef::new(Snapshot::ResourceName).string_len(128).not_null())
.col(ColumnDef::new(Snapshot::ResourcePkey).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_snapshot_userid")
.from(Snapshot::Table, Snapshot::UserId)
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::SetNull),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Snapshot::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Snapshot {
Table,
Id,
#[sea_orm(iden = "creation_time")]
CreationTime,
#[sea_orm(iden = "resource_type")]
ResourceType,
Operation,
#[sea_orm(iden = "user_id")]
UserId,
Data,
#[sea_orm(iden = "resource_name")]
ResourceName,
#[sea_orm(iden = "resource_pkey")]
ResourcePkey,
}

View file

@ -0,0 +1,71 @@
use sea_orm_migration::prelude::*;
use crate::m20240225_224934_create_user::User;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.create_table(
Table::create()
.table(UserToken::Table)
.if_not_exists()
.col(
ColumnDef::new(UserToken::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(UserToken::UserId).integer().not_null())
.col(ColumnDef::new(UserToken::Token).string_len(36).not_null())
.col(ColumnDef::new(UserToken::Note).string_len(128))
.col(ColumnDef::new(UserToken::Enabled).boolean().not_null())
.col(ColumnDef::new(UserToken::ExpirationTime).timestamp())
.col(ColumnDef::new(UserToken::CreationTime).timestamp().not_null())
.col(ColumnDef::new(UserToken::LastEditTime).timestamp())
.col(ColumnDef::new(UserToken::LastUsageTime).timestamp())
.col(ColumnDef::new(UserToken::Version).integer().not_null())
.foreign_key(
ForeignKey::create()
.name("FK_user_token_userid")
.from(UserToken::Table, UserToken::UserId)
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(UserToken::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum UserToken {
Table,
Id,
#[sea_orm(iden = "user_id")]
UserId,
Token,
Note,
Enabled,
#[sea_orm(iden = "expiration_time")]
ExpirationTime,
#[sea_orm(iden = "creation_time")]
CreationTime,
#[sea_orm(iden = "last_edit_time")]
LastEditTime,
#[sea_orm(iden = "last_usage_time")]
LastUsageTime,
Version,
}

6
migration/src/main.rs Normal file
View file

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