chore: 添加迁移文件定义(非最终)

This commit is contained in:
amtoaer
2024-03-23 00:06:15 +08:00
parent 3757c4f917
commit 762dad5d83
6 changed files with 2702 additions and 0 deletions

2395
migration/Cargo.lock generated Normal file

File diff suppressed because it is too large Load Diff

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.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-native-tls", # `ASYNC_RUNTIME` feature
"sqlx-sqlite", # `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
```

12
migration/src/lib.rs Normal file
View File

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

View File

@@ -0,0 +1,226 @@
use sea_orm_migration::prelude::*;
use sea_orm_migration::sea_orm::{EnumIter, Iterable};
#[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(Favorite::Table)
.if_not_exists()
.col(
ColumnDef::new(Favorite::Id)
.unsigned()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(Favorite::FId)
.unique_key()
.unsigned()
.not_null(),
)
.col(ColumnDef::new(Favorite::Name).string().not_null())
.col(ColumnDef::new(Favorite::Path).string().not_null())
.col(ColumnDef::new(Favorite::Enabled).boolean().not_null())
.col(
ColumnDef::new(Favorite::CreatedAt)
.timestamp()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Favorite::UpdatedAt)
.timestamp()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Video::Table)
.if_not_exists()
.col(
ColumnDef::new(Video::Id)
.unsigned()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Video::FavoriteId).unsigned().not_null())
.col(ColumnDef::new(Video::UpperId).unsigned().not_null())
.col(ColumnDef::new(Video::Name).string().not_null())
.col(ColumnDef::new(Video::Path).string().not_null())
.col(
ColumnDef::new(Video::Category)
.enumeration(Alias::new("category"), Category::iter())
.not_null(),
)
.col(ColumnDef::new(Video::Bvid).string().not_null())
.col(ColumnDef::new(Video::Intro).string().not_null())
.col(ColumnDef::new(Video::Cover).string().not_null())
.col(ColumnDef::new(Video::Ctime).timestamp().not_null())
.col(ColumnDef::new(Video::Pubtime).timestamp().not_null())
.col(ColumnDef::new(Video::Favtime).timestamp().not_null())
.col(ColumnDef::new(Video::Downloaded).boolean().not_null())
.col(ColumnDef::new(Video::Valid).boolean().not_null())
.col(ColumnDef::new(Video::Tags).json_binary().not_null())
.col(ColumnDef::new(Video::SinglePage).boolean().not_null())
.col(
ColumnDef::new(Video::CreatedAt)
.timestamp()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Video::UpdatedAt)
.timestamp()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await?;
manager
.create_table(
Table::create()
.table(Page::Table)
.if_not_exists()
.col(
ColumnDef::new(Page::Id)
.unsigned()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Page::VideoId).unsigned().not_null())
.col(ColumnDef::new(Page::Cid).unsigned().not_null())
.col(ColumnDef::new(Page::Pid).unsigned().not_null())
.col(ColumnDef::new(Page::Name).string().not_null())
.col(ColumnDef::new(Page::Path).string().not_null())
.col(ColumnDef::new(Page::Image).string().not_null())
.col(ColumnDef::new(Page::Valid).boolean().not_null())
.col(ColumnDef::new(Page::DownloadStatus).unsigned().not_null())
.col(ColumnDef::new(Page::Downloaded).boolean().not_null())
.col(
ColumnDef::new(Page::CreatedAt)
.timestamp()
.default(Expr::current_timestamp())
.not_null(),
)
.col(
ColumnDef::new(Page::UpdatedAt)
.timestamp()
.default(Expr::current_timestamp())
.not_null(),
)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.table(Video::Table)
.name("idx_video_favorite_id_bvid")
.col(Video::FavoriteId)
.col(Video::Bvid)
.to_owned(),
)
.await?;
manager
.create_index(
Index::create()
.table(Page::Table)
.name("idx_page_video_id_pid")
.col(Page::VideoId)
.col(Page::Pid)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Favorite::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Video::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Page::Table).to_owned())
.await?;
Ok(())
}
}
#[derive(DeriveIden)]
enum Favorite {
Table,
Id,
FId,
Name,
Path,
Enabled,
CreatedAt,
UpdatedAt,
}
#[derive(DeriveIden)]
enum Video {
Table,
Id,
FavoriteId,
UpperId,
Name,
Path,
Category,
Bvid,
Intro,
Cover,
Ctime,
Pubtime,
Favtime,
Downloaded,
Valid,
Tags,
SinglePage,
CreatedAt,
UpdatedAt,
}
#[derive(DeriveIden)]
enum Page {
Table,
Id,
VideoId,
Cid,
Pid,
Name,
Path,
Image,
Valid,
DownloadStatus,
Downloaded,
CreatedAt,
UpdatedAt,
}
// 参考: https://socialsisteryi.github.io/bilibili-API-collect/docs/fav/list.html#%E8%8E%B7%E5%8F%96%E6%94%B6%E8%97%8F%E5%A4%B9%E5%86%85%E5%AE%B9%E6%98%8E%E7%BB%86%E5%88%97%E8%A1%A8
#[derive(Iden, EnumIter)]
pub enum Category {
#[iden = "2"]
Video,
#[iden = "12"]
Audio,
#[iden = "21"]
VideoCollection,
}

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