initial commit of gitara site

This commit is contained in:
Priec
2026-06-16 12:12:25 +02:00
commit 29eac1ffcd
156 changed files with 16165 additions and 0 deletions

22
migration/Cargo.toml Normal file
View File

@@ -0,0 +1,22 @@
[package]
name = "migration"
version = "0.1.0"
edition = "2021"
publish = false
[lib]
name = "migration"
path = "src/lib.rs"
[dependencies]
loco-rs = { workspace = true }
[dependencies.sea-orm-migration]
version = "1.1.0"
features = [
# Enable at least one `ASYNC_RUNTIME` and `DATABASE_DRIVER` feature if you want to run migration via CLI.
# View the list of supported features at https://www.sea-ql.org/SeaORM/docs/install-and-config/database-and-async-runtime.
# e.g.
"runtime-tokio-rustls", # `ASYNC_RUNTIME` feature
]

40
migration/src/lib.rs Normal file
View File

@@ -0,0 +1,40 @@
#![allow(elided_lifetimes_in_paths)]
#![allow(clippy::wildcard_imports)]
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_users;
mod m20260517_000001_add_theme_to_users;
mod m20260517_000002_user_roles;
mod m20260517_000003_blog_articles;
mod m20260517_000004_audit_logs;
mod m20260517_000005_audio_albums;
mod m20260517_000006_audio_tracks;
mod m20260517_000007_audio_tags;
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;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![
Box::new(m20220101_000001_users::Migration),
Box::new(m20260517_000001_add_theme_to_users::Migration),
Box::new(m20260517_000002_user_roles::Migration),
Box::new(m20260517_000003_blog_articles::Migration),
Box::new(m20260517_000004_audit_logs::Migration),
Box::new(m20260517_000005_audio_albums::Migration),
Box::new(m20260517_000006_audio_tracks::Migration),
Box::new(m20260517_000007_audio_tags::Migration),
Box::new(m20260517_000008_audio_track_tags::Migration),
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,41 @@
use loco_rs::schema::*;
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
create_table(
m,
"users",
&[
("id", ColType::PkAuto),
("pid", ColType::Uuid),
("email", ColType::StringUniq),
("password", ColType::String),
("api_key", ColType::StringUniq),
("name", ColType::String),
("reset_token", ColType::StringNull),
("reset_sent_at", ColType::TimestampWithTimeZoneNull),
("email_verification_token", ColType::StringNull),
(
"email_verification_sent_at",
ColType::TimestampWithTimeZoneNull,
),
("email_verified_at", ColType::TimestampWithTimeZoneNull),
("magic_link_token", ColType::StringNull),
("magic_link_expiration", ColType::TimestampWithTimeZoneNull),
],
&[],
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
drop_table(m, "users").await?;
Ok(())
}
}

View File

@@ -0,0 +1,24 @@
use loco_rs::schema::*;
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
add_column(
m,
"users",
"theme",
ColType::StringLenWithDefault(20, "light".to_string()),
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
remove_column(m, "users", "theme").await?;
Ok(())
}
}

View File

@@ -0,0 +1,97 @@
use sea_orm_migration::{prelude::*, sea_orm::ConnectionTrait, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum UserRoles {
Table,
UserId,
Role,
AssignedBy,
AssignedAt,
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(UserRoles::Table)
.if_not_exists()
.col(ColumnDef::new(UserRoles::UserId).integer().not_null())
.col(ColumnDef::new(UserRoles::Role).string_len(50).not_null())
.col(ColumnDef::new(UserRoles::AssignedBy).integer().null())
.col(
ColumnDef::new(UserRoles::AssignedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.primary_key(
Index::create()
.name("pk-user_roles")
.col(UserRoles::UserId)
.col(UserRoles::Role),
)
.foreign_key(
ForeignKey::create()
.name("fk-user_roles-user_id-to-users")
.from(UserRoles::Table, UserRoles::UserId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk-user_roles-assigned_by-to-users")
.from(UserRoles::Table, UserRoles::AssignedBy)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::SetNull)
.on_update(ForeignKeyAction::NoAction),
)
.to_owned(),
)
.await?;
m.create_index(
Index::create()
.name("idx-user_roles-user_id")
.table(UserRoles::Table)
.col(UserRoles::UserId)
.to_owned(),
)
.await?;
let sql = match m.get_database_backend() {
sea_orm_migration::sea_orm::DatabaseBackend::Postgres => {
"INSERT INTO user_roles (user_id, role, assigned_at) \
SELECT id, 'user', CURRENT_TIMESTAMP FROM users \
ON CONFLICT (user_id, role) DO NOTHING"
}
sea_orm_migration::sea_orm::DatabaseBackend::Sqlite => {
"INSERT OR IGNORE INTO user_roles (user_id, role, assigned_at) \
SELECT id, 'user', CURRENT_TIMESTAMP FROM users"
}
sea_orm_migration::sea_orm::DatabaseBackend::MySql => {
"INSERT IGNORE INTO user_roles (user_id, role, assigned_at) \
SELECT id, 'user', CURRENT_TIMESTAMP FROM users"
}
};
m.get_connection().execute_unprepared(sql).await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(UserRoles::Table).to_owned())
.await?;
Ok(())
}
}

View File

@@ -0,0 +1,139 @@
use sea_orm_migration::{prelude::*, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum BlogArticles {
Table,
Id,
Title,
Slug,
Content,
Excerpt,
Published,
AuthorId,
FeaturedImageId,
ViewCount,
CreatedAt,
UpdatedAt,
PublishedAt,
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(BlogArticles::Table)
.if_not_exists()
.col(
ColumnDef::new(BlogArticles::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(BlogArticles::Title)
.string_len(500)
.not_null(),
)
.col(
ColumnDef::new(BlogArticles::Slug)
.string_len(500)
.not_null()
.unique_key(),
)
.col(ColumnDef::new(BlogArticles::Content).text().not_null())
.col(
ColumnDef::new(BlogArticles::Excerpt)
.string_len(1000)
.null(),
)
.col(
ColumnDef::new(BlogArticles::Published)
.boolean()
.not_null()
.default(false),
)
.col(ColumnDef::new(BlogArticles::AuthorId).integer().not_null())
.col(
ColumnDef::new(BlogArticles::FeaturedImageId)
.string_len(500)
.null(),
)
.col(
ColumnDef::new(BlogArticles::ViewCount)
.integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(BlogArticles::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(BlogArticles::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(BlogArticles::PublishedAt)
.timestamp_with_time_zone()
.null(),
)
.foreign_key(
ForeignKey::create()
.name("fk-blog_articles-author_id-to-users")
.from(BlogArticles::Table, BlogArticles::AuthorId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
create_index(m, "idx-blog_articles-slug", BlogArticles::Slug).await?;
m.create_index(
Index::create()
.name("idx-blog_articles-published-published_at")
.table(BlogArticles::Table)
.col(BlogArticles::Published)
.col(BlogArticles::PublishedAt)
.to_owned(),
)
.await?;
create_index(m, "idx-blog_articles-author_id", BlogArticles::AuthorId).await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(BlogArticles::Table).to_owned())
.await?;
Ok(())
}
}
async fn create_index<T>(m: &SchemaManager<'_>, name: &str, col: T) -> Result<(), DbErr>
where
T: Iden + 'static,
{
m.create_index(
Index::create()
.name(name)
.table(BlogArticles::Table)
.col(col)
.to_owned(),
)
.await
}

View File

@@ -0,0 +1,98 @@
use sea_orm_migration::{prelude::*, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum AuditLogs {
Table,
Id,
AdminUserId,
Action,
TargetType,
TargetId,
Details,
IpAddress,
UserAgent,
CreatedAt,
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(AuditLogs::Table)
.if_not_exists()
.col(
ColumnDef::new(AuditLogs::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(AuditLogs::AdminUserId).integer().not_null())
.col(ColumnDef::new(AuditLogs::Action).string_len(100).not_null())
.col(ColumnDef::new(AuditLogs::TargetType).string_len(50).null())
.col(ColumnDef::new(AuditLogs::TargetId).uuid().null())
.col(ColumnDef::new(AuditLogs::Details).json_binary().null())
.col(ColumnDef::new(AuditLogs::IpAddress).inet().null())
.col(ColumnDef::new(AuditLogs::UserAgent).text().null())
.col(
ColumnDef::new(AuditLogs::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk-audit_logs-admin_user_id-to-users")
.from(AuditLogs::Table, AuditLogs::AdminUserId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
create_index(m, "idx-audit_logs-admin_user_id", AuditLogs::AdminUserId).await?;
create_index(m, "idx-audit_logs-action", AuditLogs::Action).await?;
m.create_index(
Index::create()
.name("idx-audit_logs-target")
.table(AuditLogs::Table)
.col(AuditLogs::TargetType)
.col(AuditLogs::TargetId)
.to_owned(),
)
.await?;
create_index(m, "idx-audit_logs-created_at", AuditLogs::CreatedAt).await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(AuditLogs::Table).to_owned())
.await?;
Ok(())
}
}
async fn create_index<T>(m: &SchemaManager<'_>, name: &str, col: T) -> Result<(), DbErr>
where
T: Iden + 'static,
{
m.create_index(
Index::create()
.name(name)
.table(AuditLogs::Table)
.col(col)
.to_owned(),
)
.await
}

View File

@@ -0,0 +1,129 @@
use sea_orm_migration::{prelude::*, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum AudioAlbums {
Table,
Id,
Title,
Slug,
Description,
CoverImageId,
Artist,
ReleaseDate,
Published,
UploaderId,
ViewCount,
CreatedAt,
UpdatedAt,
PublishedAt,
}
#[derive(DeriveIden)]
enum Users {
Table,
Id,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(AudioAlbums::Table)
.if_not_exists()
.col(
ColumnDef::new(AudioAlbums::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(AudioAlbums::Title)
.string_len(500)
.not_null(),
)
.col(
ColumnDef::new(AudioAlbums::Slug)
.string_len(500)
.not_null()
.unique_key(),
)
.col(ColumnDef::new(AudioAlbums::Description).text().null())
.col(
ColumnDef::new(AudioAlbums::CoverImageId)
.string_len(500)
.null(),
)
.col(ColumnDef::new(AudioAlbums::Artist).string_len(500).null())
.col(ColumnDef::new(AudioAlbums::ReleaseDate).date().null())
.col(
ColumnDef::new(AudioAlbums::Published)
.boolean()
.not_null()
.default(false),
)
.col(ColumnDef::new(AudioAlbums::UploaderId).integer().not_null())
.col(
ColumnDef::new(AudioAlbums::ViewCount)
.integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(AudioAlbums::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(AudioAlbums::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(AudioAlbums::PublishedAt)
.timestamp_with_time_zone()
.null(),
)
.foreign_key(
ForeignKey::create()
.name("fk-audio_albums-uploader_id-to-users")
.from(AudioAlbums::Table, AudioAlbums::UploaderId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
create_index(m, "idx-audio_albums-slug", AudioAlbums::Slug).await?;
create_index(m, "idx-audio_albums-published", AudioAlbums::Published).await?;
create_index(m, "idx-audio_albums-uploader_id", AudioAlbums::UploaderId).await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(AudioAlbums::Table).to_owned())
.await?;
Ok(())
}
}
async fn create_index<T>(m: &SchemaManager<'_>, name: &str, col: T) -> Result<(), DbErr>
where
T: Iden + 'static,
{
m.create_index(
Index::create()
.name(name)
.table(AudioAlbums::Table)
.col(col)
.to_owned(),
)
.await
}

View File

@@ -0,0 +1,109 @@
use sea_orm_migration::{prelude::*, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum AudioTracks {
Table,
Id,
AlbumId,
Title,
Slug,
AudioFileId,
TrackNumber,
Duration,
Featured,
PlayCount,
CreatedAt,
UpdatedAt,
}
#[derive(DeriveIden)]
enum AudioAlbums {
Table,
Id,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(AudioTracks::Table)
.if_not_exists()
.col(
ColumnDef::new(AudioTracks::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(ColumnDef::new(AudioTracks::AlbumId).uuid().not_null())
.col(
ColumnDef::new(AudioTracks::Title)
.string_len(500)
.not_null(),
)
.col(ColumnDef::new(AudioTracks::Slug).string_len(500).not_null())
.col(
ColumnDef::new(AudioTracks::AudioFileId)
.string_len(500)
.not_null(),
)
.col(ColumnDef::new(AudioTracks::TrackNumber).integer().null())
.col(ColumnDef::new(AudioTracks::Duration).integer().null())
.col(
ColumnDef::new(AudioTracks::Featured)
.boolean()
.not_null()
.default(false),
)
.col(
ColumnDef::new(AudioTracks::PlayCount)
.integer()
.not_null()
.default(0),
)
.col(
ColumnDef::new(AudioTracks::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(AudioTracks::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.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?;
m.create_index(
Index::create()
.name("idx-audio_tracks-album_id-slug")
.table(AudioTracks::Table)
.col(AudioTracks::AlbumId)
.col(AudioTracks::Slug)
.unique()
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(AudioTracks::Table).to_owned())
.await?;
Ok(())
}
}

View File

@@ -0,0 +1,57 @@
use sea_orm_migration::{prelude::*, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum AudioTags {
Table,
Id,
Name,
Slug,
CreatedAt,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(AudioTags::Table)
.if_not_exists()
.col(
ColumnDef::new(AudioTags::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(AudioTags::Name)
.string_len(100)
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(AudioTags::Slug)
.string_len(100)
.not_null()
.unique_key(),
)
.col(
ColumnDef::new(AudioTags::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(AudioTags::Table).to_owned())
.await?;
Ok(())
}
}

View File

@@ -0,0 +1,74 @@
use sea_orm_migration::{prelude::*, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum AudioTrackTags {
Table,
TrackId,
TagId,
CreatedAt,
}
#[derive(DeriveIden)]
enum AudioTracks {
Table,
Id,
}
#[derive(DeriveIden)]
enum AudioTags {
Table,
Id,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(AudioTrackTags::Table)
.if_not_exists()
.col(ColumnDef::new(AudioTrackTags::TrackId).uuid().not_null())
.col(ColumnDef::new(AudioTrackTags::TagId).uuid().not_null())
.col(
ColumnDef::new(AudioTrackTags::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.primary_key(
Index::create()
.name("pk-audio_track_tags")
.col(AudioTrackTags::TrackId)
.col(AudioTrackTags::TagId),
)
.foreign_key(
ForeignKey::create()
.name("fk-audio_track_tags-track_id-to-audio_tracks")
.from(AudioTrackTags::Table, AudioTrackTags::TrackId)
.to(AudioTracks::Table, AudioTracks::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.foreign_key(
ForeignKey::create()
.name("fk-audio_track_tags-tag_id-to-audio_tags")
.from(AudioTrackTags::Table, AudioTrackTags::TagId)
.to(AudioTags::Table, AudioTags::Id)
.on_delete(ForeignKeyAction::Cascade)
.on_update(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(AudioTrackTags::Table).to_owned())
.await?;
Ok(())
}
}

View File

@@ -0,0 +1,60 @@
use sea_orm_migration::{prelude::*, sea_orm::ConnectionTrait};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum AudioTrackTags {
Table,
TagId,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
if matches!(
m.get_database_backend(),
sea_orm_migration::sea_orm::DatabaseBackend::Postgres
) {
m.get_connection()
.execute_unprepared(
"ALTER TABLE users \
ADD CONSTRAINT chk_users_theme \
CHECK (theme IN ('light', 'dark'))",
)
.await?;
}
m.create_index(
Index::create()
.name("idx-audio_track_tags-tag_id")
.table(AudioTrackTags::Table)
.col(AudioTrackTags::TagId)
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_index(
Index::drop()
.name("idx-audio_track_tags-tag_id")
.table(AudioTrackTags::Table)
.to_owned(),
)
.await?;
if matches!(
m.get_database_backend(),
sea_orm_migration::sea_orm::DatabaseBackend::Postgres
) {
m.get_connection()
.execute_unprepared("ALTER TABLE users DROP CONSTRAINT chk_users_theme")
.await?;
}
Ok(())
}
}

View File

@@ -0,0 +1,28 @@
use sea_orm_migration::prelude::*;
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum UserRoles {
Table,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(
Table::drop()
.table(UserRoles::Table)
.if_exists()
.cascade()
.to_owned(),
)
.await?;
Ok(())
}
async fn down(&self, _m: &SchemaManager) -> Result<(), DbErr> {
Ok(())
}
}

View File

@@ -0,0 +1,70 @@
use sea_orm_migration::{prelude::*, sea_orm::ConnectionTrait, sea_query::Expr};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[derive(DeriveIden)]
enum SitePages {
Table,
Id,
Slug,
Title,
Content,
CreatedAt,
UpdatedAt,
}
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.create_table(
Table::create()
.table(SitePages::Table)
.if_not_exists()
.col(
ColumnDef::new(SitePages::Id)
.uuid()
.not_null()
.primary_key(),
)
.col(
ColumnDef::new(SitePages::Slug)
.string_len(100)
.not_null()
.unique_key(),
)
.col(ColumnDef::new(SitePages::Title).string_len(500).not_null())
.col(ColumnDef::new(SitePages::Content).text().not_null())
.col(
ColumnDef::new(SitePages::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(SitePages::UpdatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await?;
m.get_connection()
.execute_unprepared(
"INSERT INTO site_pages (id, slug, title, content, created_at, updated_at) \
VALUES (gen_random_uuid(), 'about', 'About', '', CURRENT_TIMESTAMP, CURRENT_TIMESTAMP) \
ON CONFLICT (slug) DO NOTHING",
)
.await?;
Ok(())
}
async fn down(&self, m: &SchemaManager) -> Result<(), DbErr> {
m.drop_table(Table::drop().table(SitePages::Table).to_owned())
.await?;
Ok(())
}
}

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(())
}
}