rave/migration/src/m000006_create_album.rs

144 lines
4.6 KiB
Rust
Raw Normal View History

use sea_orm_migration::prelude::*;
use crate::{
2023-10-10 18:25:02 +00:00
m000002_create_music_folder::MusicFolder, m000003_create_cover_art::CoverArt,
m000004_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::ArtistId).big_integer().null())
.col(ColumnDef::new(Album::CoverArtId).big_integer().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())
2023-10-10 18:25:02 +00:00
.col(
ColumnDef::new(Album::GenreIds)
.array(ColumnType::BigInteger)
.null(),
)
.col(
ColumnDef::new(Album::Played)
.timestamp_with_time_zone()
.null(),
2023-10-10 18:25:02 +00:00
)
.col(ColumnDef::new(Album::UserRating).tiny_integer().null())
.col(
ColumnDef::new(Album::ArtistIds)
.array(ColumnType::BigInteger)
.null(),
)
.col(
ColumnDef::new(Album::OriginalReleaseDate)
.timestamp_with_time_zone()
.null(),
)
.col(
ColumnDef::new(Album::MusicFolderId)
.big_integer()
.not_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?;
manager
.create_foreign_key(
ForeignKey::create()
.from_tbl(Album::Table)
.from_col(Album::CoverArtId)
.to_tbl(CoverArt::Table)
.to_col(CoverArt::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,
ArtistId,
CoverArtId,
SongCount,
Duration,
PlayCount,
Created,
Starred,
Year,
2023-10-10 18:25:02 +00:00
GenreIds,
Played,
UserRating,
ArtistIds,
OriginalReleaseDate,
MusicFolderId,
}