105 lines
3.3 KiB
Rust
105 lines
3.3 KiB
Rust
|
|
use sea_orm_migration::prelude::*;
|
||
|
|
|
||
|
|
use crate::{
|
||
|
|
m20231009_181004_create_music_folder::MusicFolder, m20231009_181104_create_artist::Artist,
|
||
|
|
};
|
||
|
|
|
||
|
|
#[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(Album::Table)
|
||
|
|
.if_not_exists()
|
||
|
|
.col(
|
||
|
|
ColumnDef::new(Album::Id)
|
||
|
|
.big_integer()
|
||
|
|
.not_null()
|
||
|
|
.auto_increment()
|
||
|
|
.primary_key(),
|
||
|
|
)
|
||
|
|
.col(ColumnDef::new(Album::Name).string().not_null())
|
||
|
|
.col(ColumnDef::new(Album::Artist).string().null())
|
||
|
|
.col(ColumnDef::new(Album::ArtistId).big_integer().null())
|
||
|
|
.col(ColumnDef::new(Album::CoverArt).string().null())
|
||
|
|
.col(ColumnDef::new(Album::SongCount).integer().not_null())
|
||
|
|
.col(ColumnDef::new(Album::Duration).big_integer().not_null())
|
||
|
|
.col(
|
||
|
|
ColumnDef::new(Album::PlayCount)
|
||
|
|
.big_integer()
|
||
|
|
.not_null()
|
||
|
|
.default(0),
|
||
|
|
)
|
||
|
|
.col(
|
||
|
|
ColumnDef::new(Album::Created)
|
||
|
|
.timestamp_with_time_zone()
|
||
|
|
.not_null(),
|
||
|
|
)
|
||
|
|
.col(
|
||
|
|
ColumnDef::new(Album::Starred)
|
||
|
|
.timestamp_with_time_zone()
|
||
|
|
.null(),
|
||
|
|
)
|
||
|
|
.col(ColumnDef::new(Album::Year).integer().null())
|
||
|
|
.col(ColumnDef::new(Album::Genre).string().null())
|
||
|
|
.col(ColumnDef::new(Album::MusicFolderId).big_integer().null())
|
||
|
|
.to_owned(),
|
||
|
|
)
|
||
|
|
.await?;
|
||
|
|
|
||
|
|
manager
|
||
|
|
.create_foreign_key(
|
||
|
|
ForeignKey::create()
|
||
|
|
.from_tbl(Album::Table)
|
||
|
|
.from_col(Album::MusicFolderId)
|
||
|
|
.to_tbl(MusicFolder::Table)
|
||
|
|
.to_col(MusicFolder::Id)
|
||
|
|
.on_delete(ForeignKeyAction::Cascade)
|
||
|
|
.to_owned(),
|
||
|
|
)
|
||
|
|
.await?;
|
||
|
|
|
||
|
|
manager
|
||
|
|
.create_foreign_key(
|
||
|
|
ForeignKey::create()
|
||
|
|
.from_tbl(Album::Table)
|
||
|
|
.from_col(Album::ArtistId)
|
||
|
|
.to_tbl(Artist::Table)
|
||
|
|
.to_col(Artist::Id)
|
||
|
|
.on_delete(ForeignKeyAction::SetNull)
|
||
|
|
.to_owned(),
|
||
|
|
)
|
||
|
|
.await?;
|
||
|
|
|
||
|
|
Ok(())
|
||
|
|
}
|
||
|
|
|
||
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||
|
|
manager
|
||
|
|
.drop_table(Table::drop().table(Album::Table).to_owned())
|
||
|
|
.await
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
#[derive(DeriveIden)]
|
||
|
|
pub enum Album {
|
||
|
|
Table,
|
||
|
|
Id,
|
||
|
|
Name,
|
||
|
|
Artist,
|
||
|
|
ArtistId,
|
||
|
|
CoverArt,
|
||
|
|
SongCount,
|
||
|
|
Duration,
|
||
|
|
PlayCount,
|
||
|
|
Created,
|
||
|
|
Starred,
|
||
|
|
Year,
|
||
|
|
Genre,
|
||
|
|
MusicFolderId,
|
||
|
|
}
|