Initial commit
This commit is contained in:
146
src/migration/m20260317_000003_create_tracks.rs
Normal file
146
src/migration/m20260317_000003_create_tracks.rs
Normal file
@@ -0,0 +1,146 @@
|
||||
use sea_orm_migration::prelude::*;
|
||||
|
||||
use super::m20260317_000001_create_artists::Artists;
|
||||
use super::m20260317_000002_create_albums::Albums;
|
||||
|
||||
#[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(Tracks::Table)
|
||||
.if_not_exists()
|
||||
.col(
|
||||
ColumnDef::new(Tracks::Id)
|
||||
.integer()
|
||||
.not_null()
|
||||
.auto_increment()
|
||||
.primary_key(),
|
||||
)
|
||||
.col(ColumnDef::new(Tracks::FilePath).text().not_null().unique_key())
|
||||
.col(ColumnDef::new(Tracks::Title).text())
|
||||
.col(ColumnDef::new(Tracks::Artist).text())
|
||||
.col(ColumnDef::new(Tracks::Album).text())
|
||||
.col(ColumnDef::new(Tracks::AlbumArtist).text())
|
||||
.col(ColumnDef::new(Tracks::TrackNumber).integer())
|
||||
.col(ColumnDef::new(Tracks::DiscNumber).integer())
|
||||
.col(ColumnDef::new(Tracks::Duration).double())
|
||||
.col(ColumnDef::new(Tracks::Genre).text())
|
||||
.col(ColumnDef::new(Tracks::Year).integer())
|
||||
.col(ColumnDef::new(Tracks::Codec).text())
|
||||
.col(ColumnDef::new(Tracks::Bitrate).integer())
|
||||
.col(ColumnDef::new(Tracks::FileSize).big_integer().not_null())
|
||||
.col(ColumnDef::new(Tracks::Fingerprint).text())
|
||||
.col(ColumnDef::new(Tracks::MusicbrainzId).text())
|
||||
.col(ColumnDef::new(Tracks::ArtistId).integer())
|
||||
.col(ColumnDef::new(Tracks::AlbumId).integer())
|
||||
.col(ColumnDef::new(Tracks::FileMtime).date_time())
|
||||
.col(
|
||||
ColumnDef::new(Tracks::AddedAt)
|
||||
.date_time()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.col(
|
||||
ColumnDef::new(Tracks::UpdatedAt)
|
||||
.date_time()
|
||||
.not_null()
|
||||
.default(Expr::current_timestamp()),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_tracks_artist_id")
|
||||
.from(Tracks::Table, Tracks::ArtistId)
|
||||
.to(Artists::Table, Artists::Id)
|
||||
.on_delete(ForeignKeyAction::SetNull),
|
||||
)
|
||||
.foreign_key(
|
||||
ForeignKey::create()
|
||||
.name("fk_tracks_album_id")
|
||||
.from(Tracks::Table, Tracks::AlbumId)
|
||||
.to(Albums::Table, Albums::Id)
|
||||
.on_delete(ForeignKeyAction::SetNull),
|
||||
)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_tracks_artist_id")
|
||||
.table(Tracks::Table)
|
||||
.col(Tracks::ArtistId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_tracks_album_id")
|
||||
.table(Tracks::Table)
|
||||
.col(Tracks::AlbumId)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_tracks_title")
|
||||
.table(Tracks::Table)
|
||||
.col(Tracks::Title)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
manager
|
||||
.create_index(
|
||||
Index::create()
|
||||
.name("idx_tracks_artist")
|
||||
.table(Tracks::Table)
|
||||
.col(Tracks::Artist)
|
||||
.to_owned(),
|
||||
)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
||||
manager
|
||||
.drop_table(Table::drop().table(Tracks::Table).to_owned())
|
||||
.await
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(DeriveIden)]
|
||||
pub(crate) enum Tracks {
|
||||
Table,
|
||||
Id,
|
||||
FilePath,
|
||||
Title,
|
||||
Artist,
|
||||
Album,
|
||||
AlbumArtist,
|
||||
TrackNumber,
|
||||
DiscNumber,
|
||||
Duration,
|
||||
Genre,
|
||||
Year,
|
||||
Codec,
|
||||
Bitrate,
|
||||
FileSize,
|
||||
Fingerprint,
|
||||
MusicbrainzId,
|
||||
ArtistId,
|
||||
AlbumId,
|
||||
FileMtime,
|
||||
AddedAt,
|
||||
UpdatedAt,
|
||||
}
|
||||
Reference in New Issue
Block a user