song upload

This commit is contained in:
Priec
2026-05-17 18:25:23 +02:00
parent d164edf87c
commit c1ecfa459d
10 changed files with 459 additions and 35 deletions

View File

@@ -13,6 +13,7 @@ mod m20260517_000008_audio_track_tags;
mod m20260517_000009_simple_constraints;
mod m20260517_000010_drop_user_roles;
mod m20260517_000011_site_pages;
mod m20260517_000012_standalone_audio_tracks;
pub struct Migrator;
@@ -32,6 +33,7 @@ impl MigratorTrait for Migrator {
Box::new(m20260517_000009_simple_constraints::Migration),
Box::new(m20260517_000010_drop_user_roles::Migration),
Box::new(m20260517_000011_site_pages::Migration),
Box::new(m20260517_000012_standalone_audio_tracks::Migration),
// inject-above (do not remove this comment)
]
}

View File

@@ -0,0 +1,131 @@
use sea_orm_migration::{prelude::*, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum AudioTracks {
Table,
AlbumId,
Published,
PublishedAt,
}
#[derive(DeriveIden)]
enum AudioAlbums {
Table,
Id,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_foreign_key(
ForeignKey::drop()
.name("fk-audio_tracks-album_id-to-audio_albums")
.table(AudioTracks::Table)
.to_owned(),
)
.await?;
m.alter_table(
Table::alter()
.table(AudioTracks::Table)
.modify_column(ColumnDef::new(AudioTracks::AlbumId).uuid().null())
.add_column(
ColumnDef::new(AudioTracks::Published)
.boolean()
.not_null()
.default(false),
)
.add_column(
ColumnDef::new(AudioTracks::PublishedAt)
.timestamp_with_time_zone()
.null(),
)
.to_owned(),
)
.await?;
m.create_foreign_key(
ForeignKey::create()
.name("fk-audio_tracks-album_id-to-audio_albums")
.from(AudioTracks::Table, AudioTracks::AlbumId)
.to(AudioAlbums::Table, AudioAlbums::Id)
.on_delete(ForeignKeyAction::SetNull)
.on_update(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
m.get_connection()
.execute_unprepared(
r#"
UPDATE audio_tracks t
SET published = TRUE,
published_at = COALESCE(a.published_at, CURRENT_TIMESTAMP)
FROM audio_albums a
WHERE t.album_id = a.id
AND a.published = TRUE
"#,
)
.await?;
m.create_index(
Index::create()
.name("idx-audio_tracks-published")
.table(AudioTracks::Table)
.col(AudioTracks::Published)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_index(
Index::drop()
.name("idx-audio_tracks-published")
.table(AudioTracks::Table)
.to_owned(),
)
.await?;
m.drop_foreign_key(
ForeignKey::drop()
.name("fk-audio_tracks-album_id-to-audio_albums")
.table(AudioTracks::Table)
.to_owned(),
)
.await?;
m.alter_table(
Table::alter()
.table(AudioTracks::Table)
.drop_column(AudioTracks::PublishedAt)
.drop_column(AudioTracks::Published)
.modify_column(
ColumnDef::new(AudioTracks::AlbumId)
.uuid()
.not_null()
.default(Expr::cust("'00000000-0000-0000-0000-000000000000'::uuid")),
)
.to_owned(),
)
.await?;
m.create_foreign_key(
ForeignKey::create()
.name("fk-audio_tracks-album_id-to-audio_albums")
.from(AudioTracks::Table, AudioTracks::AlbumId)
.to(AudioAlbums::Table, AudioAlbums::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade)
.to_owned(),
)
.await?;
Ok(())
}
}