137 lines
4.2 KiB
Rust
137 lines
4.2 KiB
Rust
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(Playlists::Table)
|
|
.if_not_exists()
|
|
.col(
|
|
ColumnDef::new(Playlists::Id)
|
|
.integer()
|
|
.not_null()
|
|
.auto_increment()
|
|
.primary_key(),
|
|
)
|
|
.col(ColumnDef::new(Playlists::Name).text().not_null())
|
|
.col(ColumnDef::new(Playlists::Description).text())
|
|
.col(ColumnDef::new(Playlists::UserId).integer())
|
|
.col(
|
|
ColumnDef::new(Playlists::CreatedAt)
|
|
.date_time()
|
|
.not_null()
|
|
.default(Expr::current_timestamp()),
|
|
)
|
|
.col(
|
|
ColumnDef::new(Playlists::UpdatedAt)
|
|
.date_time()
|
|
.not_null()
|
|
.default(Expr::current_timestamp()),
|
|
)
|
|
.foreign_key(
|
|
ForeignKey::create()
|
|
.from(Playlists::Table, Playlists::UserId)
|
|
.to(Users::Table, Users::Id),
|
|
)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
manager
|
|
.create_table(
|
|
Table::create()
|
|
.table(PlaylistTracks::Table)
|
|
.if_not_exists()
|
|
.col(
|
|
ColumnDef::new(PlaylistTracks::Id)
|
|
.integer()
|
|
.not_null()
|
|
.auto_increment()
|
|
.primary_key(),
|
|
)
|
|
.col(
|
|
ColumnDef::new(PlaylistTracks::PlaylistId)
|
|
.integer()
|
|
.not_null(),
|
|
)
|
|
.col(ColumnDef::new(PlaylistTracks::TrackId).integer().not_null())
|
|
.col(
|
|
ColumnDef::new(PlaylistTracks::Position)
|
|
.integer()
|
|
.not_null(),
|
|
)
|
|
.foreign_key(
|
|
ForeignKey::create()
|
|
.from(PlaylistTracks::Table, PlaylistTracks::PlaylistId)
|
|
.to(Playlists::Table, Playlists::Id)
|
|
.on_delete(ForeignKeyAction::Cascade),
|
|
)
|
|
.foreign_key(
|
|
ForeignKey::create()
|
|
.from(PlaylistTracks::Table, PlaylistTracks::TrackId)
|
|
.to(Tracks::Table, Tracks::Id),
|
|
)
|
|
.to_owned(),
|
|
)
|
|
.await?;
|
|
|
|
manager
|
|
.create_index(
|
|
Index::create()
|
|
.name("idx_playlist_tracks_unique")
|
|
.table(PlaylistTracks::Table)
|
|
.col(PlaylistTracks::PlaylistId)
|
|
.col(PlaylistTracks::Position)
|
|
.unique()
|
|
.to_owned(),
|
|
)
|
|
.await
|
|
}
|
|
|
|
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
|
|
manager
|
|
.drop_table(Table::drop().table(PlaylistTracks::Table).to_owned())
|
|
.await?;
|
|
manager
|
|
.drop_table(Table::drop().table(Playlists::Table).to_owned())
|
|
.await
|
|
}
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum Playlists {
|
|
Table,
|
|
Id,
|
|
Name,
|
|
Description,
|
|
UserId,
|
|
CreatedAt,
|
|
UpdatedAt,
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum PlaylistTracks {
|
|
Table,
|
|
Id,
|
|
PlaylistId,
|
|
TrackId,
|
|
Position,
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum Users {
|
|
Table,
|
|
Id,
|
|
}
|
|
|
|
#[derive(DeriveIden)]
|
|
enum Tracks {
|
|
Table,
|
|
Id,
|
|
}
|