Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions morutine-infra/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ edition = "2024"

[dependencies]
sea-orm = { version = "1.1.19", features = ["runtime-tokio-rustls", "macros", "sqlx-postgres", "chrono"] }
sea-orm-migration = { version = "1.1.19", features = ["runtime-tokio-rustls", "sqlx-postgres"] }
async-trait = "0.1"
serde = { version = "1.0", features = ["derive"] }
tracing = "0.1"
Expand Down
228 changes: 228 additions & 0 deletions morutine-infra/src/database/postgres/migration/init_tables.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,228 @@
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> {
// 1. users 테이블
manager
.create_table(
Table::create()
.table(Users::Table)
.if_not_exists()
.col(
ColumnDef::new(Users::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Users::Nickname).string().not_null())
.col(ColumnDef::new(Users::ProfileImageUrl).string().null())
.col(
ColumnDef::new(Users::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Users::ModifiedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.to_owned(),
)
.await?;

// 2. social_accounts 테이블
manager
.create_table(
Table::create()
.table(SocialAccounts::Table)
.if_not_exists()
.col(
ColumnDef::new(SocialAccounts::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(
ColumnDef::new(SocialAccounts::UserId)
.big_integer()
.not_null(),
)
.col(ColumnDef::new(SocialAccounts::Provider).string().not_null())
.col(
ColumnDef::new(SocialAccounts::ProviderUserId)
.string()
.not_null(),
)
.col(
ColumnDef::new(SocialAccounts::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(SocialAccounts::ModifiedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk-social_accounts-user_id")
.from(SocialAccounts::Table, SocialAccounts::UserId)
.to(Users::Table, Users::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;

// 3. todos 테이블
manager
.create_table(
Table::create()
.table(Todos::Table)
.if_not_exists()
.col(
ColumnDef::new(Todos::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(Todos::UserId).big_integer().not_null())
.col(ColumnDef::new(Todos::Date).date().not_null())
.col(
ColumnDef::new(Todos::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(Todos::ModifiedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk-todos-user_id")
.from(Todos::Table, Todos::UserId)
.to(Users::Table, Users::Id),
)
.to_owned(),
)
.await?;

// 4. todo_items 테이블
manager
.create_table(
Table::create()
.table(TodoItems::Table)
.if_not_exists()
.col(
ColumnDef::new(TodoItems::Id)
.big_integer()
.not_null()
.auto_increment()
.primary_key(),
)
.col(ColumnDef::new(TodoItems::TodoId).big_integer().not_null())
.col(ColumnDef::new(TodoItems::Content).string().not_null())
.col(ColumnDef::new(TodoItems::Status).string().not_null())
.col(ColumnDef::new(TodoItems::AlteredContent).string().null())
.col(ColumnDef::new(TodoItems::ImageUrl).string().null())
.col(
ColumnDef::new(TodoItems::CreatedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.col(
ColumnDef::new(TodoItems::ModifiedAt)
.timestamp_with_time_zone()
.not_null()
.default(Expr::current_timestamp()),
)
.foreign_key(
ForeignKey::create()
.name("fk-todo_items-todo_id")
.from(TodoItems::Table, TodoItems::TodoId)
.to(Todos::Table, Todos::Id)
.on_delete(ForeignKeyAction::Cascade),
)
.to_owned(),
)
.await?;

Ok(())
}

async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> {
manager
.drop_table(Table::drop().table(TodoItems::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Todos::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(SocialAccounts::Table).to_owned())
.await?;
manager
.drop_table(Table::drop().table(Users::Table).to_owned())
.await?;

Ok(())
}
}

#[derive(Iden)]
enum Users {
Table,
Id,
Nickname,
ProfileImageUrl,
CreatedAt,
ModifiedAt,
}

#[derive(Iden)]
enum SocialAccounts {
Table,
Id,
UserId,
Provider,
ProviderUserId,
CreatedAt,
ModifiedAt,
}

#[derive(Iden)]
enum Todos {
Table,
Id,
UserId,
Date,
CreatedAt,
ModifiedAt,
}

#[derive(Iden)]
enum TodoItems {
Table,
Id,
TodoId,
Content,
Status,
AlteredContent,
ImageUrl,
CreatedAt,
ModifiedAt,
}
12 changes: 12 additions & 0 deletions morutine-infra/src/database/postgres/migration/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
pub use sea_orm_migration::prelude::*;

mod init_tables;

pub struct Migrator;

#[async_trait::async_trait]
impl MigratorTrait for Migrator {
fn migrations() -> Vec<Box<dyn MigrationTrait>> {
vec![Box::new(init_tables::Migration)]
}
}
3 changes: 2 additions & 1 deletion morutine-infra/src/database/postgres/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod migration;
pub mod todo;
mod user;
pub mod user;