[feat]: initial migration

This commit is contained in:
aprilthepink 2024-04-18 01:47:18 +02:00
parent ed912004b8
commit 99304aa53a
12 changed files with 1474 additions and 243 deletions

View file

@ -1,12 +1,18 @@
pub use sea_orm_migration::prelude::*;
mod m20220101_000001_create_table;
mod m20220101_000001_post_table;
mod m20240417_230111_user_table;
mod m20240417_233430_post_user_keys;
pub struct Migrator;
#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(m20220101_000001_create_table::Migration)]
vec![
Box::new(m20220101_000001_post_table::Migration),
Box::new(m20240417_230111_user_table::Migration),
Box::new(m20240417_233430_post_user_keys::Migration),
]
}
}

View file

@ -1,47 +0,0 @@
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> {
// Replace the sample below with your own migration scripts
todo!();
manager
.create_table(
Table::create()
.table(Post::Table)
.if_not_exists()
.col(
ColumnDef::new(Post::Id)
.integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Post::Title).string().not_null())
.col(ColumnDef::new(Post::Text).string().not_null())
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
// Replace the sample below with your own migration scripts
todo!();
manager
.drop_table(Table::drop().table(Post::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
enum Post {
Table,
Id,
Title,
Text,
}

View file

@ -0,0 +1,59 @@
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(Post::Table)
.if_not_exists()
.col(ColumnDef::new(Post::Id).string().not_null().primary_key())
.col(ColumnDef::new(Post::Title).string())
.col(ColumnDef::new(Post::Content).string().not_null())
.col(ColumnDef::new(Post::Local).boolean().not_null())
.col(ColumnDef::new(Post::CreatedAt).timestamp().not_null())
.col(ColumnDef::new(Post::UpdatedAt).timestamp())
.col(ColumnDef::new(Post::ReblogId).string())
.col(ColumnDef::new(Post::ContentType).string().not_null())
.col(ColumnDef::new(Post::Visibility).string().not_null())
.col(ColumnDef::new(Post::ReplyId).string())
.col(ColumnDef::new(Post::QuotingId).string())
.col(ColumnDef::new(Post::Sensitive).boolean().not_null())
.col(ColumnDef::new(Post::SpoilerText).string())
.col(ColumnDef::new(Post::Creator).string().not_null())
.col(ColumnDef::new(Post::Url).string().not_null())
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(Post::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
pub enum Post {
Table,
Id,
Url,
Creator,
Title,
Content,
Local,
CreatedAt,
UpdatedAt,
ReblogId,
ContentType,
Visibility,
ReplyId,
QuotingId,
Sensitive,
SpoilerText,
}

View file

@ -0,0 +1,61 @@
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(User::Table)
.if_not_exists()
.col(ColumnDef::new(User::Id).string().not_null().primary_key())
.col(ColumnDef::new(User::Username).string().not_null())
.col(ColumnDef::new(User::Name).string().not_null())
.col(ColumnDef::new(User::Summary).string())
.col(ColumnDef::new(User::Url).string().not_null())
.col(ColumnDef::new(User::PublicKey).string().not_null())
.col(ColumnDef::new(User::PrivateKey).string())
.col(ColumnDef::new(User::LastRefreshedAt).timestamp().not_null())
.col(ColumnDef::new(User::Local).boolean().not_null())
.col(ColumnDef::new(User::FollowerCount).integer().not_null())
.col(ColumnDef::new(User::FollowingCount).integer().not_null())
.col(ColumnDef::new(User::CreatedAt).timestamp().not_null())
.col(ColumnDef::new(User::UpdatedAt).timestamp())
.col(ColumnDef::new(User::Following).string())
.col(ColumnDef::new(User::Followers).string())
.col(ColumnDef::new(User::Inbox).string().not_null())
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(User::Table).to_owned())
.await
}
}
#[derive(DeriveIden)]
pub enum User {
Table,
Id,
Username,
Name,
Summary,
Url,
PublicKey,
PrivateKey,
LastRefreshedAt,
Local,
FollowerCount,
FollowingCount,
CreatedAt,
UpdatedAt,
Following,
Followers,
Inbox,
}

View file

@ -0,0 +1,62 @@
use sea_orm_migration::prelude::*;
use crate::{m20220101_000001_post_table::Post, m20240417_230111_user_table::User};
#[derive(DeriveMigrationName)]
pub struct Migration;
#[async_trait::async_trait]
impl MigrationTrait for Migration {
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.alter_table(
Table::alter()
.table(Post::Table)
.add_foreign_key(
ForeignKey::create()
.name("fk_post_creator_user_id")
.from(Post::Table, Post::Creator)
.to(User::Table, User::Id)
.on_delete(ForeignKeyAction::Cascade)
.get_foreign_key(),
)
.add_foreign_key(
ForeignKey::create()
.name("fk_post_reblog_id")
.from(Post::Table, Post::ReblogId)
.to(Post::Table, Post::Id)
.on_delete(ForeignKeyAction::Cascade)
.get_foreign_key(),
)
.add_foreign_key(
ForeignKey::create()
.name("fk_post_reply_id")
.from(Post::Table, Post::ReplyId)
.to(Post::Table, Post::Id)
.on_delete(ForeignKeyAction::Cascade)
.get_foreign_key(),
)
.add_foreign_key(
ForeignKey::create()
.name("fk_post_quoting_id")
.from(Post::Table, Post::QuotingId)
.to(Post::Table, Post::Id)
.on_delete(ForeignKeyAction::Cascade)
.get_foreign_key(),
)
.to_owned(),
)
.await
}
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_foreign_key(
ForeignKey::drop()
.name("fk_post_creator_user_id")
.table(Post::Table)
.to_owned(),
)
.await
}
}